aleksandar-trajkovic-db commented on code in PR #60455:
URL: https://github.com/apache/spark/pull/60455#discussion_r4587501631
##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala:
##########
@@ -544,6 +560,17 @@ abstract class JdbcDialect extends Serializable with
Logging {
}
}
+ /**
+ * Truncates `expr` toward zero, so that a cast from a fractional type to an
integral type that
+ * is pushed down matches Spark, which truncates rather than rounds.
Dialects for databases that
+ * round such casts override this with the database's truncating function.
The default returns
+ * `expr` unchanged, for databases whose cast already truncates like Spark.
+ * @param expr The SQL string of the value being cast.
+ * @return The SQL string to cast instead of `expr`.
+ */
+ @Since("4.4.0")
+ def truncateFractionalValue(expr: String): String = expr
Review Comment:
Fixed in a543d8fca32: `AggregatedDialect` now forwards
`truncateFractionalValue` with its first-non-neutral rule. A JDBCSuite test
covers a custom dialect wrapped with `PostgresDialect` in both orders.
##########
sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala:
##########
@@ -1397,6 +1397,58 @@ class JDBCSuite extends SharedSparkSession {
assert(msSqlServer.compileExpression(bareIsNull).get === "\"a\" IS NULL")
}
+ test("fractional to integral cast pushdown is wrapped in the dialect
truncating function " +
+ "when needed") {
+ val cast = new V2Cast(FieldReference("a"), DoubleType, IntegerType)
+
+ assert(JdbcDialects.get("jdbc:").compileExpression(cast).get ===
"""CAST("a" AS INTEGER)""")
+
+
assert(JdbcDialects.get("jdbc:postgresql://127.0.0.1/db").compileExpression(cast).get
===
+ """CAST(TRUNC("a") AS INTEGER)""")
+
assert(JdbcDialects.get("jdbc:oracle://127.0.0.1/db").compileExpression(cast).get
===
+ """CAST(TRUNC("a") AS NUMBER(10))""")
+
assert(JdbcDialects.get("jdbc:snowflake://127.0.0.1/db").compileExpression(cast).get
===
+ """CAST(TRUNC("a") AS INTEGER)""")
+
assert(JdbcDialects.get("jdbc:mysql://127.0.0.1/db").compileExpression(cast).get
===
+ "CAST(TRUNCATE(`a`, 0) AS SIGNED)")
+
+ withSQLConf(SQLConf.LEGACY_JDBC_ROUND_INTEGRAL_CAST_PUSHDOWN.key ->
"true") {
+
assert(JdbcDialects.get("jdbc:postgresql://127.0.0.1/db").compileExpression(cast).get
===
+ """CAST("a" AS INTEGER)""")
+
assert(JdbcDialects.get("jdbc:oracle://127.0.0.1/db").compileExpression(cast).get
===
+ """CAST("a" AS NUMBER(10))""")
+
assert(JdbcDialects.get("jdbc:snowflake://127.0.0.1/db").compileExpression(cast).get
===
+ """CAST("a" AS INTEGER)""")
+
assert(JdbcDialects.get("jdbc:mysql://127.0.0.1/db").compileExpression(cast).get
===
+ "CAST(`a` AS SIGNED)")
+ }
+ }
+
+ test("SPARK-58372: H2 truncates fractional to integral casts") {
+ val h2 = JdbcDialects.get("jdbc:h2:mem:testdb0")
+ Seq(DoubleType, DecimalType(10, 2)).foreach { from =>
+ val cast = new V2Cast(FieldReference("a"), from, IntegerType)
+ assert(h2.compileExpression(cast).get === """CAST(TRUNC("a") AS
INTEGER)""")
+ withSQLConf(SQLConf.LEGACY_JDBC_ROUND_INTEGRAL_CAST_PUSHDOWN.key ->
"true") {
+ assert(h2.compileExpression(cast).get === """CAST("a" AS INTEGER)""")
+ }
+ }
+ }
+
+ test("SPARK-58372: integral cast targets without a dialect mapping use SQL
type names") {
Review Comment:
Fixed in a543d8fca32: the test is now "unmapped SMALLINT, INTEGER and BIGINT
cast targets use SQL type names", and the fallback comment says `ByteType`
keeps Spark's name because TINYINT isn't portable.
##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala:
##########
@@ -460,8 +460,24 @@ abstract class JdbcDialect extends Serializable with
Logging {
override def visitCast(expr: String, exprDataType: DataType, dataType:
DataType): String = {
val databaseTypeDefinition =
-
getJDBCType(dataType).map(_.databaseTypeDefinition).getOrElse(dataType.typeName)
- s"CAST($expr AS $databaseTypeDefinition)"
+ getJDBCType(dataType).map(_.databaseTypeDefinition).getOrElse(dataType
match {
+ // Spark's type names `short` and `long` are not valid SQL types in
most databases.
+ case ShortType => "SMALLINT"
+ case IntegerType => "INTEGER"
+ case LongType => "BIGINT"
+ case _ => dataType.typeName
+ })
+ // Spark truncates toward zero when casting a fractional value to an
integral type, but many
+ // databases round instead, so a pushed down cast can return a different
result than Spark.
+ // Dialects for such databases override `truncateFractionalValue` to
truncate the value first.
+ val castedExpr = if (exprDataType.isInstanceOf[FractionalType] &&
+ dataType.isInstanceOf[IntegralType] &&
+ !SQLConf.get.legacyJdbcRoundIntegralCastPushdown) {
Review Comment:
Fixed in a543d8fca32: with the conf on, pushed casts compile to the SQL
Spark 4.3 sent, with Spark's type names, no truncation, and MySQL's previous
path. The doc says casts the database rejects fail again or, in an aggregate,
run in Spark. The docker legacy block expects that on MySQL and for Postgres
BIGINT.
--
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]