prestodb / presto

The official home of the Presto distributed SQL query engine for big data
http://prestodb.io
Apache License 2.0
16.07k stars 5.38k forks source link

Interval values can overflow #24087

Open tdcmeehan opened 2 days ago

tdcmeehan commented 2 days ago

This produces the correct value:

presto> select interval '1' month * 2147483647;
    _col0    
-------------
 178956970-7 
(1 row)

The next higher number will overflow:

presto> select interval '1' month * 2147483648;
    _col0     
--------------
 -178956970-8 
(1 row)

None of the interval year to month operators for division, multiplication, subtraction and addition check boundaries, so this problem is not specific to multiplication. Also, similar boundary checks should be added for multiplication by double.

Likewise, the interval day time operators will also overflow and need the same checks, adjusted to work for the long type that backs IntervalDayTimeType.

Your Environment

This is true for any functional environment of Presto.

Expected Behavior

It should be an error to exceed the boundary of the interval type. The underlying datastructure for interval year to month is an integer, so we should raise an error for any value that exceed's Java integer range.

Current Behavior

The interval will silently overflow and return incorrect results.

Possible Solution

Modify the interval operators to do proper overflow checking, as is done for e.g. INTEGER operators.

Steps to Reproduce

See the above queries for a simple reproduction.

Screenshots (if appropriate)

Context

This was discovered while converting constant folding of expressions to use Velox for their evaluation.

This was previously reported in #9342.

asharan2buff commented 2 days ago

Can I take this up?

tdcmeehan commented 1 day ago

@asharan2buff sure. I have opened #24089 to address the issue for IntervalYearMonthOperators, please take up IntervalDayTimeOperators.