morrySnow commented on code in PR #12129:
URL: https://github.com/apache/doris/pull/12129#discussion_r957574813
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -131,6 +132,16 @@ public List<Rule> buildRules() {
),
RuleType.BINDING_LIMIT_SLOT.build(
logicalLimit().then(limit ->
limit.withChildren(ImmutableList.of(limit.child())))
+ ),
+ RuleType.BINDING_HAVING_SLOT.build(
+ logicalHaving().thenApply(ctx -> {
Review Comment:
use `logicalHaving(logicalAggregate())` to get aggregate as child
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindSlotReference.java:
##########
@@ -131,6 +132,16 @@ public List<Rule> buildRules() {
),
RuleType.BINDING_LIMIT_SLOT.build(
logicalLimit().then(limit ->
limit.withChildren(ImmutableList.of(limit.child())))
+ ),
+ RuleType.BINDING_HAVING_SLOT.build(
+ logicalHaving().thenApply(ctx -> {
+ LogicalHaving<GroupPlan> having = ctx.root;
+ LogicalAggregate<GroupPlan> aggregate =
(LogicalAggregate<GroupPlan>) having.child().getGroup()
+ .getLogicalExpression().getPlan();
+ Expression boundPredicates = bind(
+ having.getPredicates(), aggregate.children(),
having, ctx.cascadesContext);
Review Comment:
having could use slot both in aggregate and aggregate's child, so we should
bind with both of them
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveHaving.java:
##########
@@ -0,0 +1,125 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.analysis;
+
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalHaving;
+
+import com.google.common.collect.Maps;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Resolve having clause to the aggregation.
+ */
+public class ResolveHaving extends OneAnalysisRuleFactory {
+ @Override
+ public Rule build() {
+ return RuleType.RESOLVE_HAVING.build(
+ logicalHaving().thenApply(ctx -> {
+ LogicalHaving<GroupPlan> having = ctx.root;
+ LogicalAggregate<GroupPlan> aggregate =
(LogicalAggregate<GroupPlan>) having.child().getGroup()
+ .getLogicalExpression().getPlan();
+ Set<Expression> groupByExpressions = new
HashSet<>(aggregate.getGroupByExpressions());
+ Set<Expression> tokens =
extractTokensForAggregation(having.getPredicates()).collect(
+ Collectors.toSet());
+ tokens.forEach(token -> {
+ if (token instanceof SlotReference &&
!groupByExpressions.contains(token)
+ || (token instanceof AggregateFunction
+ &&
checkWhetherNestedAggregateFunctionsExist((AggregateFunction) token))) {
+ throw new AnalysisException("Invalid having clause, "
+ having.getPredicates());
+ }
Review Comment:
1. group by key could has Alias
2. Aggregate Function does not exists in LogicalAggregate is OK.
such as below query could run on TPC-H table lineitem
```sql
select l_suppkey + 1, sum(l_linenumber), sum(l_partkey) as b from lineitem
group by l_suppkey + 1 having count(l_suppkey) > 5;
```
so, what we do is
1. split all predicates into two parts: has Aggregate Function and do not
2. for has part:
2.1 if we could not find any expression subtree in Aggregate Output, add it
to aggregate output
2.2 use aggregate output's SlotReference to replace Expression subtree in
predicates
3. for do not have part:
3.1 if we could not find any expression subtree in Aggregate Output or Group
Key, throw exception
3.2 if we could find any expression subtree in Group Key but do not in
Aggregate Output, add this group by key to aggregate output
3.3 use aggregate output's SlotReference to replace Expression subtree in
predicates
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveHaving.java:
##########
@@ -0,0 +1,125 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.analysis;
+
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalHaving;
+
+import com.google.common.collect.Maps;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Resolve having clause to the aggregation.
+ */
+public class ResolveHaving extends OneAnalysisRuleFactory {
+ @Override
+ public Rule build() {
+ return RuleType.RESOLVE_HAVING.build(
+ logicalHaving().thenApply(ctx -> {
Review Comment:
ditto, use `logicalHaving(logicalAggregate())` to get what u want
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]