urosstan-db commented on code in PR #57564:
URL: https://github.com/apache/spark/pull/57564#discussion_r3657507698


##########
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:
   nit: Do we need to fix this on v2 expression builder level? Other data 
sources may benefit of stricter API for this truncation fix.



-- 
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]

Reply via email to