parthchandra commented on code in PR #3559:
URL: https://github.com/apache/datafusion-comet/pull/3559#discussion_r2893062255
##########
native/spark-expr/src/conversion_funcs/numeric.rs:
##########
@@ -913,6 +963,56 @@ pub(crate) fn cast_int_to_timestamp(
Ok(Arc::new(builder.finish().with_timezone_opt(target_tz.clone())) as
ArrayRef)
}
+pub(crate) fn cast_decimal_to_timestamp(
+ array_ref: &ArrayRef,
+ target_tz: &Option<Arc<str>>,
+ scale: i8,
+) -> SparkResult<ArrayRef> {
+ let arr = array_ref.as_primitive::<Decimal128Type>();
+ let scale_factor = 10_i128.pow(scale as u32);
+ let mut builder = TimestampMicrosecondBuilder::with_capacity(arr.len());
+
+ for i in 0..arr.len() {
+ if arr.is_null(i) {
+ builder.append_null();
+ } else {
+ let value = arr.value(i);
+ // Note: spark's big decimal
Review Comment:
Incomplete comment?
##########
native/spark-expr/src/conversion_funcs/numeric.rs:
##########
@@ -913,6 +963,56 @@ pub(crate) fn cast_int_to_timestamp(
Ok(Arc::new(builder.finish().with_timezone_opt(target_tz.clone())) as
ArrayRef)
}
+pub(crate) fn cast_decimal_to_timestamp(
+ array_ref: &ArrayRef,
+ target_tz: &Option<Arc<str>>,
+ scale: i8,
+) -> SparkResult<ArrayRef> {
+ let arr = array_ref.as_primitive::<Decimal128Type>();
+ let scale_factor = 10_i128.pow(scale as u32);
+ let mut builder = TimestampMicrosecondBuilder::with_capacity(arr.len());
+
+ for i in 0..arr.len() {
+ if arr.is_null(i) {
+ builder.append_null();
+ } else {
+ let value = arr.value(i);
+ // Note: spark's big decimal
+ let value_256 = i256::from_i128(value);
+ let micros_256 = value_256 * i256::from_i128(MICROS_PER_SECOND as
i128);
+ let ts = micros_256 / i256::from_i128(scale_factor);
+ builder.append_value(ts.as_i128() as i64);
Review Comment:
This doesn't look right. Casting down from i256 to i128 and then to i64 will
truncate too many bits silently. Should probably check for overflow here before
the cast.
##########
native/spark-expr/src/conversion_funcs/numeric.rs:
##########
@@ -75,6 +75,56 @@ pub(crate) fn
is_df_cast_from_decimal_spark_compatible(to_type: &DataType) -> bo
)
}
+macro_rules! cast_float_to_timestamp_impl {
+ ($array:expr, $builder:expr, $primitive_type:ty, $eval_mode:expr) => {{
+ let arr = $array.as_primitive::<$primitive_type>();
+ for i in 0..arr.len() {
+ if arr.is_null(i) {
+ $builder.append_null();
+ } else {
+ let val = arr.value(i) as f64;
+ // Path 1: NaN/Infinity check - error says TIMESTAMP
+ if val.is_nan() || val.is_infinite() {
+ if $eval_mode == EvalMode::Ansi {
+ return Err(SparkError::CastInvalidValue {
+ value: val.to_string(),
+ from_type: "DOUBLE".to_string(),
+ to_type: "TIMESTAMP".to_string(),
+ });
+ }
+ $builder.append_null();
+ } else {
+ // Path 2: Multiply then check overflow - error says BIGINT
+ let micros = val * MICROS_PER_SECOND as f64;
+ if micros.floor() <= i64::MAX as f64 && micros.ceil() >=
i64::MIN as f64 {
Review Comment:
There may be a boundary condition issue here but I am not sure if there is a
better way.
`i64::MAX as f64` is actually greater than `i64::MAX` so this check here has
gap where we might get some incorrect results?
##########
native/spark-expr/src/conversion_funcs/numeric.rs:
##########
@@ -913,6 +963,56 @@ pub(crate) fn cast_int_to_timestamp(
Ok(Arc::new(builder.finish().with_timezone_opt(target_tz.clone())) as
ArrayRef)
}
+pub(crate) fn cast_decimal_to_timestamp(
+ array_ref: &ArrayRef,
+ target_tz: &Option<Arc<str>>,
+ scale: i8,
+) -> SparkResult<ArrayRef> {
+ let arr = array_ref.as_primitive::<Decimal128Type>();
+ let scale_factor = 10_i128.pow(scale as u32);
+ let mut builder = TimestampMicrosecondBuilder::with_capacity(arr.len());
+
+ for i in 0..arr.len() {
+ if arr.is_null(i) {
+ builder.append_null();
+ } else {
+ let value = arr.value(i);
+ // Note: spark's big decimal
+ let value_256 = i256::from_i128(value);
+ let micros_256 = value_256 * i256::from_i128(MICROS_PER_SECOND as
i128);
+ let ts = micros_256 / i256::from_i128(scale_factor);
+ builder.append_value(ts.as_i128() as i64);
Review Comment:
You'll probably need to pass eval_mode here to check whether to return null
or throw error on overflow. Or you could just restrict this to legacy mode
(probably easier).
--
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]