-
Notifications
You must be signed in to change notification settings - Fork 4.8k
HIVE-29322: Avoid TopNKeyOperator When ReduceSink TopNkey Filtering Provides Better Pruning for ORDER BY LIMIT Queries #6202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,9 +54,11 @@ | |
| import org.apache.hadoop.hive.ql.exec.FunctionRegistry; | ||
| import org.apache.hadoop.hive.ql.exec.GroupByOperator; | ||
| import org.apache.hadoop.hive.ql.exec.JoinOperator; | ||
| import org.apache.hadoop.hive.ql.exec.LateralViewJoinOperator; | ||
| import org.apache.hadoop.hive.ql.exec.MapJoinOperator; | ||
| import org.apache.hadoop.hive.ql.exec.Operator; | ||
| import org.apache.hadoop.hive.ql.exec.OperatorUtils; | ||
| import org.apache.hadoop.hive.ql.exec.PTFOperator; | ||
| import org.apache.hadoop.hive.ql.exec.ReduceSinkOperator; | ||
| import org.apache.hadoop.hive.ql.exec.SelectOperator; | ||
| import org.apache.hadoop.hive.ql.exec.TableScanOperator; | ||
|
|
@@ -1300,9 +1302,10 @@ private static void runTopNKeyOptimization(OptimizeTezProcContext procCtx) | |
| return; | ||
| } | ||
|
|
||
| String topNKeyRegexPattern = buildTopNKeyRegexPattern(procCtx); | ||
| Map<SemanticRule, SemanticNodeProcessor> opRules = new LinkedHashMap<SemanticRule, SemanticNodeProcessor>(); | ||
| opRules.put( | ||
| new RuleRegExp("Top n key optimization", ReduceSinkOperator.getOperatorName() + "%"), | ||
| new RuleRegExp("Top n key optimization", topNKeyRegexPattern), | ||
| new TopNKeyProcessor( | ||
| HiveConf.getIntVar(procCtx.conf, HiveConf.ConfVars.HIVE_MAX_TOPN_ALLOWED), | ||
| HiveConf.getFloatVar(procCtx.conf, ConfVars.HIVE_TOPN_EFFICIENCY_THRESHOLD), | ||
|
|
@@ -1322,6 +1325,49 @@ private static void runTopNKeyOptimization(OptimizeTezProcContext procCtx) | |
| ogw.startWalking(topNodes, null); | ||
| } | ||
|
|
||
| /* | ||
| * Build the ReduceSink matching pattern used by TopNKey optimization. | ||
| * | ||
| * For ORDER BY / LIMIT queries that do not involve GROUP BY or JOIN, | ||
| * applying TopNKey results in a performance regression. ReduceSink | ||
| * operators created only for ordering must therefore be excluded from | ||
| * TopNKey. | ||
| * | ||
| * When ORDER BY or LIMIT is present, restrict TopNKey to ReduceSink | ||
| * operators that originate from GROUP BY, JOIN, MAPJOIN, LATERAL VIEW | ||
| * JOIN or PTF query shapes | ||
| */ | ||
| private static String buildTopNKeyRegexPattern(OptimizeTezProcContext procCtx) { | ||
| String reduceSinkOp = ReduceSinkOperator.getOperatorName() + "%"; | ||
|
|
||
| boolean hasOrderOrLimit = | ||
| procCtx.parseContext.getQueryProperties().hasLimit() || | ||
| procCtx.parseContext.getQueryProperties().hasOrderBy(); | ||
|
|
||
| if (hasPTFReduceSink(procCtx) || !hasOrderOrLimit) { | ||
| return reduceSinkOp; | ||
| } | ||
|
|
||
| return "(" | ||
| + GroupByOperator.getOperatorName() + "|" | ||
| + PTFOperator.getOperatorName() + "|" | ||
| + JoinOperator.getOperatorName() + "|" | ||
| + MapJoinOperator.getOperatorName() + "|" | ||
| + LateralViewJoinOperator.getOperatorName() + "|" | ||
| + CommonMergeJoinOperator.getOperatorName() | ||
| + ").*%" | ||
| + reduceSinkOp; | ||
| } | ||
|
|
||
| private static boolean hasPTFReduceSink(OptimizeTezProcContext ctx) { | ||
| for (ReduceSinkOperator rs : ctx.visitedReduceSinks) { | ||
| if (rs.getConf().isPTFReduceSink()) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
Comment on lines
+1362
to
+1370
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this achieves the desired outcome. Basically, if there is a PTF RS anywhere in the plan we will apply the rule on every RS (no matter if it is PTF or not). Moreover, by relying on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For windowing queries, since there is not much performance issues with TopNKey enabled, currently making the queries to use TopNkey Path. But to match the plan, there is no sequence of PTF%RS% patterns for some queries. only RS% will work for this case.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it a better solution to traverse the tree to find PTF ? |
||
| private boolean findParallelSemiJoinBranch(Operator<?> mapjoin, TableScanOperator bigTableTS, | ||
| ParseContext parseContext, | ||
| Map<ReduceSinkOperator, TableScanOperator> semijoins, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this? It is usually better if we can keep the optimization/transformation rules independent of the SQL syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is added for windowing queries to use topNkey Path - without group by / join in the query.
example: windowing_streaming.q
select * from ( select p_mfgr, rank() over(partition by p_mfgr order by p_name) r from part) a where r < 4