aleksandar-trajkovic-db commented on code in PR #60455:
URL: https://github.com/apache/spark/pull/60455#discussion_r4587507281
##########
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] &&
Review Comment:
Fixed in a543d8fca32: DECIMAL(p, 0) sources skip the truncating function,
here and in MySQL, through a shared `dropsFraction` check.
##########
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] &&
Review Comment:
Fixed in a543d8fca32: a JDBCSuite test now checks that DOUBLE to DECIMAL(10,
2) and to STRING compile without TRUNC on Postgres, along with the DECIMAL(p,
0) cases.
--
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]