Skip to content

fix: Support scalar inputs for datetime expressions when constant folding is disabled#3448

Open
0lai0 wants to merge 13 commits intoapache:mainfrom
0lai0:native_engine_panics_on_time
Open

fix: Support scalar inputs for datetime expressions when constant folding is disabled#3448
0lai0 wants to merge 13 commits intoapache:mainfrom
0lai0:native_engine_panics_on_time

Conversation

@0lai0
Copy link
Contributor

@0lai0 0lai0 commented Feb 9, 2026

Which issue does this PR close?

Closes #3336

Rationale for this change

Comet's native engine expects all-literal datetime expressions (e.g., hour(timestamp('...'))) to be folded by the Spark JVM. When ConstantFolding is disabled, these expressions reach the native engine as ColumnarValue::Scalar, causing a panic or execution error.

What changes are included in this PR?

  • extract_date_part.rs: Added Scalar match arm to extract_date_part! macro.
  • unix_timestamp.rs: Refactored core logic into eval_array and added Scalar support.

How are these changes tested?

CometExpressionSuite.scala

Comment on lines +105 to +114
let result = date_part(&array, DatePart::$date_part_variant)?;
let result_arr = result
.as_any()
.downcast_ref::<Int32Array>()
.expect("date_part should return Int32Array");

let scalar_result =
ScalarValue::try_from_array(result_arr, 0).map_err(DataFusionError::from)?;

Ok(ColumnarValue::Scalar(scalar_result))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return the array as is so we dont need to add scalar type handler in SparkUnixTimestamp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! That's a good point. I will modify it. Thanks for review.

}
}

test("scalar hour/minute/second/unix_timestamp with ConstantFolding disabled") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! Let's migrate these tests to SQL File Tests, see https://datafusion.apache.org/comet/contributor-guide/sql-file-tests.html#sql-file-tests

Copy link
Member

@andygrove andygrove left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of suggestions on this one.

Similar to #3668, there are already SQL file tests for hour, minute, second, and unix_timestamp that have literal test cases marked with ignore(https://github.com/apache/datafusion-comet/issues/3336). Since this PR fixes that issue, it would be better to remove those ignore annotations and let the existing SQL tests cover the fix. The SQL tests also automatically compare against Spark, which is stronger coverage than the Scala test here which asserts against hardcoded expected values. The new Scala test could then be dropped.

On the Rust side, the scalar extraction in both extract_date_part.rs and unix_timestamp.rs uses .expect() with a manual downcast and null check. That would panic on an unexpected type. You could simplify this to ScalarValue::try_from_array(&result, 0)? which is already used elsewhere in the codebase (e.g. in cast.rs) and returns a Result instead of panicking.

@0lai0
Copy link
Contributor Author

0lai0 commented Mar 14, 2026

Got it! Thanks @andygrove for the feedback and the reminder about try_from_array. I'll review and make changes based on the comments, removing the Scala tests and updating the Rust implementation. Thanks you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native engine panics on all-scalar inputs for hour, minute, second, unix_timestamp

4 participants