You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/06/22 07:50:25 UTC

(datafusion) branch main updated: feat(unparser): support DISTINCT FROM operators in the MySQL dialect (#22999)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 330d654817 feat(unparser): support DISTINCT FROM operators in the MySQL dialect (#22999)
330d654817 is described below

commit 330d654817b7842a522bfe20436d396543fbd898
Author: Louis Vialar <zy...@users.noreply.github.com>
AuthorDate: Mon Jun 22 09:26:41 2026 +0200

    feat(unparser): support DISTINCT FROM operators in the MySQL dialect (#22999)
    
    ## Which issue does this PR close?
    
    Closes #22997
    
    ## Rationale for this change
    
    MySQL does not support the `IS NOT DISTINCT FROM` syntax, but has an
    alternative syntax which does the same, the spaceship operator.
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand
    your changes and offer better suggestions for fixes.
    -->
    
    ## What changes are included in this PR?
    
    <!--
    There is no need to duplicate the description in the issue here but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    
    Adds a `DistinctFromStyle` attribute to dialects that dictates how `IS
    DISTINCT FROM` is unparsed.
    
    ## Are these changes tested?
    
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    -->
    
    Yes
    
    ## Are there any user-facing changes?
    
    No, we provide a default implementation for `DistinctFromStyle`
    
    <!--
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    -->
    
    <!--
    If there are any breaking changes to public APIs, please add the `api
    change` label.
    -->
    
    ---------
    
    Co-authored-by: kosiew <ko...@gmail.com>
---
 datafusion/sql/src/unparser/dialect.rs | 30 ++++++++++++++++++++++
 datafusion/sql/src/unparser/expr.rs    | 47 ++++++++++++++++++++++++++--------
 2 files changed, 67 insertions(+), 10 deletions(-)

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]


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org