You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kumarUjjawal (via GitHub)" <gi...@apache.org> on 2026/01/04 08:01:43 UTC

Re: [I] Date + interval returns a type inconsistent with other databases [datafusion]

kumarUjjawal commented on issue #19527:
URL: https://github.com/apache/datafusion/issues/19527#issuecomment-3707844141

   I was loooking into this issue and attempted to make `Date + Interval` return `Timestamp` instead of `Date`.
   
   What I did:
   
   1. **Type Coercion** (`expr-common/src/type_coercion/binary.rs`): Added check in `get_result` branch to override return type to `Timestamp(ns)` when detecting `Date +/- Interval`
   2. **Execution Layer** (`physical-expr/src/expressions/binary.rs`): Cast Date → Timestamp before applying interval arithmetic
   
   Finding:
   
   Both `Date + Interval` and `Date + Integer` look identical after type coercion:
   
   ```
   Date + interval '1 hour'  →  Date + Interval(MonthDayNano)  → should return Timestamp
   Date + 7                  →  Date + Interval(MonthDayNano)  → should return Date
   ```
   
   The `temporal_math_coercion` function coerces integers to `Interval(MonthDayNano)`. After this, `BinaryExpr::data_type()` re-runs type coercion with the coerced child types, sees `Date + Interval`, and returns `Timestamp` - breaking Date + Integer behavior.
   
   - After coercion, we lose information about original types
   - `CastExpr` wraps the integer, so at runtime the right operand IS an Interval
   - Both interval literals and coerced integers become `Interval(MonthDayNano)`
   
   Questions:
   
   1. **Accept that Date + Integer also returns Timestamp** (breaking change,  lots of affected tests)
   2. **Only convert when interval has sub-day precision** (hours/minutes/seconds) - (quite complex IMO)
   
   @Omega359 Would like your feedback on this one.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org