Skip to content

Fix GREATEST with single argument simplified in Databricks SQL output#2328

Open
moomindani wants to merge 1 commit intodatabrickslabs:mainfrom
moomindani:fix/2308-greatest-single-arg
Open

Fix GREATEST with single argument simplified in Databricks SQL output#2328
moomindani wants to merge 1 commit intodatabrickslabs:mainfrom
moomindani:fix/2308-greatest-single-arg

Conversation

@moomindani
Copy link

Summary

Fixes #2308

Spark SQL requires GREATEST to have more than one argument:

The `greatest` requires > 1 parameters but the actual number is 1.

Oracle (and other dialects) allow GREATEST(single_expr) as a redundant no-op wrapper. This PR adds a custom _greatest_sql transform in lakebridge's Databricks generator that:

  • Single argument: unwraps GREATEST(expr)expr
  • Multiple arguments: keeps the normal GREATEST(a, b, ...) behaviour unchanged

Example

Input (Oracle) Output before Output after
GREATEST(NVL(col, TO_DATE('1000-01-01', 'YYYY-MM-DD'))) GREATEST(COALESCE(col, TO_DATE('1000-01-01'))) COALESCE(col, TO_DATE('1000-01-01'))
GREATEST(col1) GREATEST(col1) col1
GREATEST(a, b) GREATEST(a, b) GREATEST(a, b)

Changes

  • src/databricks/labs/lakebridge/transpiler/sqlglot/generator/databricks.py: Add _greatest_sql function and register it in TRANSFORMS
  • tests/resources/functional/oracle/functions/test_greatest_1.sql: Multi-arg GREATEST (no change)
  • tests/resources/functional/oracle/functions/test_greatest_2.sql: Single-arg GREATEST with NVL → simplifies
  • tests/resources/functional/oracle/functions/test_greatest_3.sql: Single-arg GREATEST with column → simplifies

Test plan

  • tests/unit/transpiler/test_oracle.py — all 4 cases pass (including 3 new)
  • tests/unit/transpiler/test_snowflake.py::test_snowflake[test_greatest_1] — multi-arg case unchanged

🤖 Generated with Claude Code

databrickslabs#2308)

Spark SQL requires GREATEST to have more than one argument, but Oracle (and
other dialects) allow a single-argument form as a no-op wrapper. Add a custom
`_greatest_sql` transform in the Databricks generator that unwraps
`GREATEST(expr)` → `expr` when only one argument is present, while keeping
the normal `GREATEST(a, b, ...)` behaviour for multiple arguments.

Add three oracle functional test cases covering single-column, single NVL
expression, and multi-argument GREATEST to prevent regressions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Mar 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.43%. Comparing base (7ff77c2) to head (661a60d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2328      +/-   ##
==========================================
+ Coverage   66.41%   66.43%   +0.01%     
==========================================
  Files          99       99              
  Lines        9094     9098       +4     
  Branches      974      975       +1     
==========================================
+ Hits         6040     6044       +4     
  Misses       2878     2878              
  Partials      176      176              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@asnare asnare left a comment

Choose a reason for hiding this comment

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

I'm afraid this PR doesn't address #2308 as intended: the reported issue reports a problem in the Switch transpiler/component, and that's not part of this repository.

The files that have been modified are part of a legacy transpiler that is not in use; it's currently dead code and scheduled for removal.

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.

[BUG]: GREATEST with single param does not get simplified

2 participants