You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/05/09 17:39:03 UTC

[GitHub] [hive] jcamachor opened a new pull request #1010: HIVE-23389

jcamachor opened a new pull request #1010:
URL: https://github.com/apache/hive/pull/1010


   


----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] jcamachor commented on a change in pull request #1010: HIVE-23389

Posted by GitBox <gi...@apache.org>.
jcamachor commented on a change in pull request #1010:
URL: https://github.com/apache/hive/pull/1010#discussion_r422568569



##########
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:
       You are right... I copied the code as-is from Calcite but I think going through the program is actually not necessary.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] zabetak commented on a change in pull request #1010: HIVE-23389

Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] jcamachor commented on pull request #1010: HIVE-23389

Posted by GitBox <gi...@apache.org>.
jcamachor commented on pull request #1010:
URL: https://github.com/apache/hive/pull/1010#issuecomment-626258279


   > I guess you plan to add a test when you commit in Calcite.
   Yes, I could not repro the issue in Hive and I do not see any regressions, hence that should be sufficient. However, I will add a test to Calcite as part of the commit (assuming I can reproduce the issue over there).


----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] jcamachor edited a comment on pull request #1010: HIVE-23389

Posted by GitBox <gi...@apache.org>.
jcamachor edited a comment on pull request #1010:
URL: https://github.com/apache/hive/pull/1010#issuecomment-626258279


   > I guess you plan to add a test when you commit in Calcite.
   
   Yes, I could not repro the issue in Hive and I do not see any regressions, hence that should be sufficient. However, I will add a test to Calcite as part of the commit (assuming I can reproduce the issue over there).


----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org