You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2015/01/03 01:23:56 UTC
incubator-calcite git commit: [CALCITE-548] Extend induce method to
return CUBE and ROLLUP (Jesus Camacho Rodriguez)
Repository: incubator-calcite
Updated Branches:
refs/heads/master dad74b97a -> 3b34d300d
[CALCITE-548] Extend induce method to return CUBE and ROLLUP (Jesus Camacho Rodriguez)
Close apache/incubator-calcite#38
Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/3b34d300
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/3b34d300
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/3b34d300
Branch: refs/heads/master
Commit: 3b34d300d516f5455a1c29f154c66e70e9d29455
Parents: dad74b9
Author: Jesus Camacho Rodriguez <jc...@hortonworks.com>
Authored: Wed Dec 24 14:47:54 2014 +0100
Committer: julianhyde <jh...@apache.org>
Committed: Fri Jan 2 15:46:00 2015 -0800
----------------------------------------------------------------------
.../org/apache/calcite/rel/core/Aggregate.java | 21 +++++
.../org/apache/calcite/test/CalciteSuite.java | 1 +
.../calcite/test/InduceGroupingTypeTest.java | 97 ++++++++++++++++++++
3 files changed, 119 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/3b34d300/core/src/main/java/org/apache/calcite/rel/core/Aggregate.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/core/Aggregate.java b/core/src/main/java/org/apache/calcite/rel/core/Aggregate.java
index b795eb8..bb27b50 100644
--- a/core/src/main/java/org/apache/calcite/rel/core/Aggregate.java
+++ b/core/src/main/java/org/apache/calcite/rel/core/Aggregate.java
@@ -45,6 +45,7 @@ import org.apache.calcite.util.Util;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
+import com.google.common.math.IntMath;
import java.util.List;
@@ -385,6 +386,26 @@ public abstract class Aggregate extends SingleRel {
if (groupSets.size() == 1 && groupSets.get(0).equals(groupSet)) {
return SIMPLE;
}
+ if (groupSets.size() == IntMath.pow(2, groupSet.cardinality())) {
+ return CUBE;
+ }
+ if (groupSets.size() == groupSet.cardinality() + 1) {
+ for (int i = groupSet.cardinality(); i >= 0; i--) {
+ ImmutableBitSet groupingSet =
+ groupSets.get(groupSet.cardinality() - i);
+ if (groupingSet.cardinality() != i) {
+ return OTHER;
+ }
+ for (int j = 0, pos = 0; j < i; j++) {
+ int nextPos = groupSet.nextSetBit(pos);
+ if (nextPos != groupingSet.nextSetBit(pos)) {
+ return OTHER;
+ }
+ pos = nextPos + 1;
+ }
+ }
+ return ROLLUP;
+ }
return OTHER;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/3b34d300/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 bedd425..d36e3a5 100644
--- a/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
+++ b/core/src/test/java/org/apache/calcite/test/CalciteSuite.java
@@ -83,6 +83,7 @@ import org.junit.runners.Suite;
BinarySearchTest.class,
EnumerablesTest.class,
ExceptionMessageTest.class,
+ InduceGroupingTypeTest.class,
// medium tests (above 0.1s)
SqlParserTest.class,
http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/3b34d300/core/src/test/java/org/apache/calcite/test/InduceGroupingTypeTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/InduceGroupingTypeTest.java b/core/src/test/java/org/apache/calcite/test/InduceGroupingTypeTest.java
new file mode 100644
index 0000000..41fd3f7
--- /dev/null
+++ b/core/src/test/java/org/apache/calcite/test/InduceGroupingTypeTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.test;
+
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import com.google.common.collect.Lists;
+
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit test for
+ * {@link org.apache.calcite.rel.core.Aggregate.Group#induce(ImmutableBitSet, List)}.
+ */
+public class InduceGroupingTypeTest {
+
+ @Test public void testInduceGroupingType() {
+ ImmutableBitSet groupSet = ImmutableBitSet.of(1, 2, 4, 5);
+
+ // SIMPLE
+ List<ImmutableBitSet> groupSets = Lists.newArrayList();
+ groupSets.add(groupSet);
+ assertEquals(Aggregate.Group.SIMPLE,
+ Aggregate.Group.induce(groupSet, groupSets));
+
+ // CUBE
+ groupSets = Lists.newArrayList(groupSet.powerSet());
+ assertEquals(Aggregate.Group.CUBE,
+ Aggregate.Group.induce(groupSet, groupSets));
+
+ // ROLLUP
+ groupSets = Lists.newArrayList();
+ groupSets.add(ImmutableBitSet.of(1, 2, 4, 5));
+ groupSets.add(ImmutableBitSet.of(1, 2, 4));
+ groupSets.add(ImmutableBitSet.of(1, 2));
+ groupSets.add(ImmutableBitSet.of(1));
+ groupSets.add(ImmutableBitSet.of());
+ assertEquals(Aggregate.Group.ROLLUP,
+ Aggregate.Group.induce(groupSet, groupSets));
+
+ // OTHER
+ groupSets = Lists.newArrayList();
+ groupSets.add(ImmutableBitSet.of(1, 2, 4, 5));
+ groupSets.add(ImmutableBitSet.of(1, 2, 4));
+ groupSets.add(ImmutableBitSet.of(1, 2));
+ groupSets.add(ImmutableBitSet.of());
+ assertEquals(Aggregate.Group.OTHER,
+ Aggregate.Group.induce(groupSet, groupSets));
+
+ groupSets = Lists.newArrayList();
+ groupSets.add(ImmutableBitSet.of(1, 2, 4, 5));
+ groupSets.add(ImmutableBitSet.of(1, 2, 4));
+ groupSets.add(ImmutableBitSet.of(1, 2));
+ groupSets.add(ImmutableBitSet.of(1));
+ assertEquals(Aggregate.Group.OTHER,
+ Aggregate.Group.induce(groupSet, groupSets));
+
+ groupSets = Lists.newArrayList();
+ groupSets.add(ImmutableBitSet.of(1, 2, 5));
+ groupSets.add(ImmutableBitSet.of(1, 2, 4));
+ groupSets.add(ImmutableBitSet.of(1, 2));
+ groupSets.add(ImmutableBitSet.of(1));
+ groupSets.add(ImmutableBitSet.of());
+ assertEquals(Aggregate.Group.OTHER,
+ Aggregate.Group.induce(groupSet, groupSets));
+
+ groupSets = Lists.newArrayList();
+ assertEquals(Aggregate.Group.OTHER,
+ Aggregate.Group.induce(groupSet, groupSets));
+
+ groupSets = Lists.newArrayList();
+ groupSets.add(ImmutableBitSet.of());
+ assertEquals(Aggregate.Group.OTHER,
+ Aggregate.Group.induce(groupSet, groupSets));
+ }
+}
+
+// End InduceGroupingTypeTest.java