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

feat(sql): add casting support for shortened intervals #4220

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 77 additions & 11 deletions src/sql/src/statements/transform/expand_interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::ops::ControlFlow;
use itertools::Itertools;
use lazy_static::lazy_static;
use regex::Regex;
use sqlparser::ast::{Expr, Interval, Value};
use sqlparser::ast::{DataType, Expr, Interval, Value};

use crate::statements::transform::TransformRule;

Expand All @@ -36,7 +36,7 @@ lazy_static! {
("s","seconds"),
("millis","milliseconds"),
("mils","milliseconds"),
etolbakov marked this conversation as resolved.
Show resolved Hide resolved
("ms","microseconds"),
("ms","milliseconds"),
("us","microseconds"),
("ns","nanoseconds"),
]);
etolbakov marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -52,7 +52,7 @@ lazy_static! {
/// - `s` for `seconds`
/// - `millis` for `milliseconds`
/// - `mils` for `milliseconds`
/// - `ms` for `microseconds`
/// - `ms` for `milliseconds`
/// - `us` for `microseconds`
/// - `ns` for `nanoseconds`
/// Required for use cases that use the shortened version of Interval declaration,
Expand All @@ -65,8 +65,8 @@ impl TransformRule for ExpandIntervalTransformRule {
/// it's AST has `left` part of type `Value::SingleQuotedString` which needs to be handled specifically.
/// To handle the `right` part which is `Interval` no extra steps are needed.
fn visit_expr(&self, expr: &mut Expr) -> ControlFlow<()> {
if let Expr::Interval(interval) = expr {
match *interval.value.clone() {
match expr {
Expr::Interval(interval) => match *interval.value.clone() {
etolbakov marked this conversation as resolved.
Show resolved Hide resolved
Expr::Value(Value::SingleQuotedString(value))
| Expr::Value(Value::DoubleQuotedString(value)) => {
if let Some(data) = expand_interval_name(&value) {
Expand All @@ -91,7 +91,30 @@ impl TransformRule for ExpandIntervalTransformRule {
_ => {}
},
_ => {}
},
Expr::Cast {
expr: cast_exp,
data_type,
..
} => {
if DataType::Interval == *data_type {
match *cast_exp.clone() {
Expr::Value(Value::SingleQuotedString(value))
| Expr::Value(Value::DoubleQuotedString(value)) => {
let data = expand_interval_name(&value).unwrap_or(value);
*expr = Expr::Interval(Interval {
value: single_quoted_string_expr(data),
leading_field: None,
leading_precision: None,
last_field: None,
fractional_seconds_precision: None,
})
}
_ => {}
}
}
}
etolbakov marked this conversation as resolved.
Show resolved Hide resolved
_ => {}
}
ControlFlow::<()>::Continue(())
}
Expand Down Expand Up @@ -135,7 +158,7 @@ fn expand_interval_name(interval_str: &str) -> Option<String> {
mod tests {
use std::ops::ControlFlow;

use sqlparser::ast::{BinaryOperator, Expr, Interval, Value};
use sqlparser::ast::{BinaryOperator, DataType, Expr, Interval, Value};

use crate::statements::transform::expand_interval::{
expand_interval_name, single_quoted_string_expr, ExpandIntervalTransformRule,
Expand All @@ -154,7 +177,7 @@ mod tests {
("2m", "2 minutes"),
("100millis", "100 milliseconds"),
("150mils", "150 milliseconds"),
("200ms", "200 microseconds"),
("200ms", "200 milliseconds"),
("350us", "350 microseconds"),
("400ns", "400 nanoseconds"),
];
Expand All @@ -175,8 +198,8 @@ mod tests {
("2y4mon6w", "2 years 4 months 6 weeks"),
("5d3h1m", "5 days 3 hours 1 minutes"),
(
"10s312millis789ms",
"10 seconds 312 milliseconds 789 microseconds",
"10s312ms789ns",
"10 seconds 312 milliseconds 789 nanoseconds",
),
(
"23mils987us754ns",
Expand All @@ -186,8 +209,8 @@ mod tests {
("-2y-4mon-6w", "-2 years -4 months -6 weeks"),
("-5d-3h-1m", "-5 days -3 hours -1 minutes"),
(
"-10s-312millis-789ms",
"-10 seconds -312 milliseconds -789 microseconds",
"-10s-312ms-789ns",
"-10 seconds -312 milliseconds -789 nanoseconds",
),
(
"-23mils-987us-754ns",
Expand Down Expand Up @@ -275,4 +298,47 @@ mod tests {
})
);
}

#[test]
fn test_visit_expr_when_cast_expr() {
let interval_transformation_rule = ExpandIntervalTransformRule {};

let mut cast_to_interval_expr = Expr::Cast {
expr: single_quoted_string_expr("3y2mon".to_string()),
data_type: DataType::Interval,
format: None,
};

let control_flow = interval_transformation_rule.visit_expr(&mut cast_to_interval_expr);

assert_eq!(control_flow, ControlFlow::Continue(()));
assert_eq!(
cast_to_interval_expr,
Expr::Interval(Interval {
value: Box::new(Expr::Value(Value::SingleQuotedString(
"3 years 2 months".to_string()
))),
leading_field: None,
leading_precision: None,
last_field: None,
fractional_seconds_precision: None,
})
);

let mut cast_to_i64_expr = Expr::Cast {
expr: single_quoted_string_expr("5".to_string()),
data_type: DataType::Int64,
format: None,
};
let control_flow = interval_transformation_rule.visit_expr(&mut cast_to_i64_expr);
assert_eq!(control_flow, ControlFlow::Continue(()));
assert_eq!(
cast_to_i64_expr,
Expr::Cast {
expr: single_quoted_string_expr("5".to_string()),
data_type: DataType::Int64,
format: None,
}
);
}
}
16 changes: 16 additions & 0 deletions tests/cases/standalone/common/types/interval/interval.result
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ SELECT INTERVAL '-2 months';
| 0 years -2 mons 0 days 0 hours 0 mins 0.000000000 secs |
+---------------------------------------------------------+

SELECT '3 hours'::INTERVAL;

+-------------------------------------------------------+
| IntervalMonthDayNano("10800000000000") |
+-------------------------------------------------------+
| 0 years 0 mons 0 days 3 hours 0 mins 0.000000000 secs |
+-------------------------------------------------------+

SELECT INTERVAL '1 year 2 months 3 days 4 hours' + INTERVAL '1 year';

+------------------------------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -305,6 +313,14 @@ SELECT INTERVAL '1y2w3d4h';
| 0 years 12 mons 17 days 4 hours 0 mins 0.000000000 secs |
+---------------------------------------------------------+

SELECT '3y2mon'::INTERVAL;

+---------------------------------------------------------+
| IntervalMonthDayNano("3010670175542044828554670112768") |
+---------------------------------------------------------+
| 0 years 38 mons 0 days 0 hours 0 mins 0.000000000 secs |
+---------------------------------------------------------+

SELECT INTERVAL '7 days' - INTERVAL '1d';

+----------------------------------------------------------------------------------------------+
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/standalone/common/types/interval/interval.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ SELECT INTERVAL '1.5 year';

SELECT INTERVAL '-2 months';

SELECT '3 hours'::INTERVAL;

SELECT INTERVAL '1 year 2 months 3 days 4 hours' + INTERVAL '1 year';

SELECT INTERVAL '1 year 2 months 3 days 4 hours' - INTERVAL '1 year';
Expand Down Expand Up @@ -77,6 +79,8 @@ SELECT INTERVAL '-1h-5m';

SELECT INTERVAL '1y2w3d4h';

SELECT '3y2mon'::INTERVAL;

SELECT INTERVAL '7 days' - INTERVAL '1d';

SELECT INTERVAL '2h' + INTERVAL '1h';
Expand Down