Skip to content

feat(duckdb): Add transpilation support for SUBSTR-SUBSTRING functions#7471

Open
fivetran-amrutabhimsenayachit wants to merge 1 commit intomainfrom
RD-1147762-substr-substring
Open

feat(duckdb): Add transpilation support for SUBSTR-SUBSTRING functions#7471
fivetran-amrutabhimsenayachit wants to merge 1 commit intomainfrom
RD-1147762-substr-substring

Conversation

@fivetran-amrutabhimsenayachit
Copy link
Copy Markdown
Collaborator

This PR aims to resolve the following issues:
Issue 1 — Zero start position
Snowflake SUBSTR(str, 0, n) treats 0 as 1. DuckDB returns a shorter string. Fix: clamp 0 → 1.

Issue 2 — Negative length
Snowflake SUBSTR(str, pos, -n) returns ''. DuckDB silently returns wrong data. Fix: clamp negative → 0.

python3 -c 'import sqlglot; print(sqlglot.transpile("SELECT SUBSTR('"'"'testing 1 2 3'"'"', 9, 3) AS basic, SUBSTR('"'"'testing 1 2 3'"'"', 9) AS no_len, SUBSTR('"'"'testing 1 2 3'"'"', 0, 3) AS zero_start, SUBSTR('"'"'testing 1 2 3'"'"', -5, 3) AS neg_start, SUBSTR(NULL, 1, 3) AS null_str, SUBSTR('"'"'testing'"'"', 1, 0) AS zero_len, SUBSTR('"'"'testing 1 2 3'"'"', 5, -3) AS neg_len", read="snowflake", write="duckdb")[0])' | duckdb
┌─────────┬─────────┬────────────┬───────────┬──────────┬──────────┬─────────┐
│  basic  │ no_len  │ zero_start │ neg_start │ null_str │ zero_len │ neg_len │
│ varchar │ varchar │  varchar   │  varchar  │ varchar  │ varchar  │ varchar │
├─────────┼─────────┼────────────┼───────────┼──────────┼──────────┼─────────┤
│ 1 2     │ 1 2 3   │ tes        │ 1 2       │ NULL     │          │         │
└─────────┴─────────┴────────────┴───────────┴──────────┴──────────┴─────────┘

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:RD-1147762-substr-substring, sqlglot version: RD-1147762-substr-substring)
  • baseline (main, sqlglot version: 0.0.1.dev1)

By Dialect

dialect main sqlglot:RD-1147762-substr-substring transitions links
bigquery -> bigquery 22925/22930 passed (100.0%) 21255/21255 passed (100.0%) No change full result / delta
bigquery -> duckdb 1304/1674 passed (77.9%) 0/0 passed (0.0%) Results not found full result / delta
snowflake -> duckdb 1521/2678 passed (56.8%) 1521/2678 passed (56.8%) No change full result / delta
snowflake -> snowflake 65927/65927 passed (100.0%) 65927/65927 passed (100.0%) No change full result / delta
databricks -> databricks 1370/1370 passed (100.0%) 1370/1370 passed (100.0%) No change full result / delta
postgres -> postgres 6042/6042 passed (100.0%) 6042/6042 passed (100.0%) No change full result / delta
redshift -> redshift 7101/7101 passed (100.0%) 7101/7101 passed (100.0%) No change full result / delta

Overall

main: 107722 total, 106190 passed (pass rate: 98.6%), sqlglot version: 0.0.1.dev1

sqlglot:RD-1147762-substr-substring: 104373 total, 103216 passed (pass rate: 98.9%), sqlglot version: RD-1147762-substr-substring

Transitions:
No change

Dialect pair changes: 0 previous results not found, 1 curent results not found

✅ 17 test(s) passed

@georgesittas georgesittas force-pushed the RD-1147762-substr-substring branch from 771cfa0 to 1728111 Compare April 8, 2026 22:04
@georgesittas georgesittas force-pushed the RD-1147762-substr-substring branch from 1728111 to 0d33d57 Compare April 8, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant