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

Support inlining view / dataframes logical plan #3923

Merged
merged 24 commits into from
Oct 22, 2022

Conversation

Dandandan
Copy link
Contributor

Which issue does this PR close?

Closes #3913

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@Dandandan Dandandan changed the title Inline TableScans for views and dataframes Support inlining view / dataframes logical plan Oct 21, 2022
@github-actions github-actions bot added core Core DataFusion crate optimizer Optimizer rules labels Oct 21, 2022
@github-actions github-actions bot added the logical-expr Logical plan and expressions label Oct 21, 2022
@Dandandan Dandandan marked this pull request as ready for review October 21, 2022 17:20
@@ -148,6 +149,7 @@ impl Optimizer {
/// Create a new optimizer using the recommended list of rules
pub fn new(config: &OptimizerConfig) -> Self {
let mut rules: Vec<Arc<dyn OptimizerRule + Sync + Send>> = vec![
Arc::new(InlineTableScan::new()),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alamb @andygrove this might be a good candidate as well to an Analysis phase.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree

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.

👨‍🍳 👌 -- this is very cool @Dandandan

// under the License.

//! Optimizer rule to replace TableScan references
//! such as DataFrames and Views and inlines the LogicalPlan
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Comment on lines 42 to 43
// Match only on scans without filter/projection
// As DataFrames / Views don't have those
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 see why we couldn't also create a projection / limit node as part of this rewrite as well if the table scan had them -- maybe we could file that as a future optimization

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe filters/projections/limit etc. won't be on the tablescan directly for view- / dataframes so removed it as it would be mostly dead code (and requires some more tests to cover those cases).
Might be a future possibility for tablescans with those set or there is a good usecase for it for custom tableproviders, not sure...

Copy link
Contributor

Choose a reason for hiding this comment

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

So maybe the comment could be updated to say "table scan won't have projecton / filters at this stage" (especially if this is run as one of the first optimizer passes)

We could also potentially add a debug! log if they were ever not None to hint to someone in the future it could be improved

fetch: None,
projected_schema,
projection: None,
}) if filters.is_empty() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise, if it has filters, we could add a LogicalPlan::Filter here I think

@@ -148,6 +149,7 @@ impl Optimizer {
/// Create a new optimizer using the recommended list of rules
pub fn new(config: &OptimizerConfig) -> Self {
let mut rules: Vec<Arc<dyn OptimizerRule + Sync + Send>> = vec![
Arc::new(InlineTableScan::new()),
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree

@Dandandan
Copy link
Contributor Author

There seems to be some subtle bug with projection with alias being wrong. Needs more investigation

@Dandandan
Copy link
Contributor Author

Turns out there was a small bug being introduced recently with avoiding duplicate projections during projection pushdown

@@ -527,7 +527,9 @@ fn optimize_plan(
}

fn projection_equal(p: &Projection, p2: &Projection) -> bool {
p.expr.len() == p2.expr.len() && p.expr.iter().zip(&p2.expr).all(|(l, r)| l == r)
p.expr.len() == p2.expr.len()
&& p.alias == p2.alias
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI @andygrove a small change because otherwise it also removes projections with (different) alias.

@Dandandan
Copy link
Contributor Author

Going to merge this when everything is green

@Dandandan Dandandan merged commit 318b4ad into apache:master Oct 22, 2022
Dandandan added a commit to yuuch/arrow-datafusion that referenced this pull request Nov 5, 2022
* Inline TableScans for views and dataframes

* Inline TableScans for views and dataframes

* Inline TableScans for views and dataframes

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* fmt

* doc

* Fix test

* Simplify

* Fix

* Rename test source

* Use plan instead of projected schema

* Docs

* Use SubqueryAlias

* Revert "Use SubqueryAlias"

This reverts commit 207c2a9.

* WIP

* Fix issue

* Clippy

* Format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate logical-expr Logical plan and expressions optimizer Optimizer rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support inlining view / dataframes logical plan
3 participants