Vladsz83 commented on code in PR #11972:
URL: https://github.com/apache/ignite/pull/11972#discussion_r2028836363


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/RexSimplificationPlannerTest.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.ignite.internal.processors.query.calcite.planner;
+
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.util.Util;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.ProjectableFilterableTableScan;
+import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import org.junit.Test;
+
+import static org.apache.calcite.sql.type.SqlTypeName.INTEGER;
+
+/**
+ * Tests for Rex simplification.
+ */
+public class RexSimplificationPlannerTest extends AbstractPlannerTest {
+    /**
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testExtractCommonDisjunctionPart() throws Exception {
+        IgniteSchema schema = createSchema(
+            createTable("T1", IgniteDistributions.single(), "C1", INTEGER, 
"C2", INTEGER, "C3", INTEGER)
+                .addIndex("IDX1", 0),
+            createTable("T2", IgniteDistributions.single(), "C1", INTEGER, 
"C2", INTEGER, "C3", INTEGER)
+                .addIndex("IDX2", 0)
+        );
+
+        // Simple equality predicate.
+        assertPlan("SELECT * FROM t1 WHERE " +
+                "(c1 = 0 and c2 = 1) or " +
+                "(c1 = 0 and c3 = 2)", schema,
+            isIndexScan("T1", "IDX1")
+                .and(s -> "AND(=($t0, 0), OR(=($t1, 1), =($t2, 
2)))".equals(s.condition().toString())));
+
+        // Reversed equality predicate.
+        assertPlan("SELECT * FROM t1 WHERE " +
+                "(c1 = 0 and c2 = 1) or " +
+                "(0 = c1 and c3 = 2)", schema,
+            isIndexScan("T1", "IDX1")
+                .and(s -> "AND(=($t0, 0), OR(=($t1, 1), =($t2, 
2)))".equals(s.condition().toString())));
+
+        // Simple more-than predicate.
+        assertPlan("SELECT * FROM t1 WHERE " +
+                "(c1 > 0 and c2 = 1) or " +
+                "(c1 > 0 and c3 = 2)", schema,
+            isIndexScan("T1", "IDX1")
+                .and(s -> "AND(>($t0, 0), OR(=($t1, 1), =($t2, 
2)))".equals(s.condition().toString())));
+
+        // Reversed more-than predicate.
+        assertPlan("SELECT * FROM t1 WHERE " +
+                "(c1 > 0 and c2 = 1) or " +
+                "(0 < c1 and c3 = 2)", schema,
+            isIndexScan("T1", "IDX1")
+                .and(s -> "AND(>($t0, 0), OR(=($t1, 1), =($t2, 
2)))".equals(s.condition().toString())));
+
+        // Three operands disjunction.
+        assertPlan("SELECT * FROM t1 WHERE " +
+                "(c1 = 0 and c2 = 1) or " +
+                "(c1 = 0 and c3 = 2) or " +
+                "(c1 = 0 and c2 = c3)", schema,
+            isIndexScan("T1", "IDX1")
+                .and(s -> "AND(=($t0, 0), OR(=($t1, 1), =($t2, 2), =($t1, 
$t2)))".equals(s.condition().toString())));
+
+        // Two operands extraction.
+        assertPlan("SELECT * FROM t1 WHERE " +

Review Comment:
   Maybe also some mixed test with the commutes and duplicates like:
   ```
           assertPlan("SELECT * FROM t1 WHERE " +
                   "(c1 = 0 and c2 = 0 and c3 = 0) or " +
                   "(0 = c2 and c3 = 1 and c1 = 0 and c2 = 0) or " +
                   "(c2 = 0 and c3 = 2 and c1 = 0 and c2 = 0)", schema,
               isIndexScan("T1", "IDX1")
                   .and(s -> "AND(=($t0, 0), =($t1, 0), SEARCH($t2, Sarg[0, 1, 
2]))".equals(s.condition().toString())));
   ```



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgnitePlanner.java:
##########
@@ -586,6 +590,117 @@ private RelNode 
visitLeftAndRightCorrelateHands(LogicalCorrelate correlate, Corr
         return relShuttle.visit(rel);
     }
 
+    /**
+     * Due to distributive property of conjunction over disjunction we can 
extract common part of conjunctions.
+     * This can help us to simplify and push down filters.
+     * For example, condition:
+     *      (a = 0 and x = 1) or (a = 0 and y = 2) or (a = 0 and z = 3)
+     * can be translated to:
+     *      a = 0 and (x = 1 or y = 2 or z = 3)
+     * after such a transformation condition "a = 0" can be used as index 
access predicate.
+     */
+    public RelNode extractConjunctionOverDisjunctionCommonPart(RelNode rel) {
+        return new RelHomogeneousShuttle() {
+            /** {@inheritDoc} */
+            @Override public RelNode visit(LogicalFilter filter) {
+                RexNode condition = 
transform(filter.getCluster().getRexBuilder(), filter.getCondition());
+
+                if (condition != filter.getCondition())
+                    filter = filter.copy(filter.getTraitSet(), 
filter.getInput(), condition);
+
+                return super.visit(filter);
+            }
+
+            /** {@inheritDoc} */
+            @Override public RelNode visit(LogicalJoin join) {
+                RexNode condition = 
transform(join.getCluster().getRexBuilder(), join.getCondition());
+
+                if (condition != join.getCondition()) {
+                    join = join.copy(join.getTraitSet(), condition, 
join.getLeft(), join.getRight(),
+                        join.getJoinType(), join.isSemiJoinDone());
+                }
+
+                return super.visit(join);
+            }
+
+            /** */
+            private RexNode transform(RexBuilder rexBuilder, RexNode 
condition) {
+                // It makes sence to extract only top level disjunction common 
part.
+                if (!condition.isA(SqlKind.OR))
+                    return condition;
+
+                Set<RexNode> commonPart = new HashSet<>();
+
+                List<RexNode> orOps = ((RexCall)condition).getOperands();
+
+                RexNode firstOp = orOps.get(0);
+
+                for (RexNode andOpFirst : conjunctionOperands(firstOp)) {
+                    boolean found = false;
+
+                    for (int i = 1; i < orOps.size(); i++) {
+                        found = false;
+
+                        for (RexNode andOpOther : 
conjunctionOperands(orOps.get(i))) {
+                            if (andOpFirst.equals(andOpOther)) {
+                                found = true;
+                                break;
+                            }
+                        }
+
+                        if (!found)
+                            break;
+                    }
+
+                    if (found)
+                        commonPart.add(andOpFirst);
+                }
+
+                if (commonPart.isEmpty())
+                    return condition;
+
+                List<RexNode> newOrOps = new ArrayList<>(orOps.size());
+
+                for (RexNode orOp : orOps) {
+                    List<RexNode> newAndOps = new ArrayList<>();
+
+                    for (RexNode andOp : conjunctionOperands(orOp)) {
+                        if (!commonPart.contains(andOp))
+                            newAndOps.add(andOp);
+                    }
+
+                    if (!newAndOps.isEmpty()) {
+                        RexNode newAnd = 
RexUtil.composeConjunction(rexBuilder, newAndOps);

Review Comment:
   Do we have a rex/exec/planner test when newOrOps.size() > 1 ?



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgnitePlanner.java:
##########
@@ -586,6 +590,117 @@ private RelNode 
visitLeftAndRightCorrelateHands(LogicalCorrelate correlate, Corr
         return relShuttle.visit(rel);
     }
 
+    /**
+     * Due to distributive property of conjunction over disjunction we can 
extract common part of conjunctions.
+     * This can help us to simplify and push down filters.
+     * For example, condition:
+     *      (a = 0 and x = 1) or (a = 0 and y = 2) or (a = 0 and z = 3)
+     * can be translated to:
+     *      a = 0 and (x = 1 or y = 2 or z = 3)
+     * after such a transformation condition "a = 0" can be used as index 
access predicate.
+     */
+    public RelNode extractConjunctionOverDisjunctionCommonPart(RelNode rel) {
+        return new RelHomogeneousShuttle() {
+            /** {@inheritDoc} */
+            @Override public RelNode visit(LogicalFilter filter) {
+                RexNode condition = 
transform(filter.getCluster().getRexBuilder(), filter.getCondition());
+
+                if (condition != filter.getCondition())
+                    filter = filter.copy(filter.getTraitSet(), 
filter.getInput(), condition);
+
+                return super.visit(filter);
+            }
+
+            /** {@inheritDoc} */
+            @Override public RelNode visit(LogicalJoin join) {
+                RexNode condition = 
transform(join.getCluster().getRexBuilder(), join.getCondition());
+
+                if (condition != join.getCondition()) {
+                    join = join.copy(join.getTraitSet(), condition, 
join.getLeft(), join.getRight(),
+                        join.getJoinType(), join.isSemiJoinDone());
+                }
+
+                return super.visit(join);
+            }
+
+            /** */
+            private RexNode transform(RexBuilder rexBuilder, RexNode 
condition) {
+                // It makes sence to extract only top level disjunction common 
part.
+                if (!condition.isA(SqlKind.OR))
+                    return condition;
+
+                Set<RexNode> commonPart = new HashSet<>();
+
+                List<RexNode> orOps = ((RexCall)condition).getOperands();
+
+                RexNode firstOp = orOps.get(0);
+
+                for (RexNode andOpFirst : conjunctionOperands(firstOp)) {
+                    boolean found = false;
+
+                    for (int i = 1; i < orOps.size(); i++) {
+                        found = false;
+
+                        for (RexNode andOpOther : 
conjunctionOperands(orOps.get(i))) {
+                            if (andOpFirst.equals(andOpOther)) {
+                                found = true;
+                                break;

Review Comment:
   Up to you. Might be on the next line.



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgnitePlanner.java:
##########
@@ -586,6 +590,117 @@ private RelNode 
visitLeftAndRightCorrelateHands(LogicalCorrelate correlate, Corr
         return relShuttle.visit(rel);
     }
 
+    /**
+     * Due to distributive property of conjunction over disjunction we can 
extract common part of conjunctions.
+     * This can help us to simplify and push down filters.
+     * For example, condition:
+     *      (a = 0 and x = 1) or (a = 0 and y = 2) or (a = 0 and z = 3)
+     * can be translated to:
+     *      a = 0 and (x = 1 or y = 2 or z = 3)
+     * after such a transformation condition "a = 0" can be used as index 
access predicate.
+     */
+    public RelNode extractConjunctionOverDisjunctionCommonPart(RelNode rel) {
+        return new RelHomogeneousShuttle() {
+            /** {@inheritDoc} */
+            @Override public RelNode visit(LogicalFilter filter) {
+                RexNode condition = 
transform(filter.getCluster().getRexBuilder(), filter.getCondition());
+
+                if (condition != filter.getCondition())
+                    filter = filter.copy(filter.getTraitSet(), 
filter.getInput(), condition);
+
+                return super.visit(filter);
+            }
+
+            /** {@inheritDoc} */
+            @Override public RelNode visit(LogicalJoin join) {
+                RexNode condition = 
transform(join.getCluster().getRexBuilder(), join.getCondition());
+
+                if (condition != join.getCondition()) {
+                    join = join.copy(join.getTraitSet(), condition, 
join.getLeft(), join.getRight(),
+                        join.getJoinType(), join.isSemiJoinDone());
+                }
+
+                return super.visit(join);
+            }
+
+            /** */
+            private RexNode transform(RexBuilder rexBuilder, RexNode 
condition) {
+                // It makes sence to extract only top level disjunction common 
part.
+                if (!condition.isA(SqlKind.OR))
+                    return condition;
+
+                Set<RexNode> commonPart = new HashSet<>();
+
+                List<RexNode> orOps = ((RexCall)condition).getOperands();
+
+                RexNode firstOp = orOps.get(0);
+
+                for (RexNode andOpFirst : conjunctionOperands(firstOp)) {
+                    boolean found = false;
+
+                    for (int i = 1; i < orOps.size(); i++) {
+                        found = false;
+
+                        for (RexNode andOpOther : 
conjunctionOperands(orOps.get(i))) {
+                            if (andOpFirst.equals(andOpOther)) {
+                                found = true;
+                                break;
+                            }
+                        }
+
+                        if (!found)
+                            break;
+                    }
+
+                    if (found)
+                        commonPart.add(andOpFirst);
+                }
+
+                if (commonPart.isEmpty())
+                    return condition;
+
+                List<RexNode> newOrOps = new ArrayList<>(orOps.size());
+
+                for (RexNode orOp : orOps) {
+                    List<RexNode> newAndOps = new ArrayList<>();
+
+                    for (RexNode andOp : conjunctionOperands(orOp)) {
+                        if (!commonPart.contains(andOp))
+                            newAndOps.add(andOp);
+                    }
+
+                    if (!newAndOps.isEmpty()) {
+                        RexNode newAnd = 
RexUtil.composeConjunction(rexBuilder, newAndOps);

Review Comment:
   Up to you. Minor optimization. There is check for the size. However, there 
would be fewer calls, nulls checks, collection wraps, etc:
   ```
    RexNode newAnd = newAndOps.size() > 1
                               ? RexUtil.composeConjunction(rexBuilder, 
newAndOps)
                               : newAndOps.get(0);
   ```
   
   The same for a couple of composes below.



-- 
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.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to