nuno-faria commented on code in PR #26888:
URL: https://github.com/apache/datafusion/pull/26888#discussion_r4427884991
##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -3106,6 +3106,74 @@ mod tests {
Ok(())
}
+ #[test]
+ fn mysql_int8_cast_uses_signed() -> 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)");
+ Ok(())
+ }
+
+ // MySQL's `CAST` rejects `SMALLINT` and the `... UNSIGNED` integer types.
+ #[test]
+ fn mysql_int16_and_unsigned_casts() -> Result<()> {
+ let unparser = Unparser::new(&MySqlDialect {});
+ for (data_type, expected) in [
+ (DataType::Int16, "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(())
+ }
Review Comment:
Could these tests by combined into one? And also for completeness can you
add the other integer types as well?
##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -3106,6 +3106,74 @@ mod tests {
Ok(())
}
+ #[test]
+ fn mysql_int8_cast_uses_signed() -> 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)");
+ Ok(())
+ }
+
+ // MySQL's `CAST` rejects `SMALLINT` and the `... UNSIGNED` integer types.
+ #[test]
+ fn mysql_int16_and_unsigned_casts() -> Result<()> {
+ let unparser = Unparser::new(&MySqlDialect {});
+ for (data_type, expected) in [
+ (DataType::Int16, "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_dialect_int16_and_unsigned_casts() -> Result<()> {
+ let unparser = Unparser::new(&DefaultDialect {});
+ for (data_type, expected) in [
+ (DataType::Int16, "CAST(a AS SMALLINT)"),
+ (DataType::UInt8, "CAST(a AS TINYINT UNSIGNED)"),
+ (DataType::UInt16, "CAST(a AS SMALLINT UNSIGNED)"),
+ (DataType::UInt32, "CAST(a AS INTEGER UNSIGNED)"),
+ (DataType::UInt64, "CAST(a AS BIGINT 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 custom_dialect_int16_and_unsigned_casts() -> Result<()> {
+ let dialect = CustomDialectBuilder::new()
+ .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 unparser = Unparser::new(&dialect);
+ for (data_type, expected) in [
+ (DataType::Int16, "CAST(a AS INTEGER)"),
+ (DataType::UInt8, "CAST(a AS SMALLINT)"),
+ (DataType::UInt16, "CAST(a AS INTEGER)"),
+ (DataType::UInt32, "CAST(a AS BIGINT)"),
+ (DataType::UInt64, "CAST(a AS NUMERIC)"),
+ ] {
+ 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(())
+ }
Review Comment:
Similarly for these two tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]