Skip to content

Commit

Permalink
Cast sort expressions to Int64 to match DataFusion's window frame (of…
Browse files Browse the repository at this point in the history
…fsets are Int64)
  • Loading branch information
huaxingao committed Aug 20, 2024
1 parent 6d1276a commit df38f7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 11 additions & 1 deletion native/core/src/execution/datafusion/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,16 @@ impl PhysicalPlanner {
));
}

// Cast sort expressions to Int64 to match DataFusion's window frame (offsets are Int64)
let cast_sort_exprs: Vec<PhysicalSortExpr> = sort_exprs.iter().map(|sort_expr| {
let expr = sort_expr.expr.clone();
let cast_expr = Arc::new(CastExpr::new(expr, DataType::Int64, None));
PhysicalSortExpr {
expr: cast_expr,
options: sort_expr.options.clone(),
}
}).collect();

let window_func = match self.find_df_window_function(&window_func_name) {
Some(f) => f,
_ => {
Expand Down Expand Up @@ -1668,7 +1678,7 @@ impl PhysicalPlanner {
&window_args,
&[],
partition_by,
sort_exprs,
&cast_sort_exprs,
window_frame.into(),
&input_schema,
false, // TODO: Ignore nulls
Expand Down
10 changes: 8 additions & 2 deletions spark/src/test/scala/org/apache/comet/exec/CometExecSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,17 @@ class CometExecSuite extends CometTestBase {
|(3, 1L, 1.0D, date("2017-08-01"), timestamp_seconds(1501545600), null)
|AS testData(val, val_long, val_double, val_date, val_timestamp, cate)
|""".stripMargin)
val df = sql("""
val df1 = sql("""
|SELECT val, cate, count(val) OVER(PARTITION BY cate ORDER BY val ROWS CURRENT ROW)
|FROM testData ORDER BY cate, val
|""".stripMargin)
checkSparkAnswer(df)
checkSparkAnswer(df1)

// ORDER BY with RANGE frame
val df2 = sql("""
|SELECT val, cate, count(val) OVER(PARTITION BY cate ORDER BY val RANGE 1 PRECEDING) FROM testData
|""".stripMargin)
checkSparkAnswer(df2)
}
}

Expand Down

0 comments on commit df38f7f

Please sign in to comment.