Calculation of a Component · calc

🟢 6 of 6 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
        ---
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["Calculation of a Component · calc 📌<br/>🟢6"]:::ok
    v_measure_default["DS#91;calc Me #58;#61; expr#93;"]:::ok
    v_measure_explicit["DS#91;calc measure Me #58;#61; expr#93;"]:::ok
    v_identifier["DS#91;calc identifier Id #58;#61; expr#93;"]:::ok
    v_attribute["DS#91;calc attribute At #58;#61; expr#93;"]:::ok
    v_viral_attribute["DS#91;calc viral attribute At #58;#61; expr#93; 📌"]:::ok
    v_multiple["DS#91;calc Me #58;#61; expr, attribute At #58;#61; expr#93;"]:::ok
    subgraph G_use["Used with"]
        direction LR
        used__join["Join operators (4)"]:::ext
        used__aggregate["Aggregate and Analytic operators (17)"]:::ext
    end
    v_measure_default --- O
    v_measure_explicit --- O
    v_identifier --- O
    v_attribute --- O
    v_viral_attribute --- O
    O --- v_multiple
    O -.- G_use
    style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
    click v_measure_default href "#form-measure-default"
    click v_measure_explicit href "#form-measure-explicit"
    click v_identifier href "#form-identifier"
    click v_attribute href "#form-attribute"
    click v_viral_attribute href "#form-viral-attribute"
    click v_multiple href "#form-multiple"
    click used__join href "../join/index.html"
    click used__aggregate href "../aggregate/index.html"
    
Used with — 21 operators in 2 blocks 🟢21
Join operators (4) 🟢4
Aggregate and Analytic operators (17) 🟢17

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[calc Me := expr] 🟢 · any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

calc Me_1 := Me_1 * 2 with no role, on the existing measure Me_1.

Manual:

Calculation of a Component, Example 1, output verbatim: “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 “If the Calculated component is a Measure or an Attribute that already exists in the input Data Set, the calculated values overwrite the original values”.

Expected:

Me_1 stays a Measure, with its values doubled: 40, 4 and 4.

script.vtl
DS_r := DS_1 [ calc Me_1:= Me_1 * 2 ];
Input DS_1
Id_1Id_2Id_3Me_1
1ACA20
1BCA2
2ACA2
Expected DS_r
Id_1Id_2Id_3Me_1
1ACA40
1BCA4
2ACA4

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

New component is measure 🟢 pandas ✅ · DuckDB ✅
Tests:

calc Me_2 := Me_1 * 2 with no role: Me_2 is not in DS_1.

Manual:

Calculation of a Component: “When the role is omitted, the following rule is applied: … if the component does not exist in the operand Data Set then its role is Measure”, and “If the calculated Component is a new Component, it is added to the output Data Set”.

Expected:

Id_1 and Me_1, plus the Measure Me_2 = 20, 40 and 60, an Integer as Me_1 * 2 is in Manual example 1. Manual example 1 covers the other half of the rule, an existing measure, and Existing attribute keeps role an existing attribute.

script.vtl
DS_r := DS_1 [ calc Me_2 := Me_1 * 2 ];
Input DS_1
Id_1Me_1
110
220
330
Expected DS_r
Id_1Me_1Me_2
11020
22040
33060

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

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

calc At_1 := "X" with no role: At_1 is an Attribute of DS_1.

Manual:

Calculation of a Component: “if the component exists in the operand Data Set then it maintains its role”, and “If the Calculated component is a Measure or an Attribute that already exists in the input Data Set, the calculated values overwrite the original values”.

Expected:

At_1 stays an Attribute, with X on every data point; Id_1 and Me_1 are unchanged.

script.vtl
DS_r := DS_1 [ calc At_1 := "X" ];
Input DS_1
Id_1Me_1At_1
110A
220B
Expected DS_r
Id_1Me_1At_1
110X
220X

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

DS[calc measure Me := expr] 🟢 · any scalar → any type

Explicit role on new component 🟢 pandas ✅ · DuckDB ✅
Tests:

calc measure Me_2 := Me_1 + 1: a new component, with the role measure written.

Manual:

Calculation of a Component: “It is possible to specify the role of the calculated Component among measure, identifier, attribute, or viral attribute”, and “If the calculated Component is a new Component, it is added to the output Data Set”.

Expected:

Id_1 and Me_1 unchanged, plus the Measure Me_2 = 11 and 21, an Integer: “If the type of both operands is integer then the result has type integer” (Addition).

script.vtl
DS_r := DS_1 [ calc measure Me_2 := Me_1 + 1 ];
Input DS_1
Id_1Me_1
110
220
Expected DS_r
Id_1Me_1Me_2
11011
22021

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

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

calc measure At_1 := At_1: the Attribute At_1 recalculated with its own values and the role measure.

Manual:

Calculation of a Component: “It is possible to specify the role of the calculated Component among measure, identifier, attribute, or viral attribute, therefore the calc clause can be used also to change the role of a Component when possible.”

Expected:

At_1 keeps its name and its values, 5 and 6, as a Measure: the result has two Measures, Me_1 and At_1.

script.vtl
DS_r := DS_1 [ calc measure At_1 := At_1 ];
Input DS_1
Id_1Me_1At_1
1105
2206
Expected DS_r
Id_1Me_1At_1
1105
2206

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

DS[calc identifier Id := expr] 🟢 · any scalar → any type

New identifier from measure 🟢 pandas ✅ · DuckDB ✅
Tests:

calc identifier Id_2 := Me_1: a new identifier with the values of the String measure Me_1, which DS_1 declares non-nullable.

Manual:

Calculation of a Component: “The operator calculates new Identifier, Measure or Attribute Components on the basis of sub-expressions at Component level”, and “If the calculated Component is a new Component, it is added to the output Data Set”.

Expected:

Id_2 is added next to Id_1, with A and B; Me_1 and Me_2 are unchanged. By the team decision of #1010, calc identifier raises SemanticError 1-1-1-16 over an expression declared nullable, whatever the data (intended), and 2-1-1-16 when a null reaches a non-nullable one at run time: Me_1 is declared non-nullable, the form that the manual and the decision both accept, and the decision does not change this output.

script.vtl
DS_r := DS_1 [ calc identifier Id_2 := Me_1 ];
Input DS_1
Id_1Me_1Me_2
1A10
2B20
Expected DS_r
Id_1Id_2Me_1Me_2
1AA10
2BB20

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

DS[calc attribute At := expr] 🟢 · any scalar → any type

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

calc attribute At_1 := "EP": a new attribute with a constant value.

Manual:

Calculation of a Component, Example 2, output verbatim: “It is possible to specify the role of the calculated Component among measure, identifier, attribute, or viral attribute”, and “If the calculated Component is a new Component, it is added to the output Data Set”.

Expected:

The input, plus the String Attribute At_1 = EP on its three data points.

script.vtl
DS_r := DS_1 [ calc attribute At_1:= "EP" ];
Input DS_1
Id_1Id_2Id_3Me_1
1ACA20
1BCA2
2ACA2
Expected DS_r
Id_1Id_2Id_3Me_1At_1
1ACA20EP
1BCA2EP
2ACA2EP

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

DS[calc viral attribute At := expr] 🟢 · any scalar → any type

Constant viral attribute 🟢 pandas ✅ · DuckDB ✅
Tests:

calc viral attribute At_1 := "EP": the calculation of Manual example 2 of the attribute form, with the role viral attribute.

Manual:

Calculation of a Component: “The keyword viral allows controlling the virality of the calculated Attributes”.

Expected:

The input, plus the Viral Attribute At_1 = EP on every data point. calc combines no data points, so no propagation rule is involved: the attribute is only created.

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 [ calc viral attribute At_1 := "EP" ];
Input DS_1
Id_1Me_1
110
220
Expected DS_r
Id_1Me_1At_1
110EP
220EP

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

DS[calc Me := expr, attribute At := expr] 🟢 · any scalar → any type

Default and explicit roles 🟢 pandas ✅ · DuckDB ✅
Tests:

calc Me_2 := Me_1 + 1, attribute At_1 := "N": two calculated components in one clause, each with its own role.

Manual:

Calculation of a Component: “Each Component is calculated through an independent sub-expression”, and when the role is omitted, “if the component does not exist in the operand Data Set then its role is Measure”.

Expected:

Both are added to DS_1: Me_2, a new component with no role, is a Measure, 11 and 21, and At_1 is an Attribute, N.

script.vtl
DS_r := DS_1 [ calc Me_2 := Me_1 + 1, attribute At_1 := "N" ];
Input DS_1
Id_1Me_1
110
220
Expected DS_r
Id_1Me_1Me_2At_1
11011N
22021N

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

Items are independent 🟢 pandas ✅ · DuckDB ✅
Tests:

calc Me_1 := Me_1 + 1, Me_2 := Me_1 * 10: the second item reads Me_1, which the first one recalculates.

Manual:

Calculation of a Component: “The calcExpr sub-expressions are independent one another, they can only reference Components of the input Data Set and cannot use Components generated, for example, by other calcExpr.”

Expected:

Me_1 = 11 and 21, and Me_2 = 100 and 200, from the Me_1 of the input (10 and 20), not from the first item; a sequential evaluation would give 110 and 210.

script.vtl
DS_r := DS_1 [ calc Me_1 := Me_1 + 1, Me_2 := Me_1 * 10 ];
Input DS_1
Id_1Me_1
110
220
Expected DS_r
Id_1Me_1Me_2
111100
221200

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