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-22256: Rewriting fails when
IN
clause has items in different o…
…rder in MV and query (Jesus Camacho Rodriguez, reviewed by Vineet Garg) Close apache#1002
- Loading branch information
Showing
8 changed files
with
610 additions
and
38 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
198 changes: 198 additions & 0 deletions
198
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveInBetweenExpandRule.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,198 @@ | ||
/* | ||
* 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.ArrayList; | ||
import java.util.List; | ||
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.core.Join; | ||
import org.apache.calcite.rel.core.Project; | ||
import org.apache.calcite.rex.RexBuilder; | ||
import org.apache.calcite.rex.RexCall; | ||
import org.apache.calcite.rex.RexNode; | ||
import org.apache.calcite.rex.RexShuttle; | ||
import org.apache.calcite.rex.RexUtil; | ||
import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories; | ||
import org.apache.hadoop.hive.ql.optimizer.calcite.translator.RexNodeConverter; | ||
|
||
/** | ||
* This class contains rules to rewrite IN/BETWEEN clauses into their | ||
* corresponding AND/OR versions. | ||
* It is the counterpart to {@link HivePointLookupOptimizerRule}. | ||
*/ | ||
public class HiveInBetweenExpandRule { | ||
|
||
public static final FilterRule FILTER_INSTANCE = new FilterRule(); | ||
public static final JoinRule JOIN_INSTANCE = new JoinRule(); | ||
public static final ProjectRule PROJECT_INSTANCE = new ProjectRule(); | ||
|
||
/** Rule adapter to apply the transformation to Filter conditions. */ | ||
private static class FilterRule extends RelOptRule { | ||
|
||
FilterRule() { | ||
super(operand(Filter.class, any()), HiveRelFactories.HIVE_BUILDER, null); | ||
} | ||
|
||
@Override | ||
public void onMatch(RelOptRuleCall call) { | ||
final Filter filter = call.rel(0); | ||
RexInBetweenExpander expander = new RexInBetweenExpander( | ||
filter.getCluster().getRexBuilder()); | ||
RexNode condition = expander.apply(filter.getCondition()); | ||
|
||
if (!expander.modified) { | ||
return; | ||
} | ||
|
||
RelNode newFilter = filter.copy(filter.getTraitSet(), | ||
filter.getInput(), condition); | ||
|
||
call.transformTo(newFilter); | ||
} | ||
} | ||
|
||
/** Rule adapter to apply the transformation to Join conditions. */ | ||
private static class JoinRule extends RelOptRule { | ||
|
||
JoinRule() { | ||
super(operand(Join.class, any()), HiveRelFactories.HIVE_BUILDER, null); | ||
} | ||
|
||
@Override | ||
public void onMatch(RelOptRuleCall call) { | ||
final Join join = call.rel(0); | ||
RexInBetweenExpander expander = new RexInBetweenExpander( | ||
join.getCluster().getRexBuilder()); | ||
RexNode condition = expander.apply(join.getCondition()); | ||
|
||
if (!expander.modified) { | ||
return; | ||
} | ||
|
||
RelNode newJoin = join.copy(join.getTraitSet(), | ||
condition, | ||
join.getLeft(), | ||
join.getRight(), | ||
join.getJoinType(), | ||
join.isSemiJoinDone()); | ||
|
||
call.transformTo(newJoin); | ||
} | ||
} | ||
|
||
/** Rule adapter to apply the transformation to Project expressions. */ | ||
private static class ProjectRule extends RelOptRule { | ||
|
||
ProjectRule() { | ||
super(operand(Project.class, any()), HiveRelFactories.HIVE_BUILDER, null); | ||
} | ||
|
||
@Override | ||
public void onMatch(RelOptRuleCall call) { | ||
final Project project = call.rel(0); | ||
RexInBetweenExpander expander = new RexInBetweenExpander( | ||
project.getCluster().getRexBuilder()); | ||
List<RexNode> newProjects = new ArrayList<>(); | ||
for (RexNode expr : project.getProjects()) { | ||
newProjects.add(expander.apply(expr)); | ||
} | ||
|
||
if (!expander.modified) { | ||
return; | ||
} | ||
|
||
Project newProject = project.copy(project.getTraitSet(), | ||
project.getInput(), newProjects, project.getRowType()); | ||
|
||
call.transformTo(newProject); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Class that transforms IN/BETWEEN clauses in an expression. | ||
* If any call is modified, the modified flag will be set to | ||
* true after its execution. | ||
*/ | ||
private static final class RexInBetweenExpander extends RexShuttle { | ||
|
||
private final RexBuilder rexBuilder; | ||
private boolean modified; | ||
|
||
private RexInBetweenExpander(RexBuilder rexBuilder) { | ||
this.rexBuilder = rexBuilder; | ||
this.modified = false; | ||
} | ||
|
||
@Override | ||
public RexNode visitCall(final RexCall call) { | ||
switch (call.getKind()) { | ||
case AND: { | ||
boolean[] update = {false}; | ||
List<RexNode> newOperands = visitList(call.operands, update); | ||
if (update[0]) { | ||
return RexUtil.composeConjunction(rexBuilder, newOperands); | ||
} | ||
return call; | ||
} | ||
case OR: { | ||
boolean[] update = {false}; | ||
List<RexNode> newOperands = visitList(call.operands, update); | ||
if (update[0]) { | ||
return RexUtil.composeDisjunction(rexBuilder, newOperands); | ||
} | ||
return call; | ||
} | ||
case IN: { | ||
List<RexNode> newOperands = RexNodeConverter.transformInToOrOperands( | ||
call.getOperands(), rexBuilder); | ||
if (newOperands == null) { | ||
// We could not execute transformation, return expression | ||
return call; | ||
} | ||
modified = true; | ||
if (newOperands.size() > 1) { | ||
return rexBuilder.makeCall(SqlStdOperatorTable.OR, newOperands); | ||
} | ||
return newOperands.get(0); | ||
} | ||
case BETWEEN: { | ||
List<RexNode> newOperands = RexNodeConverter.rewriteBetweenChildren( | ||
call.getOperands(), rexBuilder); | ||
modified = true; | ||
if (call.getOperands().get(0).isAlwaysTrue()) { | ||
return rexBuilder.makeCall(SqlStdOperatorTable.OR, newOperands); | ||
} | ||
return rexBuilder.makeCall(SqlStdOperatorTable.AND, newOperands); | ||
} | ||
default: | ||
return super.visitCall(call); | ||
} | ||
} | ||
|
||
} | ||
|
||
private HiveInBetweenExpandRule() { | ||
// Utility class, defeat instantiation | ||
} | ||
|
||
} |
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
61 changes: 61 additions & 0 deletions
61
ql/src/test/queries/clientpositive/materialized_view_rewrite_in_between.q
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,61 @@ | ||
SET hive.cli.errors.ignore=true; | ||
SET hive.support.concurrency=true; | ||
SET hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager; | ||
SET metastore.strict.managed.tables=true; | ||
SET hive.default.fileformat=textfile; | ||
SET hive.default.fileformat.managed=orc; | ||
SET metastore.create.as.acid=true; | ||
SET hive.groupby.position.alias=true; | ||
|
||
drop database if exists expr2 cascade; | ||
create database expr2; | ||
use expr2; | ||
create table sales(prod_id int, cust_id int, store_id int, sale_date timestamp, qty int, amt double, descr string); | ||
insert into sales values | ||
(11,1,101,'12/24/2013',1000,1234.00,'onedummytwo'); | ||
|
||
create materialized view mv1 stored as orc as (select prod_id, cust_id, store_id, sale_date, qty, amt, descr from sales where cust_id in (1,2,3,4,5)); | ||
-- SAME ORDER | ||
explain cbo | ||
select prod_id, cust_id from sales where cust_id in (1,2,3,4,5); | ||
-- DIFFERENT ORDER | ||
explain cbo | ||
select prod_id, cust_id from sales where cust_id in (5,1,2,3,4); | ||
|
||
drop materialized view mv1; | ||
|
||
drop database if exists in_pred cascade; | ||
create database in_pred; | ||
use in_pred; | ||
create table census_pop (state string, year int, population bigint); | ||
insert into census_pop values("AZ", 2010, 200), ("CA", 2011, 100), ("CA", 2010, 200), ("AZ", 2010, 100), ("NY", 2011, 121), ("AZ", 2011, 1000), ("OR", 2015, 1001), ("WA", 2016, 121), ("NJ", 2010, 500), ("NJ", 2010, 5000), ("AZ", 2014, 1004), ("TX", 2010, 1000), ("AZ", 2010, 1000), ("PT", 2017, 1200), ("NM", 2018, 120), ("CA", 2010, 200); | ||
|
||
create materialized view mv2 stored as orc as select state, year, sum(population) from census_pop where year IN (2010, 2018) group by state, year; | ||
-- SAME | ||
explain cbo | ||
select state, year, sum(population) from census_pop where year IN (2010, 2018) group by state, year; | ||
-- PARTIAL IN EQUALS | ||
explain cbo | ||
select state, year, sum(population) from census_pop where year = 2010 group by state, year; | ||
-- PARTIAL | ||
explain cbo | ||
select state, year, sum(population) from census_pop where year in (2010) group by state, year; | ||
|
||
drop materialized view mv2; | ||
|
||
drop database if exists expr9 cascade; | ||
create database expr9; | ||
use expr9; | ||
create table sales(prod_id int, cust_id int, store_id int, sale_date timestamp, qty int, amt double, descr string); | ||
insert into sales values | ||
(11,1,101,'12/24/2013',1000,1234.00,'onedummytwo'); | ||
|
||
create materialized view mv3 stored as orc as (select prod_id, cust_id, store_id, sale_date, qty, amt, descr from sales where cust_id >= 1 and prod_id < 31); | ||
-- SAME | ||
explain cbo | ||
select * from sales where cust_id >= 1 and prod_id < 31; | ||
-- BETWEEN AND RANGE | ||
explain cbo | ||
select * from sales where cust_id between 1 and 20 and prod_id < 31; | ||
|
||
drop materialized view mv3; |
Oops, something went wrong.