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

hyuan 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 af976e9  [CALCITE-3993] Add utility methods to RelTrait, RelTraitSet 
and RelCollation
af976e9 is described below

commit af976e9d4caa9588db8e413a58468760f3c1c0d6
Author: Haisheng Yuan <[email protected]>
AuthorDate: Tue May 12 23:17:40 2020 -0500

    [CALCITE-3993] Add utility methods to RelTrait, RelTraitSet and RelCollation
    
    Close #1973
---
 .../java/org/apache/calcite/plan/RelTrait.java     | 24 ++++++++-----
 .../java/org/apache/calcite/plan/RelTraitSet.java  | 42 ++++++++++++++++++----
 .../org/apache/calcite/plan/volcano/RelSet.java    |  2 +-
 .../java/org/apache/calcite/rel/RelCollation.java  | 17 ++++++++-
 4 files changed, 68 insertions(+), 17 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/plan/RelTrait.java 
b/core/src/main/java/org/apache/calcite/plan/RelTrait.java
index aa98827..37440a2 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelTrait.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelTrait.java
@@ -93,16 +93,17 @@ public interface RelTrait {
   /**
    * Applies a mapping to this trait.
    *
-   * <p>Some traits may be changed if the columns order is changed by a 
mapping of the
-   * {@link Project} operator. </p>
+   * <p>Some traits may be changed if the columns order is changed by a mapping
+   * of the {@link Project} operator. </p>
    *
-   * <p>For example, if relation {@code SELECT a, b ORDER BY a, b} is sorted 
by columns [0, 1],
-   * then the project {@code SELECT b, a} over this relation will be sorted by 
columns [1, 0].
-   * In the same time project {@code SELECT b} will not be sorted at all 
because it doesn't
-   * contain the collation prefix and this method will return an empty 
collation. </p>
+   * <p>For example, if relation {@code SELECT a, b ORDER BY a, b} is sorted by
+   * columns [0, 1], then the project {@code SELECT b, a} over this relation
+   * will be sorted by columns [1, 0]. In the same time project {@code SELECT 
b}
+   * will not be sorted at all because it doesn't contain the collation
+   * prefix and this method will return an empty collation. </p>
    *
-   * <p>Other traits are independent from the columns remapping. For example 
{@link Convention} or
-   * {@link RelDistributions#SINGLETON}.</p>
+   * <p>Other traits are independent from the columns remapping. For example
+   * {@link Convention} or {@link RelDistributions#SINGLETON}.</p>
    *
    * @param mapping   Mapping
    * @return trait with mapping applied
@@ -110,4 +111,11 @@ public interface RelTrait {
   default <T extends RelTrait> T apply(Mappings.TargetMapping mapping) {
     return (T) this;
   }
+
+  /**
+   * Returns whether this trait is the default trait value.
+   */
+  default boolean isDefault() {
+    return this == getTraitDef().getDefault();
+  }
 }
diff --git a/core/src/main/java/org/apache/calcite/plan/RelTraitSet.java 
b/core/src/main/java/org/apache/calcite/plan/RelTraitSet.java
index 09fb10a..c521968 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelTraitSet.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelTraitSet.java
@@ -43,6 +43,8 @@ public final class RelTraitSet extends AbstractList<RelTrait> 
{
   private final Cache cache;
   private final RelTrait[] traits;
   private final String string;
+  /** Cache the hash code for the traits */
+  private int hash = 0;
 
   //~ Constructors -----------------------------------------------------------
 
@@ -308,7 +310,8 @@ public final class RelTraitSet extends 
AbstractList<RelTrait> {
       if (traits[i].getTraitDef() == ConventionTraitDef.INSTANCE) {
         continue;
       }
-      if (!traits[i].equals(other.traits[i])) {
+      // each trait should be canonized already
+      if (traits[i] != other.traits[i]) {
         return false;
       }
     }
@@ -417,13 +420,34 @@ public final class RelTraitSet extends 
AbstractList<RelTrait> {
    * @return true if traits are equal and in the same order, false otherwise
    */
   @Override public boolean equals(Object obj) {
-    return this == obj
-        || obj instanceof RelTraitSet
-        && Arrays.equals(traits, ((RelTraitSet) obj).traits);
+    if (this == obj) {
+      return true;
+    }
+    if (!(obj instanceof RelTraitSet)) {
+      return false;
+    }
+    RelTraitSet that = (RelTraitSet) obj;
+    if (this.hash != 0
+        && that.hash != 0
+        && this.hash != that.hash) {
+      return false;
+    }
+    if (traits.length != that.traits.length) {
+      return false;
+    }
+    for (int i = 0; i < traits.length; i++) {
+      if (traits[i] != that.traits[i]) {
+        return false;
+      }
+    }
+    return true;
   }
 
   @Override public int hashCode() {
-    return Arrays.hashCode(traits);
+    if (hash == 0) {
+      hash = Arrays.hashCode(traits);
+    }
+    return hash;
   }
 
   /**
@@ -690,8 +714,12 @@ public final class RelTraitSet extends 
AbstractList<RelTrait> {
       if (traitSet1 != null) {
         return traitSet1;
       }
-      final RelTraitSet traitSet =
-          new RelTraitSet(this, traits.toArray(new RelTrait[0]));
+      final RelTraitSet traitSet;
+      if (traits instanceof RelTraitSet) {
+        traitSet = (RelTraitSet) traits;
+      } else {
+        traitSet = new RelTraitSet(this, traits.toArray(new RelTrait[0]));
+      }
       map.put(traits, traitSet);
       return traitSet;
     }
diff --git a/core/src/main/java/org/apache/calcite/plan/volcano/RelSet.java 
b/core/src/main/java/org/apache/calcite/plan/volcano/RelSet.java
index 04fa187..4aeec2a 100644
--- a/core/src/main/java/org/apache/calcite/plan/volcano/RelSet.java
+++ b/core/src/main/java/org/apache/calcite/plan/volcano/RelSet.java
@@ -159,7 +159,7 @@ class RelSet {
 
   public RelSubset getSubset(RelTraitSet traits) {
     for (RelSubset subset : subsets) {
-      if (subset.getTraitSet().equals(traits)) {
+      if (subset.getTraitSet() == traits) {
         return subset;
       }
     }
diff --git a/core/src/main/java/org/apache/calcite/rel/RelCollation.java 
b/core/src/main/java/org/apache/calcite/rel/RelCollation.java
index 811d12c..2cf5156 100644
--- a/core/src/main/java/org/apache/calcite/rel/RelCollation.java
+++ b/core/src/main/java/org/apache/calcite/rel/RelCollation.java
@@ -17,8 +17,10 @@
 package org.apache.calcite.rel;
 
 import org.apache.calcite.plan.RelMultipleTrait;
+import org.apache.calcite.util.ImmutableIntList;
 
 import java.util.List;
+import javax.annotation.Nonnull;
 
 /**
  * Description of the physical ordering of a relational expression.
@@ -32,5 +34,18 @@ public interface RelCollation extends RelMultipleTrait {
   /**
    * Returns the ordinals and directions of the columns in this ordering.
    */
-  List<RelFieldCollation> getFieldCollations();
+  @Nonnull List<RelFieldCollation> getFieldCollations();
+
+  /**
+   * Returns the ordinals of the key columns.
+   */
+  default @Nonnull List<Integer> getKeys() {
+    final List<RelFieldCollation> collations = getFieldCollations();
+    final int size = collations.size();
+    final int[] keys = new int[size];
+    for (int i = 0; i < size; i++) {
+      keys[i] = collations.get(i).getFieldIndex();
+    }
+    return ImmutableIntList.of(keys);
+  }
 }

Reply via email to