GeezerVison is showing github.com — [SQL] Date and interval arithmetic follow the SQL standard by mihaibudiu · Pull Request #7692 · feldera/feldera
[SQL] Date and interval arithmetic follow the SQL standard
#7692
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.java
(16)
.md
(3)
.py
(9)
.rs
(1)
.sh
(1)
All 5 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -2081,12 +2081,19 @@ pub fn cast_to_ShortInterval_DAYS_i64(value: i64) -> SqlResult<ShortInterval> {
}
}
/// Converts `value` units of `multiplier` microseconds to a `ShortInterval`. A unit other than the
/// second holds no fraction, so `value` is truncated toward zero first.
fn convert_to_ShortInterval_SqlDecimal<const P: usize, const S: usize>(
value: SqlDecimal<P, S>,
multiplier: i64,
kind: &'static str,
) -> SqlResult<ShortInterval> {
let dd = DynamicDecimal::from(value);
let units = if multiplier == MICROS_PER_SECOND as i64 {
Comment thread
mihaibudiu marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
value
} else {
value.trunc()
};
let dd = DynamicDecimal::from(units);
let mul = DynamicDecimal::from(multiplier);
dd.checked_mul(&mul)
.and_then(|val| i64::try_from(val).ok())
Expand Down
Expand Up
@@ -2138,7 +2145,7 @@ pub fn cast_to_ShortInterval_HOURS_i64(value: i64) -> SqlResult<ShortInterval> {
pub fn cast_to_ShortInterval_HOURS_SqlDecimal<const P: usize, const S: usize>(
value: SqlDecimal<P, S>,
) -> SqlResult<ShortInterval> {
convert_to_ShortInterval_SqlDecimal::<P, S>(value, 3600i64 * 1000 * 1000, "DAYS ")
convert_to_ShortInterval_SqlDecimal::<P, S>(value, 3600i64 * 1000 * 1000, "HOURS ")
Comment thread
mihaibudiu marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
}
#[doc(hidden)]
Expand Down
Expand Up
@@ -2646,20 +2653,72 @@ cast_function!(LongInterval_YEARS, LongInterval, s, SqlString);
cast_function!(LongInterval_YEARS_TO_MONTHS, LongInterval, s, SqlString);
cast_function!(LongInterval_MONTHS, LongInterval, s, SqlString);
// A cast between interval types truncates the value toward zero to the last field of the
// target type.
#[doc(hidden)]
#[inline]
pub fn cast_to_LongInterval_YEARS_LongInterval(value: LongInterval) -> SqlResult<LongInterval> {
Ok(LongInterval::from_months(value.months() / 12 * 12))
}
#[doc(hidden)]
#[inline]
pub fn cast_to_LongInterval_LongInterval (value: LongInterval) -> SqlResult<LongInterval> {
pub fn cast_to_LongInterval_MONTHS_LongInterval (value: LongInterval) -> SqlResult<LongInterval> {
Ok(value)
Comment thread
mihaibudiu marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
}
#[doc(hidden)]
#[inline]
pub fn cast_to_ShortInterval_ShortInterval(value: ShortInterval) -> SqlResult<ShortInterval> {
pub fn cast_to_LongInterval_YEARS_TO_MONTHS_LongInterval(
value: LongInterval,
) -> SqlResult<LongInterval> {
Ok(value)
Comment thread
mihaibudiu marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
}
cast_function!(LongInterval, LongInterval, LongInterval, LongInterval);
cast_function!(ShortInterval, ShortInterval, ShortInterval, ShortInterval);
cast_function!(LongInterval_YEARS, LongInterval, LongInterval, LongInterval);
cast_function!(
LongInterval_MONTHS,
LongInterval,
LongInterval,
LongInterval
);
cast_function!(
LongInterval_YEARS_TO_MONTHS,
LongInterval,
LongInterval,
LongInterval
);
/// Defines the cast of a short interval to the type with `units`, whose last field is `unit`
/// microseconds long.
macro_rules! cast_to_short_interval {
($units: ident, $unit: expr) => {
::paste::paste! {
#[doc(hidden)]
#[inline]
pub fn [<cast_to_ShortInterval_ $units _ShortInterval>](
value: ShortInterval,
) -> SqlResult<ShortInterval> {
let unit = $unit as i64;
Ok(ShortInterval::from_microseconds(value.microseconds() / unit * unit))
}
cast_function!([<ShortInterval_ $units>], ShortInterval, ShortInterval, ShortInterval);
}
};
}
cast_to_short_interval!(DAYS, MICROS_PER_DAY);
cast_to_short_interval!(HOURS, MICROS_PER_HOUR);
cast_to_short_interval!(DAYS_TO_HOURS, MICROS_PER_HOUR);
cast_to_short_interval!(MINUTES, MICROS_PER_MINUTE);
cast_to_short_interval!(HOURS_TO_MINUTES, MICROS_PER_MINUTE);
cast_to_short_interval!(DAYS_TO_MINUTES, MICROS_PER_MINUTE);
cast_to_short_interval!(SECONDS, 1);
cast_to_short_interval!(DAYS_TO_SECONDS, 1);
cast_to_short_interval!(HOURS_TO_SECONDS, 1);
cast_to_short_interval!(MINUTES_TO_SECONDS, 1);
// Saturating forms of the casts from numbers to an interval in one unit: a number whose
// interval is below the range converts to the minimum interval, and one above it to the
Expand Down
Expand Up
@@ -4413,6 +4472,67 @@ mod tests {
limit_or_size_string,
};
#[test]
fn interval_casts_truncate() {
use crate::{
cast_to_LongInterval_MONTHS_LongInterval, cast_to_LongInterval_YEARS_LongInterval,
cast_to_ShortInterval_DAYS_ShortInterval,
cast_to_ShortInterval_HOURS_TO_MINUTES_ShortInterval,
cast_to_ShortInterval_SECONDS_ShortInterval,
};
let months = LongInterval::from_months;
assert_eq!(
cast_to_LongInterval_YEARS_LongInterval(months(19)).unwrap(),
months(12)
);
assert_eq!(
cast_to_LongInterval_YEARS_LongInterval(months(-19)).unwrap(),
months(-12)
);
assert_eq!(
cast_to_LongInterval_YEARS_LongInterval(months(i32::MIN)).unwrap(),
months(-178956970 * 12)
);
assert_eq!(
cast_to_LongInterval_MONTHS_LongInterval(months(19)).unwrap(),
months(19)
);
let hours = |h: i64| ShortInterval::from_microseconds(h * 3_600_000_000);
let micros = ShortInterval::from_microseconds;
assert_eq!(
cast_to_ShortInterval_DAYS_ShortInterval(hours(-25)).unwrap(),
hours(-24)
);
assert_eq!(
cast_to_ShortInterval_HOURS_TO_MINUTES_ShortInterval(micros(90_500_000)).unwrap(),
micros(60_000_000)
);
assert_eq!(
cast_to_ShortInterval_SECONDS_ShortInterval(micros(1_500_001)).unwrap(),
micros(1_500_001)
);
}
#[test]
fn decimal_to_short_interval_truncates() {
use crate::{
cast_to_ShortInterval_DAYS_SqlDecimal, cast_to_ShortInterval_SECONDS_SqlDecimal,
};
let value = SqlDecimal::<5, 2>::new(-150, 2).unwrap();
// A day holds no fraction; a second does
assert_eq!(
cast_to_ShortInterval_DAYS_SqlDecimal(value).unwrap(),
ShortInterval::from_microseconds(-86_400_000_000)
);
assert_eq!(
cast_to_ShortInterval_SECONDS_SqlDecimal(value).unwrap(),
ShortInterval::from_microseconds(-1_500_000)
);
}
#[test]
fn date_casts_out_of_range() {
// An out-of-range result is an error, not a panic, so SAFE_CAST produces NULL
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -44,6 +44,33 @@ Source edition can be found on github.
`storage_max`. A pipeline stored with such values still loads. See
[Storage limits](/pipelines/configuration#storage-limits).
- Incompatible change (SQL): date arithmetic now matches the SQL
standard. An interval holds whole numbers in every field except
`SECOND`, and an operation that produces an interval truncates the value
toward zero to the last field of its type (see
[the interval types](/sql/datetime#the-interval-types)):
- A cast between interval types:
`CAST(INTERVAL '19' MONTH AS INTERVAL YEAR)` is now `INTERVAL '1' YEAR`
instead of 19 months, so `DATE '2020-01-01' + CAST(INTERVAL '19' MONTH
AS INTERVAL YEAR)` is now 2021-01-01 instead of 2021-08-01.
`CAST(INTERVAL '25' HOUR AS INTERVAL DAY)` is now `INTERVAL '1' DAY`
instead of 25 hours.
- A cast of a number with a fraction: `CAST(1.5 AS INTERVAL YEAR)` is
now `INTERVAL '1' YEAR` instead of 18 months, and
`CAST(-1.5 AS INTERVAL DAY)` is now `INTERVAL '-1' DAY` instead of
-36 hours.
- The product or the quotient of an interval and a number, which has
the type of the interval: `INTERVAL '1' YEAR * 1.5` is now
`INTERVAL '1' YEAR` instead of 18 months, and `INTERVAL '1' DAY / 2`
is now `INTERVAL '0' DAY` instead of 12 hours.
- The difference of two dates, times or timestamps, which has the type
of its qualifier and counts the boundaries of its last field between
the two values: `(DATE '2021-01-01' - DATE '2020-12-31') YEAR` is now
`INTERVAL '1' YEAR` instead of 0 months, and
`(DATE '2021-03-01' - DATE '2020-01-01') YEAR` is now
`INTERVAL '1' YEAR` instead of 14 months. `TIMESTAMPDIFF` does not
change.
## v0.372.0
- Behavior change (non-fault-tolerant Kafka output): temporary delivery
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -37,6 +37,10 @@ interval expressed in the interval's unit. For example, `CAST(INTERVAL
'10' DAY AS BIGINT)` returns 10, and `CAST(INTERVAL '10.6' SECONDS AS
INTEGER)` returns 10. Long intervals use years or months as the unit;
short intervals use days, hours, minutes, or seconds.
A cast of a number to an interval type other than `SECOND` truncates
the number to an integer first: `CAST(1.5 AS INTERVAL YEAR)` is
`INTERVAL '1' YEAR`, and `CAST(-1.5 AS INTERVAL DAY)` is `INTERVAL
'-1' DAY`.
Casts of strings to numeric types, `BOOLEAN`, `DATE`, `TIME`,
`TIMESTAMP`, and `TIMESTAMP WITH TIME ZONE` first remove the
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -291,6 +291,31 @@ most about 106 million days (about 292,000 years), positive or
negative. A long interval can be at most about 179 million years,
positive or negative.
As the SQL standard specifies, an interval value holds whole numbers
in every field except `SECOND`: an `INTERVAL YEAR` holds a whole number of years, and an `INTERVAL DAY
TO HOUR` holds whole days and hours. An operation that produces an
interval truncates the value toward zero to the last field of the
result type:
- a cast between interval types: `CAST(INTERVAL '19' MONTH AS INTERVAL
YEAR)` is `INTERVAL '1' YEAR`, and `CAST(INTERVAL '-25' HOUR AS
INTERVAL DAY)` is `INTERVAL '-1' DAY`;
- the product or the quotient of an interval and a number, which has
the type of the interval: `INTERVAL '1' YEAR * 1.5` is `INTERVAL '1'
YEAR`, and `INTERVAL '1' DAY / 2` is `INTERVAL '0' DAY`;
- the difference of two dates, times or timestamps, which has the type
of its qualifier, and counts the boundaries of its last field between
the two values: `(DATE '2021-01-01' - DATE '2020-12-31') YEAR` is
`INTERVAL '1' YEAR`, and `(TIMESTAMP '2020-01-02 01:00:00' - TIMESTAMP
'2020-01-01 23:00:00') DAY` is `INTERVAL '1' DAY`. `TIMESTAMPDIFF`
counts the elapsed units instead: `TIMESTAMPDIFF(YEAR, DATE
'2020-12-31', DATE '2021-01-01')` is 0;
- a cast of a number: `CAST(-1.5 AS INTERVAL DAY)` is `INTERVAL '-1'
DAY`.
Seconds can have fractions, with the standard's default precision of
microseconds: `INTERVAL '1' SECOND * 1.5` is `INTERVAL '1.5' SECOND`.
### Interval literals
Interval literals (constant values) can be written using the following
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -250,13 +250,14 @@ class aggtst_atbl_interval_months_res(TstView):
"""Interval months converted to varchar"""
def __init__(self):
# Validated on HSQLDB
self.data = [
{
"id": 0,
"f_c1": "+605",
"f_c2": "-605",
"f_c3": "-33 ",
"f_c4": "+33 ",
"f_c3": "-34 ",
"f_c4": "+34 ",
"f_c5": "-639",
"f_c6": "+639",
},
Expand All
@@ -266,35 +267,35 @@ def __init__(self):
"f_c2": "+121",
"f_c3": "-64",
"f_c4": "+64",
"f_c5": "+56 ",
"f_c6": "-56 ",
"f_c5": "+57 ",
"f_c6": "-57 ",
},
{
"id": 1,
"f_c1": "+120 ",
"f_c2": "-120 ",
"f_c3": "+87 ",
"f_c4": "-87 ",
"f_c1": "+121 ",
"f_c2": "-121 ",
"f_c3": "+88 ",
"f_c4": "-88 ",
"f_c5": "-33",
"f_c6": "+33",
},
{
"id": 1,
"f_c1": "+605",
"f_c2": "-605",
"f_c3": "-16 ",
"f_c4": "+16 ",
"f_c3": "-17 ",
"f_c4": "+17 ",
"f_c5": "-622",
"f_c6": "+622",
},
{
"id": 1,
"f_c1": "-557 ",
"f_c2": "+557 ",
"f_c1": "-558 ",
"f_c2": "+558 ",
"f_c3": "-217",
"f_c4": "+217",
"f_c5": "+340 ",
"f_c6": "-340 ",
"f_c5": "+341 ",
"f_c6": "-341 ",
},
]
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.