Repository: calcite Updated Branches: refs/heads/master d7c4627c7 -> 9c5ebe340
[CALCITE-1990] Make RelDistribution extend RelMultipleTrait (LeoWangLZ) Close apache/calcite#541 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/9c5ebe34 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/9c5ebe34 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/9c5ebe34 Branch: refs/heads/master Commit: 9c5ebe340ac45d2d71f3dd3cde391d04ca19a771 Parents: d7c4627 Author: LeoWangLZ <[email protected]> Authored: Fri Sep 22 10:15:34 2017 +0800 Committer: Julian Hyde <[email protected]> Committed: Sat Nov 4 16:28:01 2017 -0700 ---------------------------------------------------------------------- .../org/apache/calcite/rel/RelDistribution.java | 4 +- .../apache/calcite/rel/RelDistributions.java | 18 +++++++ .../apache/calcite/rel/RelDistributionTest.java | 53 ++++++++++++++++++++ .../org/apache/calcite/test/CalciteSuite.java | 2 + 4 files changed, 75 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/9c5ebe34/core/src/main/java/org/apache/calcite/rel/RelDistribution.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/rel/RelDistribution.java b/core/src/main/java/org/apache/calcite/rel/RelDistribution.java index 16f3f7c..22b722e 100644 --- a/core/src/main/java/org/apache/calcite/rel/RelDistribution.java +++ b/core/src/main/java/org/apache/calcite/rel/RelDistribution.java @@ -16,7 +16,7 @@ */ package org.apache.calcite.rel; -import org.apache.calcite.plan.RelTrait; +import org.apache.calcite.plan.RelMultipleTrait; import org.apache.calcite.util.mapping.Mappings; import java.util.List; @@ -35,7 +35,7 @@ import javax.annotation.Nonnull; * registered with the planner that are not trait-defs. * </ul> */ -public interface RelDistribution extends RelTrait { +public interface RelDistribution extends RelMultipleTrait { /** Returns the type of distribution. */ @Nonnull Type getType(); http://git-wip-us.apache.org/repos/asf/calcite/blob/9c5ebe34/core/src/main/java/org/apache/calcite/rel/RelDistributions.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/rel/RelDistributions.java b/core/src/main/java/org/apache/calcite/rel/RelDistributions.java index f739da8..3c5dc4a 100644 --- a/core/src/main/java/org/apache/calcite/rel/RelDistributions.java +++ b/core/src/main/java/org/apache/calcite/rel/RelDistributions.java @@ -16,6 +16,7 @@ */ package org.apache.calcite.rel; +import org.apache.calcite.plan.RelMultipleTrait; import org.apache.calcite.plan.RelOptPlanner; import org.apache.calcite.plan.RelTrait; import org.apache.calcite.util.ImmutableIntList; @@ -82,6 +83,8 @@ public class RelDistributions { /** Implementation of {@link org.apache.calcite.rel.RelDistribution}. */ private static class RelDistributionImpl implements RelDistribution { + private static final Ordering<Iterable<Integer>> ORDERING = + Ordering.<Integer>natural().lexicographical(); private final Type type; private final ImmutableIntList keys; @@ -170,6 +173,21 @@ public class RelDistributions { public void register(RelOptPlanner planner) { } + + @Override public boolean isTop() { + return type == Type.ANY; + } + + @Override public int compareTo(@Nonnull RelMultipleTrait o) { + final RelDistribution distribution = (RelDistribution) o; + if (type == distribution.getType() + && (type == Type.HASH_DISTRIBUTED + || type == Type.RANGE_DISTRIBUTED)) { + return ORDERING.compare(getKeys(), distribution.getKeys()); + } + + return type.compareTo(distribution.getType()); + } } } http://git-wip-us.apache.org/repos/asf/calcite/blob/9c5ebe34/core/src/test/java/org/apache/calcite/rel/RelDistributionTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/rel/RelDistributionTest.java b/core/src/test/java/org/apache/calcite/rel/RelDistributionTest.java new file mode 100644 index 0000000..1621855 --- /dev/null +++ b/core/src/test/java/org/apache/calcite/rel/RelDistributionTest.java @@ -0,0 +1,53 @@ +/* + * 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.rel; + +import org.apache.calcite.plan.RelTraitSet; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link RelDistribution}. + */ +public class RelDistributionTest { + @Test public void testRelDistributionSatisfy() { + RelDistribution distribution1 = RelDistributions.hash(ImmutableList.of(0)); + RelDistribution distribution2 = RelDistributions.hash(ImmutableList.of(1)); + + RelTraitSet traitSet = RelTraitSet.createEmpty(); + RelTraitSet simpleTrait1 = traitSet.plus(distribution1); + RelTraitSet simpleTrait2 = traitSet.plus(distribution2); + RelTraitSet compositeTrait = + traitSet.replace(RelDistributionTraitDef.INSTANCE, + ImmutableList.of(distribution1, distribution2)); + + assertThat(compositeTrait.satisfies(simpleTrait1), is(true)); + assertThat(compositeTrait.satisfies(simpleTrait2), is(true)); + + assertThat(distribution1.compareTo(distribution2), is(-1)); + assertThat(distribution2.compareTo(distribution1), is(1)); + //noinspection EqualsWithItself + assertThat(distribution2.compareTo(distribution2), is(0)); + } +} + +// End RelDistributionTest.java http://git-wip-us.apache.org/repos/asf/calcite/blob/9c5ebe34/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 4f9b075..b6f2b9f 100644 --- a/core/src/test/java/org/apache/calcite/test/CalciteSuite.java +++ b/core/src/test/java/org/apache/calcite/test/CalciteSuite.java @@ -29,6 +29,7 @@ import org.apache.calcite.plan.volcano.VolcanoPlannerTest; import org.apache.calcite.plan.volcano.VolcanoPlannerTraitTest; import org.apache.calcite.prepare.LookupOperatorOverloadsTest; import org.apache.calcite.rel.RelCollationTest; +import org.apache.calcite.rel.RelDistributionTest; import org.apache.calcite.rel.rel2sql.RelToSqlConverterTest; import org.apache.calcite.rel.rules.DateRangeRulesTest; import org.apache.calcite.rex.RexBuilderTest; @@ -99,6 +100,7 @@ import org.junit.runners.Suite; VolcanoPlannerTest.class, HepPlannerTest.class, TraitPropagationTest.class, + RelDistributionTest.class, RelWriterTest.class, RexProgramTest.class, SqlOperatorBindingTest.class,
