Skip to content

Apache DataFusion SQL Query Engine

fix: PostgreSQL dialect can not support tinyint type (#21445)

Zhen Chenb46634c192c1
diff --git a/datafusion/sql/src/unparser/dialect.rs b/datafusion/sql/src/unparser/dialect.rs
index 9e0683bdd7..ee31190b68 100644
--- a/datafusion/sql/src/unparser/dialect.rs
+++ b/datafusion/sql/src/unparser/dialect.rs
@@ -100,6 +100,12 @@ pub trait Dialect: Send + Sync {
ast::DataType::BigInt(None)
}
+ /// The SQL type to use for Arrow Int8 unparsing
+ /// Most dialects use TinyInt, but PostgreSQL prefers SmallInt
+ fn int8_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::TinyInt(None)
+ }
+
/// The SQL type to use for Arrow Int32 unparsing
/// Most dialects use Integer, but some, like MySQL, require SIGNED
fn int32_cast_dtype(&self) -> ast::DataType {
@@ -345,6 +351,10 @@ impl Dialect for PostgreSqlDialect {
ast::DataType::DoublePrecision
}
+ fn int8_cast_dtype(&self) -> ast::DataType {
+ ast::DataType::SmallInt(None)
+ }
+
fn scalar_function_to_sql_overrides(
&self,
unparser: &Unparser,
@@ -664,6 +674,7 @@ pub struct CustomDialect {
large_utf8_cast_dtype: ast::DataType,
date_field_extract_style: DateFieldExtractStyle,
character_length_style: CharacterLengthStyle,
+ int8_cast_dtype: ast::DataType,
int64_cast_dtype: ast::DataType,
int32_cast_dtype: ast::DataType,
timestamp_cast_dtype: ast::DataType,
@@ -689,6 +700,7 @@ impl Default for CustomDialect {
large_utf8_cast_dtype: ast::DataType::Text,
date_field_extract_style: DateFieldExtractStyle::DatePart,
character_length_style: CharacterLengthStyle::CharacterLength,
+ int8_cast_dtype: ast::DataType::TinyInt(None),
int64_cast_dtype: ast::DataType::BigInt(None),
int32_cast_dtype: ast::DataType::Integer(None),
timestamp_cast_dtype: ast::DataType::Timestamp(None, TimezoneInfo::None),
@@ -748,6 +760,10 @@ impl Dialect for CustomDialect {
self.int64_cast_dtype.clone()
}
+ fn int8_cast_dtype(&self) -> ast::DataType {
+ self.int8_cast_dtype.clone()
+ }
+
fn int32_cast_dtype(&self) -> ast::DataType {
self.int32_cast_dtype.clone()
}
@@ -839,6 +855,7 @@ pub struct CustomDialectBuilder {
large_utf8_cast_dtype: ast::DataType,
date_field_extract_style: DateFieldExtractStyle,
character_length_style: CharacterLengthStyle,
+ int8_cast_dtype: ast::DataType,
int64_cast_dtype: ast::DataType,
int32_cast_dtype: ast::DataType,
timestamp_cast_dtype: ast::DataType,
@@ -870,6 +887,7 @@ impl CustomDialectBuilder {
large_utf8_cast_dtype: ast::DataType::Text,
date_field_extract_style: DateFieldExtractStyle::DatePart,
character_length_style: CharacterLengthStyle::CharacterLength,
+ int8_cast_dtype: ast::DataType::TinyInt(None),
int64_cast_dtype: ast::DataType::BigInt(None),
int32_cast_dtype: ast::DataType::Integer(None),
timestamp_cast_dtype: ast::DataType::Timestamp(None, TimezoneInfo::None),
@@ -898,6 +916,7 @@ impl CustomDialectBuilder {
large_utf8_cast_dtype: self.large_utf8_cast_dtype,
date_field_extract_style: self.date_field_extract_style,
character_length_style: self.character_length_style,
+ int8_cast_dtype: self.int8_cast_dtype,
int64_cast_dtype: self.int64_cast_dtype,
int32_cast_dtype: self.int32_cast_dtype,
timestamp_cast_dtype: self.timestamp_cast_dtype,
@@ -952,6 +971,12 @@ impl CustomDialectBuilder {
self
}
+ /// Customize the dialect with a specific SQL type for Int8 casting: TinyInt, SmallInt, etc.
+ pub fn with_int8_cast_dtype(mut self, int8_cast_dtype: ast::DataType) -> Self {
+ self.int8_cast_dtype = int8_cast_dtype;
+ self
+ }
+
/// Customize the dialect with a specific SQL type for Float64 casting: DOUBLE, DOUBLE PRECISION, etc.
pub fn with_float64_ast_dtype(mut self, float64_ast_dtype: ast::DataType) -> Self {
self.float64_ast_dtype = float64_ast_dtype;
diff --git a/datafusion/sql/src/unparser/expr.rs b/datafusion/sql/src/unparser/expr.rs
index 3601febe74..9c6d9d97a5 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -1734,7 +1734,7 @@ impl Unparser<'_> {
not_impl_err!("Unsupported DataType: conversion: {data_type}")
}
DataType::Boolean => Ok(ast::DataType::Bool),
- DataType::Int8 => Ok(ast::DataType::TinyInt(None)),
+ DataType::Int8 => Ok(self.dialect.int8_cast_dtype()),
DataType::Int16 => Ok(ast::DataType::SmallInt(None)),
DataType::Int32 => Ok(self.dialect.int32_cast_dtype()),
DataType::Int64 => Ok(self.dialect.int64_cast_dtype()),
diff --git a/datafusion/sql/tests/cases/plan_to_sql.rs b/datafusion/sql/tests/cases/plan_to_sql.rs
index 1d80b5b481..923812138c 100644
--- a/datafusion/sql/tests/cases/plan_to_sql.rs
+++ b/datafusion/sql/tests/cases/plan_to_sql.rs
@@ -1937,6 +1937,28 @@ fn test_without_offset() {
)
}
+#[test]
+fn test_cast_to_tinyint() -> Result<(), DataFusionError> {
+ roundtrip_statement_with_dialect_helper!(
+ sql: "select cast(3 as tinyint)",
+ parser_dialect: GenericDialect {},
+ unparser_dialect: UnparserPostgreSqlDialect {},
+ expected: @"SELECT CAST(3 AS SMALLINT)",
+ );
+ Ok(())
+}
+
+#[test]
+fn test_cast_to_tinyint_default_dialect() -> Result<(), DataFusionError> {
+ roundtrip_statement_with_dialect_helper!(
+ sql: "select cast(3 as tinyint)",
+ parser_dialect: GenericDialect {},
+ unparser_dialect: UnparserDefaultDialect {},
+ expected: @"SELECT CAST(3 AS TINYINT)",
+ );
+ Ok(())
+}
+
#[test]
fn test_with_offset0() {
let statement = generate_round_trip_statement(MySqlDialect {}, "select 1 offset 0");