[ https://issues.apache.org/jira/browse/HIVE-23389?focusedWorklogId=432448&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-432448 ]
ASF GitHub Bot logged work on HIVE-23389: ----------------------------------------- Author: ASF GitHub Bot Created on: 09/May/20 23:40 Start Date: 09/May/20 23:40 Worklog Time Spent: 10m Work Description: zabetak commented on a change in pull request #1010: URL: https://github.com/apache/hive/pull/1010#discussion_r422558992 ########## File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/Bug.java ########## @@ -0,0 +1,41 @@ +/* + * 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; + +import org.apache.calcite.util.Util; Review comment: Useless import? ########## File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/Bug.java ########## @@ -0,0 +1,41 @@ +/* + * 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; + +import org.apache.calcite.util.Util; + +/** + * Holder for a list of constants describing which bugs which have not been Review comment: typo: **which** bugs **which** suggestion: A holder of constants describing existing bugs in Calcite. ########## File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveFilterMergeRule.java ########## @@ -0,0 +1,117 @@ +/* + * 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.List; +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexLocalRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexProgramBuilder; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.hadoop.hive.ql.optimizer.calcite.Bug; +import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories; +import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter; + +/** + * Mostly a copy of {@link org.apache.calcite.rel.rules.FilterMergeRule}. + * However, it flattens the predicate before creating the new filter. + */ +public class HiveFilterMergeRule extends RelOptRule { + + public static final HiveFilterMergeRule INSTANCE = + new HiveFilterMergeRule(); + + /** Private constructor. */ + private HiveFilterMergeRule() { + super(operand(HiveFilter.class, + operand(HiveFilter.class, any())), + HiveRelFactories.HIVE_BUILDER, null); + if (Bug.CALCITE_3982_FIXED) { + throw new AssertionError("Remove logic in HiveFilterMergeRule when [CALCITE-3982] " + + "has been fixed and use directly Calcite's FilterMergeRule instead."); + } + } + + //~ Methods ---------------------------------------------------------------- + + public void onMatch(RelOptRuleCall call) { + final HiveFilter topFilter = call.rel(0); + final HiveFilter bottomFilter = call.rel(1); + + RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder(); + RexProgram bottomProgram = createProgram(bottomFilter); + RexProgram topProgram = createProgram(topFilter); + + RexProgram mergedProgram = + RexProgramBuilder.mergePrograms( + topProgram, + bottomProgram, + rexBuilder); + + RexNode newCondition = expandLocalRef(rexBuilder, + mergedProgram.getCondition(), mergedProgram.getExprList()); + + final RelBuilder relBuilder = call.builder(); + relBuilder.push(bottomFilter.getInput()) + .filter(newCondition); + + call.transformTo(relBuilder.build()); + } + + /** + * Creates a RexProgram corresponding to a LogicalFilter + * + * @param filterRel the LogicalFilter + * @return created RexProgram + */ + private RexProgram createProgram(Filter filterRel) { + RexProgramBuilder programBuilder = + new RexProgramBuilder( + filterRel.getRowType(), + filterRel.getCluster().getRexBuilder()); + programBuilder.addIdentity(); + programBuilder.addCondition(filterRel.getCondition()); + return programBuilder.getProgram(); + } + + private RexNode expandLocalRef(RexBuilder rexBuilder, + RexLocalRef ref, List<RexNode> exprs) { + return ref.accept(new ExpansionShuttle(rexBuilder, exprs)); + } + + private static class ExpansionShuttle extends RexShuttle { + private final RexBuilder rexBuilder; + private final List<RexNode> exprs; + + ExpansionShuttle(RexBuilder rexBuilder, List<RexNode> exprs) { + this.rexBuilder = rexBuilder; + this.exprs = exprs; + } + + @Override + public RexNode visitLocalRef(RexLocalRef localRef) { + RexNode tree = this.exprs.get(localRef.getIndex()); + return RexUtil.flatten(rexBuilder, tree.accept(this)); + } + } +} Review comment: If what I said above holds then we can probably get rid of this code. ########## File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveFilterMergeRule.java ########## @@ -0,0 +1,117 @@ +/* + * 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.List; +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.rel.core.Filter; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexLocalRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexProgramBuilder; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.hadoop.hive.ql.optimizer.calcite.Bug; +import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories; +import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter; + +/** + * Mostly a copy of {@link org.apache.calcite.rel.rules.FilterMergeRule}. + * However, it flattens the predicate before creating the new filter. + */ +public class HiveFilterMergeRule extends RelOptRule { + + public static final HiveFilterMergeRule INSTANCE = + new HiveFilterMergeRule(); + + /** Private constructor. */ + private HiveFilterMergeRule() { + super(operand(HiveFilter.class, + operand(HiveFilter.class, any())), + HiveRelFactories.HIVE_BUILDER, null); + if (Bug.CALCITE_3982_FIXED) { + throw new AssertionError("Remove logic in HiveFilterMergeRule when [CALCITE-3982] " + + "has been fixed and use directly Calcite's FilterMergeRule instead."); + } + } + + //~ Methods ---------------------------------------------------------------- + + public void onMatch(RelOptRuleCall call) { + final HiveFilter topFilter = call.rel(0); + final HiveFilter bottomFilter = call.rel(1); + + RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder(); + RexProgram bottomProgram = createProgram(bottomFilter); + RexProgram topProgram = createProgram(topFilter); + + RexProgram mergedProgram = + RexProgramBuilder.mergePrograms( + topProgram, + bottomProgram, + rexBuilder); + + RexNode newCondition = expandLocalRef(rexBuilder, + mergedProgram.getCondition(), mergedProgram.getExprList()); + + final RelBuilder relBuilder = call.builder(); + relBuilder.push(bottomFilter.getInput()) + .filter(newCondition); Review comment: I have the impression that we could replace this code with: ``` final RelBuilder relBuilder = call.builder(); relBuilder.push(bottomFilter.getInput()) .filter(topFilter.getCondition(), bottomFilter.getCondition()); ``` If I remember well, there is a simplification inside the `builder.filter(...)` which takes cares of flattening. ---------------------------------------------------------------- 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: 432448) Time Spent: 20m (was: 10m) > FilterMergeRule can lead to AssertionError > ------------------------------------------ > > Key: HIVE-23389 > URL: https://issues.apache.org/jira/browse/HIVE-23389 > Project: Hive > Issue Type: Bug > Components: CBO > Reporter: Jesus Camacho Rodriguez > Assignee: Jesus Camacho Rodriguez > Priority: Major > Labels: pull-request-available > Attachments: HIVE-23389.01.patch, HIVE-23389.01.patch, > HIVE-23389.patch > > Time Spent: 20m > Remaining Estimate: 0h > > I have not been able to reproduce the issue in latest master. However, this > could potentially happen since Filter creation has a check on whether the > expression is flat > ([here|https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/rel/core/Filter.java#L74]) > and Filter merge does not flatten an expression when it is created. > {noformat} > java.lang.AssertionError: AND(=($3, 100), OR(OR(null, IS NOT > NULL(CAST(100):INTEGER)), =(CAST(100):INTEGER, CAST(200):INTEGER))) > at org.apache.calcite.rel.core.Filter.<init>(Filter.java:74) > at > org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter.<init>(HiveFilter.java:39) > at > org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories$HiveFilterFactoryImpl.createFilter(HiveRelFactories.java:126) > at > org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelBuilder.filter(HiveRelBuilder.java:99) > at org.apache.calcite.tools.RelBuilder.filter(RelBuilder.java:1055) > at > org.apache.calcite.rel.rules.FilterMergeRule.onMatch(FilterMergeRule.java:81) > {noformat} -- This message was sent by Atlassian Jira (v8.3.4#803005)