[BUG] Error when adding Date32 and Int64
Describe the bug
DataFusion 41
> select to_date('1970-01-01', 'yyyy-mm-dd') + 5;
Error during planning: Cannot coerce arithmetic expression Date32 + Int64 to valid types
The same query runs ok in Postgres Workaround
> select to_date('1970-01-01') + interval '5' day;
+-------------------------------------------------------------------------------------------------------------------+
| to_date(Utf8("1970-01-01")) + IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 5, nanoseconds: 0 }") |
+-------------------------------------------------------------------------------------------------------------------+
| 1970-01-06 |
+-------------------------------------------------------------------------------------------------------------------+
To Reproduce
select to_date('1970-01-01', 'yyyy-mm-dd') + 5;
Expected behavior
Query should work
Additional context
No response
That is an odd thing for pg to support because the unit for the int is implied as it could just as well be months or years.
That is an odd thing for pg to support because the unit for the int is implied as it could just as well be months or years.
I cannot find the standard right away, but for binary ops with Date and numeric, the numeric is supposed to be number of days
Using intervals, both of the following work:
select to_date('1970-01-01') + interval '5' day; select interval '5' day + to_date('1970-01-01');
Do we want to support both cases for Date32 and Int64 as well?
select to_date('1970-01-01', 'yyyy-mm-dd') + 5; select 5 + to_date('1970-01-01', 'yyyy-mm-dd');
How about minus?
select to_date('1970-01-01', 'yyyy-mm-dd') - 5;
take
I put up a PR, but it's not perfect as I describe in its summary. Would love to get feedback and suggestions from the experts!
Ah, looks like datafusion doesn't even support this to_date('1970-01-01', 'yyyy-mm-dd'); in v46.0.1
> select to_date('1970-01-01', 'yyyy-mm-dd');
Execution error: Error parsing timestamp from '1970-01-01' using format 'yyyy-mm-dd': input contains invalid characters
Which said we won't be able to fully support this query select to_date('1970-01-01', 'yyyy-mm-dd') + 5; even after the operator issue is sorted out, this is the correct syntax in DF:
> select to_date('1970-01-01');
+-----------------------------+
| to_date(Utf8("1970-01-01")) |
+-----------------------------+
| 1970-01-01 |
+-----------------------------+
1 row(s) fetched.
Elapsed 0.007 seconds.
Ah, looks like datafusion doesn't even support this
to_date('1970-01-01', 'yyyy-mm-dd');in v46.0.1
Wrong format. See https://datafusion.apache.org/user-guide/sql/scalar_functions.html#to-date
Oh I see, thanks @Omega359 for sending the doc.
Hope you don't mind me asking another question. I'm still wondering why we want DF to support operations like select to_date('1970-01-01', 'yyyy-mm-dd') + 5;. Originally I thought maybe if DF support the exactly syntax as pgsql, then it would be easier for someone to migrate to DF based databases. But the syntax will not be exactly the same even if this operation is supported, plus we have a workaround.
then it would be easier for someone to migrate to DF based databases
So the above is probably not a valid reason then ... I'm a bit confused here, what is the benefit for DF to support select to_date('1970-01-01') + 5; this operation?
PS: I'm new to DF, still learning about everything.
Thanks in advance.
Date parsing and date arithmetic is very common in some applications. using to_date('1970-01-01') + 5 is just easier than to_date('1970-01-01') + INTERVAL 5 days or something similar.
I get it that PostgreSQL would be working fine because PostgreSQL has implicit type coercion rules. Coming to why it is not working here : This line, Operator::Plus => add_wrapping(&l, &r), expects Arrow's kernel functions to add Date32 and Interval(MonthDayNano), but Arrow's add_wrapping doesn't know how to do it
We should try it without converting to Interval, I guess it should work. Let me try
This was fixed in https://github.com/apache/datafusion/pull/19460
@comphead this can be closed now.
Got it 👍
I checked this on current main (e7e037d).
The original planning error is gone. Date32/Date64 +/- integer is treated as a number of days (Postgres-compatible), including the commutative integer + date form:
SELECT to_date('1970-01-01') + 5;
-- 1970-01-06
SELECT '1970-01-01'::date + 5;
-- 1970-01-06
This was added in #19460 (arith_date_integer.slt and the temporal_math_coercion Date+Int → Date+Interval path).
One caveat on the exact issue query: to_date('1970-01-01', 'yyyy-mm-dd') still fails because DataFusion uses Chrono strftime patterns (%Y-%m-%d), not Oracle/yyyy-mm-dd. That is independent of the Date32+Int64 coercion bug. to_date('1970-01-01') + 5 and to_date('1970-01-01', '%Y-%m-%d') + 5 both work.
I think this can be closed as fixed by #19460, as @Omega359 noted.