Intersection · intersect

🟢 2 of 2 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc3 (9e1c81a6) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart LR
    classDef ok fill:#d4edda,stroke:#28a745,color:#000
    classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
    classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
    classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
    classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
    classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
    classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
    O["Intersection · intersect<br/>🟢2"]:::ok
    v_two["intersect(DS, DS)"]:::ok
    v_n["intersect(DS, DS, ...)"]:::ok
    v_two --- O
    O --- v_n
    click v_two href "#form-two"
    click v_n href "#form-n"
    

Types

Takes any scalar · Gives any type

Comes from: any operator that returns a value

Goes to: whatever takes its input's type

Forms

intersect(DS, DS) 🟢 · any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

intersect(DS_1, DS_2): two data sets that share one data point, (2012, G, Total, Total), with the measure 2 in both.

Manual:

Intersection, Example 1, output verbatim: the resulting Data Set “contains the Data Points belonging to all the operand Data Sets”.

Expected:

the page’s one data point, (2012, G, Total, Total) with the measure 2.

script.vtl
DS_r := intersect(DS_1, DS_2);
Input DS_1
Id_1Id_2Id_3Id_4Me_1
2012BTotalTotal1
2012GTotalTotal2
2012FTotalTotal3
Input DS_2
Id_1Id_2Id_3Id_4Me_1
2011BTotalTotal10
2012GTotalTotal2
2012MTotalTotal40
Expected DS_r
Id_1Id_2Id_3Id_4Me_1
2012GTotalTotal2

Files: script.vtl · input/DS_1.csv · input/DS_1.json · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

Leftmost wins when measures differ 🟢 pandas ✅ · DuckDB ✅
Tests:

intersect(DS_1, DS_2) where ids 2 and 3 are in both operands with different measures (the page’s example shares one data point, with the same measure in both), to show whether data points are matched by identifiers alone and which one is kept. The measure is 10 * Id_1 + k, with k 0 for DS_1 and 1 for DS_2, so its last digit names the operand.

Manual:

Intersection: duplicate Data Points “are filtered by choosing the Data Point belonging to the leftmost operand Data Set”; the User Manual: “The Data Points common (or not common) to the input Data Sets are determined by taking into account only the values of the Identifiers”.

Expected:

Me_1 is 20 and 30 for ids 2 and 3, the values of DS_1, the leftmost operand; ids 1 and 4 are each in one operand only, so they are out.

script.vtl
DS_r := intersect(DS_1, DS_2);
Input DS_1
Id_1Me_1
110
220
330
Input DS_2
Id_1Me_1
221
331
441
Expected DS_r
Id_1Me_1
220
330

Files: script.vtl · input/DS_1.csv · input/DS_1.json · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

intersect(DS, DS, ...) 🟢 · any scalar → any type

Point in all three operands 🟢 pandas ✅ · DuckDB ✅
Tests:

intersect(DS_1, DS_2, DS_3), the list form with three operands, where the page’s example has two: ids 3 and 4 are in all three, and ids 1, 2 and 5 are each in two, lacking DS_2, DS_3 and DS_1 in turn. The measure is 10 * Id_1 + k, with k 0, 1 and 2 for DS_1, DS_2 and DS_3.

Manual:

Intersection: dsList ::= ds { , ds }*, the resulting Data Set “contains the Data Points belonging to all the operand Data Sets”, and duplicate Data Points “are filtered by choosing the Data Point belonging to the leftmost operand Data Set”.

Expected:

Me_1 is 30 and 40 for ids 3 and 4, the values of DS_1. Leaving out DS_1, DS_2 or DS_3 would add id 5, 1 or 2, so every operand takes part in the intersection.

script.vtl
DS_r := intersect(DS_1, DS_2, DS_3);
Input DS_1
Id_1Me_1
110
220
330
440
Input DS_2
Id_1Me_1
221
331
441
551
Input DS_3
Id_1Me_1
112
332
442
552
Expected DS_r
Id_1Me_1
330
440

Files: script.vtl · input/DS_1.csv · input/DS_1.json · input/DS_2.csv · input/DS_2.json · input/DS_3.csv · input/DS_3.json · expected/DS_r.csv · expected/DS_r.json · example page ↗