Skip to content

Commit e64e3f7

Browse files
Preserve grouping ID during aggregate CSE (apache#24144)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes apache#24143. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. Please explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the implementation. "COUNT(DISTINCT) returns wrong results when the column contains nulls" is the user-visible problem. --> See apache#24143. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR. --> - Preserve `__grouping_id` in CSE recovery projections for grouping-set aggregates. - Add regression coverage. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please add the `api change` label. --> No. Co-authored-by: Gabriel <[email protected]>
1 parent f05869b commit e64e3f7

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

‎datafusion/optimizer/src/common_subexpr_eliminate.rs‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,9 @@ fn extract_expressions(expr: &Expr, result: &mut Vec<Expr>) {
826826
let col = Column::new(qualifier, field_name);
827827
result.push(Expr::Column(col))
828828
}
829+
result.push(Expr::Column(Column::from_name(
830+
Aggregate::INTERNAL_GROUPING_ID,
831+
)));
829832
} else {
830833
let (qualifier, field_name) = expr.qualified_name();
831834
let col = Column::new(qualifier, field_name);
@@ -1106,6 +1109,27 @@ mod test {
11061109
)
11071110
}
11081111

1112+
#[test]
1113+
fn common_aggregate_grouping_set_preserves_internal_id() -> Result<()> {
1114+
let plan = LogicalPlanBuilder::from(test_table_scan()?)
1115+
.aggregate(
1116+
vec![grouping_set(vec![vec![col("a")]])],
1117+
vec![avg(col("b")).alias("first"), avg(col("b")).alias("second")],
1118+
)?
1119+
.filter(col(Aggregate::INTERNAL_GROUPING_ID).eq(lit(0_u8)))?
1120+
.build()?;
1121+
1122+
assert_optimized_plan_equal!(
1123+
plan,
1124+
@ r"
1125+
Filter: __grouping_id = UInt8(0)
1126+
Projection: test.a, __grouping_id, __common_expr_1 AS first, __common_expr_1 AS second
1127+
Aggregate: groupBy=[[GROUPING SETS ((test.a))]], aggr=[[avg(test.b) AS __common_expr_1]]
1128+
TableScan: test
1129+
"
1130+
)
1131+
}
1132+
11091133
#[test]
11101134
fn subexpr_in_same_order() -> Result<()> {
11111135
let table_scan = test_table_scan()?;
@@ -1288,20 +1312,31 @@ mod test {
12881312

12891313
#[test]
12901314
fn test_extract_expressions_from_grouping_set() -> Result<()> {
1291-
let mut result = Vec::with_capacity(3);
1315+
let mut result = Vec::with_capacity(4);
12921316
let grouping = grouping_set(vec![vec![col("a"), col("b")], vec![col("c")]]);
12931317
extract_expressions(&grouping, &mut result);
12941318

1295-
assert!(result.len() == 3);
1319+
assert_eq!(
1320+
result,
1321+
vec![
1322+
col("a"),
1323+
col("b"),
1324+
col("c"),
1325+
col(Aggregate::INTERNAL_GROUPING_ID),
1326+
]
1327+
);
12961328
Ok(())
12971329
}
12981330

12991331
#[test]
13001332
fn test_extract_expressions_from_grouping_set_with_identical_expr() -> Result<()> {
1301-
let mut result = Vec::with_capacity(2);
1333+
let mut result = Vec::with_capacity(3);
13021334
let grouping = grouping_set(vec![vec![col("a"), col("b")], vec![col("a")]]);
13031335
extract_expressions(&grouping, &mut result);
1304-
assert!(result.len() == 2);
1336+
assert_eq!(
1337+
result,
1338+
vec![col("a"), col("b"), col(Aggregate::INTERNAL_GROUPING_ID),]
1339+
);
13051340
Ok(())
13061341
}
13071342

0 commit comments

Comments
 (0)