You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "alex-plekhanov (via GitHub)" <gi...@apache.org> on 2025/03/27 18:28:04 UTC

[PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

alex-plekhanov opened a new pull request, #11972:
URL: https://github.com/apache/ignite/pull/11972

   …ion in conditions
   
   Thank you for submitting the pull request to the Apache Ignite.
   
   In order to streamline the review of the contribution 
   we ask you to ensure the following steps have been taken:
   
   ### The Contribution Checklist
   - [ ] There is a single JIRA ticket related to the pull request. 
   - [ ] The web-link to the pull request is attached to the JIRA ticket.
   - [ ] The JIRA ticket has the _Patch Available_ state.
   - [ ] The pull request body describes changes that have been made. 
   The description explains _WHAT_ and _WHY_ was made instead of _HOW_.
   - [ ] The pull request title is treated as the final commit message. 
   The following pattern must be used: `IGNITE-XXXX Change summary` where `XXXX` - number of JIRA issue.
   - [ ] A reviewer has been mentioned through the JIRA comments 
   (see [the Maintainers list](https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute#HowtoContribute-ReviewProcessandMaintainers)) 
   - [ ] The pull request has been checked by the Teamcity Bot and 
   the `green visa` attached to the JIRA ticket (see [TC.Bot: Check PR](https://mtcga.gridgain.com/prs.html))
   
   ### Notes
   - [How to Contribute](https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute)
   - [Coding abbreviation rules](https://cwiki.apache.org/confluence/display/IGNITE/Abbreviation+Rules)
   - [Coding Guidelines](https://cwiki.apache.org/confluence/display/IGNITE/Coding+Guidelines)
   - [Apache Ignite Teamcity Bot](https://cwiki.apache.org/confluence/display/IGNITE/Apache+Ignite+Teamcity+Bot)
   
   If you need any help, please email dev@ignite.apache.org or ask anу advice on http://asf.slack.com _#ignite_ channel.
   


-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "Vladsz83 (via GitHub)" <gi...@apache.org>.
Vladsz83 commented on code in PR #11972:
URL: https://github.com/apache/ignite/pull/11972#discussion_r2031065195


##########
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:
   > testExtractCommonDisjunctionPart
   
   Can't catch a debug point with the option `newAndOps.size() > 1`



-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #11972:
URL: https://github.com/apache/ignite/pull/11972#issuecomment-2783085719

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_ignite&pullRequest=11972&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_ignite&pullRequest=11972&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_ignite&pullRequest=11972&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_ignite&pullRequest=11972&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_ignite&pullRequest=11972&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972)
   
   


-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "alex-plekhanov (via GitHub)" <gi...@apache.org>.
alex-plekhanov commented on code in PR #11972:
URL: https://github.com/apache/ignite/pull/11972#discussion_r2031058759


##########
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:
   Fixed



##########
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:
   FIxed



-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #11972:
URL: https://github.com/apache/ignite/pull/11972#issuecomment-2759067440

   ## [![Quality Gate Failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-failed-20px.png 'Quality Gate Failed')](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972) **Quality Gate failed**  
   Failed conditions  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/failed-16px.png '') [3 New Code Smells](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972) (required ≤ 1)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972)
   
   ##   
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/light_bulb-16px.png '') Catch issues before they fail your Quality Gate with our IDE extension ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/sonarlint-16px.png '') [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
   
   


-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "asfgit (via GitHub)" <gi...@apache.org>.
asfgit closed pull request #11972: IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct…
URL: https://github.com/apache/ignite/pull/11972


-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #11972:
URL: https://github.com/apache/ignite/pull/11972#issuecomment-2762284994

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_ignite&pullRequest=11972&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_ignite&pullRequest=11972&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_ignite&pullRequest=11972&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_ignite&pullRequest=11972&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_ignite&pullRequest=11972&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972)
   
   


-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "alex-plekhanov (via GitHub)" <gi...@apache.org>.
alex-plekhanov commented on code in PR #11972:
URL: https://github.com/apache/ignite/pull/11972#discussion_r2030951332


##########
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:
   This check already exists inside `composeConjunction`



-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #11972:
URL: https://github.com/apache/ignite/pull/11972#issuecomment-2762231004

   ## [![Quality Gate Failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-failed-20px.png 'Quality Gate Failed')](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972) **Quality Gate failed**  
   Failed conditions  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/failed-16px.png '') [2 New Code Smells](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972) (required ≤ 1)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972)
   
   ##   
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/light_bulb-16px.png '') Catch issues before they fail your Quality Gate with our IDE extension ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/sonarlint-16px.png '') [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
   
   


-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "alex-plekhanov (via GitHub)" <gi...@apache.org>.
alex-plekhanov commented on code in PR #11972:
URL: https://github.com/apache/ignite/pull/11972#discussion_r2030955054


##########
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:
   Almos all tests in `testExtractCommonDisjunctionPart` have `newOrOps.size() > 1`. "Three operands disjunction" has `newOrOps.size() == 3`, for example.



-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #11972:
URL: https://github.com/apache/ignite/pull/11972#issuecomment-2783053373

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_ignite&pullRequest=11972&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_ignite&pullRequest=11972&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_ignite&pullRequest=11972&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_ignite&pullRequest=11972&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_ignite&pullRequest=11972&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_ignite&pullRequest=11972)
   
   


-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "alex-plekhanov (via GitHub)" <gi...@apache.org>.
alex-plekhanov commented on code in PR #11972:
URL: https://github.com/apache/ignite/pull/11972#discussion_r2031075899


##########
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 you meant `newAndOps.size() > 1`? I've added the test for such a case.



-- 
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-unsubscribe@ignite.apache.org

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


Re: [PR] IGNITE-24756 SQL Calcite: Add extraction of common part from disjunct… [ignite]

Posted by "Vladsz83 (via GitHub)" <gi...@apache.org>.
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-unsubscribe@ignite.apache.org

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