MaxGekk commented on code in PR #60413:
URL: https://github.com/apache/spark/pull/60413#discussion_r4640577924


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1212,6 +1212,17 @@ case class Cast(
     }
   }
 
+  // Selects exact (default) or legacy Double-based truncation of 
BigDecimal-backed DECIMALs in
+  // ANSI casts to TINYINT/SMALLINT/INT; see Decimal.roundToNumeric.
+  private lazy val decimalToIntegralExactTruncation: Boolean =

Review Comment:
   `decimalToIntegralExactTruncation` is a `lazy val` that reads `SQLConf.get` 
on first use, not when the `Cast` is built. The interpreted converter (`lazy 
val cast` -> `castToInt`/`castToShort`/`castToByte`) is built on the executor, 
so it reads the flag there, while the generated code reads it on the driver. 
With `spark.sql.legacy.castDecimalToIntegralTypeViaDouble=true`, an action 
outside `SQLExecution` (e.g. `df.rdd.collect()`) under `NO_CODEGEN` or a 
codegen fallback returns 0 for `CAST(0.999999999999999999BD AS INT)`, while 
codegen returns 1. It also lets a Dataset analyzed under one setting run with a 
later one, unlike `evalMode`. Could it be an eager `private val`, as 
`legacyCastToStr` is in the same class, so the value is fixed at analysis and 
serialized with the expression?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -7150,6 +7150,18 @@ object SQLConf {
       .booleanConf
       .createWithDefault(false)
 
+  val LEGACY_CAST_DECIMAL_TO_INTEGRAL_TYPE_VIA_DOUBLE =
+    buildConf("spark.sql.legacy.castDecimalToIntegralTypeViaDouble")
+      .internal()
+      .doc("When true, an ANSI cast to TINYINT, SMALLINT or INT of a DECIMAL 
value held as a " +
+        "BigDecimal, such as a value of a DECIMAL column of precision above 18 
or a decimal " +
+        "literal of any precision, converts it to a Double first, so 
0.999999999999999999 " +
+        "becomes 1. When false, the exact value is truncated toward zero.")
+      .version("4.4.0")

Review Comment:
   nit: `branch-4.4` has been cut, and `dev/next_version_candidates.py` now 
gives `branch-4.x` 4.5.0, so the new config should be `.version("4.5.0")`, and 
the migration note should go in the 4.4 to 4.5 section, unless this is 
backported to `branch-4.4`.



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