Skip to content

feat(duckdb): Add transpilation support for neg position args for CHARINDEX#7334

Open
fivetran-amrutabhimsenayachit wants to merge 2 commits intomainfrom
RD-1147696-charindex
Open

feat(duckdb): Add transpilation support for neg position args for CHARINDEX#7334
fivetran-amrutabhimsenayachit wants to merge 2 commits intomainfrom
RD-1147696-charindex

Conversation

@fivetran-amrutabhimsenayachit
Copy link
Collaborator

CHARINDEX did not transpile as expected for neg position arg.
This PR fixes the same.

snow sql -q "SELECT CHARINDEX('l', 'hello world', -2)"
 CHARINDEX('L', 'HELLO WORLD', -2) |
|-----------------------------------|
| 3                                 |

python -m sqlglot --read snowflake --write duckdb "SELECT CHARINDEX('l', 'hello world', -2)"
SELECT
  CASE
    WHEN STRPOS(SUBSTRING('hello world', CASE WHEN -2 <= 0 THEN 1 ELSE -2 END), 'l') = 0
    THEN 0
    ELSE STRPOS(SUBSTRING('hello world', CASE WHEN -2 <= 0 THEN 1 ELSE -2 END), 'l') + CASE WHEN -2 <= 0 THEN 1 ELSE -2 END - 1
  END
  
Duckdb Result
│      3 │

@github-actions
Copy link
Contributor

github-actions bot commented Mar 18, 2026

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:RD-1147696-charindex, sqlglot version: RD-1147696-charindex)
  • baseline (main, sqlglot version: 0.0.1.dev1)

⚠️ Limited to dialects: duckdb, snowflake

By Dialect

dialect main sqlglot:RD-1147696-charindex transitions links
duckdb -> duckdb 4003/4004 passed (100.0%) 4003/4004 passed (100.0%) No change full result / delta
snowflake -> duckdb 1508/2674 passed (56.4%) 1508/2674 passed (56.4%) No change full result / delta
snowflake -> snowflake 65910/65910 passed (100.0%) 65910/65910 passed (100.0%) No change full result / delta

Overall

main: 72588 total, 71421 passed (pass rate: 98.4%), sqlglot version: 0.0.1.dev1

sqlglot:RD-1147696-charindex: 72588 total, 71421 passed (pass rate: 98.4%), sqlglot version: RD-1147696-charindex

Transitions:
No change

Comment on lines +2566 to +2571
position = expression.args.get("position")
if (
expression.args.get("clamp_position")
and position
and not (position.is_number and position.to_py() > 0)
):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should apply the logic on the transpiled query, right ?

WITH t as (select -1 as p) SELECT CHARINDEX('l', 'hello world', p) from t

We don't cover this ^ case. On parse time we don't know the value of p.

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.

2 participants