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

Add support for IntervalStyle::MySQL #18

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Changes from 3 commits
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
168 changes: 150 additions & 18 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use arrow::util::display::array_value_to_string;
use arrow_array::{Date32Array, Date64Array, PrimitiveArray};
use arrow_schema::DataType;
use core::fmt;
use sqlparser::ast::TimezoneInfo;
use sqlparser::ast::Value::SingleQuotedString;
use sqlparser::ast::{
self, Expr as AstExpr, Function, FunctionArg, Ident, Interval, UnaryOperator,
};
use sqlparser::ast::{DateTimeField, TimezoneInfo};
use std::sync::Arc;
use std::{fmt::Display, vec};

Expand Down Expand Up @@ -951,6 +951,90 @@ impl Unparser<'_> {
}
}

/// MySQL requires INTERVAL sql to be in the format: INTERVAL 1 YEAR + INTERVAL 1 MONTH + INTERVAL 1 DAY etc
/// https://dev.mysql.com/doc/refman/8.4/en/expressions.html#temporal-intervals
sgrebnov marked this conversation as resolved.
Show resolved Hide resolved
/// MySQL supports the DAY_MICROSECOND unit type (format is DAYS HOURS:MINUTES:SECONDS.MICROSECONDS), which can be used to optimize
/// the number of INTERVAL elements for complex cases. Current implementaion is made based of separate INTERVALs to generate
/// cleaner SQL for typical scenarios where only a few INTERVAL elements are used (day, hours, etc.).
fn interval_to_mysql_expr(
&self,
years: i32,
months: i32,
days: i32,
hours: i32,
minutes: i32,
secs: i32,
micros: i64,
) -> Result<ast::Expr> {
let secs = secs + (micros / 1_000_000) as i32;
let micros = micros % 1_000_000;

let minutes = minutes + secs / 60;
let secs = secs % 60;

let hours = hours + minutes / 60;
let minutes = minutes % 60;

let days = days + hours / 24;
let hours = hours % 24;

let years = years + months / 12;
let months = months % 12;

let mut intervals = [
(years as i64, DateTimeField::Year),
(months as i64, DateTimeField::Month),
(days as i64, DateTimeField::Day),
(hours as i64, DateTimeField::Hour),
(minutes as i64, DateTimeField::Minute),
(secs as i64, DateTimeField::Second),
(micros, DateTimeField::Microsecond),
]
.into_iter()
.filter(|(value, _)| *value != 0)
.map(|(value, field)| {
ast::Expr::Interval(Interval {
value: Box::new(ast::Expr::Value(ast::Value::Number(
value.to_string(),
false,
))),
leading_field: Some(field),
leading_precision: None,
last_field: None,
fractional_seconds_precision: None,
})
})
.collect::<Vec<_>>();

// Combine all intervals into single expression
let initial_interval = if !intervals.is_empty() {
intervals.remove(0)
} else {
// if for some reason INTERVAL value is 0, we fallback to `INTERVAL 0 DAY` as returning ast::Value::Null can break the query
return Ok(ast::Expr::Interval(Interval {
value: Box::new(ast::Expr::Value(ast::Value::Number(
"0".to_string(),
false,
))),
leading_field: Some(DateTimeField::Day),
leading_precision: None,
last_field: None,
fractional_seconds_precision: None,
}));
};

let combined_intervals =
intervals
.into_iter()
.fold(initial_interval, |acc, interval| ast::Expr::BinaryOp {
left: Box::new(acc),
op: ast::BinaryOperator::Plus,
right: Box::new(interval),
});

return Ok(combined_intervals);
}

fn interval_scalar_to_sql(&self, v: &ScalarValue) -> Result<ast::Expr> {
match self.dialect.interval_style() {
IntervalStyle::PostgresVerbose => {
Expand All @@ -973,10 +1057,7 @@ impl Unparser<'_> {
}
// If the interval standard is SQLStandard, implement a simple unparse logic
IntervalStyle::SQLStandard => match v {
ScalarValue::IntervalYearMonth(v) => {
let Some(v) = v else {
return Ok(ast::Expr::Value(ast::Value::Null));
};
ScalarValue::IntervalYearMonth(Some(v)) => {
let interval = Interval {
value: Box::new(ast::Expr::Value(
ast::Value::SingleQuotedString(v.to_string()),
Expand All @@ -988,10 +1069,7 @@ impl Unparser<'_> {
};
Ok(ast::Expr::Interval(interval))
}
ScalarValue::IntervalDayTime(v) => {
let Some(v) = v else {
return Ok(ast::Expr::Value(ast::Value::Null));
};
ScalarValue::IntervalDayTime(Some(v)) => {
let days = v.days;
let secs = v.milliseconds / 1_000;
let mins = secs / 60;
Expand All @@ -1014,11 +1092,7 @@ impl Unparser<'_> {
};
Ok(ast::Expr::Interval(interval))
}
ScalarValue::IntervalMonthDayNano(v) => {
let Some(v) = v else {
return Ok(ast::Expr::Value(ast::Value::Null));
};

ScalarValue::IntervalMonthDayNano(Some(v)) => {
if v.months >= 0 && v.days == 0 && v.nanoseconds == 0 {
let interval = Interval {
value: Box::new(ast::Expr::Value(
Expand Down Expand Up @@ -1060,11 +1134,37 @@ impl Unparser<'_> {
not_impl_err!("Unsupported IntervalMonthDayNano scalar with both Month and DayTime for IntervalStyle::SQLStandard")
}
}
_ => Ok(ast::Expr::Value(ast::Value::Null)),
_ => not_impl_err!(
"Unsupported ScalarValue for Interval conversion: {v:?}"
),
},
IntervalStyle::MySQL => match v {
ScalarValue::IntervalYearMonth(Some(v)) => {
self.interval_to_mysql_expr(0, v.clone(), 0, 0, 0, 0, 0)
}
ScalarValue::IntervalDayTime(Some(v)) => self.interval_to_mysql_expr(
0,
0,
v.days,
0,
0,
0,
v.milliseconds as i64 * 1_000,
),
ScalarValue::IntervalMonthDayNano(Some(v)) => self
.interval_to_mysql_expr(
0,
v.months,
v.days,
0,
0,
0,
v.nanoseconds / 1_000,
),
_ => not_impl_err!(
"Unsupported ScalarValue for Interval conversion: {v:?}"
),
},
IntervalStyle::MySQL => {
not_impl_err!("Unsupported interval scalar for IntervalStyle::MySQL")
}
}
}

Expand Down Expand Up @@ -1799,6 +1899,38 @@ mod tests {
IntervalStyle::PostgresVerbose,
r#"INTERVAL '1 YEARS 7 MONS 0 DAYS 0 HOURS 0 MINS 0.00 SECS'"#,
),
(
interval_month_day_nano_lit(
sgrebnov marked this conversation as resolved.
Show resolved Hide resolved
"1 YEAR 1 MONTH 1 DAY 3 HOUR 10 MINUTE 20 SECOND",
),
IntervalStyle::MySQL,
r#"INTERVAL 1 YEAR + INTERVAL 1 MONTH + INTERVAL 1 DAY + INTERVAL 3 HOUR + INTERVAL 10 MINUTE + INTERVAL 20 SECOND"#,
sgrebnov marked this conversation as resolved.
Show resolved Hide resolved
),
(
interval_month_day_nano_lit("1.5 MONTH"),
IntervalStyle::MySQL,
r#"INTERVAL 1 MONTH + INTERVAL 15 DAY"#,
),
(
interval_month_day_nano_lit("-3 MONTH"),
IntervalStyle::MySQL,
r#"INTERVAL -3 MONTH"#,
),
(
interval_datetime_lit("10 DAY 1.5 HOUR 10 MINUTE 20 SECOND"),
IntervalStyle::MySQL,
r#"INTERVAL 10 DAY + INTERVAL 1 HOUR + INTERVAL 40 MINUTE + INTERVAL 20 SECOND"#,
),
(
interval_year_month_lit("0 DAY 0 HOUR"),
IntervalStyle::MySQL,
r#"INTERVAL 0 DAY"#,
),
(
interval_month_day_nano_lit("1296000000 SECOND"),
IntervalStyle::MySQL,
r#"INTERVAL 15000 DAY"#,
),
];

for (value, style, expected) in tests {
Expand Down