Union · union

🟢 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["Union · union<br/>🟢2"]:::ok
    v_two["union(DS, DS)"]:::ok
    v_n["union(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

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

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

union( DS_1, DS_2 ): two data sets that have no identifier combination in common.

Manual:

Union, Example 1, output verbatim: the resulting Data Set “contains the Data Points belonging to any of the operand Data Sets”.

Expected:

the page’s five data points: the three of DS_1 and the two of DS_2.

script.vtl
DS_r := union( DS_1, DS_2 );
Input DS_1
Id_1Id_2Id_3Id_4Me_1
2012BTotalTotal5
2012GTotalTotal2
2012FTotalTotal3
Input DS_2
Id_1Id_2Id_3Id_4Me_1
2012NTotalTotal23
2012STotalTotal5
Expected DS_r
Id_1Id_2Id_3Id_4Me_1
2012BTotalTotal5
2012GTotalTotal2
2012FTotalTotal3
2012NTotalTotal23
2012STotalTotal5

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 ↗

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

union ( DS_1, DS_3 ): two data sets that share one data point, (2012, B, Total, Total), with the measure 5 in DS_1 and 23 in DS_3.

Manual:

Union, Example 2, output verbatim: duplicate Data Points “are filtered by choosing the Data Point belonging to the left most operand Data Set”.

Expected:

the page’s four data points: (2012, B, Total, Total) has the measure 5 of DS_1, the leftmost operand, and not the 23 of DS_3.

script.vtl
DS_r := union ( DS_1, DS_3 );
Input DS_1
Id_1Id_2Id_3Id_4Me_1
2012BTotalTotal5
2012GTotalTotal2
2012FTotalTotal3
Input DS_3
Id_1Id_2Id_3Id_4Me_1
2012BTotalTotal23
2012STotalTotal5
Expected DS_r
Id_1Id_2Id_3Id_4Me_1
2012BTotalTotal5
2012GTotalTotal2
2012FTotalTotal3
2012STotalTotal5

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

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

union(DS_1, DS_2) where ids 2 and 3 are in both operands with different measures (Manual example 2 repeats one data point), to show which one the result keeps. 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:

Union: duplicate Data Points “are filtered by choosing the Data Point belonging to the left most operand Data Set”, whatever the other operand holds; the User Manual: “If for a common Data Point one or more dependent variables (Measures and Attributes) have different values in different Data Sets, the Data Point of the leftmost Data Set are returned in the result”.

Expected:

Me_1 is 10, 20, 30 and 41 for ids 1 to 4: ids 2 and 3 keep the values of DS_1, the leftmost operand, and ids 1 and 4 are each in one operand only.

script.vtl
DS_r := union(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
110
220
330
441

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 ↗

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

Leftmost of three operands wins 🟢 pandas ✅ · DuckDB ✅
Tests:

union(DS_1, DS_2, DS_3), the list form with three operands, where the page’s examples have two: id 1 is only in DS_1, id 2 in all three, id 3 in DS_2 and DS_3, and id 4 only in DS_3. The measure is 10 * Id_1 + k, with k 0, 1 and 2 for DS_1, DS_2 and DS_3.

Manual:

Union: dsList ::= ds { , ds }*, the resulting Data Set “contains the Data Points belonging to any of the operand Data Sets”, and duplicate Data Points “are filtered by choosing the Data Point belonging to the left most operand Data Set”.

Expected:

Me_1 is 10, 20, 31 and 42 for ids 1 to 4: each id keeps the data point of its leftmost operand, DS_1, DS_1, DS_2 and DS_3.

script.vtl
DS_r := union(DS_1, DS_2, DS_3);
Input DS_1
Id_1Me_1
110
220
Input DS_2
Id_1Me_1
221
331
Input DS_3
Id_1Me_1
222
332
442
Expected DS_r
Id_1Me_1
110
220
331
442

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 ↗