Skip to content

Conversation

@cht42
Copy link
Contributor

@cht42 cht42 commented Jan 16, 2026

Which issue does this PR close?

Rationale for this change

Add support for spark https://spark.apache.org/docs/latest/api/sql/index.html#date_diff function

What changes are included in this PR?

Are these changes tested?

yes in SLT

Are there any user-facing changes?

yes

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) spark labels Jan 16, 2026
SELECT date_diff(column1, column2)
FROM VALUES
('2009-07-30'::date, '2009-07-31'::date),
('2009-07-31'::date, '2009-07-30'::date),
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to add a test case for different input types ?
E.g. a Date and a String:

Suggested change
('2009-07-31'::date, '2009-07-30'::date),
('2009-07-31'::date, '2009-07-30'::date),
('2009-07-31'::date, '2009-07-30'),

Copy link
Member

Choose a reason for hiding this comment

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

and also a Timestamp and a Date

Suggested change
('2009-07-31'::date, '2009-07-30'::date),
('2009-07-31 23:45:01'::timestamp, '2009-07-30'::date),


fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
internal_err!(
"spark date_diff should have been simplified to standard subtraction"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"spark date_diff should have been simplified to standard subtraction"
"Spark `date_diff` should have been simplified to standard subtraction"

I am not sure whether ASF branding rules should be obeyed here. Usually all Apache projects should be referred with Apache in front, i.e. Apache Spark ...

Ok(ExprSimplifyResult::Simplified(binary_expr(
end.cast_to(&DataType::Int32, info.schema())?,
Operator::Minus,
start.cast_to(&DataType::Int32, info.schema())?,
Copy link
Member

Choose a reason for hiding this comment

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

What if the argument data type is Date64 ? The signature allows it.
Date32 keeps the seconds since epoch, but Date64 keeps the milliseconds since epoch.

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.

[datafusion-spark] Add date_diff function

3 participants