Sub-string extraction · substr

🟢 8 of 8 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["Sub-string extraction · substr<br/>🟢8"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds_start_len["substr(DS, start, length)"]:::ok
        v_ds_start["substr(DS, start)"]:::ok
        v_ds_start_placeholder["substr(DS, start, _)"]:::ok
        v_ds_placeholder_len["substr(DS, _, length)"]:::ok
        v_ds_noargs["substr(DS)"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp_start_len["substr(comp, start, length)"]:::ok
        v_comp_params["substr(comp, comp, comp)"]:::ok
    end
    subgraph G_sc["Scalar"]
        direction LR
        v_sc_start_len["substr(sc, start, length)"]:::ok
    end
    O --- G_ds
    O --- G_comp
    O --- G_sc
    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_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
    click v_ds_start_len href "#form-ds-start-len"
    click v_ds_start href "#form-ds-start"
    click v_ds_start_placeholder href "#form-ds-start-placeholder"
    click v_ds_placeholder_len href "#form-ds-placeholder-len"
    click v_ds_noargs href "#form-ds-noargs"
    click v_comp_start_len href "#form-comp-start-len"
    click v_comp_params href "#form-comp-params"
    click v_sc_start_len href "#form-sc-start-len"
    

Types

Takes integer, string · Gives string

Comes from · integer, string — 27 operators in 6 blocks 🟢27
General purpose operators (2) 🟢2
String operators (9) 🟢9
Numeric operators (4) 🟢4
Time operators (7) 🟢7
Aggregate and Analytic operators (2) 🟢2
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 37 operators in 8 blocks 🟢37
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Time operators (3) 🟢3
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Goes to · string — 16 operators in 5 blocks 🟢16
General purpose operators (1) 🟢1
String operators (10) 🟢10
Comparison operators (1) 🟢1
Data validation operators (1) 🟢1
Definitions (VTL-DL) (3) 🟢3
Goes to · operators that take any scalar — 49 operators in 9 blocks 🟢49
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (10) 🟢10
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Definitions (VTL-DL) (1) 🟢1

Forms

substr(DS, start, length) 🟢 · string, integer → string

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

substr(DS_1, 1, 5): the first five characters of every value of both measures.

Manual:

Sub-string extraction, Example 2, output verbatim: “The substring starts from the startth character of the input string and has a number of characters equal to the length parameter.”

Expected:

Me_1 is hello, abcde and pqrst; Me_2 is mediu, short and "this " (with its trailing space).

script.vtl
DS_r:= substr ( DS_1 , 1 , 5 );
Input DS_1
Id_1Id_2Me_1Me_2
1Ahello worldmedium size text
1Babcdefghilmnoshort text
2Apqrstuvwxyzthis is a long description
Expected DS_r
Id_1Id_2Me_1Me_2
1Ahellomediu
1Babcdeshort
2Apqrstthis

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

substr(DS, start) 🟢 · string, integer → string

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

substr(DS_1, 7): every value of both measures from the 7th character to its end.

Manual:

Sub-string extraction, Example 1, output verbatim: “If length is omitted or overcomes the length of the input string, the substring ends at the end of the input string.”

Expected:

Me_1 is world, ghilmno and vwxyz; Me_2 is " size text" (with its leading space), text and s a long description.

script.vtl
DS_r:= substr ( DS_1 , 7 );
Input DS_1
Id_1Id_2Me_1Me_2
1Ahello worldmedium size text
1Babcdefghilmnoshort text
2Apqrstuvwxyzthis is a long description
Expected DS_r
Id_1Id_2Me_1Me_2
1Aworld size text
1Bghilmnotext
2Avwxyzs a long description

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

substr(DS, start, _) 🟢 · string, integer → string

Placeholder for length 🟢 pandas ✅ · DuckDB ✅
Tests:

the placeholder _ for length: substr(DS_1, 2, _) is substr(DS_1, 2) with the underscore written out.

Manual:

In the invocation, a non-specified optional operand “is substituted by an underscore or, if it is in the end of the invocation, can be omitted” (Overview and conventions, syntax conventions); Sub-string extraction: “If length is omitted or overcomes the length of the input string, the substring ends at the end of the input string.”

Expected:

each value loses its first character: hello gives ello and ab gives b. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := substr(DS_1, 2, _);
Input DS_1
Id_1Me_1
1hello
2ab
3
Expected DS_r
Id_1Me_1
1ello
2b
3

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

substr(DS, _, length) 🟢 · string, integer → string

Placeholder for start 🟢 pandas ✅ · DuckDB ✅
Tests:

the placeholder _ for start in substr(DS_1, _, 3).

Manual:

Sub-string extraction: “If start is omitted, the substring starts from the 1st position.” and “If length is omitted or overcomes the length of the input string, the substring ends at the end of the input string.” Its valid syntaxes include substr ( DS_1, _ , 3 ).

Expected:

each value keeps its first three characters: hello world gives hel, and ab gives ab, as the length of 3 overcomes it. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := substr(DS_1, _, 3);
Input DS_1
Id_1Me_1
1hello world
2ab
3
Expected DS_r
Id_1Me_1
1hel
2ab
3

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

substr(DS) 🟢 · string → string

Start and length omitted 🟢 pandas ✅ · DuckDB ✅
Tests:

substr(DS_1) with neither start nor length.

Manual:

Sub-string extraction: “If start is omitted, the substring starts from the 1st position.” and “If length is omitted or overcomes the length of the input string, the substring ends at the end of the input string.”

Expected:

the substring runs from the 1st position to the end, so every value comes out as it went in. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := substr(DS_1);
Input DS_1
Id_1Me_1
1hello world
2ab
3
Expected DS_r
Id_1Me_1
1hello world
2ab
3

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

substr(comp, start, length) 🟢 · string, integer → string

Manual example 3 🟢 pandas ✅ · DuckDB ✅
Tests:

substr(Me_2, 1, 5) inside calc, where the result replaces the measure Me_2.

Manual:

Sub-string extraction, Example 3, output verbatim: “The substring starts from the startth character of the input string and has a number of characters equal to the length parameter.”

Expected:

Me_2 is mediu, short and "this " (with its trailing space); Me_1 is unchanged.

script.vtl
DS_r:= DS_1 [ calc Me_2:= substr ( Me_2 , 1 , 5 ) ];
Input DS_1
Id_1Id_2Me_1Me_2
1Ahello worldmedium size text
1Babcdefghilmnoshort text
2Apqrstuvwxyzthis is a long description
Expected DS_r
Id_1Id_2Me_1Me_2
1Ahello worldmediu
1Babcdefghilmnoshort
2Apqrstuvwxyzthis

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

substr(comp, comp, comp) 🟢 · string, integer → string

Start and length from components 🟢 pandas ✅ · DuckDB ✅
Tests:

substr(Me_1, Me_2, Me_3) inside calc: start and length come from components, a different pair for each data point.

Manual:

Sub-string extraction types start as component<integer [value >= 1]> | integer [value >= 1] and length as component<integer [value >= 0]> | integer [value >= 0], and “If length is omitted or overcomes the length of the input string, the substring ends at the end of the input string.”

Expected:

Me_4 is world, bcd and bc: abc has only two characters from the 2nd, and the length of 10 overcomes them. A null operand gives a null result: “most of the scalar operations (arithmetic, logical, and others) return null when any of their arguments is null” (User Manual), as the manual’s length and instr examples show.

script.vtl
DS_r := DS_1[calc Me_4 := substr(Me_1, Me_2, Me_3)];
Input DS_1
Id_1Me_1Me_2Me_3
1hello world75
2abcdef23
3abc210
412
Expected DS_r
Id_1Me_1Me_2Me_3Me_4
1hello world75world
2abcdef23bcd
3abc210bc
412

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

substr(sc, start, length) 🟢 · string, integer → string

Substring inside the string 🟢 pandas ✅ · DuckDB ✅
Tests:

substr(SC_1, SC_2, SC_3) with SC_1 = abcdefghijklmnopqrstuvwxyz, SC_2 = 5 and SC_3 = 10: a start and a length that fit in the string.

Manual:

Sub-string extraction: “The substring starts from the startth character of the input string and has a number of characters equal to the length parameter.”, as in substr ("abcdefghijklmnopqrstuvwxyz", 5 , 10 ) gives "efghijklmn".

Expected:

SC_r is efghijklmn, a String: the 10 characters from the 5th, e, on.

script.vtl
SC_r := substr(SC_1, SC_2, SC_3);
Input scalars
SC_1SC_2SC_3
abcdefghijklmnopqrstuvwxyz510
Expected scalars

Name

Type

Value

SC_r

String

"efghijklmn"

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

Length beyond the end 🟢 pandas ✅ · DuckDB ✅
Tests:

substr(SC_1, SC_2, SC_3) with SC_1 = abcdefghijklmnopqrstuvwxyz, SC_2 = 25 and SC_3 = 10, where length overcomes what is left of the string.

Manual:

Sub-string extraction: “If length is omitted or overcomes the length of the input string, the substring ends at the end of the input string.”, and substr ("abcdefghijklmnopqrstuvwxyz", 25 , 10 ) gives "yz".

Expected:

SC_r is yz, a String: only two characters are left from the 25th.

script.vtl
SC_r := substr(SC_1, SC_2, SC_3);
Input scalars
SC_1SC_2SC_3
abcdefghijklmnopqrstuvwxyz2510
Expected scalars

Name

Type

Value

SC_r

String

"yz"

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

Start beyond the end 🟢 pandas ✅ · DuckDB ✅
Tests:

substr(SC_1, SC_2, SC_3) with SC_1 = abcdefghijklmnopqrstuvwxyz, SC_2 = 30 and SC_3 = 10, where start is past the end of the string.

Manual:

Sub-string extraction: “If start is greater than the length of the input string, an empty string is extracted.”, and substr ("abcdefghijklmnopqrstuvwxyz", 30 , 10 ) gives "".

Expected:

SC_r is the empty string. The map compares an empty value and a null as the same one, so this example cannot tell the two apart.

script.vtl
SC_r := substr(SC_1, SC_2, SC_3);
Input scalars
SC_1SC_2SC_3
abcdefghijklmnopqrstuvwxyz3010
Expected scalars

Name

Type

Value

SC_r

String

""

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