Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Mar 21, 2024
1 parent c1cdc96 commit 7d5d3f0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.FunctionRegistry;
import org.apache.doris.common.Pair;
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.NereidsPlanner;
import org.apache.doris.nereids.StatementContext;
Expand Down Expand Up @@ -376,29 +377,49 @@ private LogicalHaving<Plan> bindHavingAggregate(
Scope aggOuputScope = toScope(cascadesContext, aggregate.getOutput());
Supplier<CustomSlotBinderAnalyzer> bindByGroupByThenAggOutputThenAggChild = Suppliers.memoize(() -> {
List<Expression> groupByExprs = aggregate.getGroupByExpressions();
ImmutableList.Builder<Slot> groupBySlotsBuilder
ImmutableList.Builder<Slot> groupBySlots
= ImmutableList.builderWithExpectedSize(groupByExprs.size());
for (Expression groupBy : groupByExprs) {
if (groupBy instanceof Slot) {
groupBySlotsBuilder.add((Slot) groupBy);
groupBySlots.add((Slot) groupBy);
}
}
List<Slot> groupBySlots = groupBySlotsBuilder.build();
Scope groupBySlotsScope = toScope(cascadesContext, groupBySlots);
Scope groupBySlotsScope = toScope(cascadesContext, groupBySlots.build());

Supplier<Pair<Scope, Scope>> separateAggOutputScopes = Suppliers.memoize(() -> {
ImmutableList.Builder<Slot> groupByOutputs = ImmutableList.builderWithExpectedSize(
aggregate.getOutputExpressions().size());
ImmutableList.Builder<Slot> aggFunOutputs = ImmutableList.builderWithExpectedSize(
aggregate.getOutputExpressions().size());
for (NamedExpression outputExpression : aggregate.getOutputExpressions()) {
if (outputExpression.anyMatch(AggregateFunction.class::isInstance)) {
aggFunOutputs.add(outputExpression.toSlot());
} else {
groupByOutputs.add(outputExpression.toSlot());
}
}
Scope nonAggFunSlotsScope = toScope(cascadesContext, groupByOutputs.build());
Scope aggFuncSlotsScope = toScope(cascadesContext, aggFunOutputs.build());
return Pair.of(nonAggFunSlotsScope, aggFuncSlotsScope);
});

return (analyzer, unboundSlot) -> {
List<Slot> boundInGroupBy = analyzer.bindSlotByScope(unboundSlot, groupBySlotsScope);
if (boundInGroupBy.size() == 1) {
return boundInGroupBy;
}

List<Slot> boundInAggOutput = analyzer.bindSlotByScope(unboundSlot, aggOuputScope);
if (boundInAggOutput.size() == 1) {
return boundInAggOutput;
Pair<Scope, Scope> separateAggOutputScope = separateAggOutputScopes.get();
List<Slot> boundInNonAggFuncs = analyzer.bindSlotByScope(unboundSlot, separateAggOutputScope.first);
if (boundInNonAggFuncs.size() == 1) {
return boundInNonAggFuncs;
}

List<Slot> boundInAggChild = (List) bindByAggChild.get().bindSlot(analyzer, unboundSlot);
return boundInAggChild.size() == 1 ? boundInAggChild : boundInGroupBy;
List<Slot> boundInAggFuncs = analyzer.bindSlotByScope(unboundSlot, separateAggOutputScope.second);
if (boundInAggFuncs.size() == 1) {
return boundInAggFuncs;
}
return analyzer.bindSlotByScope(unboundSlot, aggOuputScope);
};
});

Expand Down
34 changes: 34 additions & 0 deletions regression-test/suites/nereids_syntax_p0/bind_priority.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,39 @@ suite("bind_priority") {
group by id
having sum(id) + id >= 6
"""





sql "drop table if exists test_bind_having_slots3"

sql """CREATE TABLE `test_bind_having_slots3`(pk int, pk2 int)
DUPLICATE KEY(`pk`)
DISTRIBUTED BY HASH(`pk`) BUCKETS 10
properties('replication_num'='1');
"""
sql "insert into test_bind_having_slots3 values(1, 1), (2, 2), (2, 2), (3, 3), (3, 3), (3, 3);"

order_qt_having_bind_group_by """
SELECT pk + 6 as ps, COUNT(pk ) * 3 as pk
FROM test_bind_having_slots3 tbl_alias1
GROUP by pk
HAVING pk = 1
"""

order_qt_having_bind_group_by """
SELECT pk + 6 as pk, COUNT(pk ) * 3 as pk
FROM test_bind_having_slots3 tbl_alias1
GROUP by pk + 6
HAVING pk = 7
"""

order_qt_having_bind_group_by """
SELECT pk + 6, COUNT(pk ) * 3 as pk
FROM test_bind_having_slots3 tbl_alias1
GROUP by pk + 6
HAVING pk = 3
"""
}()
}

0 comments on commit 7d5d3f0

Please sign in to comment.