Aggregate invocation · group by

🟢 4 of 4 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
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["Aggregate invocation · group by<br/>🟢4"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_group_by["agg(DS group by Id)"]:::ok
        v_group_by_two["agg(DS group by Id, Id)"]:::ok
        v_no_group["agg(DS)"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_aggr_multiple["DS#91;aggr Me #58;#61; agg(Me), Me #58;#61; agg(Me) group by Id#93;"]:::ok
    end
    subgraph G_use["Used with"]
        direction LR
        used__clause__aggr["aggr ↗<br/><small>used in</small>"]:::ext
        used__time__time_agg["time_agg ↗<br/><small>uses, in group all</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_group_by href "#form-group-by"
    click v_group_by_two href "#form-group-by-two"
    click v_no_group href "#form-no-group"
    click v_aggr_multiple href "#form-aggr-multiple"
    click used__clause__aggr href "../clause/aggr.html" "Aggregation · aggr"
    click used__time__time_agg href "../time/time_agg.html" "Time aggregation · time_agg"
    

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

agg(DS group by Id) 🟢 · any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

avg at data set level grouped by Id_1, on an operand with the non-viral attribute At_1.

Manual:

Aggregate invocation, Example 1, output verbatim: “For invocation at Data Set level, the Attribute propagation rule is applied”.

Expected:

At_1 is dropped, as a non-viral attribute; 2010 gives (20 + 1 + 1 + 23) / 4 = 11.25, 2011 gives 10 and 2012 gives 35.

Erratum:

the script comments its two alternative forms with #, which is the membership operator, not a VTL comment. The manual’s conventions define /* */ and //, so they are commented with // here.

script.vtl
DS_r := avg ( DS_1 group by Id_1 );

// Or equivalently:
// DS_r := avg ( DS_1 group except Id_2, Id_3 )
// DS_r := avg ( DS_1#Me_1 group by Id_1 )
Input DS_1
Id_1Id_2Id_3Me_1At_1
2010EXX20
2010BXX1H
2010RXX1A
2010FYY23
2011EXX20P
2011BZZ1N
2011RYY-1P
2011FXX20Z
2012LZZ40P
2012EYY30P
Expected DS_r
Id_1Me_1
201011.25
201110.0
201235.0

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

agg(DS group by Id, Id) 🟢 · any scalar → any type

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

sum grouped by two identifiers, Id_1 and Id_3.

Manual:

Aggregate invocation, Example 2, with the manual’s data points: “the Data Points are grouped by the values of the specified Identifiers. The Identifiers not specified are dropped in the result.”

Expected:

one data point per pair, without Id_2: 2010, XX gives 20 + 1 + 1 = 22. Me_1 may be Integer, as the manual prints it, or Number: the Sum page says “implementations may preserve the integer type for the resulting measure or component when all operand values are integer and the sum is computed in integer arithmetic”.

script.vtl
DS_r := sum ( DS_1 group by Id_1, Id_3 );
Input DS_1
Id_1Id_2Id_3Me_1At_1
2010EXX20
2010BXX1H
2010RXX1A
2010FYY23
2011EXX20P
2011BZZ1N
2011RYY-1P
2011FXX20Z
2012LZZ40P
2012EYY30P
Expected DS_r
Id_1Id_3Me_1
2010XX22
2010YY23
2011XX40
2011ZZ1
2011YY-1
2012ZZ40
2012YY30

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

agg(DS) 🟢 · any scalar → any type

Manual example 3 🟢 pandas ✅ · DuckDB ✅
Tests:

avg with no grouping clause: the whole operand is one group.

Manual:

Aggregate invocation, Example 3, output verbatim: “If no grouping clause is specified, then all the input Data Points are aggregated in a single group and the operator returns a Data Set that contains a single Data Point and has no Identifiers.”

Expected:

one data point with no identifiers: Me_1 = 155 / 10 = 15.5, a Number, and the non-viral At_1 is dropped.

script.vtl
DS_r := avg ( DS_1 );
Input DS_1
Id_1Id_2Id_3Me_1At_1
2010EXX20
2010BXX1H
2010RXX1A
2010FYY23
2011EXX20P
2011BZZ1N
2011RYY-1P
2011FXX20Z
2012LZZ40P
2012EYY30P
Expected DS_r
Me_1
15.5

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

DS[aggr Me := agg(Me), Me := agg(Me) group by Id] 🟢 · any scalar → any type

Manual example 4 🟢 pandas ✅ · DuckDB ✅
Tests:

two aggregates of one measure in aggr, max and min grouped by Id_1, on an operand with the non-viral attribute At_1.

Manual:

Aggregate invocation, Example 4: “the Attributes calculated within the aggr clause are maintained in the result; for all the other Attributes that are defined as viral, the Attribute propagation rule is applied”.

Expected:

Id_1, Me_2 and Me_3, Integer as Me_1: 23 and 1 for 2010, 20 and -1 for 2011, 40 and 30 for 2012.

Erratum:

the manual’s result is named DS_3 and keeps At_1 as a ViralAttribute, but the script assigns DS_r and DS_1 declares At_1 a plain Attribute. The User Manual says the Attribute “is considered as non-viral” when its virality is not defined, so At_1, neither calculated nor viral, is dropped, as in the page’s other three examples.

script.vtl
DS_r := DS_1 [ aggr Me_2 := max ( Me_1 ) , Me_3 := min ( Me_1 ) group by Id_1 ];
Input DS_1
Id_1Id_2Id_3Me_1At_1
2010EXX20
2010BXX1H
2010RXX1A
2010FYY23
2011EXX20P
2011BZZ1N
2011RYY-1P
2011FXX20Z
2012LZZ40P
2012EYY30P
Expected DS_r
Id_1Me_2Me_3
2010231
201120-1
20124030

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