Lead · lead

🟢 4 of 4 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc2 (90ee9376) · 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
Dataset formsComponent formsScalar forms
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart TB
    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["Lead · lead<br/>🟢4"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds_no_offset["lead(DS over (partition by Id order by Id))"]:::ok
        v_ds_offset["lead(DS, n over (partition by Id order by Id))"]:::ok
        v_ds_offset_default["lead(DS, n, default over (partition by Id order by Id))"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp_offset["DS#91;calc Me #58;#61; lead(Me, n over (partition by Id order by Id))#93;"]:::ok
    end
    subgraph G_use["Used with"]
        direction LR
        used__clause__calc["calc ↗<br/><small>used in over()</small>"]:::ext
    end
    O --- G_ds
    O --- G_comp
    O -.- G_use
    style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
    style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
    style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
    click v_ds_no_offset href "#form-ds-no-offset"
    click v_ds_offset href "#form-ds-offset"
    click v_ds_offset_default href "#form-ds-offset-default"
    click v_comp_offset href "#form-comp-offset"
    click used__clause__calc href "../clause/calc.html" "Calculation of a Component · calc"
    

Types

Takes integer, any scalar · Gives any type

Viral attributes: combined with the propagation rules → Viral propagation

Comes from: any operator that returns a value

Goes to: whatever takes its input's type

Forms

lead(DS over (partition by Id order by Id)) 🟢 · any scalar → any type

No default offset 🟢 pandas ✅ · DuckDB ✅
Tests:

lead at data set level without an offset, which its syntax makes optional.

Manual:

Lead: “the operator returns the value(s) taken from the Data Point at the specified physical offset beyond the current Data Point”; the page does not say which offset applies when it is omitted.

Expected:

the error 0-5-1-3, “{op} requires an offset parameter”: with no offset there is no “specified physical offset”, and the page gives no default.

script.vtl
DS_r := lead ( DS_1 over ( partition by Id_1 order by Id_2 ) );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000

Expected error 0-5-1-3: {op} requires an offset parameter.

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

lead(DS, n over (partition by Id order by Id)) 🟢 · any scalar, integer → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

lead with offset 1 at data set level, partitioned by Id_1 and Id_2 and ordered by Id_3.

Manual:

Lead, Example 1, output verbatim: “If defaultValue is not specified then the value returned when the offset goes outside the partition is null”.

Expected:

each data point gets the values of the year after; 1996, the last of each partition, gets null.

script.vtl
DS_r := lead ( DS_1 , 1 over ( partition by Id_1 , Id_2 order by Id_3 ) );
Input DS_1
Id_1Id_2Id_3Me_1Me_2
AXX199331
AXX199449
AXX199575
AXX199668
AYY199393
AYY199454
AYY1995102
AYY199627
Expected DS_r
Id_1Id_2Id_3Me_1Me_2
AXX199349
AXX199475
AXX199568
AXX1996
AYY199354
AYY1994102
AYY199527
AYY1996

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

lead(DS, n, default over (partition by Id order by Id)) 🟢 · any scalar, integer → any type

Offset two default zero 🟢 pandas ✅ · DuckDB ✅
Tests:

lead at data set level with offset 2 and the default value 0.

Manual:

Lead: “the operator returns the value(s) taken from the Data Point at the specified physical offset beyond the current Data Point”, and the default value is “the value returned when the offset goes outside of the partition”.

Expected:

each data point gets the values of the one two after it; where that is outside the partition, the last two of A and both of B, both measures are 0.

script.vtl
DS_r := lead ( DS_1 , 2 , 0 over ( partition by Id_1 order by Id_2 ) );
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Id_2Me_1Me_2
A2020Q152.500000
A2020Q210.500000
A2021Q100
A2021Q200
B2020Q100
B2020Q200

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

DS[calc Me := lead(Me, n over (partition by Id order by Id))] 🟢 · any scalar, integer → any type

Next value or null 🟢 pandas ✅ · DuckDB ✅
Tests:

lead with offset 1 in calc, partitioned by Id_1 and ordered by Id_2.

Manual:

Lead: “If defaultValue is not specified then the value returned when the offset goes outside the partition is null”.

Expected:

Me_3 is the Me_1 of the data point after: 1, 5, 1, null in A and 8, null in B.

script.vtl
DS_r := DS_1 [ calc Me_3 := lead ( Me_1 , 1 over ( partition by Id_1 order by Id_2 ) ) ];
Input DS_1
Id_1Id_2Me_1Me_2
A2020Q131.500000
A2020Q213.500000
A2021Q152.500000
A2021Q210.500000
B2020Q146.500000
B2020Q284.500000
Expected DS_r
Id_1Id_2Me_1Me_2Me_3
A2020Q131.5000001
A2020Q213.5000005
A2021Q152.5000001
A2021Q210.500000
B2020Q146.5000008
B2020Q284.500000

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