datafusion icon indicating copy to clipboard operation
datafusion copied to clipboard

[dbolympics] Support multiply and divide on intervals

Open alamb opened this issue 1 year ago • 8 comments

Is your feature request related to a problem or challenge?

Copied directly from https://github.com/apache/arrow-rs/issues/6335 from @samuelcolvin

In Postgres I can do:

select 3 * interval '1 day';
select interval '1 day' * 3;  -- same
select 1.23 * interval '1 day';
-- and divide
select interval '1 day' / 3;

I started trying to add support for this to datafusion but discovered that the operations aren't supported in arrow-rs and adding them (with support for all numeric types) is far from trivial.

Describe the solution you'd like

The idea is to support the syntax above in DataFusion

High level steps are:

  • [ ] Arrow-rs support: https://github.com/apache/arrow-rs/pull/6906
  • [ ] PR in DataFusion to allow the operations and add appropriate tests

Describe alternatives you've considered

No response

Additional context

I believe @milevin is already working on this (so perhaps you would like to assign this ticket to yourself via take)

alamb avatar Dec 19 '24 20:12 alamb

take

milevin avatar Dec 19 '24 20:12 milevin

take

peterxcli avatar Jul 12 '26 18:07 peterxcli

With this, datafusion-comet could have native support for multiply_ym_interval, multiply_dt_interval and divide_dt_interval, instead of using the codegen dispatch path.

peterxcli avatar Jul 12 '26 18:07 peterxcli

https://github.com/apache/arrow-rs/issues/6335 is implemented. I will try to integrate it into df whenever the next arrow-rs release is out.

peterxcli avatar Aug 05 '26 05:08 peterxcli

@peterxcli are you still working on this? The arrow-rs side (apache/arrow-rs#6335) is in the arrow 60 that DataFusion now depends on, so the remaining part is on the DataFusion side: temporal_math_coercion currently coerces the number to Interval(MonthDayNano), which arrow rejects as Interval * Interval. If you have not started, I am happy to take it over and send a PR that keeps the numeric operand as Int64/Float64 and adds sqllogictests for * and /.

RishavTiwari25 avatar Oct 12 '26 16:10 RishavTiwari25

@peterxcli since the arrow side has been released for a while and I did not hear back, I went ahead and opened #26258 with the DataFusion side (coercion to Int64/Float64 instead of Interval, checked mul kernel, sqllogictests for * and / with integers, floats, NULLs and columns). If you already have work in progress I am happy to close mine or rebase on yours.

RishavTiwari25 avatar Oct 13 '26 10:10 RishavTiwari25

Affects DB Olympics, see: https://github.com/apache/datafusion/issues/26661

alamb avatar Oct 24 '26 13:10 alamb

#26258 implements this (interval * number, number * interval, interval / number, with sqllogictests). It has no conflicts with main and is ready for review whenever someone has time.

RishavTiwari25 avatar Oct 26 '26 11:10 RishavTiwari25