marko-sisovic-db commented on code in PR #57564:
URL: https://github.com/apache/spark/pull/57564#discussion_r3666411353
##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala:
##########
@@ -442,7 +442,17 @@ 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)"
+ // 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.getConf(SQLConf.LEGACY_JDBC_ROUND_INTEGRAL_CAST_PUSHDOWN)) {
+ truncateFractionalValue(expr)
Review Comment:
As discussed offline, it is not clear what is the way to go here, as some
external engines match SPARK semantics, some don't, some support TRUNC
semantics some don't. As agreed, I opened a spark ticket with an improvement
proposal in case someone wants to pick this up in the future:
https://issues.apache.org/jira/browse/SPARK-58406
--
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]