Skip to content
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

mark appropriate window functions as nullable: false in logical plan #7638

Closed
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
8 changes: 8 additions & 0 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,14 @@ impl WindowFunction {
window_frame,
}
}

pub fn nullable(&self) -> bool {
use window_function::WindowFunction as F;
match &self.fun {
F::BuiltInWindowFunction(f) => f.nullable(),
F::AggregateFunction(_) | F::WindowUDF(_) | F::AggregateUDF(_) => true,
}
}
}

// Exists expression.
Expand Down
2 changes: 1 addition & 1 deletion datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ impl ExprSchemable for Expr {
}
}
Expr::Cast(Cast { expr, .. }) => expr.nullable(input_schema),
Expr::WindowFunction(window_function) => Ok(window_function.nullable()),
Expr::ScalarVariable(_, _)
| Expr::TryCast { .. }
| Expr::ScalarFunction(..)
| Expr::ScalarUDF(..)
| Expr::WindowFunction { .. }
| Expr::AggregateFunction { .. }
| Expr::AggregateUDF { .. }
| Expr::Placeholder(_) => Ok(true),
Expand Down
40 changes: 28 additions & 12 deletions datafusion/expr/src/window_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,35 @@ pub enum BuiltInWindowFunction {

impl BuiltInWindowFunction {
fn name(&self) -> &str {
use BuiltInWindowFunction::*;
use BuiltInWindowFunction as F;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

flyby fix. Using wildcard here means that if a variant were deleted from the enum, the bottom-most variant in the match statement would become a variable catch-all.

match self {
RowNumber => "ROW_NUMBER",
Rank => "RANK",
DenseRank => "DENSE_RANK",
PercentRank => "PERCENT_RANK",
CumeDist => "CUME_DIST",
Ntile => "NTILE",
Lag => "LAG",
Lead => "LEAD",
FirstValue => "FIRST_VALUE",
LastValue => "LAST_VALUE",
NthValue => "NTH_VALUE",
F::RowNumber => "ROW_NUMBER",
F::Rank => "RANK",
F::DenseRank => "DENSE_RANK",
F::PercentRank => "PERCENT_RANK",
F::CumeDist => "CUME_DIST",
F::Ntile => "NTILE",
F::Lag => "LAG",
F::Lead => "LEAD",
F::FirstValue => "FIRST_VALUE",
F::LastValue => "LAST_VALUE",
F::NthValue => "NTH_VALUE",
}
}

// these values need to stay in sync with the `field` value defined on the physical expressions
pub fn nullable(&self) -> bool {
use BuiltInWindowFunction as F;
match self {
F::RowNumber | F::Ntile | F::Rank | F::CumeDist => false,
// the rest are assumed to be nullable
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure if there is a way that we can derive this from the definitions of the physical plans or elsewhere? Kinda sucks to have this information hardcoded this information in two places (right now, it exists also in the field method definition on the BuildInWindowFunctionExpr trait (example)

F::DenseRank
| F::PercentRank
| F::Lag
| F::Lead
| F::FirstValue
| F::LastValue
| F::NthValue => true,
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions datafusion/sqllogictest/test_files/window.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3315,3 +3315,23 @@ SELECT
window1 AS (ORDER BY C3)
ORDER BY C3
LIMIT 5

# Create table with window functions that have nullable: false columns should result in correct schema during cross join
Copy link
Contributor Author

@matthewgapp matthewgapp Sep 25, 2023

Choose a reason for hiding this comment

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

these tests fail on main but pass on this branch as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that I couldn't include a test for the NTILE window function because it failed. I filed a separate bug here

Copy link
Contributor Author

@matthewgapp matthewgapp Sep 27, 2023

Choose a reason for hiding this comment

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

Note that starting with version 47.0.0 of Arrow, the tests referenced will no longer fail (without the fixes provided in this PR) because Arrow has stopped checking for schema nullability. For more context, visit issue comment.

Therefore, I plan to introduce tests that explicitly check for nullable: false in the logical schema where it is expected, such as in columns created via row_number(), rank(), and similar functions. I will address this tonight.


statement ok
CREATE TABLE row_num_table as SELECT *, ROW_NUMBER() OVER (ORDER BY c2) AS row_num FROM aggregate_test_100 LIMIT 10

statement ok
CREATE TABLE rank_table as SELECT *, RANK() OVER (ORDER BY c2) AS rank_num FROM aggregate_test_100 LIMIT 10

statement ok
CREATE TABLE cum_dist_table as SELECT *, CUME_DIST() OVER (ORDER BY c2) AS cum_dist_num FROM aggregate_test_100 LIMIT 10

statement ok
SELECT a.*, b.* FROM row_num_table a, row_num_table b

statement ok
SELECT a.*, b.* FROM rank_table a, rank_table b

statement ok
SELECT a.*, b.* FROM cum_dist_table a, cum_dist_table b