Skip to content

Support ::interval casting / INTERVAL SQL datatype #5651

@alamb

Description

@alamb

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
In DataFusion, we support SQL syntax like INTERVAL '3 months' that is then converted into Intervals (I think originally contributed by @ovr).

This has a few limitations:

  1. It can't represent the full range of intervals, leading to issues like date_bin doesn't work with months or years #5689
  2. It only works for constant strings in INTERVAL ... not more generally

Describe the solution you'd like
I would like to be able to use the interval type from sql as normal:

select '1 day`::interval
select cast('1 day' as interval);
select arrow_cast('1 day', 'Interval(MonthDayNano)');

Here is what happens today

❯ select '1 day'::interval;
This feature is not implemented: Unsupported SQL type Interval
❯ select cast('1 day' as interval);
This feature is not implemented: Unsupported SQL type Interval
❯ select arrow_cast('1 day', 'Interval(MonthDayNano)');
Error during planning: Cannot automatically convert Utf8 to Interval(MonthDayNano)
❯ select arrow_cast('1 day', 'Interval(YearMonth)');
Error during planning: Cannot automatically convert Utf8 to Interval(YearMonth)
❯ select arrow_cast('1 day', 'Interval(DayTime)');
Error during planning: Cannot automatically convert Utf8 to Interval(DayTime)

Describe alternatives you've considered
One thing we need to figure out is what Arrow interval type will map to the sql interval type.

Intervals can be either YearMonth or DayTime or MonthDayNano

I think in order to support '1 day' and '1 minute' with the same type, we will need to use IntervalMonthDayNano:

https://docs.rs/arrow/35.0.0/arrow/datatypes/struct.IntervalMonthDayNanoType.html

Additional context
I believe the casting support is added upstream in arrow-rs by @doki23 -- apache/arrow-rs#3762 in arrow 35.0.0.

However, that upgrade is blocked (see #5441) so we may have to wait for arrow-36.0.0 to implement this feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestsqlSQL Planner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions