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

Add window frame constructs #492

Closed
wants to merge 1 commit into from

Conversation

jimexist
Copy link
Member

@jimexist jimexist commented Jun 3, 2021

Which issue does this PR close?

Related #361
Based on #463

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@codecov-commenter
Copy link

codecov-commenter commented Jun 3, 2021

Codecov Report

Merging #492 (1e09359) into master (e713bc3) will increase coverage by 0.05%.
The diff coverage is 85.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #492      +/-   ##
==========================================
+ Coverage   75.95%   76.00%   +0.05%     
==========================================
  Files         154      155       +1     
  Lines       26349    26494     +145     
==========================================
+ Hits        20014    20138     +124     
- Misses       6335     6356      +21     
Impacted Files Coverage Δ
...sta/rust/core/src/serde/logical_plan/from_proto.rs 35.66% <0.00%> (-0.30%) ⬇️
...lista/rust/core/src/serde/logical_plan/to_proto.rs 62.09% <0.00%> (-0.39%) ⬇️
...ta/rust/core/src/serde/physical_plan/from_proto.rs 38.38% <0.00%> (-0.41%) ⬇️
datafusion/src/optimizer/utils.rs 47.34% <0.00%> (-0.17%) ⬇️
datafusion/src/physical_plan/mod.rs 78.70% <ø> (ø)
datafusion/src/sql/utils.rs 64.70% <50.00%> (-0.22%) ⬇️
datafusion/src/physical_plan/planner.rs 80.19% <71.42%> (-0.14%) ⬇️
datafusion/src/sql/planner.rs 84.48% <94.73%> (+0.11%) ⬆️
datafusion/src/logical_plan/expr.rs 84.62% <100.00%> (+0.02%) ⬆️
datafusion/src/physical_plan/window_frames.rs 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e713bc3...1e09359. Read the comment docs.

@alamb
Copy link
Contributor

alamb commented Jun 4, 2021

Checking this one out...

@alamb alamb added the datafusion Changes in the datafusion crate label Jun 4, 2021
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

I think this is looking good -- thanks @jimexist . I have some comments about code structure -- let me know what you think

Also, thank you very much for breaking this work up into multiple PRs @jimexist -- it really makes reviewing them much more tractable (though I realize it is more work for you).

@@ -35,7 +35,7 @@ futures = "0.3"
log = "0.4"
prost = "0.7"
serde = {version = "1", features = ["derive"]}
sqlparser = "0.8"
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

//! - An EXCLUDE clause.

use crate::error::{DataFusionError, Result};
use sqlparser::ast::{WindowFrame, WindowFrameBound, WindowFrameUnits};
Copy link
Contributor

Choose a reason for hiding this comment

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

We can always change it later, but it might make sense eventually to translate from the SQL parser AST to datafusion specific structs. This would involve some code duplication, but might be helpful if we wanted to add DataFusion specific optimizations / annotations to the window. Just a thought for later. Not needed for this PR

Copy link
Member Author

Choose a reason for hiding this comment

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

i see your point, let's review #506 as an alternative

end_bound: Some(WindowFrameBound::CurrentRow),
};

fn get_bound_rank(bound: &WindowFrameBound) -> (u8, u64) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the hard coded numbers in this function. Perhaps giving them names or adding some doc comments would help

}

/// Validate a window frame if present, otherwise return the default window frame.
pub(crate) fn validate_window_frame(
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, it would be cool if this validation was done as part of the SQL AST --> LogicalPlan / Expr. Then the rest of the optimizers could assume that the structures were valid.

if we added a DataFusion specific WindowFrame struct and then performed this validation while constructing it, by the time the physical plan saw a Window Frame it would be in a known good state

Copy link
Member Author

Choose a reason for hiding this comment

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

see #506

let sql = "SELECT order_id, MAX(qty) OVER (ORDER BY order_id RANGE BETWEEN 3 PRECEDING and 3 FOLLOWING), MIN(qty) OVER (ORDER BY order_id DESC) from orders";
let expected = "\
Projection: #order_id, #MAX(qty), #MIN(qty)\
\n WindowAggr: windowExpr=[[MAX(#qty)]] partitionBy=[]\
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be a good idea to have the window bounds appear in the explain plans

@alamb alamb closed this in #506 Jun 7, 2021
@jimexist jimexist deleted the add-window-frame branch June 10, 2021 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datafusion Changes in the datafusion crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants