Aggregation · aggr

🟢 9 of 9 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
        ---
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["Aggregation · aggr 📌<br/>🟢9"]:::ok
    v_no_group["DS#91;aggr Me #58;#61; agg(Me)#93;"]:::ok
    v_group_by["DS#91;aggr Me #58;#61; agg(Me) group by Id#93;"]:::ok
    v_group_except["DS#91;aggr Me #58;#61; agg(Me) group except Id#93;"]:::ok
    v_group_all["DS#91;aggr Me #58;#61; agg(Me) group all time_agg(#quot;A#quot;)#93;"]:::ok
    v_having["DS#91;aggr Me #58;#61; agg(Me) group by Id having cond#93;"]:::ok
    v_measure_role["DS#91;aggr measure Me #58;#61; agg(Me) group by Id#93;"]:::ok
    v_attribute_role["DS#91;aggr attribute At #58;#61; agg(Me) group by Id#93;"]:::ok
    v_viral_attribute_role["DS#91;aggr viral attribute At #58;#61; agg(Me) group by Id#93; 📌"]:::ok
    v_role_omitted_attribute["DS#91;aggr At #58;#61; agg(At) group by Id#93;"]:::ok
    subgraph G_use["Used with"]
        direction LR
        used__join["Join operators (4)"]:::ext
        used__time["Time operators (1)"]:::ext
        used__aggregate["Aggregate and Analytic operators (11)"]:::ext
    end
    v_no_group --- O
    v_group_by --- O
    v_group_except --- O
    v_group_all --- O
    v_having --- O
    v_measure_role --- O
    v_attribute_role --- O
    O --- v_viral_attribute_role
    O --- v_role_omitted_attribute
    O -.- G_use
    style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
    click v_no_group href "#form-no-group"
    click v_group_by href "#form-group-by"
    click v_group_except href "#form-group-except"
    click v_group_all href "#form-group-all"
    click v_having href "#form-having"
    click v_measure_role href "#form-measure-role"
    click v_attribute_role href "#form-attribute-role"
    click v_viral_attribute_role href "#form-viral-attribute-role"
    click v_role_omitted_attribute href "#form-role-omitted-attribute"
    click used__join href "../join/index.html"
    click used__time href "../time/index.html"
    click used__aggregate href "../aggregate/index.html"
    
Used with — 16 operators in 3 blocks 🟢16
Join operators (4) 🟢4
Time operators (1) 🟢1
Aggregate and Analytic operators (11) 🟢11

Types

Takes boolean, 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

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

Single group no identifiers 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr Me_1 := sum ( Me_1 ) with no grouping clause.

Manual:

Aggregation: “If no grouping clause is specified, then all the input Data Points are aggregated in a single group and the clause returns a Data Set that contains a single Data Point and has no Identifiers.”

Expected:

One data point with no identifiers: the Measure Me_1 (no role written, and Me_1 is a Measure of DS_1) = 2 + 5 + 3 = 10. It may be Integer or Number: Sum 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 := DS_1 [ aggr Me_1 := sum ( Me_1 ) ];
Input DS_1
Id_1Id_2Me_1
1A2
1B5
2A3
Expected DS_r
Me_1
10

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) group by Id] 🟢 · any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr Me_1 := sum( Me_1 ) group by Id_1 , Id_2, over an Integer measure.

Manual:

Aggregation, Example 1, with the manual’s data points: “group by: the Data Points are grouped by the values of the specified Identifiers (groupingId). The Identifiers not specified are dropped in the result.”

Expected:

Id_3 is dropped, and the sums are 2, 8 and 9. Me_1 may be Integer, as the manual’s ex_1.json writes it, or Number: Sum 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 := DS_1 [ aggr Me_1:= sum( Me_1 ) group by Id_1 , Id_2 ];
Input DS_1
Id_1Id_2Id_3Me_1
1AXX0
1AYY2
1BXX3
1BYY5
2AXX7
2AYY2
Expected DS_r
Id_1Id_2Me_1
1A2
1B8
2A9

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) group except Id] 🟢 · any scalar → any type

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr Me_3 := min( Me_1 ) group except Id_3, over an Integer measure.

Manual:

Aggregation, Example 2, output verbatim: “group except: the Data Points are grouped by the values of the Identifiers not specified as groupingId. The Identifiers specified as groupingId are dropped in the result.”

Expected:

Id_1, Id_2 and the smallest Me_1 of each group, 0, 3 and 2. Me_3 stays an Integer, as the manual’s ex_2.json writes it: unlike sum, min allows no other type, as Minimun value says “For numeric operands, the result has the same type as the operand”.

script.vtl
DS_r := DS_1 [ aggr Me_3:= min( Me_1 ) group except Id_3 ];
Input DS_1
Id_1Id_2Id_3Me_1
1AXX0
1AYY2
1BXX3
1BYY5
2AXX7
2AYY2
Expected DS_r
Id_1Id_2Me_3
1A0
1B3
2A2

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) group all time_agg("A")] 🟢 · any scalar → any type

Quarters to years 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr Me_1 := sum ( Me_1 ) group all time_agg ( "A" ), on the quarterly time_period identifier Id_1.

Manual:

Aggregation: group all “converts the values of an Identifier Component using conversionExpr and keeps all the resulting Identifiers”. Time aggregation: in an aggregation “op must not be specified, and the source dataset must have exactly one Identifier of type time, date or time_period”, and on time_period “the original period indicator is converted in the target one”.

Expected:

The data points are grouped by the annual Id_1 and by Id_2: (2020A, A) = 1 + 2 = 3, (2021A, A) = 4 and (2020A, B) = 8, where Me_1 may be Integer or Number, by the integer rule of the Sum page. Aggregate invocation marks group all with “This option is deprecated”; the Aggregation page does not.

script.vtl
DS_r := DS_1 [ aggr Me_1 := sum ( Me_1 ) group all time_agg ( "A" ) ];
Input DS_1
Id_1Id_2Me_1
2020Q1A1
2020Q2A2
2021Q1A4
2020Q1B8
Expected DS_r
Id_1Id_2Me_1
2020AA3
2021AA4
2020AB8

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) group by Id having cond] 🟢 · any scalar, boolean → any type

Manual example 3 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr Me_1 := sum( Me_1 ), Me_2 := max( Me_1) group by Id_1 , Id_2 with having avg (Me_1 ) > 2.

Manual:

Aggregation, Example 3, with the manual’s data points: “only groups for which havingCondition evaluates to true appear in the result”, and the condition “must invoke aggregate operators” (avg here, where the manual and the reading of #1119 agree).

Expected:

(1,B) and (2,A), whose averages are 4 and 4.5, are kept with the sums 8 and 9 and the maxima 5 and 7; (1,A) averages 1. Me_1 may be Integer, as the manual writes it, or Number, by the integer rule of Sum, and Me_2 stays an Integer, as for max “For numeric operands, the result has the same type as the operand”.

script.vtl
DS_r := DS_1 [ aggr Me_1:= sum( Me_1 ), Me_2 := max( Me_1) group by Id_1 , Id_2 having avg (Me_1 ) > 2 ];
Input DS_1
Id_1Id_2Id_3Me_1
1AXX0
1AYY2
1BXX3
1BYY5
2AXX7
2AYY2
Expected DS_r
Id_1Id_2Me_1Me_2
1B85
2A97

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

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

Only the new measure kept 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr measure Me_2 := sum ( Me_1 ) group by Id_1: a new component, with the role measure written.

Manual:

Aggregation: aggrRole ::= measure | attribute | viral attribute, and “The aggr computed Measures and Attributes are the only Measures and Attributes returned in the output Data Set (plus the possible viral Attributes)”.

Expected:

Id_1 and the Measure Me_2, the sums 1 + 5 = 6 and 3 + 4 = 7: group by Id_1 drops Id_2, and Me_1 is not in the result. Me_2 may be Integer or Number: Sum 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 := DS_1 [ aggr measure Me_2 := sum ( Me_1 ) group by Id_1 ];
Input DS_1
Id_1Id_2Me_1
1A1
1B5
2A3
2B4
Expected DS_r
Id_1Me_2
16
27

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

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

Attribute next to measure 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr Me_1 := sum ( Me_1 ), attribute At_1 := max ( Me_1 ) group by Id_1: a new attribute next to the aggregated measure.

Manual:

Aggregation: “It is possible to specify the role of the calculated Component among measure, attribute, or viral attribute”, and “The Attributes calculated through the aggr clauses are maintained in the result”.

Expected:

Per Id_1, with Id_2 dropped, the sums Me_1 = 6 and 7 and the maxima At_1 = 5 and 4, an Integer Attribute, as for max “For numeric operands, the result has the same type as the operand”; Me_1 may be Integer or Number, by the integer rule of Sum. The Me_1 item keeps a Measure in the result: Attribute without measure leaves it out.

script.vtl
DS_r := DS_1 [ aggr Me_1 := sum ( Me_1 ), attribute At_1 := max ( Me_1 ) group by Id_1 ];
Input DS_1
Id_1Id_2Me_1
1A1
1B5
2A3
2B4
Expected DS_r
Id_1Me_1At_1
165
274

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

Attribute without measure 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr attribute At_1 := max ( Me_1 ) group by Id_1: an attribute as the only aggregated component.

Manual:

Aggregation: “The aggr computed Measures and Attributes are the only Measures and Attributes returned in the output Data Set”; the User Manual: “There can be from 0 to N Measures in a Data Structure. A Data Set without Measures is allowed”.

Expected:

Id_1 and the Attribute At_1 = 5 and 4, the maxima, with no Measure: Me_1 is not in the result. At_1 is an Integer, as for max “For numeric operands, the result has the same type as the operand”.

script.vtl
DS_r := DS_1 [ aggr attribute At_1 := max ( Me_1 ) group by Id_1 ];
Input DS_1
Id_1Id_2Me_1
1A1
1B5
2A3
2B4
Expected DS_r
Id_1At_1
15
24

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

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

Viral attribute next to measure 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr Me_1 := sum ( Me_1 ), viral attribute At_1 := max ( Me_1 ) group by Id_1: the aggregation of Attribute next to measure, with the role viral attribute.

Manual:

Aggregation: “The Attributes calculated through the aggr clauses are maintained in the result. For all the other Attributes that are defined as viral, the Attribute propagation rule is applied”.

Expected:

Per Id_1, the sums Me_1 = 6 and 7 and the Integer Viral Attribute At_1 = 5 and 4; DS_1 has no other attribute. Me_1 may be Integer or Number, by the integer rule of Sum, and Viral attribute without measure leaves its item out.

Team decision:

#1257: as the comments on sdmx-twg/vtl#731 propose, a viral Attribute needs a propagation rule only where its values are combined, until the VTL Task Force discusses it.

script.vtl
DS_r := DS_1 [ aggr Me_1 := sum ( Me_1 ), viral attribute At_1 := max ( Me_1 ) group by Id_1 ];
Input DS_1
Id_1Id_2Me_1
1A1
1B5
2A3
2B4
Expected DS_r
Id_1Me_1At_1
165
274

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

Viral attribute without measure 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr viral attribute At_1 := max ( Me_1 ) group by Id_1: the aggregation of Attribute without measure, with the role viral attribute.

Manual:

Aggregation: “The aggr computed Measures and Attributes are the only Measures and Attributes returned in the output Data Set (plus the possible viral Attributes)”; the User Manual: “A Data Set without Measures is allowed”.

Expected:

Id_1 and the Viral Attribute At_1 = 5 and 4, the maxima, with no Measure. DS_1 has no attribute of its own, so no propagation rule is involved.

Team decision:

#1257: as the comments on sdmx-twg/vtl#731 propose, a viral Attribute needs a propagation rule only where its values are combined, until the VTL Task Force discusses it.

script.vtl
DS_r := DS_1 [ aggr viral attribute At_1 := max ( Me_1 ) group by Id_1 ];
Input DS_1
Id_1Id_2Me_1
1A1
1B5
2A3
2B4
Expected DS_r
Id_1At_1
15
24

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

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

Existing attribute keeps role 🟢 pandas ✅ · DuckDB ✅
Tests:

aggr Me_1 := sum ( Me_1 ), At_1 := max ( At_1 ) group by Id_1 with no role: At_1 is an Attribute of DS_1.

Manual:

Aggregation: “When the role is omitted, the following rule is applied: if the component exists in the operand Data Set then it maintains its role”, and “The Attributes calculated through the aggr clauses are maintained in the result”.

Expected:

Per Id_1, the Measure Me_1 = 6 and 7, Integer or Number by the integer rule of the Sum page, and the Integer Attribute At_1 = 7 and 9, as for max “For numeric operands, the result has the same type as the operand”. At_1 stays non-viral, as “a non-viral Attribute in the input generates a non-viral Attribute also in the result” (User Manual).

script.vtl
DS_r := DS_1 [ aggr Me_1 := sum ( Me_1 ), At_1 := max ( At_1 ) group by Id_1 ];
Input DS_1
Id_1Id_2Me_1At_1
1A17
1B52
2A34
2B49
Expected DS_r
Id_1Me_1At_1
167
279

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