Repository: calcite
Updated Branches:
  refs/heads/master c3215241e -> b31da2246


Canonize simple cases for composite traits in trait factory

This avoids creating distinct subsets for cases like "empty collation trait"


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/b31da224
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/b31da224
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/b31da224

Branch: refs/heads/master
Commit: b31da22465e355cf7a6f3c5f9de9bf9ff0190a4b
Parents: c321524
Author: Vladimir Sitnikov <sitnikov.vladi...@gmail.com>
Authored: Mon Sep 24 19:33:59 2018 +0300
Committer: Vladimir Sitnikov <sitnikov.vladi...@gmail.com>
Committed: Tue Sep 25 15:56:01 2018 +0300

----------------------------------------------------------------------
 .../apache/calcite/plan/RelCompositeTrait.java  | 22 ++-----
 .../org/apache/calcite/plan/RelTraitTest.java   | 62 ++++++++++++++++++++
 .../org/apache/calcite/test/CalciteSuite.java   |  2 +
 .../calcite/test/MaterializationTest.java       |  2 +-
 .../org/apache/calcite/test/QuidemTest.java     |  3 +
 core/src/test/resources/sql/misc.iq             |  2 +-
 6 files changed, 73 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/b31da224/core/src/main/java/org/apache/calcite/plan/RelCompositeTrait.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/plan/RelCompositeTrait.java 
b/core/src/main/java/org/apache/calcite/plan/RelCompositeTrait.java
index a66bf7b..892088e 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelCompositeTrait.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelCompositeTrait.java
@@ -52,11 +52,13 @@ class RelCompositeTrait<T extends RelMultipleTrait> 
implements RelTrait {
 
   /** Creates a RelCompositeTrait. The constituent traits are canonized. */
   @SuppressWarnings("unchecked")
-  static <T extends RelMultipleTrait> RelCompositeTrait<T> of(RelTraitDef def,
+  static <T extends RelMultipleTrait> RelTrait of(RelTraitDef def,
       List<T> traitList) {
     final RelCompositeTrait<T> compositeTrait;
     if (traitList.isEmpty()) {
-      compositeTrait = new EmptyCompositeTrait<>(def);
+      return def.getDefault();
+    } else if (traitList.size() == 1) {
+      return def.canonize(traitList.get(0));
     } else {
       final RelMultipleTrait[] traits =
           traitList.toArray(new RelMultipleTrait[0]);
@@ -130,22 +132,6 @@ class RelCompositeTrait<T extends RelMultipleTrait> 
implements RelTrait {
   public int size() {
     return traits.length;
   }
-
-  /** Composite trait with 0 elements.
-   *
-   * @param <T> trait type */
-  private static class EmptyCompositeTrait<T extends RelMultipleTrait>
-      extends RelCompositeTrait<T> {
-    private EmptyCompositeTrait(RelTraitDef traitDef) {
-      //noinspection unchecked
-      super(traitDef, (T[]) new RelMultipleTrait[0]);
-    }
-
-    @Override public boolean satisfies(RelTrait trait) {
-      //noinspection unchecked
-      return ((T) trait).isTop();
-    }
-  }
 }
 
 // End RelCompositeTrait.java

http://git-wip-us.apache.org/repos/asf/calcite/blob/b31da224/core/src/test/java/org/apache/calcite/plan/RelTraitTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/plan/RelTraitTest.java 
b/core/src/test/java/org/apache/calcite/plan/RelTraitTest.java
new file mode 100644
index 0000000..043a5ab
--- /dev/null
+++ b/core/src/test/java/org/apache/calcite/plan/RelTraitTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.calcite.plan;
+
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollationTraitDef;
+import org.apache.calcite.rel.RelCollations;
+
+import com.google.common.collect.ImmutableList;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.function.Supplier;
+
+/**
+ * Test to verify {@link RelCompositeTrait}.
+ */
+public class RelTraitTest {
+  private static final RelCollationTraitDef COLLATION = 
RelCollationTraitDef.INSTANCE;
+
+  private void assertCanonical(String message, Supplier<List<RelCollation>> 
collation) {
+    RelTrait trait1 = RelCompositeTrait.of(COLLATION, collation.get());
+    RelTrait trait2 = RelCompositeTrait.of(COLLATION, collation.get());
+
+    Assert.assertEquals(
+        "RelCompositeTrait.of should return the same instance for " + message,
+        trait1 + " @" + Integer.toHexString(System.identityHashCode(trait1)),
+        trait2 + " @" + Integer.toHexString(System.identityHashCode(trait2)));
+  }
+
+  @Test public void compositeEmpty() {
+    assertCanonical("empty composite", ImmutableList::of);
+  }
+
+  @Test public void compositeOne() {
+    assertCanonical("composite with one element",
+        () -> ImmutableList.of(RelCollations.of(ImmutableList.of())));
+  }
+
+  @Test public void compositeTwo() {
+    assertCanonical("composite with two elements",
+        () -> ImmutableList.of(RelCollations.of(0), RelCollations.of(1)));
+  }
+}
+
+// End RelTraitTest.java

http://git-wip-us.apache.org/repos/asf/calcite/blob/b31da224/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/CalciteSuite.java 
b/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
index 27c7b1b..4d3e67d 100644
--- a/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
+++ b/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
@@ -21,6 +21,7 @@ import org.apache.calcite.adapter.clone.ArrayTableTest;
 import org.apache.calcite.jdbc.CalciteRemoteDriverTest;
 import org.apache.calcite.plan.RelOptPlanReaderTest;
 import org.apache.calcite.plan.RelOptUtilTest;
+import org.apache.calcite.plan.RelTraitTest;
 import org.apache.calcite.plan.RelWriterTest;
 import org.apache.calcite.plan.volcano.CollationConversionTest;
 import org.apache.calcite.plan.volcano.ComboRuleTest;
@@ -105,6 +106,7 @@ import org.junit.runners.Suite;
     InterpreterTest.class,
     TestUtilTest.class,
     VolcanoPlannerTest.class,
+    RelTraitTest.class,
     HepPlannerTest.class,
     TraitPropagationTest.class,
     RelDistributionTest.class,

http://git-wip-us.apache.org/repos/asf/calcite/blob/b31da224/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/calcite/test/MaterializationTest.java 
b/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
index 1c95b8f..4b465f1 100644
--- a/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
+++ b/core/src/test/java/org/apache/calcite/test/MaterializationTest.java
@@ -545,7 +545,7 @@ public class MaterializationTest {
         HR_FKUK_MODEL,
         CalciteAssert.checkResultContains(
             "EnumerableCalc(expr#0..1=[{inputs}], expr#2=[1], "
-                + "expr#3=[+($t1, $t2)], $f0=[$t3], deptno=[$t0])\n"
+                + "expr#3=[+($t1, $t2)], C=[$t3], deptno=[$t0])\n"
                 + "  EnumerableAggregate(group=[{1}], agg#0=[$SUM0($2)])\n"
                 + "    EnumerableTableScan(table=[[hr, m0]])"));
   }

http://git-wip-us.apache.org/repos/asf/calcite/blob/b31da224/core/src/test/java/org/apache/calcite/test/QuidemTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/QuidemTest.java 
b/core/src/test/java/org/apache/calcite/test/QuidemTest.java
index 202b8c1..12e5d2e 100644
--- a/core/src/test/java/org/apache/calcite/test/QuidemTest.java
+++ b/core/src/test/java/org/apache/calcite/test/QuidemTest.java
@@ -200,6 +200,9 @@ public abstract class QuidemTest {
         if (cause instanceof Exception) {
           throw (Exception) cause;
         }
+        if (cause instanceof Error) {
+          throw (Error) cause;
+        }
         throw e;
       }
     } else {

http://git-wip-us.apache.org/repos/asf/calcite/blob/b31da224/core/src/test/resources/sql/misc.iq
----------------------------------------------------------------------
diff --git a/core/src/test/resources/sql/misc.iq 
b/core/src/test/resources/sql/misc.iq
index 51c99ba..0862a54 100644
--- a/core/src/test/resources/sql/misc.iq
+++ b/core/src/test/resources/sql/misc.iq
@@ -683,7 +683,7 @@ from "sales_fact_1997" as s
   join "product_class" as pc on p."product_class_id" = pc."product_class_id"
 where c."city" = 'San Francisco'
  and pc."product_department" = 'Snacks';
-EnumerableCalc(expr#0..56=[{inputs}], product_id0=[$t20], time_id=[$t21], 
customer_id=[$t22], promotion_id=[$t23], store_id=[$t24], store_sales=[$t25], 
store_cost=[$t26], unit_sales=[$t27], customer_id0=[$t28], account_num=[$t29], 
lname=[$t30], fname=[$t31], mi=[$t32], address1=[$t33], address2=[$t34], 
address3=[$t35], address4=[$t36], city=[$t37], state_province=[$t38], 
postal_code=[$t39], country=[$t40], customer_region_id=[$t41], phone1=[$t42], 
phone2=[$t43], birthdate=[$t44], marital_status=[$t45], yearly_income=[$t46], 
gender=[$t47], total_children=[$t48], num_children_at_home=[$t49], 
education=[$t50], date_accnt_opened=[$t51], member_card=[$t52], 
occupation=[$t53], houseowner=[$t54], num_cars_owned=[$t55], fullname=[$t56], 
product_class_id0=[$t5], product_id=[$t6], brand_name=[$t7], 
product_name=[$t8], SKU=[$t9], SRP=[$t10], gross_weight=[$t11], 
net_weight=[$t12], recyclable_package=[$t13], low_fat=[$t14], 
units_per_case=[$t15], cases_per_pallet=[$t16], shelf_width=[$t17], she
 lf_height=[$t18], shelf_depth=[$t19], product_class_id=[$t0], 
product_subcategory=[$t1], product_category=[$t2], product_department=[$t3], 
product_family=[$t4])
+EnumerableCalc(expr#0..56=[{inputs}], product_id=[$t20], time_id=[$t21], 
customer_id=[$t22], promotion_id=[$t23], store_id=[$t24], store_sales=[$t25], 
store_cost=[$t26], unit_sales=[$t27], customer_id0=[$t28], account_num=[$t29], 
lname=[$t30], fname=[$t31], mi=[$t32], address1=[$t33], address2=[$t34], 
address3=[$t35], address4=[$t36], city=[$t37], state_province=[$t38], 
postal_code=[$t39], country=[$t40], customer_region_id=[$t41], phone1=[$t42], 
phone2=[$t43], birthdate=[$t44], marital_status=[$t45], yearly_income=[$t46], 
gender=[$t47], total_children=[$t48], num_children_at_home=[$t49], 
education=[$t50], date_accnt_opened=[$t51], member_card=[$t52], 
occupation=[$t53], houseowner=[$t54], num_cars_owned=[$t55], fullname=[$t56], 
product_class_id=[$t5], product_id0=[$t6], brand_name=[$t7], 
product_name=[$t8], SKU=[$t9], SRP=[$t10], gross_weight=[$t11], 
net_weight=[$t12], recyclable_package=[$t13], low_fat=[$t14], 
units_per_case=[$t15], cases_per_pallet=[$t16], shelf_width=[$t17], shel
 f_height=[$t18], shelf_depth=[$t19], product_class_id0=[$t0], 
product_subcategory=[$t1], product_category=[$t2], product_department=[$t3], 
product_family=[$t4])
   EnumerableJoin(condition=[=($6, $20)], joinType=[inner])
     EnumerableJoin(condition=[=($0, $5)], joinType=[inner])
       EnumerableCalc(expr#0..4=[{inputs}], expr#5=['Snacks'], expr#6=[=($t3, 
$t5)], proj#0..4=[{exprs}], $condition=[$t6])

Reply via email to