ethan-tyler opened a new issue, #27995:
URL: https://github.com/apache/datafusion/issues/27995

   ### Describe the bug
   
   Filtering a `Timestamp(Microsecond)` column with a `TIMESTAMP '...'` 
literal, a string, or a `DATE` literal fails during planning when the value is 
before 1677-09-21 or after 2262-04-11. The column's type can hold these values. 
The query fails because the literal is first cast to `Timestamp(Nanosecond)`, 
and these values are outside the nanosecond range.
   
   `CAST(... AS TIMESTAMP)`, `to_timestamp` and `to_timestamp_micros` fail the 
same way. Dates like `0001-01-01` are common sentinels in real data, and 
historical datasets contain years such as 1000.
   
   ### To Reproduce
   
   Reproduced with DataFusion 55.3.0 and 56.0.0.
   
   ```sql
   CREATE TABLE t AS SELECT arrow_cast(0, 'Timestamp(Microsecond, 
Some("UTC"))') AS ts;
   
   SELECT ts FROM t WHERE ts < TIMESTAMP '1000-01-01 00:00:00';
   -- Optimizer rule 'simplify_expressions' failed
   -- caused by
   -- Failed to cast field 'lit' from Utf8 to Timestamp(ns)
   -- caused by
   -- Arrow error: Cast error: Overflow converting 1000-01-01 00:00:00 to 
Nanosecond. The dates that can be represented as nanoseconds have to be between 
1677-09-21T00:12:44.0 and 2262-04-11T23:47:16.854775804
   
   SELECT ts FROM t WHERE ts < '1000-01-01T00:00:00';
   -- same error, casting to Timestamp(ns, "UTC")
   
   SELECT ts FROM t WHERE ts < DATE '1000-01-01';
   -- Optimizer rule 'simplify_expressions' failed
   -- caused by
   -- Failed to cast field 'lit' from Date32 to Timestamp(ns)
   -- caused by
   -- Execution error: Cannot cast Date32 value -354285 to Timestamp(ns): 
converted value exceeds the representable i64 range
   
   SELECT TIMESTAMP '1000-01-01 00:00:00';
   SELECT CAST('1000-01-01' AS TIMESTAMP);
   -- same overflow error as the first query
   
   SELECT to_timestamp_micros('1000-01-01T00:00:00');
   -- Execution error: The dates that can be represented as nanoseconds have to 
be between 1677-09-21T00:12:44.0 and 2262-04-11T23:47:16.854775804
   ```
   
   A `Timestamp(Microsecond, None)` column behaves the same way. Only an 
explicit cast to the column's unit works today, for example 
`arrow_cast('1000-01-01T00:00:00', 'Timestamp(Microsecond, Some("UTC"))')`.
   
   ### Expected behavior
   
   Every query above should succeed when the value fits in the target type:
   - Comparing a `Timestamp(Microsecond)` column with a string, `DATE` or 
`TIMESTAMP` literal should coerce the literal to the column's unit, not to 
nanoseconds.
   - `to_timestamp_micros` should accept any value that fits in microseconds.
   
   ### Additional context
   
   These are the places that force nanoseconds, on 56.0.0 and unchanged on 
`main`:
   - **SQL planner:** `TIMESTAMP` without a precision maps to 
`TimeUnit::Nanosecond` (`datafusion/sql/src/planner.rs`).
   - **Coercion:** `string_temporal_coercion` in 
`datafusion/expr-common/src/type_coercion/binary.rs` returns 
`Timestamp(Nanosecond, tz)` for string vs. timestamp, whatever the column's 
unit is.
   - **Planning-time error:** since #18332, constant folding reports the failed 
cast at planning time.
   - **`to_timestamp*`:** these parse to nanoseconds first, then divide by the 
unit.
   
   A possible fix is to coerce strings and date/timestamp literals to the other 
side's unit during comparison coercion, and to have `to_timestamp*` convert 
directly to the requested unit.
   
   Open question: should a standalone `TIMESTAMP '...'` keep defaulting to 
nanoseconds? Changing that default would change `arrow_typeof` results for 
existing queries.
   
   Related:
   - #27952: the same nanosecond conversion in `date_trunc`.
   - #27955: `date_bin` and `to_char` cast timezone-aware timestamps to 
nanoseconds.
   - #7249: precision specifier for timestamp literals.
   - #8336: earlier timestamp overflow report.
   
   ### Willingness to contribute
   
   I plan to contribute a fix for this bug independently
   
   ---
   
   *Disclosure: I used AI tools to help investigate this, reproduce it, and 
draft this issue. I reviewed it before filing.*
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to