Skip to content

fix(databricks)!: transpile TRY_DIVIDE to/from other dialects [CLAUDE]#7489

Merged
georgesittas merged 1 commit intotobymao:mainfrom
honeydew-ai:fix/databricks-try-divide
Apr 14, 2026
Merged

fix(databricks)!: transpile TRY_DIVIDE to/from other dialects [CLAUDE]#7489
georgesittas merged 1 commit intotobymao:mainfrom
honeydew-ai:fix/databricks-try-divide

Conversation

@baruchoxman
Copy link
Copy Markdown
Contributor

@baruchoxman baruchoxman commented Apr 10, 2026

Summary

Databricks' TRY_DIVIDE(a, b) returns NULL when the divisor is 0 (instead of raising an error). Previously it was left as exp.Anonymous, so it passed through untranspiled.

TRY_DIVIDE also exists in Spark (https://spark.apache.org/docs/latest/api/sql/index.html#try_divide), so this fix places the parsing and generation logic in the Spark dialect — Databricks inherits it automatically.

The fix maps TRY_DIVIDE to the existing exp.SafeDivide, which already encodes the exact same semantics. The base generator's safedivide_sql then handles transpilation to all target dialects correctly.

Changes:

  • parsers/spark.py: parse TRY_DIVIDE(a, b)exp.SafeDivide.from_arg_list (alongside TRY_ADD, TRY_MULTIPLY, TRY_SUBTRACT)
  • generators/spark.py: emit native TRY_DIVIDE when targeting Spark/Databricks

Transpilation examples:

Source Target Output
Spark / Databricks Spark / Databricks TRY_DIVIDE(a, b)
Spark / Databricks Snowflake IFF(b <> 0, a / b, NULL)
Spark / Databricks DuckDB CASE WHEN b <> 0 THEN a / b ELSE NULL END

Fixes #7312

Test plan

  • test_try_divide in tests/dialects/test_spark.py — verifies parsing from both spark and databricks and cross-dialect transpilation
  • Full Spark and Databricks test suites pass

@baruchoxman baruchoxman force-pushed the fix/databricks-try-divide branch 2 times, most recently from c444b88 to 4934a87 Compare April 10, 2026 05:32
@geooo109 geooo109 self-assigned this Apr 14, 2026
Comment thread sqlglot/parsers/databricks.py Outdated
Comment thread tests/dialects/test_databricks.py
@baruchoxman baruchoxman force-pushed the fix/databricks-try-divide branch 2 times, most recently from 0cfe5c2 to b9b2cb9 Compare April 14, 2026 13:45
Databricks' TRY_DIVIDE(a, b) returns NULL on division by zero. It is
now mapped to exp.SafeDivide, which already has the correct semantics
and generates appropriate SQL for all target dialects.

Closes tobymao#7312
@baruchoxman baruchoxman force-pushed the fix/databricks-try-divide branch from b9b2cb9 to 2b37400 Compare April 14, 2026 13:55
@baruchoxman
Copy link
Copy Markdown
Contributor Author

@geooo109 handled the comments, please take another look

Copy link
Copy Markdown
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution, @baruchoxman.

@georgesittas georgesittas changed the title fix(databricks): transpile TRY_DIVIDE to/from other dialects [CLAUDE] fix(databricks)!: transpile TRY_DIVIDE to/from other dialects [CLAUDE] Apr 14, 2026
@georgesittas georgesittas merged commit 98ca4cd into tobymao:main Apr 14, 2026
9 checks passed
fivetran-amrutabhimsenayachit pushed a commit that referenced this pull request Apr 14, 2026
#7489)

Databricks' TRY_DIVIDE(a, b) returns NULL on division by zero. It is
now mapped to exp.SafeDivide, which already has the correct semantics
and generates appropriate SQL for all target dialects.

Closes #7312
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.

Databricks TRY_DIVIDE function is not correctly transpiled to snowflake and duckdb

3 participants