-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Provide user-defined invariants for logical node extensions. #14329
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
876959d
feat(13525): permit user-defined invariants on logical plan extensions
wiedld 3836444
test(13525): demonstrate extension node invariants catching improper …
wiedld 0cda030
chore: update docs
wiedld 159d62d
refactor: remove the extra Invariant interface around an FnMut, since…
wiedld b8e6a57
Merge remote-tracking branch 'apache/main' into 13525/lp-extension-in…
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ | |
//! | ||
|
||
use std::fmt::Debug; | ||
use std::hash::Hash; | ||
use std::task::{Context, Poll}; | ||
use std::{any::Any, collections::BTreeMap, fmt, sync::Arc}; | ||
|
||
|
@@ -93,7 +94,7 @@ use datafusion::{ | |
use datafusion_common::config::ConfigOptions; | ||
use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode}; | ||
use datafusion_common::ScalarValue; | ||
use datafusion_expr::{FetchType, Projection, SortExpr}; | ||
use datafusion_expr::{FetchType, Invariant, InvariantLevel, Projection, SortExpr}; | ||
use datafusion_optimizer::optimizer::ApplyOrder; | ||
use datafusion_optimizer::AnalyzerRule; | ||
use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType}; | ||
|
@@ -295,20 +296,175 @@ async fn topk_plan() -> Result<()> { | |
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
/// Run invariant checks on the logical plan extension [`TopKPlanNode`]. | ||
async fn topk_invariants() -> Result<()> { | ||
// Test: pass an InvariantLevel::Always | ||
let pass = InvariantMock { | ||
should_fail_invariant: false, | ||
kind: InvariantLevel::Always, | ||
}; | ||
let ctx = setup_table(make_topk_context_with_invariants(Some(pass))).await?; | ||
run_and_compare_query(ctx, "Topk context").await?; | ||
|
||
// Test: fail an InvariantLevel::Always | ||
let fail = InvariantMock { | ||
should_fail_invariant: true, | ||
kind: InvariantLevel::Always, | ||
}; | ||
let ctx = setup_table(make_topk_context_with_invariants(Some(fail))).await?; | ||
matches!( | ||
&*run_and_compare_query(ctx, "Topk context") | ||
.await | ||
.unwrap_err() | ||
.message(), | ||
"node fails check, such as improper inputs" | ||
); | ||
|
||
// Test: pass an InvariantLevel::Executable | ||
let pass = InvariantMock { | ||
should_fail_invariant: false, | ||
kind: InvariantLevel::Executable, | ||
}; | ||
let ctx = setup_table(make_topk_context_with_invariants(Some(pass))).await?; | ||
run_and_compare_query(ctx, "Topk context").await?; | ||
|
||
// Test: fail an InvariantLevel::Executable | ||
let fail = InvariantMock { | ||
should_fail_invariant: true, | ||
kind: InvariantLevel::Executable, | ||
}; | ||
let ctx = setup_table(make_topk_context_with_invariants(Some(fail))).await?; | ||
matches!( | ||
&*run_and_compare_query(ctx, "Topk context") | ||
.await | ||
.unwrap_err() | ||
.message(), | ||
"node fails check, such as improper inputs" | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn topk_invariants_after_invalid_mutation() -> Result<()> { | ||
Comment on lines
+349
to
+350
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. test: demonstrate a failed invariant check after logical plan mutation (during optimizer run). |
||
// CONTROL | ||
// Build a valid topK plan. | ||
let config = SessionConfig::new().with_target_partitions(48); | ||
let runtime = Arc::new(RuntimeEnv::default()); | ||
let state = SessionStateBuilder::new() | ||
.with_config(config) | ||
.with_runtime_env(runtime) | ||
.with_default_features() | ||
.with_query_planner(Arc::new(TopKQueryPlanner {})) | ||
// 1. adds a valid TopKPlanNode | ||
.with_optimizer_rule(Arc::new(TopKOptimizerRule { | ||
invariant_mock: Some(InvariantMock { | ||
should_fail_invariant: false, | ||
kind: InvariantLevel::Always, | ||
}), | ||
})) | ||
.with_analyzer_rule(Arc::new(MyAnalyzerRule {})) | ||
.build(); | ||
let ctx = setup_table(SessionContext::new_with_state(state)).await?; | ||
run_and_compare_query(ctx, "Topk context").await?; | ||
|
||
// Test | ||
// Build a valid topK plan. | ||
// Then have an invalid mutation in an optimizer run. | ||
let config = SessionConfig::new().with_target_partitions(48); | ||
let runtime = Arc::new(RuntimeEnv::default()); | ||
let state = SessionStateBuilder::new() | ||
.with_config(config) | ||
.with_runtime_env(runtime) | ||
.with_default_features() | ||
.with_query_planner(Arc::new(TopKQueryPlanner {})) | ||
// 1. adds a valid TopKPlanNode | ||
.with_optimizer_rule(Arc::new(TopKOptimizerRule { | ||
invariant_mock: Some(InvariantMock { | ||
should_fail_invariant: false, | ||
kind: InvariantLevel::Always, | ||
}), | ||
})) | ||
// 2. break the TopKPlanNode | ||
.with_optimizer_rule(Arc::new(OptimizerMakeExtensionNodeInvalid {})) | ||
.with_analyzer_rule(Arc::new(MyAnalyzerRule {})) | ||
.build(); | ||
let ctx = setup_table(SessionContext::new_with_state(state)).await?; | ||
matches!( | ||
&*run_and_compare_query(ctx, "Topk context") | ||
.await | ||
.unwrap_err() | ||
.message(), | ||
"node fails check, such as improper inputs" | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn make_topk_context() -> SessionContext { | ||
make_topk_context_with_invariants(None) | ||
} | ||
|
||
fn make_topk_context_with_invariants( | ||
invariant_mock: Option<InvariantMock>, | ||
) -> SessionContext { | ||
let config = SessionConfig::new().with_target_partitions(48); | ||
let runtime = Arc::new(RuntimeEnv::default()); | ||
let state = SessionStateBuilder::new() | ||
.with_config(config) | ||
.with_runtime_env(runtime) | ||
.with_default_features() | ||
.with_query_planner(Arc::new(TopKQueryPlanner {})) | ||
.with_optimizer_rule(Arc::new(TopKOptimizerRule {})) | ||
.with_optimizer_rule(Arc::new(TopKOptimizerRule { invariant_mock })) | ||
.with_analyzer_rule(Arc::new(MyAnalyzerRule {})) | ||
.build(); | ||
SessionContext::new_with_state(state) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct OptimizerMakeExtensionNodeInvalid; | ||
|
||
impl OptimizerRule for OptimizerMakeExtensionNodeInvalid { | ||
fn name(&self) -> &str { | ||
"OptimizerMakeExtensionNodeInvalid" | ||
} | ||
|
||
fn apply_order(&self) -> Option<ApplyOrder> { | ||
Some(ApplyOrder::TopDown) | ||
} | ||
|
||
fn supports_rewrite(&self) -> bool { | ||
true | ||
} | ||
|
||
// Example rewrite pass which impacts validity of the extension node. | ||
fn rewrite( | ||
&self, | ||
plan: LogicalPlan, | ||
_config: &dyn OptimizerConfig, | ||
) -> Result<Transformed<LogicalPlan>, DataFusionError> { | ||
if let LogicalPlan::Extension(Extension { node }) = &plan { | ||
if let Some(prev) = node.as_any().downcast_ref::<TopKPlanNode>() { | ||
return Ok(Transformed::yes(LogicalPlan::Extension(Extension { | ||
node: Arc::new(TopKPlanNode { | ||
k: prev.k, | ||
input: prev.input.clone(), | ||
expr: prev.expr.clone(), | ||
// In a real use case, this rewriter could have change the number of inputs, etc | ||
invariant_mock: Some(InvariantMock { | ||
should_fail_invariant: true, | ||
kind: InvariantLevel::Always, | ||
}), | ||
}), | ||
}))); | ||
} | ||
}; | ||
|
||
Ok(Transformed::no(plan)) | ||
} | ||
} | ||
|
||
// ------ The implementation of the TopK code follows ----- | ||
|
||
#[derive(Debug)] | ||
|
@@ -336,7 +492,10 @@ impl QueryPlanner for TopKQueryPlanner { | |
} | ||
|
||
#[derive(Default, Debug)] | ||
struct TopKOptimizerRule {} | ||
struct TopKOptimizerRule { | ||
/// A testing-only hashable fixture. | ||
invariant_mock: Option<InvariantMock>, | ||
} | ||
|
||
impl OptimizerRule for TopKOptimizerRule { | ||
fn name(&self) -> &str { | ||
|
@@ -380,6 +539,7 @@ impl OptimizerRule for TopKOptimizerRule { | |
k: fetch, | ||
input: input.as_ref().clone(), | ||
expr: expr[0].clone(), | ||
invariant_mock: self.invariant_mock.clone(), | ||
}), | ||
}))); | ||
} | ||
|
@@ -396,6 +556,10 @@ struct TopKPlanNode { | |
/// The sort expression (this example only supports a single sort | ||
/// expr) | ||
expr: SortExpr, | ||
|
||
/// A testing-only hashable fixture. | ||
/// For actual use, define the [`Invariant`] in the [`UserDefinedLogicalNodeCore::invariants`]. | ||
invariant_mock: Option<InvariantMock>, | ||
} | ||
|
||
impl Debug for TopKPlanNode { | ||
|
@@ -406,6 +570,20 @@ impl Debug for TopKPlanNode { | |
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)] | ||
struct InvariantMock { | ||
should_fail_invariant: bool, | ||
kind: InvariantLevel, | ||
} | ||
|
||
fn invariant_helper_mock_ok(_: &LogicalPlan) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
fn invariant_helper_mock_fails(_: &LogicalPlan) -> Result<()> { | ||
internal_err!("node fails check, such as improper inputs") | ||
} | ||
|
||
impl UserDefinedLogicalNodeCore for TopKPlanNode { | ||
fn name(&self) -> &str { | ||
"TopK" | ||
|
@@ -420,6 +598,26 @@ impl UserDefinedLogicalNodeCore for TopKPlanNode { | |
self.input.schema() | ||
} | ||
|
||
fn invariants(&self) -> Vec<Invariant> { | ||
if let Some(InvariantMock { | ||
should_fail_invariant, | ||
kind, | ||
}) = self.invariant_mock.clone() | ||
{ | ||
if should_fail_invariant { | ||
return vec![Invariant { | ||
kind, | ||
fun: Arc::new(invariant_helper_mock_fails), | ||
}]; | ||
} | ||
return vec![Invariant { | ||
kind, | ||
fun: Arc::new(invariant_helper_mock_ok), | ||
}]; | ||
} | ||
vec![] // same as default impl | ||
} | ||
|
||
fn expressions(&self) -> Vec<Expr> { | ||
vec![self.expr.expr.clone()] | ||
} | ||
|
@@ -440,6 +638,7 @@ impl UserDefinedLogicalNodeCore for TopKPlanNode { | |
k: self.k, | ||
input: inputs.swap_remove(0), | ||
expr: self.expr.with_expr(exprs.swap_remove(0)), | ||
invariant_mock: self.invariant_mock.clone(), | ||
}) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test: demonstrate the basic use case. That user-defined invariants will fail for an invalid extension node.