deniskuzZ commented on code in PR #5423: URL: https://github.com/apache/hive/pull/5423#discussion_r1824168063
########## ql/src/java/org/apache/hadoop/hive/ql/optimizer/UnionDistinctMerger.java: ########## @@ -0,0 +1,181 @@ +/* + * 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.hadoop.hive.ql.optimizer; + +import org.apache.hadoop.hive.ql.exec.GroupByOperator; +import org.apache.hadoop.hive.ql.exec.Operator; +import org.apache.hadoop.hive.ql.exec.ReduceSinkOperator; +import org.apache.hadoop.hive.ql.exec.UnionOperator; +import org.apache.hadoop.hive.ql.lib.DefaultGraphWalker; +import org.apache.hadoop.hive.ql.lib.DefaultRuleDispatcher; +import org.apache.hadoop.hive.ql.lib.Node; +import org.apache.hadoop.hive.ql.lib.NodeProcessorCtx; +import org.apache.hadoop.hive.ql.lib.RuleRegExp; +import org.apache.hadoop.hive.ql.lib.SemanticDispatcher; +import org.apache.hadoop.hive.ql.lib.SemanticGraphWalker; +import org.apache.hadoop.hive.ql.lib.SemanticNodeProcessor; +import org.apache.hadoop.hive.ql.lib.SemanticRule; +import org.apache.hadoop.hive.ql.parse.ParseContext; +import org.apache.hadoop.hive.ql.parse.SemanticException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Stack; + +public class UnionDistinctMerger extends Transform { + private static final Logger LOG = LoggerFactory.getLogger(UnionDistinctMerger.class); + + private static final String patternString = new StringBuilder() + .append(UnionOperator.getOperatorName()).append("%") + .append(GroupByOperator.getOperatorName()).append("%") + .append(ReduceSinkOperator.getOperatorName()).append("%") + .append(GroupByOperator.getOperatorName()).append("%") + .append(UnionOperator.getOperatorName()).append("%") + .append(GroupByOperator.getOperatorName()).append("%") + .append(ReduceSinkOperator.getOperatorName()).append("%") + .append(GroupByOperator.getOperatorName()).append("%") + .toString(); + + private static class UnionMergeContext implements NodeProcessorCtx { + public final ParseContext pCtx; + + public UnionMergeContext(ParseContext pCtx) { + this.pCtx = pCtx; + } + } + + private class UnionMergeProcessor implements SemanticNodeProcessor { + @Override + public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx, + Object... nodeOutputs) throws SemanticException { + UnionMergeContext context = (UnionMergeContext) procCtx; + + // The stack contains at least 8 operators, UNION-GBY-RS-GBY-UNION-GBY-RS-GBY. + // The leftmost UNION is on stack.size() - 8 and the rightmost GBY is on stack.size() - 1. + HashSet<Operator> allOps = new HashSet<>(context.pCtx.getAllOps()); + for (int i = 1; i <= 8; i ++) { + Operator<?> op = (Operator<?>) stack.get(stack.size() - i); + + // We do not apply the optimization if some operators do not belong to query plan. + // This can be happened when we already merged some UNIONs before. + // For example, Suppose that a query plan looks like the below graph: + // (1)UNION-GBY-RS-GBY-(3)UNION-GBY-RS-GBY + // (2)UNION-GBY-RS-GBY- + // Then we merge (1)-(3) to (1) and them move on to merging (2)-(3). Without checking the presence of + // operators in (2) and (3), the merge process will fail as (3) is already removed. + if (!allOps.contains(op)) { + return null; + } + + // We do not apply the optimization if intermediate outputs are used by other operators. + if (i != 1 && op.getChildOperators().size() > 1) { Review Comment: sorry, I don't follow here. why in that case we are doing ```` Redirect the output of lowerFinalGroupByOperator to children of upperFinalGroupByOperator ```` upperFinalGroupByOperator index is `stack.size() - 5`. Should we ignore it as well? -- 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: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org