forked from apache/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HIVE-23723: Limit operator pushdown through LOJ (Attila Magyar, revie…
…wed by Krisztian Kasa)
- Loading branch information
Showing
168 changed files
with
5,311 additions
and
4,779 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
ql/src/java/org/apache/hadoop/hive/ql/optimizer/OrderlessLimitPushDownOptimizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* | ||
* * 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 static org.apache.hadoop.hive.ql.optimizer.topnkey.TopNKeyProcessor.copyDown; | ||
import static org.apache.hadoop.hive.ql.optimizer.topnkey.TopNKeyPushdownProcessor.moveDown; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Stack; | ||
|
||
import org.apache.hadoop.hive.ql.exec.CommonJoinOperator; | ||
import org.apache.hadoop.hive.ql.exec.LimitOperator; | ||
import org.apache.hadoop.hive.ql.exec.Operator; | ||
import org.apache.hadoop.hive.ql.exec.ReduceSinkOperator; | ||
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.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.apache.hadoop.hive.ql.plan.JoinCondDesc; | ||
import org.apache.hadoop.hive.ql.plan.JoinDesc; | ||
import org.apache.hadoop.hive.ql.plan.LimitDesc; | ||
import org.apache.hadoop.hive.ql.plan.OperatorDesc; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Push LIMIT without an Order By through Selects and Left Outer Joins | ||
*/ | ||
public class OrderlessLimitPushDownOptimizer extends Transform { | ||
private static final Logger LOG = LoggerFactory.getLogger(OrderlessLimitPushDownOptimizer.class); | ||
|
||
@Override | ||
public ParseContext transform(ParseContext pctx) throws SemanticException { | ||
Map<SemanticRule, SemanticNodeProcessor> opRules = new LinkedHashMap<SemanticRule, SemanticNodeProcessor>(); | ||
opRules.put( | ||
new RuleRegExp("LIMIT push down", LimitOperator.getOperatorName() + "%"), | ||
new LimitPushDown()); | ||
SemanticGraphWalker walker = new DefaultGraphWalker(new DefaultRuleDispatcher(null, opRules, null)); | ||
walker.startWalking(new ArrayList<>(pctx.getTopOps().values()), null); | ||
return pctx; | ||
} | ||
|
||
private static class LimitPushDown implements SemanticNodeProcessor { | ||
@Override | ||
public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx, Object... nodeOutputs) throws SemanticException { | ||
ReduceSinkOperator reduceSink = findReduceSink(stack); | ||
if (reduceSink == null || !reduceSink.getConf().hasOrderBy()) { // LIMIT + ORDER BY handled by TopNKey push down | ||
pushDown((LimitOperator) nd); | ||
} | ||
return null; | ||
} | ||
|
||
private ReduceSinkOperator findReduceSink(Stack<Node> stack) { | ||
for (int i = stack.size() - 2 ; i >= 0; i--) { | ||
Operator<?> operator = (Operator<?>) stack.get(i); | ||
if (operator instanceof ReduceSinkOperator) { | ||
return ((ReduceSinkOperator) operator); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private void pushDown(LimitOperator limit) throws SemanticException { | ||
Operator<? extends OperatorDesc> parent = limit.getParentOperators().get(0); | ||
if (parent.getNumChild() != 1) { | ||
return; | ||
} | ||
switch (parent.getType()) { | ||
case LIMIT: | ||
combineLimits(limit); | ||
break; | ||
case SELECT: | ||
case FORWARD: | ||
pushdownThroughParent(limit); | ||
break; | ||
case MERGEJOIN: | ||
case JOIN: | ||
case MAPJOIN: | ||
pushThroughLeftOuterJoin(limit); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
private void combineLimits(LimitOperator childLimit) throws SemanticException { | ||
LimitOperator parentLimit = (LimitOperator) childLimit.getParentOperators().get(0); | ||
LimitDesc parentConf = parentLimit.getConf(); | ||
LimitDesc childConf = childLimit.getConf(); | ||
if (parentConf.getOffset() == childConf.getOffset()) { | ||
int min = Math.min(parentConf.getLimit(), childConf.getLimit()); | ||
LOG.debug("Combining two limits child={}, parent={}, newLimit={}", childLimit, parentLimit, min); | ||
parentConf.setLimit(min); | ||
parentLimit.removeChildAndAdoptItsChildren(childLimit); | ||
pushDown(parentLimit); | ||
} | ||
} | ||
|
||
private void pushdownThroughParent(LimitOperator limit) throws SemanticException { | ||
Operator<? extends OperatorDesc> parent = limit.getParentOperators().get(0); | ||
LOG.debug("Pushing {} through {}", limit.getName(), parent.getName()); | ||
moveDown(limit); | ||
pushDown(limit); | ||
} | ||
|
||
private void pushThroughLeftOuterJoin(LimitOperator limit) | ||
throws SemanticException { | ||
CommonJoinOperator<? extends JoinDesc> join = | ||
(CommonJoinOperator<? extends JoinDesc>) limit.getParentOperators().get(0); | ||
JoinCondDesc[] joinConds = join.getConf().getConds(); | ||
JoinCondDesc firstJoinCond = joinConds[0]; | ||
for (JoinCondDesc joinCond : joinConds) { | ||
if (!firstJoinCond.equals(joinCond)) { | ||
return; | ||
} | ||
} | ||
if (firstJoinCond.getType() == JoinDesc.LEFT_OUTER_JOIN) { | ||
List<Operator<? extends OperatorDesc>> joinInputs = join.getParentOperators(); | ||
final ReduceSinkOperator reduceSinkOperator = (ReduceSinkOperator) joinInputs.get(0); | ||
|
||
pushDown((LimitOperator) copyDown(reduceSinkOperator, new LimitDesc(limit.getConf()))); | ||
// the copied limit will take care of the offset, need to reset the offset in the original to not to lose rows | ||
limit.getConf().setOffset(0); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--! qt:dataset:src | ||
--! qt:dataset:alltypesorc | ||
set hive.mapred.mode=nonstrict; | ||
set hive.explain.user=false; | ||
set hive.limit.pushdown.memory.usage=0.3f; | ||
set hive.optimize.reducededuplication.min.reducer=1; | ||
|
||
-- SORT_QUERY_RESULTS | ||
|
||
-- push through LOJ | ||
|
||
set hive.optimize.limit=true; | ||
|
||
select 'positive LOJ'; | ||
explain | ||
SELECT src1.key, src2.value FROM src src1 LEFT OUTER JOIN src src2 ON (src1.key = src2.key) LIMIT 5; | ||
SELECT src1.key, src2.value FROM src src1 LEFT OUTER JOIN src src2 ON (src1.key = src2.key) LIMIT 5; | ||
|
||
select 'negative - order'; | ||
explain | ||
SELECT src1.key, src2.value FROM src src1 LEFT OUTER JOIN src src2 ON (src1.key = src2.key) ORDER BY src1.key LIMIT 5; | ||
|
||
select 'negative - group by'; | ||
explain | ||
SELECT src1.key, count(src2.value) FROM src src1 LEFT OUTER JOIN src src2 ON (src1.key = src2.key) GROUP BY src1.key LIMIT 5; | ||
|
||
set hive.optimize.limit=false; | ||
|
||
select 'negative - disabled'; | ||
|
||
explain | ||
SELECT src1.key, src2.value FROM src src1 LEFT OUTER JOIN src src2 ON (src1.key = src2.key) LIMIT 5; | ||
SELECT src1.key, src2.value FROM src src1 LEFT OUTER JOIN src src2 ON (src1.key = src2.key) LIMIT 5; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--! qt:dataset:src | ||
SET hive.vectorized.execution.enabled=true; | ||
set hive.fetch.task.conversion=none; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--! qt:dataset:src | ||
set hive.fetch.task.conversion=none; | ||
SET hive.vectorized.execution.enabled=true; | ||
|
||
|
Oops, something went wrong.