-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Introduce LogicalPlan invariants, begin automatically checking them #13651
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
Changes from 3 commits
6d43dc2
a855811
0163a40
bee7e92
4eee9c4
a7d9770
72718ad
2002b1a
fbc9c46
e52187e
ba26f13
ad1a1f8
1164a7b
7ad0b74
911d4b8
810246d
9842d19
00700ae
9bca470
529ac3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,12 +22,13 @@ use std::sync::Arc; | |
|
|
||
| use chrono::{DateTime, Utc}; | ||
| use datafusion_expr::registry::FunctionRegistry; | ||
| use datafusion_expr::Union; | ||
| use log::{debug, warn}; | ||
|
|
||
| use datafusion_common::alias::AliasGenerator; | ||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_common::instant::Instant; | ||
| use datafusion_common::tree_node::{Transformed, TreeNodeRewriter}; | ||
| use datafusion_common::tree_node::{Transformed, TreeNodeRecursion, TreeNodeRewriter}; | ||
| use datafusion_common::{internal_err, DFSchema, DataFusionError, HashSet, Result}; | ||
| use datafusion_expr::logical_plan::LogicalPlan; | ||
|
|
||
|
|
@@ -355,6 +356,16 @@ impl Optimizer { | |
| where | ||
| F: FnMut(&LogicalPlan, &dyn OptimizerRule), | ||
| { | ||
| // verify at the start, before the first LP optimizer pass. | ||
| check_plan("before_optimizers", &plan, Arc::clone(plan.schema())).map_err( | ||
| |e| { | ||
| DataFusionError::Context( | ||
| "check_plan_before_optimizers".to_string(), | ||
| Box::new(e), | ||
| ) | ||
| }, | ||
| )?; | ||
|
|
||
| let start_time = Instant::now(); | ||
| let options = config.options(); | ||
| let mut new_plan = plan; | ||
|
|
@@ -384,9 +395,15 @@ impl Optimizer { | |
| // rule handles recursion itself | ||
| None => optimize_plan_node(new_plan, rule.as_ref(), config), | ||
| } | ||
| // verify the rule didn't change the schema | ||
| .and_then(|tnr| { | ||
| assert_schema_is_the_same(rule.name(), &starting_schema, &tnr.data)?; | ||
| // verify after each optimizer pass. | ||
| check_plan(rule.name(), &tnr.data, starting_schema).map_err(|e| { | ||
|
wiedld marked this conversation as resolved.
Outdated
|
||
| DataFusionError::Context( | ||
| "check_optimized_plan".to_string(), | ||
| Box::new(e), | ||
| ) | ||
| })?; | ||
|
|
||
| Ok(tnr) | ||
| }); | ||
|
|
||
|
|
@@ -451,6 +468,33 @@ impl Optimizer { | |
| } | ||
| } | ||
|
|
||
| /// These are invariants to hold true for each logical plan. | ||
| /// Do necessary check and fail the invalid plan. | ||
| /// | ||
| /// Checks for elements which are immutable across optimizer passes. | ||
| fn check_plan( | ||
| check_name: &str, | ||
| plan: &LogicalPlan, | ||
| prev_schema: Arc<DFSchema>, | ||
| ) -> Result<()> { | ||
| // verify invariant: optimizer rule didn't change the schema | ||
| assert_schema_is_the_same(check_name, &prev_schema, plan)?; | ||
|
|
||
| // verify invariant: fields must have unique names | ||
| assert_unique_field_names(plan)?; | ||
|
|
||
| /* This current fails for: | ||
| - execution::context::tests::cross_catalog_access | ||
| - at test_files/string/string.slt:46 | ||
| External error: query failed: DataFusion error: Optimizer rule 'eliminate_nested_union' failed | ||
| */ | ||
| // verify invariant: equivalent schema across union inputs | ||
| // assert_unions_are_valid(check_name, plan)?; | ||
|
wiedld marked this conversation as resolved.
Outdated
|
||
|
|
||
| // TODO: trait API and provide extension on the Optimizer to define own validations? | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is a mention of the extensibility of invariants. Options include:
Ditto for the AnalyzerRule passes. Altho I wasn't sure about how much is added complexity and planning time overhead - as @Omega359 mentions we could make it configurable (e.g. run for CI and debugging in downstream projects). This WIP is about proposing different ideas of what we could do. 🤔
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it can be controlled through environment variables, similar to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you have example use-case for user-defined plan invariants?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have some special invariants for our
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We could also add it as a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
is this about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added this to the followup items list: #13652 (comment) |
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Returns an error if `new_plan`'s schema is different than `prev_schema` | ||
| /// | ||
| /// It ignores metadata and nullability. | ||
|
|
@@ -476,6 +520,38 @@ pub(crate) fn assert_schema_is_the_same( | |
| } | ||
| } | ||
|
|
||
| /// Returns an error if plan, and subplans, do not have unique fields. | ||
| /// | ||
| /// This invariant is subject to change. | ||
| /// refer: <https://github.com/apache/datafusion/issues/13525#issuecomment-2494046463> | ||
| fn assert_unique_field_names(plan: &LogicalPlan) -> Result<()> { | ||
| plan.schema().check_names()?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, on every creation. But not after every merge. Which should be ok if no bug is introduced in the merge -- altho I would prefer to add the check there. |
||
|
|
||
| plan.apply_with_subqueries(|plan: &LogicalPlan| { | ||
| plan.schema().check_names()?; | ||
| Ok(TreeNodeRecursion::Continue) | ||
| }) | ||
| .map(|_| ()) | ||
| } | ||
|
|
||
| /// Returns an error if any union nodes are invalid. | ||
| #[allow(dead_code)] | ||
|
wiedld marked this conversation as resolved.
Outdated
|
||
| fn assert_unions_are_valid(rule_name: &str, plan: &LogicalPlan) -> Result<()> { | ||
| plan.apply_with_subqueries(|plan: &LogicalPlan| { | ||
| if let LogicalPlan::Union(Union { schema, inputs }) = plan { | ||
| inputs.iter().try_for_each(|subplan| { | ||
| assert_schema_is_the_same( | ||
| format!("{rule_name}:union_check").as_str(), | ||
| schema, | ||
| subplan, | ||
| ) | ||
| })?; | ||
| } | ||
| Ok(TreeNodeRecursion::Continue) | ||
| }) | ||
| .map(|_| ()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.