Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions native/spark-expr/src/datetime_funcs/date_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -97,8 +110,4 @@ impl ScalarUDFImpl for SparkDateDiff {

Ok(ColumnarValue::Array(Arc::new(result)))
}

fn aliases(&self) -> &[String] {
&self.aliases
}
Comment on lines -101 to -103
Copy link
Member

Choose a reason for hiding this comment

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

Why is this removed?

}
Comment on lines 110 to 113
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

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

The aliases field still includes "datediff", but the fn aliases(&self) -> &[String] implementation was removed. If ScalarUDFImpl's default aliases() returns an empty slice (as used by other UDF impls in this crate), this will drop the datediff alias and can break Spark SQL/function resolution that relies on the alias rather than the primary name date_diff. Re-introduce aliases() (returning &self.aliases) or remove aliases entirely and ensure the UDF is registered under the intended name(s).

Copilot uses AI. Check for mistakes.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ abstract class ParquetDatetimeRebaseSuite extends CometTestBase {
}
}

test("COMET-XXXX: 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)
}
}
}

private def checkSparkNoRebaseAnswer(df: => DataFrame): Unit = {
var expected: Array[Row] = Array.empty

Expand Down