Skip to content

Commit 68ac6cc

Browse files
authored
fix: unparse Int8 casts as SIGNED for MySQL dialect (apache#26883)
## Which issue does this PR close? - Closes apache#26882. ## Rationale for this change MySQL's `CAST` does not accept `TINYINT` as a target type, so an `Int8` cast unparsed with `MySqlDialect` produces SQL that MySQL rejects with `ERROR 1064`. See https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_cast ## What changes are included in this PR? `MySqlDialect` overrides `int8_cast_dtype` to return `SIGNED`, like the existing `int32_cast_dtype` and `int64_cast_dtype` overrides. ## What is the testing strategy for this PR? New unit test `mysql_int8_cast_uses_signed` in `datafusion/sql/src/unparser/expr.rs`. ## Are there any user-facing changes? No API changes.
1 parent 10fb070 commit 68ac6cc

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

‎datafusion/sql/src/unparser/dialect.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ impl Dialect for MySqlDialect {
585585
ast::DataType::Custom(ObjectName::from(vec![Ident::new("SIGNED")]), vec![])
586586
}
587587

588+
fn int8_cast_dtype(&self) -> ast::DataType {
589+
ast::DataType::Custom(ObjectName::from(vec![Ident::new("SIGNED")]), vec![])
590+
}
591+
588592
fn timestamp_cast_dtype(
589593
&self,
590594
time_unit: &TimeUnit,

‎datafusion/sql/src/unparser/expr.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,6 +3144,15 @@ mod tests {
31443144
Ok(())
31453145
}
31463146

3147+
#[test]
3148+
fn mysql_int8_cast_uses_signed() -> Result<()> {
3149+
let unparser = Unparser::new(&MySqlDialect {});
3150+
let expr = Expr::Cast(Cast::new(Box::new(col("a")), Int8));
3151+
let ast = unparser.expr_to_sql(&expr)?;
3152+
assert_eq!(format!("{ast}"), "CAST(`a` AS SIGNED)");
3153+
Ok(())
3154+
}
3155+
31473156
#[test]
31483157
fn custom_dialect_date32_ast_dtype() -> Result<()> {
31493158
let default_dialect = CustomDialectBuilder::default().build();

0 commit comments

Comments
 (0)