Calculation of a Component · calc
---
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
over usesCounting the number of data points count uses, in over()Minimum value min uses, in over()Maximum value max uses, in over()Median value median uses, in over()Sum sum uses, in over()Average value avg uses, in over()Population standard deviation stddev_pop uses, in over()Sample standard deviation stddev_samp uses, in over()Population variance var_pop uses, in over()Sample variance var_samp uses, in over()First value first_value uses, in over()Last value last_value uses, in over()Lag lag uses, in over()Lead lead uses, in over()Rank rank uses, in over()Ratio to report ratio_to_report uses, in over()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 * 2with no role, on the existing measureMe_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_1stays a Measure, with its values doubled: 40, 4 and 4.
DS_r := DS_1 [ calc Me_1:= Me_1 * 2 ];
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| 1 | A | CA | 20 |
| 1 | B | CA | 2 |
| 2 | A | CA | 2 |
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| 1 | A | CA | 40 |
| 1 | B | CA | 4 |
| 2 | A | CA | 4 |
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 * 2with no role:Me_2is not inDS_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_1andMe_1, plus the MeasureMe_2= 20, 40 and 60, an Integer asMe_1 * 2is 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.
DS_r := DS_1 [ calc Me_2 := Me_1 * 2 ];
| Id_1 | Me_1 |
|---|---|
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 10 | 20 |
| 2 | 20 | 40 |
| 3 | 30 | 60 |
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_1is an Attribute ofDS_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_1stays an Attribute, withXon every data point;Id_1andMe_1are unchanged.
DS_r := DS_1 [ calc At_1 := "X" ];
| Id_1 | Me_1 | At_1 |
|---|---|---|
| 1 | 10 | A |
| 2 | 20 | B |
| Id_1 | Me_1 | At_1 |
|---|---|---|
| 1 | 10 | X |
| 2 | 20 | X |
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 rolemeasurewritten.- 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_1andMe_1unchanged, plus the MeasureMe_2= 11 and 21, an Integer: “If the type of both operands is integer then the result has type integer” (Addition).
DS_r := DS_1 [ calc measure Me_2 := Me_1 + 1 ];
| Id_1 | Me_1 |
|---|---|
| 1 | 10 |
| 2 | 20 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 10 | 11 |
| 2 | 20 | 21 |
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 AttributeAt_1recalculated with its own values and the rolemeasure.- 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_1keeps its name and its values, 5 and 6, as a Measure: the result has two Measures,Me_1andAt_1.
DS_r := DS_1 [ calc measure At_1 := At_1 ];
| Id_1 | Me_1 | At_1 |
|---|---|---|
| 1 | 10 | 5 |
| 2 | 20 | 6 |
| Id_1 | Me_1 | At_1 |
|---|---|---|
| 1 | 10 | 5 |
| 2 | 20 | 6 |
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 measureMe_1, whichDS_1declares 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_2is added next toId_1, withAandB;Me_1andMe_2are unchanged. By the team decision of #1010,calc identifierraises SemanticError1-1-1-16over an expression declared nullable, whatever the data (intended), and2-1-1-16when a null reaches a non-nullable one at run time:Me_1is declared non-nullable, the form that the manual and the decision both accept, and the decision does not change this output.
DS_r := DS_1 [ calc identifier Id_2 := Me_1 ];
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | A | 10 |
| 2 | B | 20 |
| Id_1 | Id_2 | Me_1 | Me_2 |
|---|---|---|---|
| 1 | A | A | 10 |
| 2 | B | B | 20 |
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=EPon its three data points.
DS_r := DS_1 [ calc attribute At_1:= "EP" ];
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| 1 | A | CA | 20 |
| 1 | B | CA | 2 |
| 2 | A | CA | 2 |
| Id_1 | Id_2 | Id_3 | Me_1 | At_1 |
|---|---|---|---|---|
| 1 | A | CA | 20 | EP |
| 1 | B | CA | 2 | EP |
| 2 | A | CA | 2 | EP |
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, andAt_1is an Attribute,N.
DS_r := DS_1 [ calc Me_2 := Me_1 + 1, attribute At_1 := "N" ];
| Id_1 | Me_1 |
|---|---|
| 1 | 10 |
| 2 | 20 |
| Id_1 | Me_1 | Me_2 | At_1 |
|---|---|---|---|
| 1 | 10 | 11 | N |
| 2 | 20 | 21 | N |
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 readsMe_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, andMe_2= 100 and 200, from theMe_1of the input (10 and 20), not from the first item; a sequential evaluation would give 110 and 210.
DS_r := DS_1 [ calc Me_1 := Me_1 + 1, Me_2 := Me_1 * 10 ];
| Id_1 | Me_1 |
|---|---|
| 1 | 10 |
| 2 | 20 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 11 | 100 |
| 2 | 21 | 200 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗