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

vladimirsitnikov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new 92978d9  [CALCITE-2764] RelCompositeTrait#satisfies does not work for 
non-trivial traits
92978d9 is described below

commit 92978d981e6ca2eb352c1654d14c7b64fb8710fd
Author: Vladimir Sitnikov <[email protected]>
AuthorDate: Thu Jan 3 18:04:03 2019 +0300

    [CALCITE-2764] RelCompositeTrait#satisfies does not work for non-trivial 
traits
---
 .../org/apache/calcite/plan/RelCompositeTrait.java | 15 ++++++
 .../java/org/apache/calcite/plan/RelTraitTest.java | 56 ++++++++++++++++++++++
 2 files changed, 71 insertions(+)

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 e3555b8..d1190e4 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelCompositeTrait.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelCompositeTrait.java
@@ -89,6 +89,21 @@ class RelCompositeTrait<T extends RelMultipleTrait> 
implements RelTrait {
   }
 
   public boolean satisfies(RelTrait trait) {
+    if (trait instanceof RelCompositeTrait) {
+      if (equals(trait)) {
+        return true;
+      }
+      //noinspection unchecked
+      RelCompositeTrait<T> other = (RelCompositeTrait<T>) trait;
+      // This trait should be the same or stricter, so this trait should 
satisfy each component
+      for (T t : other.traits) {
+        if (!satisfies(t)) {
+          return false;
+        }
+      }
+      return true;
+    }
+    // This trait might be stricter, so at least one component should satisfy 
given trait
     for (T t : traits) {
       if (t.satisfies(trait)) {
         return true;
diff --git a/core/src/test/java/org/apache/calcite/plan/RelTraitTest.java 
b/core/src/test/java/org/apache/calcite/plan/RelTraitTest.java
index 043a5ab..78fccd2 100644
--- a/core/src/test/java/org/apache/calcite/plan/RelTraitTest.java
+++ b/core/src/test/java/org/apache/calcite/plan/RelTraitTest.java
@@ -19,6 +19,7 @@ package org.apache.calcite.plan;
 import org.apache.calcite.rel.RelCollation;
 import org.apache.calcite.rel.RelCollationTraitDef;
 import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelFieldCollation;
 
 import com.google.common.collect.ImmutableList;
 
@@ -57,6 +58,61 @@ public class RelTraitTest {
     assertCanonical("composite with two elements",
         () -> ImmutableList.of(RelCollations.of(0), RelCollations.of(1)));
   }
+
+  private void assertSatisfies(RelTrait a, RelTrait b) {
+    Assert.assertTrue(a + ".satisfies(" + b + ")", a.satisfies(b));
+    if (!a.equals(b)) {
+      // a should be "the same or stricter" than b, so b cannot be stricter 
than a at the same time
+      Assert.assertTrue(b + ".NOTsatisfies(" + a + ")", !b.satisfies(a));
+    }
+  }
+
+  /**
+   * Tests for {@link RelCompositeTrait#satisfies(RelTrait)}.
+   */
+  @Test public void compositeSatisfies() {
+    //noinspection unchecked
+    RelCompositeTrait<RelCollation> abc_bc_c = (RelCompositeTrait) 
RelCompositeTrait.of(COLLATION,
+        ImmutableList.of(
+            RelCollations.of(
+                new RelFieldCollation(0),
+                new RelFieldCollation(1),
+                new RelFieldCollation(2)),
+            RelCollations.of(
+                new RelFieldCollation(1),
+                new RelFieldCollation(2)),
+            RelCollations.of(
+                new RelFieldCollation(2))));
+
+    // A composite trait must satisfy its sub-traits
+    for (RelCollation collation : abc_bc_c.traitList()) {
+      assertSatisfies(abc_bc_c, collation);
+    }
+
+    // A trait must satisfy to itself
+    assertSatisfies(abc_bc_c, abc_bc_c);
+
+    //noinspection unchecked
+    RelCompositeTrait<RelCollation> bc_c = (RelCompositeTrait) 
RelCompositeTrait.of(COLLATION,
+        ImmutableList.of(
+            RelCollations.of(
+                new RelFieldCollation(1),
+                new RelFieldCollation(2)),
+            RelCollations.of(
+                new RelFieldCollation(2))));
+
+    assertSatisfies(abc_bc_c, bc_c);
+
+    //noinspection unchecked
+    RelCompositeTrait<RelCollation> b_c = (RelCompositeTrait) 
RelCompositeTrait.of(COLLATION,
+        ImmutableList.of(
+            RelCollations.of(
+                new RelFieldCollation(1)),
+            RelCollations.of(
+                new RelFieldCollation(2))));
+
+    assertSatisfies(abc_bc_c, b_c);
+  }
 }
 
 // End RelTraitTest.java

Reply via email to