Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Introduce INTERVAL support #35521

Merged
merged 9 commits into from Nov 21, 2018
Merged

Conversation

costin
Copy link
Member

@costin costin commented Nov 13, 2018

Sorry for the big PR. It had rippling effects through the code base.

Introduce INTERVAL as a DataType
Add INTERVAL to the grammar which supports the standard SQL declaration
(without precision):

INTERVAL '1 23:45:01.123456789' DAY TO SECOND

but also number for single unit intervals:

INTERVAL 1 YEAR

as well as the plurals of the units:

INTERVAL 2 YEARS

Interval are internally supported as just another Literal being backed
by java.time.Period and java.time.Duration
Move JDBC away from JDBCType enum to SQLType interface
Refactor DataType by:

  • move it into server core
  • add dedicated (and much simpler) JDBC type
    Improve internal JDBC conversion by normalizing on the DataType
    Rename JDBC columnInfo to JdbcColumnInfo to differentiate between it and
    the SQL ColumnInfo
    Extended arithmetic operations to support operations between intervals and intervals and dates
    (underlying folding operation not implemented yet)

Left to do:

  1. add basic operation +/- between intervals
  2. add basic operation +/- between timestamps and intervals
  3. introduce now/current date/current time/current timestamp methods (likely another PR)

Fix #29990

Introduce INTERVAL as a DataType
Add INTERVAL to the grammar which supports the standard SQL declaration
 (without precision):
 > INTERVAL '1 23:45:01.123456789' DAY TO SECOND
 but also number for single unit intervals:
 > INTERVAL 1 YEAR
 as well as the plurals of the units:
 > INTERVAL 2 YEARS
Interval are internally supported as just another Literal being backed
 by java.time.Period and java.time.Duration
Move JDBC away from JDBCType enum to SQLType interface
Refactor DataType by:
 - move it into server core
 - add dedicated (and much simpler) JDBC type
Improve internal JDBC conversion by normalizing on the DataType
Rename JDBC columnInfo to JdbcColumnInfo to differentiate between it and
 the SQL ColumnInfo

Left to do:

4. add basic operation +/- between intervals
5. add basic operation +/- between timestamps and intervals
6. add conversion of intervals and other datatypes (see what functions
make sense)
7. introduce now/current date/current time/current timestamp methods

Fix elastic#29990
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search-aggs

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive. Left some minor/picky comments.

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments, but still reviewing.

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished review, left some more comments/questions. Really nice work!!

Fix datetime formatting
@@ -146,27 +152,27 @@ public static Boolean regex(String value, String pattern) {
//
// Math
//
public static Number add(Number left, Number right) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you switch to object? We should have already validated they are Numbers or not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ignore, I rushed, it's because of numeric operations between intervals.

return Arithmetics.add((ZonedDateTime) r, ((IntervalDayTime) l).interval());
}

throw new SqlIllegalArgumentException("Cannot compute [+] between [{}] [{}]", l.getClass(), r.getClass());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for this case.

throw new SqlIllegalArgumentException("Cannot substract a date from an interval; do you mean the reverse?");
}

throw new SqlIllegalArgumentException("Cannot compute [-] between [{}] [{}]", l.getClass(), r.getClass());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nice! (Added two pointers to add some test for Exception throwing)

@costin
Copy link
Member Author

costin commented Nov 21, 2018

test this please

@costin costin merged commit f0a3d32 into elastic:master Nov 21, 2018
@costin costin deleted the introduce-sql-interval branch November 21, 2018 13:46
costin added a commit that referenced this pull request Nov 21, 2018
Introduce INTERVAL as a DataType
Add INTERVAL to the grammar which supports the standard SQL declaration
 (without precision):
 > INTERVAL '1 23:45:01.123456789' DAY TO SECOND
 but also number for single unit intervals:
 > INTERVAL 1 YEAR
 as well as the plurals of the units:
 > INTERVAL 2 YEARS
Interval are internally supported as just another Literal being backed
 by java.time.Period and java.time.Duration
Move JDBC away from JDBCType enum to SQLType interface
Refactor DataType by moving it into server core and adding dedicated (and
 much simpler) JDBC driver type
Improve internal JDBC conversion by normalizing on the DataType
Rename JDBC columnInfo to JdbcColumnInfo to differentiate between it and
 the SQL ColumnInfo

Fix #29990

(cherry picked from commit f0a3d32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants