Skip to content

Apache DataFusion SQL Query Engine

Mysql unparser cast targets (#26888)

hozan2339ad68578013
diff --git a/datafusion/sql/src/unparser/dialect.rs b/datafusion/sql/src/unparser/dialect.rs
index 9f12965ff9..2a5d324091 100644
--- a/datafusion/sql/src/unparser/dialect.rs
+++ b/datafusion/sql/src/unparser/dialect.rs
@@ -110,6 +110,36 @@ pub trait Dialect: Send + Sync {
ast::DataType::Integer(None)
}
+ /// The SQL type to use for Arrow Int16 unparsing
+ /// Most dialects use SmallInt, but some, like MySQL, require SIGNED
+ fn int16_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::SmallInt(None)
+ }
+
+ /// The SQL type to use for Arrow UInt8 unparsing
+ /// Most dialects use TinyIntUnsigned, but some, like MySQL, require UNSIGNED
+ fn uint8_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::TinyIntUnsigned(None)
+ }
+
+ /// The SQL type to use for Arrow UInt16 unparsing
+ /// Most dialects use SmallIntUnsigned, but some, like MySQL, require UNSIGNED
+ fn uint16_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::SmallIntUnsigned(None)
+ }
+
+ /// The SQL type to use for Arrow UInt32 unparsing
+ /// Most dialects use IntegerUnsigned, but some, like MySQL, require UNSIGNED
+ fn uint32_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::IntegerUnsigned(None)
+ }
+
+ /// The SQL type to use for Arrow UInt64 unparsing
+ /// Most dialects use BigIntUnsigned, but some, like MySQL, require UNSIGNED
+ fn uint64_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::BigIntUnsigned(None)
+ }
+
/// The SQL type to use for Timestamp unparsing
/// Most dialects use Timestamp, but some, like MySQL, require Datetime
/// Some dialects like Dremio does not support WithTimeZone and requires always Timestamp
@@ -589,6 +619,26 @@ impl Dialect for MySqlDialect {
ast::DataType::Custom(ObjectName::from(vec![Ident::new("SIGNED")]), vec![])
}
+ fn int16_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::Custom(ObjectName::from(vec![Ident::new("SIGNED")]), vec![])
+ }
+
+ fn uint8_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::Custom(ObjectName::from(vec![Ident::new("UNSIGNED")]), vec![])
+ }
+
+ fn uint16_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::Custom(ObjectName::from(vec![Ident::new("UNSIGNED")]), vec![])
+ }
+
+ fn uint32_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::Custom(ObjectName::from(vec![Ident::new("UNSIGNED")]), vec![])
+ }
+
+ fn uint64_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::Custom(ObjectName::from(vec![Ident::new("UNSIGNED")]), vec![])
+ }
+
fn timestamp_cast_dtype(
&self,
time_unit: &TimeUnit,
@@ -843,6 +893,11 @@ pub struct CustomDialect {
int8_cast_dtype: ast::DataType,
int64_cast_dtype: ast::DataType,
int32_cast_dtype: ast::DataType,
+ int16_cast_dtype: ast::DataType,
+ uint8_cast_dtype: ast::DataType,
+ uint16_cast_dtype: ast::DataType,
+ uint32_cast_dtype: ast::DataType,
+ uint64_cast_dtype: ast::DataType,
timestamp_cast_dtype: ast::DataType,
timestamp_tz_cast_dtype: ast::DataType,
date32_cast_dtype: ast::DataType,
@@ -870,6 +925,11 @@ impl Default for CustomDialect {
int8_cast_dtype: ast::DataType::TinyInt(None),
int64_cast_dtype: ast::DataType::BigInt(None),
int32_cast_dtype: ast::DataType::Integer(None),
+ int16_cast_dtype: ast::DataType::SmallInt(None),
+ uint8_cast_dtype: ast::DataType::TinyIntUnsigned(None),
+ uint16_cast_dtype: ast::DataType::SmallIntUnsigned(None),
+ uint32_cast_dtype: ast::DataType::IntegerUnsigned(None),
+ uint64_cast_dtype: ast::DataType::BigIntUnsigned(None),
timestamp_cast_dtype: ast::DataType::Timestamp(None, TimezoneInfo::None),
timestamp_tz_cast_dtype: ast::DataType::Timestamp(
None,
@@ -936,6 +996,26 @@ impl Dialect for CustomDialect {
self.int32_cast_dtype.clone()
}
+ fn int16_cast_dtype(&self) -> ast::DataType {
+ self.int16_cast_dtype.clone()
+ }
+
+ fn uint8_cast_dtype(&self) -> ast::DataType {
+ self.uint8_cast_dtype.clone()
+ }
+
+ fn uint16_cast_dtype(&self) -> ast::DataType {
+ self.uint16_cast_dtype.clone()
+ }
+
+ fn uint32_cast_dtype(&self) -> ast::DataType {
+ self.uint32_cast_dtype.clone()
+ }
+
+ fn uint64_cast_dtype(&self) -> ast::DataType {
+ self.uint64_cast_dtype.clone()
+ }
+
fn timestamp_cast_dtype(
&self,
_time_unit: &TimeUnit,
@@ -1030,6 +1110,11 @@ pub struct CustomDialectBuilder {
int8_cast_dtype: ast::DataType,
int64_cast_dtype: ast::DataType,
int32_cast_dtype: ast::DataType,
+ int16_cast_dtype: ast::DataType,
+ uint8_cast_dtype: ast::DataType,
+ uint16_cast_dtype: ast::DataType,
+ uint32_cast_dtype: ast::DataType,
+ uint64_cast_dtype: ast::DataType,
timestamp_cast_dtype: ast::DataType,
timestamp_tz_cast_dtype: ast::DataType,
date32_cast_dtype: ast::DataType,
@@ -1063,6 +1148,11 @@ impl CustomDialectBuilder {
int8_cast_dtype: ast::DataType::TinyInt(None),
int64_cast_dtype: ast::DataType::BigInt(None),
int32_cast_dtype: ast::DataType::Integer(None),
+ int16_cast_dtype: ast::DataType::SmallInt(None),
+ uint8_cast_dtype: ast::DataType::TinyIntUnsigned(None),
+ uint16_cast_dtype: ast::DataType::SmallIntUnsigned(None),
+ uint32_cast_dtype: ast::DataType::IntegerUnsigned(None),
+ uint64_cast_dtype: ast::DataType::BigIntUnsigned(None),
timestamp_cast_dtype: ast::DataType::Timestamp(None, TimezoneInfo::None),
timestamp_tz_cast_dtype: ast::DataType::Timestamp(
None,
@@ -1093,6 +1183,11 @@ impl CustomDialectBuilder {
int8_cast_dtype: self.int8_cast_dtype,
int64_cast_dtype: self.int64_cast_dtype,
int32_cast_dtype: self.int32_cast_dtype,
+ int16_cast_dtype: self.int16_cast_dtype,
+ uint8_cast_dtype: self.uint8_cast_dtype,
+ uint16_cast_dtype: self.uint16_cast_dtype,
+ uint32_cast_dtype: self.uint32_cast_dtype,
+ uint64_cast_dtype: self.uint64_cast_dtype,
timestamp_cast_dtype: self.timestamp_cast_dtype,
timestamp_tz_cast_dtype: self.timestamp_tz_cast_dtype,
date32_cast_dtype: self.date32_cast_dtype,
@@ -1194,6 +1289,36 @@ impl CustomDialectBuilder {
self
}
+ /// Customize the dialect with a specific SQL type for Int16 casting: SmallInt, SIGNED, etc.
+ pub fn with_int16_cast_dtype(mut self, int16_cast_dtype: ast::DataType) -> Self {
+ self.int16_cast_dtype = int16_cast_dtype;
+ self
+ }
+
+ /// Customize the dialect with a specific SQL type for UInt8 casting: TinyIntUnsigned, UNSIGNED, etc.
+ pub fn with_uint8_cast_dtype(mut self, uint8_cast_dtype: ast::DataType) -> Self {
+ self.uint8_cast_dtype = uint8_cast_dtype;
+ self
+ }
+
+ /// Customize the dialect with a specific SQL type for UInt16 casting: SmallIntUnsigned, UNSIGNED, etc.
+ pub fn with_uint16_cast_dtype(mut self, uint16_cast_dtype: ast::DataType) -> Self {
+ self.uint16_cast_dtype = uint16_cast_dtype;
+ self
+ }
+
+ /// Customize the dialect with a specific SQL type for UInt32 casting: IntegerUnsigned, UNSIGNED, etc.
+ pub fn with_uint32_cast_dtype(mut self, uint32_cast_dtype: ast::DataType) -> Self {
+ self.uint32_cast_dtype = uint32_cast_dtype;
+ self
+ }
+
+ /// Customize the dialect with a specific SQL type for UInt64 casting: BigIntUnsigned, UNSIGNED, etc.
+ pub fn with_uint64_cast_dtype(mut self, uint64_cast_dtype: ast::DataType) -> Self {
+ self.uint64_cast_dtype = uint64_cast_dtype;
+ self
+ }
+
/// Customize the dialect with a specific SQL type for Timestamp casting: Timestamp, Datetime, etc.
pub fn with_timestamp_cast_dtype(
mut self,
diff --git a/datafusion/sql/src/unparser/expr.rs b/datafusion/sql/src/unparser/expr.rs
index 053b1beb63..c8eb8d0d2d 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -1845,13 +1845,13 @@ impl Unparser<'_> {
}
DataType::Boolean => Ok(ast::DataType::Bool),
DataType::Int8 => Ok(self.dialect.int8_cast_dtype()),
- DataType::Int16 => Ok(ast::DataType::SmallInt(None)),
+ DataType::Int16 => Ok(self.dialect.int16_cast_dtype()),
DataType::Int32 => Ok(self.dialect.int32_cast_dtype()),
DataType::Int64 => Ok(self.dialect.int64_cast_dtype()),
- DataType::UInt8 => Ok(ast::DataType::TinyIntUnsigned(None)),
- DataType::UInt16 => Ok(ast::DataType::SmallIntUnsigned(None)),
- DataType::UInt32 => Ok(ast::DataType::IntegerUnsigned(None)),
- DataType::UInt64 => Ok(ast::DataType::BigIntUnsigned(None)),
+ DataType::UInt8 => Ok(self.dialect.uint8_cast_dtype()),
+ DataType::UInt16 => Ok(self.dialect.uint16_cast_dtype()),
+ DataType::UInt32 => Ok(self.dialect.uint32_cast_dtype()),
+ DataType::UInt64 => Ok(self.dialect.uint64_cast_dtype()),
DataType::Float16 => {
not_impl_err!("Unsupported DataType: conversion: {data_type}")
}
@@ -3144,12 +3144,69 @@ mod tests {
Ok(())
}
+ // MySQL's `CAST` only accepts `SIGNED` and `UNSIGNED` as integer target types.
#[test]
- fn mysql_int8_cast_uses_signed() -> Result<()> {
+ fn mysql_integer_casts() -> Result<()> {
let unparser = Unparser::new(&MySqlDialect {});
- let expr = Expr::Cast(Cast::new(Box::new(col("a")), Int8));
- let ast = unparser.expr_to_sql(&expr)?;
- assert_eq!(format!("{ast}"), "CAST(`a` AS SIGNED)");
+ for (data_type, expected) in [
+ (Int8, "CAST(`a` AS SIGNED)"),
+ (DataType::Int16, "CAST(`a` AS SIGNED)"),
+ (DataType::Int32, "CAST(`a` AS SIGNED)"),
+ (DataType::Int64, "CAST(`a` AS SIGNED)"),
+ (DataType::UInt8, "CAST(`a` AS UNSIGNED)"),
+ (DataType::UInt16, "CAST(`a` AS UNSIGNED)"),
+ (DataType::UInt32, "CAST(`a` AS UNSIGNED)"),
+ (DataType::UInt64, "CAST(`a` AS UNSIGNED)"),
+ ] {
+ let expr = Expr::Cast(Cast::new(Box::new(col("a")), data_type));
+ let ast = unparser.expr_to_sql(&expr)?;
+ assert_eq!(format!("{ast}"), expected);
+ }
+ Ok(())
+ }
+
+ #[test]
+ fn default_and_custom_dialect_integer_casts() -> Result<()> {
+ let custom_dialect = CustomDialectBuilder::new()
+ .with_int8_cast_dtype(ast::DataType::SmallInt(None))
+ .with_int16_cast_dtype(ast::DataType::Integer(None))
+ .with_uint8_cast_dtype(ast::DataType::SmallInt(None))
+ .with_uint16_cast_dtype(ast::DataType::Integer(None))
+ .with_uint32_cast_dtype(ast::DataType::BigInt(None))
+ .with_uint64_cast_dtype(ast::DataType::Numeric(ExactNumberInfo::None))
+ .build();
+ let default_unparser = Unparser::new(&DefaultDialect {});
+ let custom_unparser = Unparser::new(&custom_dialect);
+ for (data_type, default_expected, custom_expected) in [
+ (Int8, "CAST(a AS TINYINT)", "CAST(a AS SMALLINT)"),
+ (DataType::Int16, "CAST(a AS SMALLINT)", "CAST(a AS INTEGER)"),
+ (
+ DataType::UInt8,
+ "CAST(a AS TINYINT UNSIGNED)",
+ "CAST(a AS SMALLINT)",
+ ),
+ (
+ DataType::UInt16,
+ "CAST(a AS SMALLINT UNSIGNED)",
+ "CAST(a AS INTEGER)",
+ ),
+ (
+ DataType::UInt32,
+ "CAST(a AS INTEGER UNSIGNED)",
+ "CAST(a AS BIGINT)",
+ ),
+ (
+ DataType::UInt64,
+ "CAST(a AS BIGINT UNSIGNED)",
+ "CAST(a AS NUMERIC)",
+ ),
+ ] {
+ let expr = Expr::Cast(Cast::new(Box::new(col("a")), data_type));
+ let ast = default_unparser.expr_to_sql(&expr)?;
+ assert_eq!(format!("{ast}"), default_expected);
+ let ast = custom_unparser.expr_to_sql(&expr)?;
+ assert_eq!(format!("{ast}"), custom_expected);
+ }
Ok(())
}