You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by am...@apache.org on 2020/06/04 11:33:43 UTC

[ignite] branch ignite-13021 updated: WIP. debug.

This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch ignite-13021
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/ignite-13021 by this push:
     new 01205d8  WIP. debug.
01205d8 is described below

commit 01205d8a93e6f3bf1c734474ff2d6d278b892da0
Author: Andrey V. Mashenkov <an...@gmail.com>
AuthorDate: Thu Jun 4 14:33:15 2020 +0300

    WIP. debug.
---
 .../query/calcite/rel/IgniteTableScan.java         |  4 +-
 .../calcite/rule/logical/LogicalOrToUnionRule.java | 59 ++++++++++------------
 .../query/calcite/rules/OrToUnionRuleTest.java     | 37 +++++++++-----
 3 files changed, 52 insertions(+), 48 deletions(-)

diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteTableScan.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteTableScan.java
index 16d39e7..2fe0310 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteTableScan.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteTableScan.java
@@ -268,10 +268,10 @@ public class IgniteTableScan extends TableScan implements IgniteRel {
         if (igniteTbl.collations().isEmpty())
             return false;
 
-        if (igniteTbl.collations().size() > 1) {
+      /*  if (igniteTbl.collations().size() > 1) {
             throw new UnsupportedOperationException("At most one table collation is currently supported: " +
                 "[collations=" + igniteTbl.collations() + ", table=" + igniteTbl + ']');
-        }
+        }*/
         return true;
     }
 
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/logical/LogicalOrToUnionRule.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/logical/LogicalOrToUnionRule.java
index 4aff453..6bb6ed38 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/logical/LogicalOrToUnionRule.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/logical/LogicalOrToUnionRule.java
@@ -22,20 +22,27 @@ import java.util.List;
 import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.plan.RelOptRuleCall;
 import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.RelFactories;
 import org.apache.calcite.rel.logical.LogicalFilter;
 import org.apache.calcite.rel.logical.LogicalUnion;
 import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexBuilder;
 import org.apache.calcite.rex.RexCall;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.SqlKind;
 import org.apache.ignite.internal.processors.query.calcite.rule.RuleUtils;
 
+/**
+ *
+ */
 public class LogicalOrToUnionRule extends RelOptRule {
+    /** Instance. */
     public static final RelOptRule INSTANCE = new LogicalOrToUnionRule();
 
     /**
-     *
+     * Constructor.
      */
     public LogicalOrToUnionRule() {
         super(
@@ -45,46 +52,34 @@ public class LogicalOrToUnionRule extends RelOptRule {
 
     /** {@inheritDoc} */
     @Override public void onMatch(RelOptRuleCall call) {
-      /*
-      final LogicalFilter rel = call.rel(0);
+        final LogicalFilter rel = call.rel(0);
+        final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
+
+        RexNode dnf = RexUtil.toDnf(rexBuilder, rel.getCondition());
 
-        final RexNode dnf = RexUtil.toDnf(rel.getCluster().getRexBuilder(), rel.getCondition());
+        if (!dnf.isA(SqlKind.OR))
+            return;
 
-         final List<RexNode> operands = ((RexCall)dnf).getOperands();
+        List<RexNode> operands = ((RexCall)dnf).getOperands();
 
-        final RelNode input = rel.getInput(0);
+        if (operands.size() != 2)
+            return; // TODO: allow more than 2 operands.
 
-        final LogicalUnion union1 =
+        RelNode input = rel.getInput(0);
+
+        LogicalUnion union1 =
             LogicalUnion.create(Arrays.asList(
                 LogicalFilter.create(input, operands.get(0)),
-                LogicalFilter.create(input, RexUtil.andNot(rel.getCluster().getRexBuilder(), operands.get(1), operands.get(0)))), true);
+                LogicalFilter.create(input, RexUtil.andNot(rexBuilder, operands.get(1), operands.get(0)))), true);
 
-        final LogicalUnion union2 =
+        LogicalUnion union2 =
             LogicalUnion.create(Arrays.asList(
                 LogicalFilter.create(input, operands.get(1)),
-                LogicalFilter.create(input, RexUtil.andNot(rel.getCluster().getRexBuilder(), operands.get(0), operands.get(1)))), true);
-
-        RuleUtils.transformTo(call, Arrays.asList(union1, union2));
-        */
-
-        final LogicalFilter rel = call.rel(0);
-
-        final RexNode dnf = RexUtil.toDnf(rel.getCluster().getRexBuilder(), rel.getCondition());
-
-        final List<RexNode> operands = ((RexCall)dnf).getOperands();
-
-        final LogicalFilter filter1 = LogicalFilter.create(rel.getInput(), operands.get(0));
-        final LogicalFilter filter2 = LogicalFilter.create(rel.getInput(), operands.get(1));
-
-        final LogicalUnion union = LogicalUnion.create(
-            Arrays.asList(
-                filter1,
-                filter2), false);
-
-        System.out.println("Filter 1: "+filter1.getRowType());
-        System.out.println("Filter 2: "+filter2.getRowType());
-        System.out.println("Union: "+union.getRowType());
+                LogicalFilter.create(input, RexUtil.andNot(rexBuilder, operands.get(0), operands.get(1)))), true);
+//
+//        RuleUtils.transformTo(call, Arrays.asList(RelOptUtil.createCastRel(union1, rel.getRowType(), false),
+//            RelOptUtil.createCastRel(union2, rel.getRowType(), false)));
 
-        RuleUtils.transformTo(call, union);
+        RuleUtils.transformTo(call, RelOptUtil.createCastRel(union1, rel.getRowType(), false));
     }
 }
diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/rules/OrToUnionRuleTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/rules/OrToUnionRuleTest.java
index 504af4a..7fc3e8d 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/rules/OrToUnionRuleTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/rules/OrToUnionRuleTest.java
@@ -19,10 +19,11 @@ package org.apache.ignite.internal.processors.query.calcite.rules;
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import java.util.stream.Collectors;
 import org.apache.calcite.DataContext;
 import org.apache.calcite.config.CalciteConnectionConfig;
 import org.apache.calcite.linq4j.Enumerable;
@@ -35,6 +36,7 @@ import org.apache.calcite.plan.RelTraitDef;
 import org.apache.calcite.plan.RelTraitSet;
 import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelCollationTraitDef;
+import org.apache.calcite.rel.RelCollations;
 import org.apache.calcite.rel.RelDistribution;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.RelReferentialConstraint;
@@ -84,7 +86,7 @@ public class OrToUnionRuleTest extends GridCommonAbstractTest {
     public void setup() {
         nodes = new ArrayList<>(4);
 
-        for (int i = 0; i < 4; i++)
+        for (int i = 0; i < 1; i++)
             nodes.add(UUID.randomUUID());
     }
 
@@ -108,6 +110,10 @@ public class OrToUnionRuleTest extends GridCommonAbstractTest {
             }
         };
 
+        products.addIndex(new IgniteIndex(RelCollations.of(1), "IDX_CATEGORY", null, null));
+        products.addIndex(new IgniteIndex(RelCollations.of(2), "IDX_SUBCATEGORY", null, null));
+        products.addIndex(new IgniteIndex(RelCollations.of(3), "IDX_CATALOG_ID", null, null));
+
         IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
 
         publicSchema.addTable("PRODUCTS", products);
@@ -115,10 +121,10 @@ public class OrToUnionRuleTest extends GridCommonAbstractTest {
         SchemaPlus schema = createRootSchema(false)
             .add("PUBLIC", publicSchema);
 
-            String sql = "SELECT *" +
-                "FROM products " +
-                "WHERE category = 'Photo' " +
-                "OR subcategory ='Camera Media'";
+        String sql = "SELECT *" +
+            "FROM products " +
+            "WHERE category = 'Photo' " +
+            "OR subcategory ='Camera Media'";
 //                "WHERE (category = 'Photo' OR category = ?)" +
 //                "AND (subcategory ='Camera Media' OR subcategory = ?)";
 
@@ -166,7 +172,7 @@ public class OrToUnionRuleTest extends GridCommonAbstractTest {
             assertNotNull(rel);
             assertEquals("LogicalProject(ID=[$0], CATEGORY=[$1], SUBCATEGORY=[$2], CATALOG_ID=[$3])\n" +
                     "  LogicalFilter(condition=[OR(=(CAST($1):VARCHAR, 'Photo'), =(CAST($2):VARCHAR, 'Camera Media'))])\n" +
-                    "    IgniteTableScan(table=[[PUBLIC, PRODUCTS]], index=[PK], lower=[[]], upper=[[]], collation=[[]])\n",
+                    "    IgniteTableScan(table=[[PUBLIC, PRODUCTS]], index=[PK], lower=[[]], upper=[[]], collation=[[0]])\n",
                 RelOptUtil.toString(rel));
 
             // Transformation chain
@@ -190,20 +196,21 @@ public class OrToUnionRuleTest extends GridCommonAbstractTest {
         private final RelProtoDataType protoType;
 
         /** */
-        private final Map<String, IgniteIndex> indexes = new HashMap<>();
+        private final Map<String, IgniteIndex> indexes = new LinkedHashMap<>();
 
         /** */
         private TestTable(RelDataType type) {
             protoType = RelDataTypeImpl.proto(type);
 
-            addIndex(new IgniteIndex(null, "PK", null, this));
+            addIndex(new IgniteIndex(RelCollations.of(0), "PK", null, this));
         }
 
         /** {@inheritDoc} */
         @Override public RelNode toRel(RelOptTable.ToRelContext ctx, RelOptTable relOptTbl) {
             RelOptCluster cluster = ctx.getCluster();
+
             RelTraitSet traitSet = cluster.traitSetOf(IgniteConvention.INSTANCE)
-                .replaceIfs(RelCollationTraitDef.INSTANCE, this::collations)
+                .replaceIf(RelCollationTraitDef.INSTANCE, () -> getIndex("PK").collation())
                 .replaceIf(DistributionTraitDef.INSTANCE, this::distribution);
 
             return new IgniteTableScan(cluster, traitSet, relOptTbl, "PK", null);
@@ -211,11 +218,14 @@ public class OrToUnionRuleTest extends GridCommonAbstractTest {
 
         /** {@inheritDoc} */
         @Override public IgniteTableScan toRel(RelOptCluster cluster, RelOptTable relOptTbl, String idxName) {
+            if (getIndex(idxName) == null)
+                return null;
+
             RelTraitSet traitSet = cluster.traitSetOf(IgniteConvention.INSTANCE)
-                .replaceIfs(RelCollationTraitDef.INSTANCE, this::collations)
+                .replaceIf(RelCollationTraitDef.INSTANCE, () -> getIndex(idxName).collation())
                 .replaceIf(DistributionTraitDef.INSTANCE, this::distribution);
 
-            return new IgniteTableScan(cluster, traitSet, relOptTbl, "PK", null);
+            return new IgniteTableScan(cluster, traitSet, relOptTbl, idxName, null);
         }
 
         /** {@inheritDoc} */
@@ -263,7 +273,6 @@ public class OrToUnionRuleTest extends GridCommonAbstractTest {
             throw new AssertionError();
         }
 
-
         /** {@inheritDoc} */
         @Override public Schema.TableType getJdbcTableType() {
             throw new AssertionError();
@@ -292,7 +301,7 @@ public class OrToUnionRuleTest extends GridCommonAbstractTest {
 
         /** {@inheritDoc} */
         @Override public List<RelCollation> collations() {
-            return Collections.emptyList();
+            return indexes.values().stream().map(IgniteIndex::collation).collect(Collectors.toList());
         }
 
         /** {@inheritDoc} */