-
Notifications
You must be signed in to change notification settings - Fork 282
fix(spark-expr): handle array length mismatch in datediff for dictionary-backed timestamps #3278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bb7ddc8
3783308
5da8d77
1d0fab5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,9 +71,22 @@ impl ScalarUDFImpl for SparkDateDiff { | |
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| let [end_date, start_date] = take_function_args(self.name(), args.args)?; | ||
|
|
||
| // Convert scalars to arrays for uniform processing | ||
| let end_arr = end_date.into_array(1)?; | ||
| let start_arr = start_date.into_array(1)?; | ||
| // Determine target length (broadcast scalars to column length) | ||
| let len = match (&end_date, &start_date) { | ||
| (ColumnarValue::Array(a), _) => a.len(), | ||
| (_, ColumnarValue::Array(a)) => a.len(), | ||
| _ => 1, | ||
| }; | ||
|
|
||
| // Convert both arguments to arrays of the same length | ||
| let end_arr = end_date.into_array(len)?; | ||
| let start_arr = start_date.into_array(len)?; | ||
|
|
||
| // Normalize dictionary arrays (important for Iceberg) | ||
| let end_arr = arrow::compute::cast(&end_arr, &DataType::Date32) | ||
| .map_err(|e| DataFusionError::Execution(e.to_string()))?; | ||
| let start_arr = arrow::compute::cast(&start_arr, &DataType::Date32) | ||
| .map_err(|e| DataFusionError::Execution(e.to_string()))?; | ||
|
|
||
| let end_date_array = end_arr | ||
| .as_any() | ||
|
|
@@ -97,8 +110,4 @@ impl ScalarUDFImpl for SparkDateDiff { | |
|
|
||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
|
|
||
| fn aliases(&self) -> &[String] { | ||
| &self.aliases | ||
| } | ||
| } | ||
|
Comment on lines
110
to
113
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,6 +116,33 @@ abstract class ParquetDatetimeRebaseSuite extends CometTestBase { | |
| } | ||
| } | ||
|
|
||
| test("datediff works with dictionary-encoded timestamp columns") { | ||
| withTempPath { path => | ||
| withSQLConf( | ||
| CometConf.COMET_NATIVE_SCAN_IMPL.key -> CometConf.SCAN_NATIVE_COMET, | ||
| CometConf.COMET_ENABLED.key -> "true", | ||
| "spark.sql.parquet.enableDictionary" -> "true") { | ||
| val df = spark | ||
| .createDataFrame( | ||
| Seq( | ||
| ("a", java.sql.Timestamp.valueOf("2024-01-02 10:00:00")), | ||
| ("b", java.sql.Timestamp.valueOf("2024-01-03 11:00:00")))) | ||
| .toDF("id", "ts") | ||
|
|
||
| df.write.mode("overwrite").parquet(path.getAbsolutePath) | ||
|
|
||
| val readDf = spark.read.parquet(path.getAbsolutePath) | ||
|
|
||
| val result = readDf | ||
| .selectExpr("datediff(current_date(), ts) as diff") | ||
| .collect() | ||
|
|
||
| // Just verify it executes correctly (no CometNativeException) | ||
| assert(result.length == 2) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to leverage
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion! For this regression test I focused on reproducing the original failure and ensuring "datediff" executes correctly without a CometNativeException. I can switch to "checkSparkAnswerAndOperator" if plan verification is preferred. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| private def checkSparkNoRebaseAnswer(df: => DataFrame): Unit = { | ||
| var expected: Array[Row] = Array.empty | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this removed?