[ https://issues.apache.org/jira/browse/HIVE-21857?focusedWorklogId=264256&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-264256 ]
ASF GitHub Bot logged work on HIVE-21857: ----------------------------------------- Author: ASF GitHub Bot Created on: 21/Jun/19 00:19 Start Date: 21/Jun/19 00:19 Worklog Time Spent: 10m Work Description: vineetgarg02 commented on pull request #671: HIVE-21857 URL: https://github.com/apache/hive/pull/671#discussion_r296055095 ########## File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveFilterSortPredicates.java ########## @@ -0,0 +1,268 @@ +/* + * 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.calcite.rules; + +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rel.metadata.RelMetadataQuery; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexDynamicParam; +import org.apache.calcite.rex.RexFieldAccess; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.rex.RexVisitorImpl; +import org.apache.calcite.util.Pair; +import org.apache.hadoop.hive.ql.optimizer.calcite.stats.FilterSelectivityEstimator; +import org.apache.hadoop.hive.ql.optimizer.calcite.stats.HiveRelMdSize; + + +/** + * Rule that sorts conditions in a filter predicate to accelerate query processing + * based on selectivity and compute cost. Currently it is not applied recursively, + * i.e., it is only applied to top predicates in the condition. + */ +public class HiveFilterSortPredicates extends RelOptRule { + + public static final HiveFilterSortPredicates INSTANCE = new HiveFilterSortPredicates(); + + + private HiveFilterSortPredicates() { + super( + operand(Filter.class, + operand(RelNode.class, any()))); + } + + @Override + public boolean matches(RelOptRuleCall call) { + final Filter filter = call.rel(0); + + HiveRulesRegistry registry = call.getPlanner().getContext().unwrap(HiveRulesRegistry.class); + + // If this operator has been visited already by the rule, + // we do not need to apply the optimization + if (registry != null && registry.getVisited(this).contains(filter)) { + return false; + } + + return true; + } + + @Override + public void onMatch(RelOptRuleCall call) { + final Filter filter = call.rel(0); + final RelNode input = call.rel(1); + + // Register that we have visited this operator in this rule + HiveRulesRegistry registry = call.getPlanner().getContext().unwrap(HiveRulesRegistry.class); + if (registry != null) { + registry.registerVisited(this, filter); + } + + final RexNode originalCond = filter.getCondition(); + RexSortPredicatesShuttle sortPredicatesShuttle = new RexSortPredicatesShuttle( + input, filter.getCluster().getMetadataQuery()); + final RexNode newCond = originalCond.accept(sortPredicatesShuttle); + if (!sortPredicatesShuttle.modified) { + // We are done, bail out + return; + } + + // We register the new filter so we do not fire the rule on it again + final Filter newFilter = filter.copy(filter.getTraitSet(), input, newCond); + if (registry != null) { + registry.registerVisited(this, newFilter); + } + + call.transformTo(newFilter); + } + + /** + * If the expression is an AND/OR, it will sort predicates accordingly + * to maximize performance. + * In particular, for AND clause: + * rank = (selectivity - 1) / cost per tuple + * Similarly, for OR clause: Review comment: Can you add a comment to explain why OR is sorted differently? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 264256) Time Spent: 0.5h (was: 20m) > Sort conditions in a filter predicate to accelerate query processing > -------------------------------------------------------------------- > > Key: HIVE-21857 > URL: https://issues.apache.org/jira/browse/HIVE-21857 > Project: Hive > Issue Type: New Feature > Components: CBO > Reporter: Jesus Camacho Rodriguez > Assignee: Jesus Camacho Rodriguez > Priority: Major > Labels: pull-request-available > Attachments: HIVE-21857.01.patch, HIVE-21857.02.patch, > HIVE-21857.03.patch, HIVE-21857.04.patch > > Time Spent: 0.5h > Remaining Estimate: 0h > > Following approach similar to > http://db.cs.berkeley.edu/jmh/miscpapers/sigmod93.pdf . > To reorder predicates in AND conditions, we could rank each of elements in > the clauses in increasing order based on following formula: > {code} > rank = (selectivity - 1) / cost per tuple > {code} > Similarly, for OR conditions: > {code} > rank = (-selectivity) / cost per tuple > {code} > Selectivity can be computed with FilterSelectivityEstimator. For cost per > tuple, we will need to come up with some heuristic based on how expensive is > the evaluation of the functions contained in that predicate. Custom UDFs > could be annotated. -- This message was sent by Atlassian JIRA (v7.6.3#76005)