datafusion icon indicating copy to clipboard operation
datafusion copied to clipboard

[BUG] Error when adding Date32 and Int64

Open comphead opened this issue 2 years ago • 14 comments

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

comphead avatar Sep 05 '24 15:09 comphead

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.

Omega359 avatar Sep 05 '24 16:09 Omega359

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

comphead avatar Sep 05 '24 16:09 comphead

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;

gnayus avatar Nov 28 '24 05:11 gnayus

take

milevin avatar Dec 09 '24 21:12 milevin

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!

milevin avatar Dec 12 '24 09:12 milevin

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.

qstommyshu avatar Apr 17 '25 00:04 qstommyshu

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

Omega359 avatar Apr 17 '25 13:04 Omega359

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.

qstommyshu avatar Apr 17 '25 13:04 qstommyshu

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.

Omega359 avatar Apr 17 '25 13:04 Omega359

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

RSAgr avatar Jan 02 '26 13:01 RSAgr

This was fixed in https://github.com/apache/datafusion/pull/19460

Omega359 avatar Jan 02 '26 14:01 Omega359

@comphead this can be closed now.

Omega359 avatar Jan 02 '26 14:01 Omega359

Got it 👍

RSAgr avatar Jan 02 '26 16:01 RSAgr

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.

shinzoxD avatar Aug 15 '26 22:08 shinzoxD