-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix error result in execute&pre_selection #16930
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
alamb
merged 7 commits into
apache:main
from
acking-you:fix_error_result_in_pre_selection
Aug 4, 2025
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
faa5c61
fix error result in execute&pre_selection
acking-you bb6e397
fix clippy
acking-you d06bc59
Merge branch 'main' into fix_error_result_in_pre_selection
acking-you 9f24c05
Optimize implementation
acking-you bea2ee7
more efficiency impl
acking-you eded26c
Merge branch 'main' into fix_error_result_in_pre_selection
acking-you c198d4e
fix CI
acking-you 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 |
|---|---|---|
|
|
@@ -375,7 +375,46 @@ impl PhysicalExpr for BinaryExpr { | |
| // as it takes into account cases where the selection contains null values. | ||
| let batch = filter_record_batch(batch, selection)?; | ||
| let right_ret = self.right.evaluate(&batch)?; | ||
| return pre_selection_scatter(selection, right_ret); | ||
|
|
||
| match &right_ret { | ||
| ColumnarValue::Array(array) => { | ||
| // When the array on the right is all true or all false, skip the scatter process | ||
| let boolean_array = array.as_boolean(); | ||
| let true_count = boolean_array.true_count(); | ||
| let length = boolean_array.len(); | ||
| if true_count == length { | ||
| return Ok(lhs); | ||
| } else if true_count == 0 && boolean_array.null_count() == 0 { | ||
| // If the right-hand array is returned at this point,the lengths will be inconsistent; | ||
| // returning a scalar can avoid this issue | ||
| return Ok(ColumnarValue::Scalar(ScalarValue::Boolean( | ||
| Some(false), | ||
| ))); | ||
| } | ||
|
|
||
| return pre_selection_scatter(selection, boolean_array); | ||
| } | ||
| ColumnarValue::Scalar(scalar) => { | ||
| if let ScalarValue::Boolean(v) = scalar { | ||
acking-you marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // When the scalar is true or false, skip the scatter process | ||
| if let Some(v) = v { | ||
| if *v { | ||
| return Ok(lhs); | ||
| } else { | ||
| return Ok(right_ret); | ||
| } | ||
| } else { | ||
| let array = | ||
|
||
| BooleanArray::from(vec![*v; batch.num_rows()]); | ||
| return pre_selection_scatter(selection, &array); | ||
| } | ||
| } else { | ||
| return internal_err!( | ||
| "Expected boolean scalar value, found: {right_ret:?}" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -974,13 +1013,8 @@ fn check_short_circuit<'a>( | |
| /// However, this is difficult to achieve under the immutable constraints of [`Arc`] and [`BooleanArray`]. | ||
| fn pre_selection_scatter( | ||
| left_result: &BooleanArray, | ||
| right_result: ColumnarValue, | ||
| right_result: &BooleanArray, | ||
| ) -> Result<ColumnarValue> { | ||
| let right_boolean_array = match &right_result { | ||
| ColumnarValue::Array(array) => array.as_boolean(), | ||
| ColumnarValue::Scalar(_) => return Ok(right_result), | ||
| }; | ||
|
|
||
| let result_len = left_result.len(); | ||
|
|
||
| let mut result_array_builder = BooleanArray::builder(result_len); | ||
|
|
@@ -998,7 +1032,7 @@ fn pre_selection_scatter( | |
|
|
||
| // copy values from right array for this slice | ||
| let len = end - start; | ||
| right_boolean_array | ||
| right_result | ||
| .slice(right_array_pos, len) | ||
| .iter() | ||
| .for_each(|v| result_array_builder.append_option(v)); | ||
|
|
@@ -5211,7 +5245,6 @@ mod tests { | |
| /// 4. Test single true at first position | ||
| /// 5. Test single true at last position | ||
| /// 6. Test nulls in right array | ||
| /// 7. Test scalar right handling | ||
| #[test] | ||
| fn test_pre_selection_scatter() { | ||
| fn create_bool_array(bools: Vec<bool>) -> BooleanArray { | ||
|
|
@@ -5222,11 +5255,9 @@ mod tests { | |
| // Left: [T, F, T, F, T] | ||
| // Right: [F, T, F] (values for 3 true positions) | ||
| let left = create_bool_array(vec![true, false, true, false, true]); | ||
| let right = ColumnarValue::Array(Arc::new(create_bool_array(vec![ | ||
| false, true, false, | ||
| ]))); | ||
| let right = create_bool_array(vec![false, true, false]); | ||
|
|
||
| let result = pre_selection_scatter(&left, right).unwrap(); | ||
| let result = pre_selection_scatter(&left, &right).unwrap(); | ||
| let result_arr = result.into_array(left.len()).unwrap(); | ||
|
|
||
| let expected = create_bool_array(vec![false, false, true, false, false]); | ||
|
|
@@ -5238,11 +5269,9 @@ mod tests { | |
| // Right: [T, F, F, T, F] | ||
| let left = | ||
| create_bool_array(vec![false, true, true, false, true, true, true]); | ||
| let right = ColumnarValue::Array(Arc::new(create_bool_array(vec![ | ||
| true, false, false, true, false, | ||
| ]))); | ||
| let right = create_bool_array(vec![true, false, false, true, false]); | ||
|
|
||
| let result = pre_selection_scatter(&left, right).unwrap(); | ||
| let result = pre_selection_scatter(&left, &right).unwrap(); | ||
| let result_arr = result.into_array(left.len()).unwrap(); | ||
|
|
||
| let expected = | ||
|
|
@@ -5254,9 +5283,9 @@ mod tests { | |
| // Left: [T, F, F] | ||
| // Right: [F] | ||
| let left = create_bool_array(vec![true, false, false]); | ||
| let right = ColumnarValue::Array(Arc::new(create_bool_array(vec![false]))); | ||
| let right = create_bool_array(vec![false]); | ||
|
|
||
| let result = pre_selection_scatter(&left, right).unwrap(); | ||
| let result = pre_selection_scatter(&left, &right).unwrap(); | ||
| let result_arr = result.into_array(left.len()).unwrap(); | ||
|
|
||
| let expected = create_bool_array(vec![false, false, false]); | ||
|
|
@@ -5267,9 +5296,9 @@ mod tests { | |
| // Left: [F, F, T] | ||
| // Right: [F] | ||
| let left = create_bool_array(vec![false, false, true]); | ||
| let right = ColumnarValue::Array(Arc::new(create_bool_array(vec![false]))); | ||
| let right = create_bool_array(vec![false]); | ||
|
|
||
| let result = pre_selection_scatter(&left, right).unwrap(); | ||
| let result = pre_selection_scatter(&left, &right).unwrap(); | ||
| let result_arr = result.into_array(left.len()).unwrap(); | ||
|
|
||
| let expected = create_bool_array(vec![false, false, false]); | ||
|
|
@@ -5280,10 +5309,9 @@ mod tests { | |
| // Left: [F, T, F, T] | ||
| // Right: [None, Some(false)] (with null at first position) | ||
| let left = create_bool_array(vec![false, true, false, true]); | ||
| let right_arr = BooleanArray::from(vec![None, Some(false)]); | ||
| let right = ColumnarValue::Array(Arc::new(right_arr)); | ||
| let right = BooleanArray::from(vec![None, Some(false)]); | ||
|
|
||
| let result = pre_selection_scatter(&left, right).unwrap(); | ||
| let result = pre_selection_scatter(&left, &right).unwrap(); | ||
| let result_arr = result.into_array(left.len()).unwrap(); | ||
|
|
||
| let expected = BooleanArray::from(vec![ | ||
|
|
@@ -5294,16 +5322,30 @@ mod tests { | |
| ]); | ||
| assert_eq!(&expected, result_arr.as_boolean()); | ||
| } | ||
| // Test scalar right handling | ||
| { | ||
| // Left: [T, F, T] | ||
| // Right: Scalar true | ||
| let left = create_bool_array(vec![true, false, true]); | ||
| let right = ColumnarValue::Scalar(ScalarValue::Boolean(Some(true))); | ||
| } | ||
|
|
||
| let result = pre_selection_scatter(&left, right).unwrap(); | ||
| assert!(matches!(result, ColumnarValue::Scalar(_))); | ||
| } | ||
| #[test] | ||
| fn test_and_true_preselection_returns_lhs() { | ||
| let schema = | ||
| Arc::new(Schema::new(vec![Field::new("c", DataType::Boolean, false)])); | ||
| let c_array = Arc::new(BooleanArray::from(vec![false, true, false, false, false])) | ||
| as ArrayRef; | ||
| let batch = RecordBatch::try_new(Arc::clone(&schema), vec![Arc::clone(&c_array)]) | ||
| .unwrap(); | ||
|
|
||
| let expr = logical2physical(&logical_col("c").and(expr_lit(true)), &schema); | ||
|
|
||
| let result = expr.evaluate(&batch).unwrap(); | ||
| let ColumnarValue::Array(result_arr) = result else { | ||
| panic!("Expected ColumnarValue::Array"); | ||
| }; | ||
|
|
||
| let expected: Vec<_> = c_array.as_boolean().iter().collect(); | ||
| let actual: Vec<_> = result_arr.as_boolean().iter().collect(); | ||
| assert_eq!( | ||
| expected, actual, | ||
| "AND with TRUE must equal LHS even with PreSelection" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.