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
17 changes: 17 additions & 0 deletions datafusion/core/tests/dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ async fn with_column_window_functions() -> DataFusionResult<()> {
Ok(())
}

#[tokio::test]
async fn duplicated_window_functions_can_be_executed() -> Result<()> {
let wexpr = datafusion::functions_window::row_number::row_number_udwf().call(vec![]);

let plan = LogicalPlanBuilder::empty(true)
.window(vec![wexpr.clone(), wexpr.alias("aliased")])?
.build()?;

let ctx = SessionContext::new();

let collected = DataFrame::new(ctx.state(), plan).collect().await?;

assert_eq!(collected.iter().map(|b| b.num_rows()).sum::<usize>(), 1);

Ok(())
}

#[tokio::test]
async fn test_coalesce_schema() -> Result<()> {
let ctx = SessionContext::new();
Expand Down
19 changes: 19 additions & 0 deletions datafusion/optimizer/src/common_subexpr_eliminate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ impl CSEController for ExprCSEController<'_> {
| Expr::Wildcard { .. }
| Expr::Lambda(_)
| Expr::LambdaVariable(_)
| Expr::WindowFunction(..)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice fix. Would it be worth adding an end-to-end SQLLogicTest that executes two identical window expressions over non-empty input, ideally with PARTITION BY and ORDER BY? The current unit test does a good job of verifying that CSE does not introduce an intermediate projection, but since the original failure happens during physical planning, an execution-level regression test would give us coverage across that boundary as well.

I noticed the existing window.slt case that uses a window expression in both SELECT and ORDER BY, but that seems to exercise a slightly different path since it resolves to a single physical window expression before CSE. This is just a suggestion and not a blocker.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you, happy that it works!

I followed your suggestion, but the SQL planner deduplicates identical window expressions before they reach CSE, so the SQLLogicTest could not reproduce the bug.

I added an execution-level regression test, tho. Is that okay?

);

let is_aggr = matches!(node, Expr::AggregateFunction(..));
Expand Down Expand Up @@ -856,6 +857,7 @@ mod test {
use crate::test::udfs::leaf_udf_expr;
use crate::test::*;
use datafusion_expr::test::function_stub::{avg, sum};
use datafusion_functions_window::row_number::row_number_udwf;

macro_rules! assert_optimized_plan_equal {
(
Expand Down Expand Up @@ -1932,4 +1934,21 @@ mod test {
"
)
}

#[test]
fn test_window_function_is_not_extracted() -> Result<()> {
let wexpr = row_number_udwf().call(vec![]);

let plan = LogicalPlanBuilder::empty(true)
.window(vec![wexpr.clone(), wexpr.alias("aliased")])?
.build()?;

assert_optimized_plan_equal!(
plan,
@r"
WindowAggr: windowExpr=[[row_number() ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, row_number() ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS aliased]]
EmptyRelation: rows=1
"
)
}
}