Set difference · setdiff

🟢 1 of 1 form 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["Set difference · setdiff<br/>🟢1"]:::ok
    v_two["setdiff(DS, DS)"]:::ok
    v_two --- O
    click v_two href "#form-two"
    

Types

Takes any scalar · Gives any type

Comes from: any operator that returns a value

Goes to: whatever takes its input's type

Forms

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

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

setdiff ( DS_1, DS_2 ): five of the six data points of DS_1 are also in DS_2, with the same measures, and DS_2 has one that DS_1 lacks.

Manual:

Set difference, Example 1, output verbatim: the operator returns a Data Set “containing the Data Points that appear in the first Data Set but not in the second”.

Expected:

the page’s one data point, (2012, B, Total, Total) with the measure 10. The (2011, B, Total, Total) of DS_2 is not in DS_1, so it adds nothing.

script.vtl
DS_r := setdiff ( DS_1, DS_2 );
Input DS_1
Id_1Id_2Id_3Id_4Me_1
2012BTotalTotal10
2012GTotalTotal20
2012FTotalTotal30
2012MTotalTotal40
2012ITotalTotal50
2012STotalTotal60
Input DS_2
Id_1Id_2Id_3Id_4Me_1
2011BTotalTotal10
2012GTotalTotal20
2012FTotalTotal30
2012MTotalTotal40
2012ITotalTotal50
2012STotalTotal60
Expected DS_r
Id_1Id_2Id_3Id_4Me_1
2012BTotalTotal10

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:

setdiff ( DS_3, DS_4 ) on data sets with three identifiers, where DS_4 holds two of the three data points of DS_3.

Manual:

Set difference, Example 2, output verbatim: the resulting Dataset contains “all the data points Data Point dp1 of the operand ds1, such that there is no Data Point dp2 of ds2 having the same values for homonym Identifier Components”.

Expected:

the page’s one data point, (R, T, 2011) with the measure 12.

script.vtl

DS_r := setdiff ( DS_3, DS_4 );
Input DS_3
Id_1Id_2Id_3Me_1
RM20117
RF201110
RT201112
Input DS_4
Id_1Id_2Id_3Me_1
RM20117
RF201110
Expected DS_r
Id_1Id_2Id_3Me_1
RT201112

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

Matched by identifiers not measures 🟢 pandas ✅ · DuckDB ✅
Tests:

setdiff(DS_1, DS_2) where ids 2 and 3 are in both operands with different measures (in the page’s examples a shared data point has the same measure in both), to show whether data points are matched by identifiers alone. 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:

Set difference: “the resulting Dataset contains all the data points Data Point dp1 of the operand ds1, such that there is no Data Point dp2 of ds2 having the same values for homonym Identifier Components”: the condition is on the identifiers alone.

Expected:

only id 1, with the measure 10 of DS_1. Ids 2 and 3 have a data point in DS_2 with the same identifiers, whatever its measure, and id 4 is only in DS_2, the subtrahend, which adds nothing.

script.vtl
DS_r := setdiff(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

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 ↗