Analytic invocation · over

🟢 11 of 11 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["Analytic invocation · over 📌<br/>🟢11"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_window_data_points["an(DS over (order by Id data points between n preceding and n following))"]:::ok
        v_window_default_ds["an(DS over (partition by Id order by Id))"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_partition_by["an(Me over (partition by Id))"]:::ok
        v_partition_except["an(Me over (partition except Id))"]:::ok
        v_partition_except_all["an(over (partition except all order by Id)) 📌"]:::ok
        v_order_asc["an(over (partition by Id order by Me asc))"]:::ok
        v_order_desc["an(over (partition by Id order by Me desc))"]:::ok
        v_window_range["an(Me over (partition by Id order by Me range between n preceding and n following))"]:::ok
        v_window_unbounded["an(Me over (... data points between unbounded preceding and unbounded following))"]:::ok
        v_window_current["an(Me over (... data points between n preceding and current data point))"]:::ok
        v_window_default["an(Me over (partition by Id order by Id))"]:::ok
    end
    subgraph G_use["Used with"]
        direction LR
        used__clause__calc["calc ↗<br/><small>used in</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_partition_by href "#form-partition-by"
    click v_partition_except href "#form-partition-except"
    click v_partition_except_all href "#form-partition-except-all"
    click v_order_asc href "#form-order-asc"
    click v_order_desc href "#form-order-desc"
    click v_window_data_points href "#form-window-data-points"
    click v_window_range href "#form-window-range"
    click v_window_unbounded href "#form-window-unbounded"
    click v_window_current href "#form-window-current"
    click v_window_default href "#form-window-default"
    click v_window_default_ds href "#form-window-default-ds"
    click used__clause__calc href "../clause/calc.html" "Calculation of a Component · calc"
    

Types

Takes 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

an(DS over (order by Id data points between n preceding and n following)) 🟢 · any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

sum at data set level over a window from one data point before to one after, with no partition clause.

Manual:

Analytic invocation, Example 1, with the manual’s data points: with no partition clause “the Data Set is partitioned by the Identifier Components that are not specified in the orderClause”, and the order names all three, so the data set is one partition.

Expected:

in the order of the identifiers, -3 + 5 = 2, -3 + 5 + 13 = 15, and so on to 3 - 2 = 1. Me_1 may be Integer or Number: the manual prints Number, and the Sum page says “implementations may preserve the integer type” when all operand values are integer.

script.vtl
DS_r := sum ( DS_1 over ( order by Id_1, Id_2, Id_3 data points between 1 preceding and 1 following ) );
Input DS_1
Id_1Id_2Id_3Me_1
2010EXX5
2010BXX-3
2010RXX9
2010EYY13
2011EXX11
2011BZZ7
2011EYY-1
2011FXX0
2012LZZ-2
2012EYY3
Expected DS_r
Id_1Id_2Id_3Me_1
2010BXX2
2010EXX15
2010EYY27
2010RXX29
2011BZZ27
2011EXX17
2011EYY10
2011FXX2
2012EYY1
2012LZZ1

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

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

Running total in time order 🟢 pandas ✅ · DuckDB ✅
Tests:

sum at data set level, partitioned by Id_1 and ordered by Id_2, with no window clause.

Manual:

Analytic invocation: “If the whole windowClause is omitted, then the default is either range between unbounded preceding and current data point or data points between unbounded preceding and unbounded following, depending respectively on whether orderClause clause was specified or not”. sdmx-twg/vtl#694 made it the default window of SQL. The Behaviour section of the page still says that the window is the whole partition, the rule of VTL 2.1.

Expected:

in A, Me_1 is 3, 4, 9 and 10, and Me_2 1.5, 5, 7.5 and 8; in B, Me_1 is 4 and 12, and Me_2 6.5 and 11.

script.vtl
DS_r := sum ( 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 DS_r
Id_1Id_2Me_1Me_2
A2020Q131.5
A2020Q245
A2021Q197.5
A2021Q2108
B2020Q146.5
B2020Q21211

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

Ties count together 🟢 pandas ✅ · DuckDB ✅
Tests:

sum at data set level, partitioned by Id_1 and ordered by Id_2, with no window clause, where (A, 1, x) and (A, 1, y) tie on Id_2.

Manual:

Analytic invocation: with an order clause and no window clause, the default is “range between unbounded preceding and current data point”. sdmx-twg/vtl#694 made it the default window of SQL, whose range takes in the data points that tie with the current one on the order keys.

Expected:

in A, Me_1 is 1 + 2 = 3 on both tied data points, then 6 and 10; in B, 5 and 11.

script.vtl
DS_r := sum ( DS_1 over ( partition by Id_1 order by Id_2 ) );
Input DS_1
Id_1Id_2Id_3Me_1
A1x1
A1y2
A2x3
A3x4
B1x5
B2x6
Expected DS_r
Id_1Id_2Id_3Me_1
A1x3
A1y3
A2x6
A3x10
B1x5
B2x11

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

an(Me over (partition by Id)) 🟢 · any scalar → any type

Sum per quarter 🟢 pandas ✅ · DuckDB ✅
Tests:

partition by Id_2: the sum of Me_1 in calc over the data points of the same quarter.

Manual:

Analytic invocation: “The input Data Set is partitioned according to the values of one or more Identifier Components”; with no window clause, the window is the whole partition.

Expected:

Me_3 = 3 + 4 = 7 for 2020Q1, 1 + 8 = 9 for 2020Q2, 5 for 2021Q1 and 1 for 2021Q2.

script.vtl
DS_r := DS_1 [ calc Me_3 := sum ( Me_1 over ( partition 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.5000007
A2020Q213.5000009
A2021Q152.5000005
A2021Q210.5000001
B2020Q146.5000007
B2020Q284.5000009

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

an(Me over (partition except Id)) 🟢 · any scalar → any type

Partition by unlisted identifier 🟢 pandas ✅ · DuckDB ✅
Tests:

partition except Id_2: the data points are partitioned by the identifier that it does not list, Id_1.

Manual:

Analytic invocation, with the syntax partition except identifier { , identifier }*: the partition is by the identifiers that are not listed.

Expected:

Me_3 = 3 + 1 + 5 + 1 = 10 on the data points of A and 4 + 8 = 12 on those of B.

script.vtl
DS_r := DS_1 [ calc Me_3 := sum ( Me_1 over ( partition except 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.50000010
A2020Q213.50000010
A2021Q152.50000010
A2021Q210.50000010
B2020Q146.50000012
B2020Q284.50000012

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

an(over (partition except all order by Id)) 🟢 · any scalar → any type

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

rank with partition except all, ordered by id_1, which the input names Id_1.

Manual:

Analytic invocation, Example 2: partition except all leaves no identifier to partition by, and the User Manual makes regular names “case insensitive”, so id_1 is Id_1.

Expected:

the error 1-1-1-10, “Component {comp_name} not found in Dataset {dataset_name}.”: vtlengine keeps regular names case sensitive, so id_1 is not Id_1.

Team decision:

#633: regular names stay case sensitive until vtlengine 2.0, to avoid backward compatibility issues, although the User Manual makes them case insensitive.

script.vtl
DS_r:=DS_1[calc Me_2 := rank(over(partition except all order by id_1))];
Input DS_1
Id_1Id_2Id_3Me_1
2010EXX5
2010BXX-3
2010RXX9
2010EYY13
2011EXX11
2011BZZ7
2011EYY-1
2011FXX0
2012LZZ-2
2012EYY3

Expected error 1-1-1-10: Component {comp_name} not found in Dataset {dataset_name}.

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

Whole data set one partition 🟢 pandas ✅ · DuckDB ✅
Tests:

partition except all with the names written as declared (Manual example 2 writes id_1): rank over the whole data set, ordered by Id_2.

Manual:

Analytic invocation: partition except all leaves no identifier to partition by, so the data set is one partition (the rule for an omitted partition clause does not apply). On ties, Rank says “the same order number (rank) is assigned and a gap appears in the sequence of the assigned ranks”.

Expected:

2020Q1 (A and B) ranks 1, 2020Q2 (A and B) ranks 3, 2021Q1 ranks 5 and 2021Q2 ranks 6.

script.vtl
DS_r := DS_1 [ calc Me_3 := rank ( over ( partition except all 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.5000003
A2021Q152.5000005
A2021Q210.5000006
B2020Q146.5000001
B2020Q284.5000003

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

an(over (partition by Id order by Me asc)) 🟢 · any scalar → any type

Ascending rank with tie 🟢 pandas ✅ · DuckDB ✅
Tests:

order by Me_1 asc, with a tie: rank in calc, partitioned by Id_1.

Manual:

Analytic invocation: “in ascending order if asc is specified”; Rank: “the same order number (rank) is assigned and a gap appears in the sequence of the assigned ranks”.

Expected:

in A the two 1s rank 1, 3 ranks 3 and 5 ranks 4; in B 4 ranks 1 and 8 ranks 2.

script.vtl
DS_r := DS_1 [ calc Me_3 := rank ( over ( partition by Id_1 order by Me_1 asc ) ) ];
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.5000003
A2020Q213.5000001
A2021Q152.5000004
A2021Q210.5000001
B2020Q146.5000001
B2020Q284.5000002

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

an(over (partition by Id order by Me desc)) 🟢 · any scalar → any type

Descending rank with tie 🟢 pandas ✅ · DuckDB ✅
Tests:

order by Me_1 desc, with a tie: rank in calc, partitioned by Id_1.

Manual:

Analytic invocation: “in descending order if desc is specified”; Rank: “the same order number (rank) is assigned and a gap appears in the sequence of the assigned ranks”.

Expected:

in A 5 ranks 1, 3 ranks 2 and the two 1s rank 3; in B 8 ranks 1 and 4 ranks 2.

script.vtl
DS_r := DS_1 [ calc Me_3 := rank ( over ( partition by Id_1 order by Me_1 desc ) ) ];
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.5000002
A2020Q213.5000003
A2021Q152.5000001
A2021Q210.5000003
B2020Q146.5000002
B2020Q284.5000001

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

an(Me over (partition by Id order by Me range between n preceding and n following)) 🟢 · any scalar → any type

Range bounds are inclusive 🟢 pandas ✅ · DuckDB ✅
Tests:

a range window of 2 on each side, where values exactly 2 away are inside it.

Manual:

Analytic invocation: the window “includes all the Data Points whose values are in a certain range in respect to the value, for the current Data Point, of the Measure which the analytic is applied to”, and num gives “the maximum difference to consider”. The analytic is applied to Me_1 and ordered by it, so SQL’s range on the order key gives the same window.

Expected:

in A (3, 1, 5, 1), Me_3 is 10 for 3 (window [1, 5]), 5 for each 1 (window [-1, 3]) and 8 for 5 (window [3, 7]); in B, 4 and 8 are alone in their windows.

script.vtl
DS_r := DS_1 [ calc Me_3 := sum ( Me_1 over ( partition by Id_1 order by Me_1 range between 2 preceding and 2 following ) ) ];
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.50000010
A2020Q213.5000005
A2021Q152.5000008
A2021Q210.5000005
B2020Q146.5000004
B2020Q284.5000008

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

an(Me over (... data points between unbounded preceding and unbounded following)) 🟢 · any scalar → any type

Unbounded covers whole partition 🟢 pandas ✅ · DuckDB ✅
Tests:

data points between unbounded preceding and unbounded following, with an order clause.

Manual:

Analytic invocation: “unbounded preceding means that the sliding window starts at the first Data Point of the partition” and “unbounded following indicates that the sliding window ends at the last Data Point of the partition”. With an order clause, an omitted window would be “range between unbounded preceding and current data point” instead.

Expected:

Me_3 is the sum of the whole partition on each data point: 10 in A and 12 in B.

script.vtl
DS_r := DS_1 [ calc Me_3 := sum ( Me_1 over ( partition by Id_1 order by Id_2 data points between unbounded preceding and unbounded following ) ) ];
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.50000010
A2020Q213.50000010
A2021Q152.50000010
A2021Q210.50000010
B2020Q146.50000012
B2020Q284.50000012

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

an(Me over (... data points between n preceding and current data point)) 🟢 · any scalar → any type

One preceding to current 🟢 pandas ✅ · DuckDB ✅
Tests:

data points between 1 preceding and current data point, in the order of Id_2.

Manual:

Analytic invocation: “current data point specifies that the window starts or ends at the current Data Point”; with data points, 1 preceding is the data point before it.

Expected:

in A (3, 1, 5, 1), Me_3 is 3, 3 + 1 = 4, 1 + 5 = 6 and 5 + 1 = 6; in B (4, 8), 4 and 4 + 8 = 12.

script.vtl
DS_r := DS_1 [ calc Me_3 := sum ( Me_1 over ( partition by Id_1 order by Id_2 data points between 1 preceding and current data point ) ) ];
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.5000003
A2020Q213.5000004
A2021Q152.5000006
A2021Q210.5000006
B2020Q146.5000004
B2020Q284.50000012

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

an(Me over (partition by Id order by Id)) 🟢 · any scalar → any type

Running total in time order 🟢 pandas ✅ · DuckDB ✅
Tests:

sum in calc, partitioned by Id_1 and ordered by Id_2, with no window clause.

Manual:

Analytic invocation: “If the whole windowClause is omitted, then the default is either range between unbounded preceding and current data point or data points between unbounded preceding and unbounded following, depending respectively on whether orderClause clause was specified or not”. sdmx-twg/vtl#694 made it the default window of SQL. The Behaviour section of the page still says that the window is the whole partition, the rule of VTL 2.1.

Expected:

in A (3, 1, 5, 1), Me_3 is 3, 3 + 1 = 4, 4 + 5 = 9 and 9 + 1 = 10; in B (4, 8), 4 and 12.

script.vtl
DS_r := DS_1 [ calc Me_3 := sum ( Me_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.5000003
A2020Q213.5000004
A2021Q152.5000009
A2021Q210.50000010
B2020Q146.5000004
B2020Q284.50000012

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

Ties count together 🟢 pandas ✅ · DuckDB ✅
Tests:

sum in calc, partitioned by Id_1 and ordered by Id_2, with no window clause, where (A, 1, x) and (A, 1, y) tie on Id_2.

Manual:

Analytic invocation: with an order clause and no window clause, the default is “range between unbounded preceding and current data point”. sdmx-twg/vtl#694 made it the default window of SQL, whose range takes in the data points that tie with the current one on the order keys.

Expected:

in A (1 and 2 at Id_2 = 1, 3 at 2, 4 at 3), Me_2 is 1 + 2 = 3 on both tied data points, then 6 and 10; in B (5, 6), 5 and 11.

script.vtl
DS_r := DS_1 [ calc Me_2 := sum ( Me_1 over ( partition by Id_1 order by Id_2 ) ) ];
Input DS_1
Id_1Id_2Id_3Me_1
A1x1
A1y2
A2x3
A3x4
B1x5
B2x6
Expected DS_r
Id_1Id_2Id_3Me_1Me_2
A1x13
A1y23
A2x36
A3x410
B1x55
B2x611

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

String order key 🟢 pandas ✅ · DuckDB ✅
Tests:

the default window when the order key is a String, Id_3: x on three data points of A and y on the fourth.

Manual:

Analytic invocation: range “cannot be used for components having the following Data Types: String, Duration and TimeInterval Point”, but the default “range between unbounded preceding and current data point” has no offset to compute on the values of the key. It is read as applying whatever the type of the key, as in SQL.

Expected:

in A, the three data points at x (1, 3 and 4) tie, so Me_2 is 8 on each, and 8 + 2 = 10 at y; in B (5 and 6, both at x), 11 on both.

script.vtl
DS_r := DS_1 [ calc Me_2 := sum ( Me_1 over ( partition by Id_1 order by Id_3 ) ) ];
Input DS_1
Id_1Id_2Id_3Me_1
A1x1
A1y2
A2x3
A3x4
B1x5
B2x6
Expected DS_r
Id_1Id_2Id_3Me_1Me_2
A1x18
A1y210
A2x38
A3x48
B1x511
B2x611

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