parthchandra commented on code in PR #3939:
URL: https://github.com/apache/datafusion-comet/pull/3939#discussion_r3102903836
##########
native/spark-expr/src/conversion_funcs/numeric.rs:
##########
@@ -569,6 +573,62 @@ pub(crate) fn format_decimal_str(value_str: &str,
precision: usize, scale: i8) -
}
}
+/// Casts a Decimal128 array to string using Java's BigDecimal.toString()
semantics,
+/// which is Spark's LEGACY eval mode behavior. Plain notation when scale >= 0
and
+/// adjusted_exponent >= -6, otherwise scientific notation (e.g. "0E-18" for
zero
+/// with scale=18, since adjusted_exponent = -18 + 0 = -18 < -6).
+///
+/// TRY and ANSI modes produce plain notation via DataFusion's cast instead.
+pub(crate) fn cast_decimal128_to_utf8(array: &ArrayRef, scale: i8) ->
SparkResult<ArrayRef> {
+ let decimal_array = array
+ .as_any()
+ .downcast_ref::<Decimal128Array>()
+ .expect("Expected a Decimal128Array");
+ let output: StringArray = decimal_array
+ .iter()
+ .map(|opt_val| opt_val.map(|unscaled|
decimal128_to_java_string(unscaled, scale)))
+ .collect();
+ Ok(Arc::new(output))
+}
+
+/// Formats a Decimal128 unscaled value as a string matching Java's
BigDecimal.toString():
+/// - Plain notation when scale >= 0 and adjusted_exponent >= -6
+/// - Scientific notation otherwise
+///
+/// adjusted_exponent = -scale + (numDigits - 1)
+fn decimal128_to_java_string(unscaled: i128, scale: i8) -> String {
+ let negative = unscaled < 0;
+ let sign = if negative { "-" } else { "" };
+ let coeff = unscaled.unsigned_abs().to_string();
+ let num_digits = coeff.len() as i64;
+ let adj_exp = -(scale as i64) + (num_digits - 1);
+
+ if scale >= 0 && adj_exp >= -6 {
+ let scale_u = scale as usize;
+ let num_digits_u = num_digits as usize;
+ if scale_u == 0 {
+ format!("{sign}{coeff}")
Review Comment:
changed
--
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]