-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
AggregateExec: Take grouping sets into account for InputOrderMode #11301
Merged
Merged
Changes from 1 commit
Commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -369,14 +369,21 @@ impl AggregateExec { | |||||
new_requirement.extend(req); | ||||||
new_requirement = collapse_lex_req(new_requirement); | ||||||
|
||||||
let input_order_mode = | ||||||
if indices.len() == groupby_exprs.len() && !indices.is_empty() { | ||||||
InputOrderMode::Sorted | ||||||
} else if !indices.is_empty() { | ||||||
InputOrderMode::PartiallySorted(indices) | ||||||
} else { | ||||||
InputOrderMode::Linear | ||||||
}; | ||||||
let indices: Vec<usize> = indices | ||||||
.into_iter() | ||||||
.filter(|idx| group_by.groups.iter().all(|group| !group[*idx])) | ||||||
.collect(); | ||||||
|
||||||
let input_order_mode = if indices.len() == groupby_exprs.len() | ||||||
&& !indices.is_empty() | ||||||
&& group_by.groups.len() == 1 | ||||||
{ | ||||||
InputOrderMode::Sorted | ||||||
} else if !indices.is_empty() { | ||||||
InputOrderMode::PartiallySorted(indices) | ||||||
} else { | ||||||
InputOrderMode::Linear | ||||||
}; | ||||||
|
||||||
// construct a map from the input expression to the output expression of the Aggregation group by | ||||||
let projection_mapping = | ||||||
|
@@ -1180,6 +1187,7 @@ mod tests { | |||||
use arrow::array::{Float64Array, UInt32Array}; | ||||||
use arrow::compute::{concat_batches, SortOptions}; | ||||||
use arrow::datatypes::DataType; | ||||||
use arrow_array::{Float32Array, Int32Array}; | ||||||
use datafusion_common::{ | ||||||
assert_batches_eq, assert_batches_sorted_eq, internal_err, DataFusionError, | ||||||
ScalarValue, | ||||||
|
@@ -1195,7 +1203,10 @@ mod tests { | |||||
use datafusion_physical_expr::expressions::{lit, OrderSensitiveArrayAgg}; | ||||||
use datafusion_physical_expr::PhysicalSortExpr; | ||||||
|
||||||
use crate::common::collect; | ||||||
use datafusion_expr::Expr; | ||||||
use datafusion_physical_expr_common::aggregate::create_aggregate_expr; | ||||||
use datafusion_physical_expr_common::expressions::Literal; | ||||||
use futures::{FutureExt, Stream}; | ||||||
|
||||||
// Generate a schema which consists of 5 columns (a, b, c, d, e) | ||||||
|
@@ -2267,4 +2278,94 @@ mod tests { | |||||
assert_eq!(new_agg.schema(), aggregate_exec.schema()); | ||||||
Ok(()) | ||||||
} | ||||||
|
||||||
#[tokio::test] | ||||||
async fn test_agg_exec_group_by_const() -> Result<()> { | ||||||
let schema = Arc::new(Schema::new(vec![ | ||||||
Field::new("a", DataType::Float32, true), | ||||||
Field::new("b", DataType::Float32, true), | ||||||
Field::new("const", DataType::Int32, false), | ||||||
])); | ||||||
|
||||||
let col_a = col("a", &schema)?; | ||||||
let col_b = col("b", &schema)?; | ||||||
let const_expr = Arc::new(Literal::new(ScalarValue::Int32(Some(1)))); | ||||||
|
||||||
let groups = PhysicalGroupBy::new( | ||||||
vec![ | ||||||
(col_a, "a".to_string()), | ||||||
(col_b, "b".to_string()), | ||||||
(const_expr, "const".to_string()), | ||||||
], | ||||||
vec![ | ||||||
( | ||||||
Arc::new(Literal::new(ScalarValue::Float32(None))), | ||||||
"a".to_string(), | ||||||
), | ||||||
( | ||||||
Arc::new(Literal::new(ScalarValue::Float32(None))), | ||||||
"b".to_string(), | ||||||
), | ||||||
( | ||||||
Arc::new(Literal::new(ScalarValue::Int32(None))), | ||||||
"const".to_string(), | ||||||
), | ||||||
], | ||||||
vec![ | ||||||
vec![false, true, true], | ||||||
vec![true, false, true], | ||||||
vec![true, true, false], | ||||||
], | ||||||
); | ||||||
|
||||||
let aggregates: Vec<Arc<dyn AggregateExpr>> = vec![create_aggregate_expr( | ||||||
count_udaf().as_ref(), | ||||||
&[lit(1)], | ||||||
&[Expr::Literal(ScalarValue::Int32(Some(1)))], | ||||||
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 think you could make this more concise like this (and ensure the type is correct)
Suggested change
|
||||||
&[], | ||||||
&[], | ||||||
schema.as_ref(), | ||||||
"1", | ||||||
false, | ||||||
false, | ||||||
)?]; | ||||||
|
||||||
let input_batches = (0..4) | ||||||
.map(|_| { | ||||||
let a = Arc::new(Float32Array::from(vec![0.; 8192])); | ||||||
let b = Arc::new(Float32Array::from(vec![0.; 8192])); | ||||||
let c = Arc::new(Int32Array::from(vec![1; 8192])); | ||||||
|
||||||
RecordBatch::try_new(schema.clone(), vec![a, b, c]).unwrap() | ||||||
}) | ||||||
.collect(); | ||||||
|
||||||
let input = | ||||||
Arc::new(MemoryExec::try_new(&[input_batches], schema.clone(), None)?); | ||||||
|
||||||
let aggregate_exec = Arc::new(AggregateExec::try_new( | ||||||
AggregateMode::Partial, | ||||||
groups, | ||||||
aggregates.clone(), | ||||||
vec![None], | ||||||
input, | ||||||
schema, | ||||||
)?); | ||||||
|
||||||
let output = | ||||||
collect(aggregate_exec.execute(0, Arc::new(TaskContext::default()))?).await?; | ||||||
|
||||||
let expected = [ | ||||||
"+-----+-----+-------+----------+", | ||||||
"| a | b | const | 1[count] |", | ||||||
"+-----+-----+-------+----------+", | ||||||
"| | 0.0 | | 32768 |", | ||||||
"| 0.0 | | | 32768 |", | ||||||
"| | | 1 | 32768 |", | ||||||
"+-----+-----+-------+----------+", | ||||||
]; | ||||||
assert_batches_sorted_eq!(expected, &output); | ||||||
|
||||||
Ok(()) | ||||||
} | ||||||
} |
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.
I recommend adding a comment here (perhaps copied from the description of the PR) explaining the subtelty involving grouping sets