Unpivoting · unpivot

🟢 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["Unpivoting · unpivot<br/>🟢1"]:::ok
    v_basic["DS#91;unpivot Id, Me#93;"]:::ok
    v_basic --- O
    click v_basic href "#form-basic"
    

Types

Takes any scalar · Gives any type

Comes from: any operator that returns a value

Goes to: whatever takes its input's type

Forms

DS[unpivot Id, Me] 🟢 · any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

unpivot Id_2, Me_1, on the Integer measures A, B and C.

Manual:

Unpivoting, Example 1, output verbatim: “The VDS identifier is assigned a value equal to the name of the Measure M of op”, and “The VDS measure is assigned a value equal to the value of the Measure M of op”.

Expected:

Six data points, one per data point and measure: Id_2 holds A, B or C and Me_1 its value, such as (1, A) = 5 and (2, C) = 9.

script.vtl
DS_r := DS_1 [ unpivot Id_2, Me_1 ];
Input DS_1
Id_1ABC
1527
2349
Expected DS_r
Id_1Id_2Me_1
1A5
2A3
1B2
2B4
1C7
2C9

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

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

unpivot Id_3, Me_2, on the measures A (Number), B and C (Integer).

Manual:

Unpivoting, Example 2, output verbatim: “All the measures of op must have the same basic scalar type, including sub-types”, which accepts them, as Integer is a sub-type of Number. The manual’s ds_2.json names its dataset DS_1, a slip: the example loads it as DS_2, the name that the page and the script give it.

Expected:

Nine data points, with Me_2 typed Number, as the manual types it. The manual prints 2.1 for (6, A) with trailing spaces, which the importer stripped, as for every non-String value.

script.vtl
DS_r := DS_2 [ unpivot Id_3, Me_2 ];
Input DS_2
Id_1ABC
43.116
54.223
62.132
Expected DS_r
Id_1Id_3Me_2
4A3.1
5A4.2
6A2.1
4B1
5B2
6B3
4C6
5C3
6C2

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

Null measure skipped 🟢 pandas ✅ · DuckDB ✅
Tests:

unpivot Id_2, Me_3, on a data point whose Me_2 is null.

Manual:

Unpivoting: “For each Data Point DP and for each Measure M of op whose value is not null, the operator inserts a Data Point into VDS”, and “When a Measure is null then unpivot does not create a Data Point for that Measure.”

Expected:

(1, Me_1) = 5, (2, Me_1) = 3 and (2, Me_2) = 4, with no data point for the null Me_2 of Id_1 = 1. Id_2 holds component names, so it is a String, and Me_3 has the type of the measures, Integer.

script.vtl
DS_r := DS_1 [ unpivot Id_2, Me_3 ];
Input DS_1
Id_1Me_1Me_2
15
234
Expected DS_r
Id_1Id_2Me_3
1Me_15
2Me_13
2Me_24

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