Skip to content

feat(spark, databricks)!: Support for DATE_ADD functions#3609

Merged
georgesittas merged 3 commits intomainfrom
vaggelisd/db_dateadd
Jun 7, 2024
Merged

feat(spark, databricks)!: Support for DATE_ADD functions#3609
georgesittas merged 3 commits intomainfrom
vaggelisd/db_dateadd

Conversation

@VaggelisD
Copy link
Copy Markdown
Collaborator

Fixes #3607

Until now Hive and it's child dialects supported DATE_ADD(startDate, numDays) which had "hardcoded" the unit to DAYS and returned a DATE.

Spark3 (testing on 3.5.1) and Databricks have introduced support for DATE_ADD(unit, value, expr) (alias of TIMESTAMPADD and DATEADD) which instead returns a TIMESTAMP.

Note that the old / 2-arg version is still supported alongside the new one and should be preserved to ensure the DATE vs TIMESTAMP semantics.

Comment thread sqlglot/dialects/spark.py Outdated
Comment thread sqlglot/dialects/spark.py
Comment thread sqlglot/dialects/spark.py Outdated
Comment thread tests/dialects/test_redshift.py
Comment thread tests/dialects/test_spark.py
@tobymao
Copy link
Copy Markdown
Owner

tobymao commented Jun 6, 2024

what happens with spark 3.4?

@VaggelisD
Copy link
Copy Markdown
Collaborator Author

VaggelisD commented Jun 6, 2024

@tobymao The new DATE_ADD version does not seem supported on 3.4.3 so it must be a 3.5+ feature . What should be our approach in such situations, support the latest dialect?

@tobymao
Copy link
Copy Markdown
Owner

tobymao commented Jun 6, 2024

you need to make sure it’s not default so that 3.4 still works

@VaggelisD VaggelisD changed the title feat(spark, databricks): Support for DATE_ADD functions feat(spark, databricks)!: Support for DATE_ADD functions Jun 6, 2024
Comment thread sqlglot/dialects/spark.py Outdated
@georgesittas georgesittas merged commit d6cfb41 into main Jun 7, 2024
@georgesittas georgesittas deleted the vaggelisd/db_dateadd branch June 7, 2024 00:01
fresioAS pushed a commit to fresioAS/sqlglot that referenced this pull request May 3, 2026
* feat(spark, databricks): Support for DATE_ADD functions

* PR Feedback 1

* PR Feedback 2
RichardHughes-amp added a commit to RichardHughes-amp/sqlglot that referenced this pull request May 4, 2026
…Parser

DatabricksParser inherits SparkParser._build_dateadd, which already
correctly disambiguates 2-arg (-> TsOrDsAdd, returns DATE) from 3-arg
(-> TimestampAdd, preserves type). The Databricks-level overrides were
reintroducing the bug that PR tobymao#3609 intended to fix at the Spark level.

Retain the TsOrDsAdd -> DATE typing entry in spark.py; the Hive ->
Spark -> Databricks descent is covered by test_hive_chain_date_add_descent.

Note: the 3-arg `date_add(unit, value, expr)` / `dateadd` form is a
Databricks-only extension (Spark OSS exposes this only as TIMESTAMPADD).
SparkParser intentionally handles it to support Databricks, which is why
inheritance is the right scoping here.
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.

Redshift DATEADD -> Databricks DATE_ADD

3 participants