Skip to content

Apache DataFusion SQL Query Engine

feat(unparser): support DISTINCT FROM operators in the MySQL dialect (#22999)

Louis Vialar330d654817b7
diff --git a/datafusion/sql/src/unparser/dialect.rs b/datafusion/sql/src/unparser/dialect.rs
index d934462240..872af63533 100644
--- a/datafusion/sql/src/unparser/dialect.rs
+++ b/datafusion/sql/src/unparser/dialect.rs
@@ -94,6 +94,11 @@ pub trait Dialect: Send + Sync {
DateFieldExtractStyle::DatePart
}
+ /// The style to use when unparsing DISTINCT FROM style expressions
+ fn distinct_from_style(&self) -> DistinctFromStyle {
+ DistinctFromStyle::FullText
+ }
+
/// The character length extraction style to use: `CharacterLengthStyle`
fn character_length_style(&self) -> CharacterLengthStyle {
CharacterLengthStyle::CharacterLength
@@ -333,6 +338,15 @@ pub enum CharacterLengthStyle {
CharacterLength,
}
+/// `DistinctFromStyle` to use for unparsing `IsDistinctFrom` and `IsNotDistinctFrom` operators
+#[derive(Clone, Copy, PartialEq)]
+pub enum DistinctFromStyle {
+ /// DBMS supports `IS (NOT) DISTINCT FROM`
+ FullText,
+ /// DBMS supports equivalent operations via `<=>` and `NOT <=>`
+ Spaceship,
+}
+
pub struct DefaultDialect {}
impl Dialect for DefaultDialect {
@@ -385,6 +399,10 @@ impl Dialect for PostgreSqlDialect {
ast::DataType::SmallInt(None)
}
+ fn distinct_from_style(&self) -> DistinctFromStyle {
+ DistinctFromStyle::FullText
+ }
+
fn scalar_function_to_sql_overrides(
&self,
unparser: &Unparser,
@@ -529,6 +547,10 @@ impl Dialect for DuckDBDialect {
Ok(None)
}
+
+ fn distinct_from_style(&self) -> DistinctFromStyle {
+ DistinctFromStyle::FullText
+ }
}
pub struct MySqlDialect {}
@@ -562,6 +584,10 @@ impl Dialect for MySqlDialect {
DateFieldExtractStyle::Extract
}
+ fn distinct_from_style(&self) -> DistinctFromStyle {
+ DistinctFromStyle::Spaceship
+ }
+
fn int64_cast_dtype(&self) -> ast::DataType {
ast::DataType::Custom(ObjectName::from(vec![Ident::new("SIGNED")]), vec![])
}
@@ -619,6 +645,10 @@ impl Dialect for SqliteDialect {
CharacterLengthStyle::Length
}
+ fn distinct_from_style(&self) -> DistinctFromStyle {
+ DistinctFromStyle::FullText
+ }
+
fn supports_column_alias_in_table_alias(&self) -> bool {
false
}
diff --git a/datafusion/sql/src/unparser/expr.rs b/datafusion/sql/src/unparser/expr.rs
index bcc46e837b..6096e17140 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -30,7 +30,7 @@ use std::sync::Arc;
use std::vec;
use super::Unparser;
-use super::dialect::IntervalStyle;
+use super::dialect::{DistinctFromStyle, IntervalStyle};
use arrow::array::{
ArrayRef, Date32Array, Date64Array, PrimitiveArray,
types::{
@@ -156,10 +156,23 @@ impl Unparser<'_> {
let l = self.expr_to_sql_inner(left.as_ref())?;
let r = self.expr_to_sql_inner(right.as_ref())?;
- Ok(ast::Expr::Nested(Box::new(ast::Expr::IsDistinctFrom(
- Box::new(l),
- Box::new(r),
- ))))
+ match self.dialect.distinct_from_style() {
+ DistinctFromStyle::FullText => Ok(ast::Expr::Nested(Box::new(
+ ast::Expr::IsDistinctFrom(Box::new(l), Box::new(r)),
+ ))),
+ DistinctFromStyle::Spaceship => {
+ Ok(ast::Expr::Nested(Box::new(ast::Expr::UnaryOp {
+ op: UnaryOperator::Not,
+ expr: Box::new(ast::Expr::Nested(Box::new(
+ ast::Expr::BinaryOp {
+ left: Box::new(l),
+ right: Box::new(r),
+ op: BinaryOperator::Spaceship,
+ },
+ ))),
+ })))
+ }
+ }
}
Expr::BinaryExpr(BinaryExpr {
left,
@@ -169,10 +182,18 @@ impl Unparser<'_> {
let l = self.expr_to_sql_inner(left.as_ref())?;
let r = self.expr_to_sql_inner(right.as_ref())?;
- Ok(ast::Expr::Nested(Box::new(ast::Expr::IsNotDistinctFrom(
- Box::new(l),
- Box::new(r),
- ))))
+ match self.dialect.distinct_from_style() {
+ DistinctFromStyle::FullText => Ok(ast::Expr::Nested(Box::new(
+ ast::Expr::IsNotDistinctFrom(Box::new(l), Box::new(r)),
+ ))),
+ DistinctFromStyle::Spaceship => {
+ Ok(ast::Expr::Nested(Box::new(ast::Expr::BinaryOp {
+ left: Box::new(l),
+ right: Box::new(r),
+ op: BinaryOperator::Spaceship,
+ })))
+ }
+ }
}
Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
let l = self.expr_to_sql_inner(left.as_ref())?;
@@ -1908,7 +1929,7 @@ mod tests {
use std::ops::{Add, Sub};
use std::{sync::Arc, vec};
- use crate::unparser::dialect::SqliteDialect;
+ use crate::unparser::dialect::{MySqlDialect, SqliteDialect};
use arrow::array::{LargeListArray, LargeListViewArray, ListArray, ListViewArray};
use arrow::datatypes::{DataType::Int8, Field, Int32Type, Schema, TimeUnit};
use ast::ObjectName;
@@ -3714,6 +3735,8 @@ mod tests {
#[test]
fn test_is_distinct_from() {
+ let mysql_unparser = Unparser::new(&MySqlDialect {});
+
let expr = Expr::BinaryExpr(BinaryExpr::new(
Box::new(col("c1")),
Operator::IsDistinctFrom,
@@ -3722,6 +3745,8 @@ mod tests {
let sql = expr_to_sql(&expr).unwrap().to_string();
assert_eq!(sql, "(c1 IS DISTINCT FROM true)");
+ let sql = mysql_unparser.expr_to_sql(&expr).unwrap().to_string();
+ assert_eq!(sql, "(NOT (`c1` <=> true))");
let expr = Expr::BinaryExpr(BinaryExpr::new(
Box::new(col("c1")),
@@ -3731,6 +3756,8 @@ mod tests {
let sql = expr_to_sql(&expr).unwrap().to_string();
assert_eq!(sql, "(c1 IS NOT DISTINCT FROM true)");
+ let sql = mysql_unparser.expr_to_sql(&expr).unwrap().to_string();
+ assert_eq!(sql, "(`c1` <=> true)");
}
#[test]