Skip to content

Conversation

@kumarUjjawal
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

What changes are included in this PR?

  • Added Time64/Time32 signatures to date_trunc
  • Added time truncation logic (hour, minute, second, millisecond, microsecond)
  • Error for invalid granularities (day, week, month, quarter, year)

Are these changes tested?

Yes

Are there any user-facing changes?

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Jan 5, 2026
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Jan 5, 2026
_ => {
return exec_err!(
"second argument of `date_trunc` must be timestamp scalar or array"
"second argument of `date_trunc` must be timestamp, time scalar or array"
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
"second argument of `date_trunc` must be timestamp, time scalar or array"
"second argument of `date_trunc` must be a timestamp/time scalar or an array"

datafusion public date_trunc datafusion public date_trunc FUNCTION true Timestamp(Second, None) SCALAR Truncates a timestamp value to a specified precision. date_trunc(precision, expression)
datafusion public date_trunc datafusion public date_trunc FUNCTION true Timestamp(Second, Some("+TZ")) SCALAR Truncates a timestamp value to a specified precision. date_trunc(precision, expression)
datafusion public date_trunc datafusion public date_trunc FUNCTION true Date SCALAR Truncates a timestamp or time value to a specified precision. date_trunc(precision, expression)
datafusion public date_trunc datafusion public date_trunc FUNCTION true String SCALAR Truncates a timestamp or time value to a specified precision. date_trunc(precision, expression)
Copy link
Member

Choose a reason for hiding this comment

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

Are those return types correct - Date and String ? Shouldn't they be converted to Timestamp(Nanosecond, None)
I think the reason is here - https://github.com/apache/datafusion/pull/19640/changes#diff-7a6e17bfd876e9e88341d68750224b8a2294d9f401eef2126a49ade1665c336cR228

Copy link
Contributor

Choose a reason for hiding this comment

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

Are these return types or input types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SELECT arrow_typeof(date_trunc('day', to_date('2024-01-15')));
-- Returns: Timestamp(ns)

SELECT arrow_typeof(date_trunc('day', '2024-01-15'));  
-- Returns: Timestamp(ns)

Yes, the actual runtime return type is Timestamp(Nanosecond), not Date or String.

The coercion from Date / String to Timestamp happens at planning time, before invoke_with_args is called.

The Date / String entries in information_schema represent the signature variants generated by the coercible API’s implicit coercion sources. However, the return_type method receives the already-coerced type (Timestamp) and returns that.

So the functional behavior is correct, this is how coercible signatures are represented in metadata.

Copy link
Member

Choose a reason for hiding this comment

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

Got it! Thanks!

@Jefffrey Jefffrey added this pull request to the merge queue Jan 8, 2026
Merged via the queue into apache:main with commit 646213e Jan 8, 2026
29 checks passed
@Jefffrey
Copy link
Contributor

Jefffrey commented Jan 8, 2026

Thanks @kumarUjjawal & @martin-g

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

Labels

documentation Improvements or additions to documentation functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants