fengys1996 opened a new issue, #22756:
URL: https://github.com/apache/datafusion/issues/22756
### Describe the bug
When comparing two Timestamp values with different TimeUnits, DataFusion
coerces both sides to the coarser time unit. This may result in a loss of
precision and lead to incorrect comparison semantics.
And other timestamp operations, such as arithmetic operations and
type-unification contexts like `UNION`, also choose the coarser time unit.
### To Reproduce
1. compare ts with different time unit.
```sql
SELECT
CAST(TIMESTAMP '2024-01-01 00:00:00' AS TIMESTAMP(0)) =
CAST(TIMESTAMP '2024-01-01 00:00:00.123' AS TIMESTAMP(3)) AS eq;
```
Expected:
```text
+---------------------------------------------------------------+
| Utf8("2024-01-01 00:00:00") = Utf8("2024-01-01 00:00:00.123") |
+---------------------------------------------------------------+
| false |
+---------------------------------------------------------------+
```
Actual:
```text
+---------------------------------------------------------------+
| Utf8("2024-01-01 00:00:00") = Utf8("2024-01-01 00:00:00.123") |
+---------------------------------------------------------------+
| true |
+---------------------------------------------------------------+
```
2. ts subtraction
```sql
SELECT
CAST(TIMESTAMP '2024-01-01 00:00:00.123' AS TIMESTAMP(3)) -
CAST(TIMESTAMP '2024-01-01 00:00:00' AS TIMESTAMP(0)) AS diff;
```
Expected:
```text
+----------+
| diff |
+----------+
| PT0.123S |
+----------+
```
Actual:
```text
+------+
| diff |
+------+
| P0D |
+------+
```
3. ts union
```sql
SELECT CAST(TIMESTAMP '2024-01-01 00:00:00' AS TIMESTAMP(0))
UNION ALL
SELECT CAST(TIMESTAMP '2024-01-01 00:00:00.123' AS TIMESTAMP(3));
```
Expected:
```text
+-----------------------------+
| Utf8("2024-01-01 00:00:00") |
+-----------------------------+
| 2024-01-01T00:00:00.123 |
| 2024-01-01T00:00:00 |
+-----------------------------+
```
Actual:
```text
+-----------------------------+
| Utf8("2024-01-01 00:00:00") |
+-----------------------------+
| 2024-01-01T00:00:00 |
| 2024-01-01T00:00:00 |
+-----------------------------+
```
### Expected behavior
_No response_
### Additional context
I also ran a few similar queries in ClickHouse. It appears that ClickHouse
prefers the finer precision.
--
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]