Skip to content

fix(spark): Use wrapping addition/subtraction in SparkDateAdd and SparkDateSub#19377

Merged
Jefffrey merged 3 commits intoapache:mainfrom
mzabaluev:date-add-wrapping
Jan 6, 2026
Merged

fix(spark): Use wrapping addition/subtraction in SparkDateAdd and SparkDateSub#19377
Jefffrey merged 3 commits intoapache:mainfrom
mzabaluev:date-add-wrapping

Conversation

@mzabaluev
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

In Spark, date_add and date_sub functions do not raise an error when the addition or subtraction overflows 32-bit integer, regardless of whether the ANSI evaluation mode is in effect.

What changes are included in this PR?

Changed the implementations to use wrapping_add and wrapping_sub.

Are these changes tested?

Updated date_add.slt to expect overflowed results rather than the errors.

Are there any user-facing changes?

The functions behave more like Spark, even if Spark is doing the wrong thing.

In Spark, `date_add` and `date_sub` functions do not raise an error
when the addition or subtraction overflows 32-bit integer, regardless
of whether the ANSI evaluation mode is in effect.
This was trickier to test than usual results: the Date32 values are
computed wrapped around, but the _formatting_ of the results fails
because the dates are out of chrono range. Cast the output to int
to compare with the exact values.
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) spark labels Dec 17, 2025
query I
SELECT date_sub('1969-01-01'::date, 2147483647::int)::int;
----
2147483284
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Apache Spark 4.0.1 returns the same results:

❯ ./bin/spark-sql
WARNING: Using incubator modules: jdk.incubator.vector
Using Spark's default log4j profile: org/apache/spark/log4j2-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
25/12/19 13:27:39 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
25/12/19 13:27:41 WARN ObjectStore: Version information not found in metastore. hive.metastore.schema.verification is not enabled so recording the schema version 2.3.0
25/12/19 13:27:41 WARN ObjectStore: setMetaStoreSchemaVersion called but recording version is disabled: version = 2.3.0, comment = Set by MetaStore mgrigorov@192.168.x.y
Spark Web UI available at http://localhost:4040
Spark master: local[*], Application Id: local-1766143660589

spark-sql (default)> SELECT UNIX_DATE(date_add('2016-07-30', 2147483647));
-2147466637
Time taken: 0.028 seconds, Fetched 1 row(s)
spark-sql (default)> SELECT UNIX_DATE(date_sub('1969-01-01', 2147483647));
2147483284
Time taken: 0.03 seconds, Fetched 1 row(s)

@Jefffrey
Copy link
Copy Markdown
Contributor

Jefffrey commented Jan 3, 2026

cc @andygrove

@Jefffrey Jefffrey added this pull request to the merge queue Jan 6, 2026
Merged via the queue into apache:main with commit 2d56253 Jan 6, 2026
27 checks passed
@Jefffrey
Copy link
Copy Markdown
Contributor

Jefffrey commented Jan 6, 2026

Thanks @mzabaluev & @martin-g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

spark sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants