Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,22 @@ public List<Rule> buildRules() {
List<NamedExpression> output =
bind(agg.getOutputExpressions(), agg.children(), agg, ctx.cascadesContext);

// The columns referenced in group by are first obtained from the child's output,
// and then from the node's output
Map<String, Expression> childOutputsToExpr = agg.child().getOutput().stream()
.collect(Collectors.toMap(Slot::getName, Slot::toSlot, (oldExpr, newExpr) -> oldExpr));
// This is consistent with the behavior of the old optimizer.
// TODO:It will be changed to the SQL standard in the future,
// first obtained from the child, and then obtained from the output of the operator.
Map<String, Expression> aliasNameToExpr = output.stream()
.filter(ne -> ne instanceof Alias)
.map(Alias.class::cast)
.collect(Collectors.toMap(Alias::getName, UnaryNode::child, (oldExpr, newExpr) -> oldExpr));
aliasNameToExpr.entrySet().stream()
.forEach(e -> childOutputsToExpr.putIfAbsent(e.getKey(), e.getValue()));

List<Expression> replacedGroupBy = agg.getGroupByExpressions().stream()
.map(groupBy -> {
if (groupBy instanceof UnboundSlot) {
UnboundSlot unboundSlot = (UnboundSlot) groupBy;
if (unboundSlot.getNameParts().size() == 1) {
String name = unboundSlot.getNameParts().get(0);
if (childOutputsToExpr.containsKey(name)) {
return childOutputsToExpr.get(name);
if (aliasNameToExpr.containsKey(name)) {
return aliasNameToExpr.get(name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,10 @@ public void test2_3() {
.checkPlannerResult("select if(col1 = null, 'all', 2) as col2, count(*) as cnt from"
+ " (select null as col1 union all select 'a' as col1 ) t group by grouping sets ((col2),());");
}

@Test
public void testGroup() {
PlanChecker.from(connectContext)
.checkPlannerResult("select b.k2 as k2 from t1 a left join (select k2 from t1) b on a.k2 = b.k2 group by k2");
}
}