-
Notifications
You must be signed in to change notification settings - Fork 2.2k
make AnalysisContext aware of empty sets to represent certainly false bounds #14279
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
Merged
berkaysynnada
merged 7 commits into
apache:main
from
buraksenn:represent-empty-set-in-analysis-context
Jan 28, 2025
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
12e985b
ready for review
buraksenn cf3a770
fmt and lint
buraksenn 53219fe
Apply suggestions from code review
buraksenn 074437e
apply reviews
buraksenn 7f9750a
fix test
buraksenn a7e5a59
Update analysis.rs
berkaysynnada a3c3cc9
Update analysis.rs
berkaysynnada 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -82,7 +82,7 @@ impl AnalysisContext { | |
| pub struct ExprBoundaries { | ||
| pub column: Column, | ||
| /// Minimum and maximum values this expression can have. | ||
| pub interval: Interval, | ||
| pub interval: Option<Interval>, | ||
|
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. I added the |
||
| /// Maximum number of distinct values this expression can produce, if known. | ||
| pub distinct_count: Precision<usize>, | ||
| } | ||
|
|
@@ -118,7 +118,7 @@ impl ExprBoundaries { | |
| let column = Column::new(field.name(), col_index); | ||
| Ok(ExprBoundaries { | ||
| column, | ||
| interval, | ||
| interval: Some(interval), | ||
| distinct_count: col_stats.distinct_count, | ||
| }) | ||
| } | ||
|
|
@@ -133,7 +133,7 @@ impl ExprBoundaries { | |
| .map(|(i, field)| { | ||
| Ok(Self { | ||
| column: Column::new(field.name(), i), | ||
| interval: Interval::make_unbounded(field.data_type())?, | ||
| interval: Some(Interval::make_unbounded(field.data_type())?), | ||
| distinct_count: Precision::Absent, | ||
| }) | ||
| }) | ||
|
|
@@ -179,7 +179,17 @@ pub fn analyze( | |
| expr.as_any() | ||
| .downcast_ref::<Column>() | ||
| .filter(|expr_column| bound.column.eq(*expr_column)) | ||
| .map(|_| (*i, bound.interval.clone())) | ||
| .map(|_| { | ||
| ( | ||
| *i, | ||
| match bound.interval.clone() { | ||
|
buraksenn marked this conversation as resolved.
Outdated
|
||
| Some(interval) => interval, | ||
| None => unreachable!( | ||
| "Boundaries should be initialized for all columns" | ||
| ), | ||
| }, | ||
| ) | ||
| }) | ||
| }) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
@@ -191,7 +201,18 @@ pub fn analyze( | |
| shrink_boundaries(graph, target_boundaries, target_expr_and_indices) | ||
| } | ||
| PropagationResult::Infeasible => { | ||
| Ok(AnalysisContext::new(target_boundaries).with_selectivity(0.0)) | ||
| // If the propagation result is infeasible, map target boundary intervals to None | ||
| Ok(AnalysisContext::new( | ||
|
buraksenn marked this conversation as resolved.
Outdated
|
||
| target_boundaries | ||
| .iter() | ||
| .map(|bound| ExprBoundaries { | ||
| column: bound.column.clone(), | ||
| interval: None, | ||
| distinct_count: bound.distinct_count, | ||
| }) | ||
| .collect(), | ||
| ) | ||
| .with_selectivity(0.0)) | ||
| } | ||
| PropagationResult::CannotPropagate => { | ||
| Ok(AnalysisContext::new(target_boundaries).with_selectivity(1.0)) | ||
|
|
@@ -215,12 +236,12 @@ fn shrink_boundaries( | |
| .iter_mut() | ||
| .find(|bound| bound.column.eq(column)) | ||
| { | ||
| bound.interval = graph.get_interval(*i); | ||
| bound.interval = Some(graph.get_interval(*i)); | ||
| }; | ||
| } | ||
| }); | ||
|
|
||
| let selectivity = calculate_selectivity(&target_boundaries, &initial_boundaries); | ||
| let selectivity = calculate_selectivity(&target_boundaries, &initial_boundaries)?; | ||
|
|
||
| if !(0.0..=1.0).contains(&selectivity) { | ||
| return internal_err!("Selectivity is out of limit: {}", selectivity); | ||
|
|
@@ -235,16 +256,25 @@ fn shrink_boundaries( | |
| fn calculate_selectivity( | ||
| target_boundaries: &[ExprBoundaries], | ||
| initial_boundaries: &[ExprBoundaries], | ||
| ) -> f64 { | ||
| ) -> Result<f64> { | ||
| // Since the intervals are assumed uniform and the values | ||
| // are not correlated, we need to multiply the selectivities | ||
| // of multiple columns to get the overall selectivity. | ||
| let mut acc: f64 = 1.0; | ||
| initial_boundaries | ||
| .iter() | ||
| .zip(target_boundaries.iter()) | ||
| .fold(1.0, |acc, (initial, target)| { | ||
| acc * cardinality_ratio(&initial.interval, &target.interval) | ||
| }) | ||
| .for_each(|(initial, target)| { | ||
|
buraksenn marked this conversation as resolved.
Outdated
|
||
| let Some(initial_interval) = initial.interval.clone() else { | ||
| unreachable!("Interval should be initialized for all columns"); | ||
| }; | ||
| let Some(target_interval) = target.interval.clone() else { | ||
| unreachable!("Interval should be initialized for all columns"); | ||
| }; | ||
| acc *= cardinality_ratio(&initial_interval, &target_interval); | ||
| }); | ||
|
|
||
| Ok(acc) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -313,16 +343,6 @@ mod tests { | |
| Some(16), | ||
| Some(19), | ||
| ), | ||
| // (a > 10 AND a < 20) AND (a > 20 AND a < 30) | ||
| ( | ||
| col("a") | ||
| .gt(lit(10)) | ||
| .and(col("a").lt(lit(20))) | ||
| .and(col("a").gt(lit(20))) | ||
| .and(col("a").lt(lit(30))), | ||
| None, | ||
| None, | ||
| ), | ||
| ]; | ||
| for (expr, lower, upper) in test_cases { | ||
| let boundaries = ExprBoundaries::try_new_unbounded(&schema).unwrap(); | ||
|
|
@@ -335,7 +355,9 @@ mod tests { | |
| df_schema.as_ref(), | ||
| ) | ||
| .unwrap(); | ||
| let actual = &analysis_result.boundaries[0].interval; | ||
| let Some(actual) = &analysis_result.boundaries[0].interval else { | ||
| panic!("Interval should be initialized for all columns"); | ||
|
buraksenn marked this conversation as resolved.
Outdated
|
||
| }; | ||
| let expected = Interval::make(lower, upper).unwrap(); | ||
| assert_eq!( | ||
| &expected, actual, | ||
|
|
@@ -344,6 +366,41 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_analyze_empty_set_boundary_exprs() { | ||
| let schema = Arc::new(Schema::new(vec![make_field("a", DataType::Int32)])); | ||
|
|
||
| let test_cases: Vec<Expr> = vec![ | ||
| // a > 10 AND a < 10 | ||
| col("a").gt(lit(10)).and(col("a").lt(lit(10))), | ||
| // a > 5 AND (a < 20 OR a > 20) | ||
| // a > 10 AND a < 20 | ||
| // (a > 10 AND a < 20) AND (a > 20 AND a < 30) | ||
| col("a") | ||
| .gt(lit(10)) | ||
| .and(col("a").lt(lit(20))) | ||
| .and(col("a").gt(lit(20))) | ||
| .and(col("a").lt(lit(30))), | ||
| ]; | ||
|
|
||
| for expr in test_cases { | ||
| let boundaries = ExprBoundaries::try_new_unbounded(&schema).unwrap(); | ||
| let df_schema = DFSchema::try_from(Arc::clone(&schema)).unwrap(); | ||
| let physical_expr = | ||
| create_physical_expr(&expr, &df_schema, &ExecutionProps::new()).unwrap(); | ||
| let analysis_result = analyze( | ||
| &physical_expr, | ||
| AnalysisContext::new(boundaries), | ||
| df_schema.as_ref(), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| analysis_result.boundaries.iter().for_each(|bound| { | ||
|
buraksenn marked this conversation as resolved.
Outdated
|
||
| assert_eq!(bound.interval, None); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_analyze_invalid_boundary_exprs() { | ||
| let schema = Arc::new(Schema::new(vec![make_field("a", DataType::Int32)])); | ||
|
|
||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.