Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(duckdb): Add transpilation support for ARRAY_SLICE function#7188

Open
fivetran-amrutabhimsenayachit wants to merge 1 commit intomainfrom
RD-1147629-array-slice
Open

feat(duckdb): Add transpilation support for ARRAY_SLICE function#7188
fivetran-amrutabhimsenayachit wants to merge 1 commit intomainfrom
RD-1147629-array-slice

Conversation

@fivetran-amrutabhimsenayachit
Copy link
Collaborator

Problem : Snowflake is 0-indexed exclusive-end; DuckDB is 1-indexed inclusive-end. SQLGlot generates no index adjustments, producing wrong results for any from > 0 or negative to values.

  Snowflake: 0-indexed from, EXCLUSIVE upper to  →  Python arr[from:to]
  DuckDB:    1-indexed from, INCLUSIVE upper to  →  SQL standard arr[from..to]

Example:
Snowflake: SELECT ARRAY_SLICE([1,2,3,4,5], 1, 3) → [2, 3]
DuckDB naive: ARRAY_SLICE([1,2,3,4,5], 1, 3) → [1, 2, 3] ✗
DuckDB fixed: ARRAY_SLICE([1,2,3,4,5], 2, 3) → [2, 3] ✓

Transpilation:
 python -m sqlglot --read snowflake --write duckdb "SELECT ARRAY_SLICE([1,2,3,4,5], 1, 3) AS sf_1_3, ARRAY_SLICE([1,2,3,4,5], 0, 3) AS sf_0_3, ARRAY_SLICE([1,2,3,4,5], 2, 5) AS sf_2_5, ARRAY_SLICE([1,2,3,4,5], 0, 0) AS sf_0_0, ARRAY_SLICE([1,2,3,4,5], NULL, 3) AS sf_null_from, ARRAY_SLICE([1,2,3,4,5], 1, NULL) AS sf_null_to, ARRAY_SLICE([0,1,2,3,4,5,6], 0, -2) AS sf_pos_neg, ARRAY_SLICE([0,1,2,3,4,5,6], -5, -3) AS sf_both_neg, ARRAY_SLICE(['a','b','c','d'], -2, -1) AS sf_neg_idx, ARRAY_SLICE(NULL, 1, 3) AS sf_null_arr, ARRAY_SLICE([], 1, 3) AS sf_empty_arr" 2>&1 | grep -v UserWarning | grep -v warnings.warn
SELECT
  ARRAY_SLICE([1, 2, 3, 4, 5], 2, 3) AS "sf_1_3",
  ARRAY_SLICE([1, 2, 3, 4, 5], 1, 3) AS "sf_0_3",
  ARRAY_SLICE([1, 2, 3, 4, 5], 3, 5) AS "sf_2_5",
  ARRAY_SLICE([1, 2, 3, 4, 5], 1, 0) AS "sf_0_0",
  ARRAY_SLICE([1, 2, 3, 4, 5], CASE WHEN NULL >= 0 THEN NULL + 1 ELSE NULL END, 3) AS "sf_null_from",
  ARRAY_SLICE([1, 2, 3, 4, 5], 2, CASE WHEN NULL < 0 THEN NULL - 1 ELSE NULL END) AS "sf_null_to",
  ARRAY_SLICE([0, 1, 2, 3, 4, 5, 6], 1, -3) AS "sf_pos_neg",
  ARRAY_SLICE([0, 1, 2, 3, 4, 5, 6], -5, -4) AS "sf_both_neg",
  ARRAY_SLICE(['a', 'b', 'c', 'd'], -2, -2) AS "sf_neg_idx",
  ARRAY_SLICE(NULL, 2, 3) AS "sf_null_arr",
  ARRAY_SLICE([], 2, 3) AS "sf_empty_arr"
  
  Duckdb:
  │ sf_1_3  │  sf_0_3   │  sf_2_5   │ sf_0_0  │ sf_null_from │ sf_null_to │   sf_pos_neg    │ sf_both_neg │ sf_neg_idx │ sf_null_arr │ sf_empty_arr │
│ int32[] │  int32[]  │  int32[]  │ int32[] │   int32[]    │  int32[]   │     int32[]     │   int32[]   │ varchar[]  │    int32    │   int32[]    │
├─────────┼───────────┼───────────┼─────────┼──────────────┼────────────┼─────────────────┼─────────────┼────────────┼─────────────┼──────────────┤
│ [2, 3]  │ [1, 2, 3] │ [3, 4, 5] │ []      │ NULL         │ NULL       │ [0, 1, 2, 3, 4] │ [2, 3]      │ [c]        │    NULL     │ []           │
└─────────┴───────────┴───────────┴─────────┴──────────────┴────────────┴─────────────────┴─────────────┴────────────┴─────────────┴──────────────┘

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:RD-1147629-array-slice, sqlglot version: RD-1147629-array-slice)
  • baseline (main, sqlglot version: 29.0.2.dev29)

⚠️ Limited to dialects: bigquery, duckdb, snowflake

By Dialect

dialect main sqlglot:RD-1147629-array-slice transitions links
bigquery -> bigquery 2592/2624 passed (98.8%) 2592/2624 passed (98.8%) No change full result / delta
bigquery -> duckdb 2077/2623 passed (79.2%) 2078/2623 passed (79.2%) 1 fail -> pass full result / delta
duckdb -> duckdb 4003/4003 passed (100.0%) 4003/4003 passed (100.0%) No change full result / delta
snowflake -> duckdb 1589/2590 passed (61.4%) 1599/2590 passed (61.7%) 10 fail -> pass full result / delta
snowflake -> snowflake 2811/2811 passed (100.0%) 2811/2811 passed (100.0%) No change full result / delta

Overall

main: 14651 total, 13072 passed (pass rate: 89.2%), sqlglot version: 29.0.2.dev29

sqlglot:RD-1147629-array-slice: 14651 total, 13083 passed (pass rate: 89.3%), sqlglot version: RD-1147629-array-slice

Transitions:
11 fail -> pass

@fivetran-amrutabhimsenayachit fivetran-amrutabhimsenayachit changed the title feat(duckdb): Add ttranspilation support for ARRAY_SLICE function feat(duckdb): Add transpilation support for ARRAY_SLICE function Mar 2, 2026
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