KYLIN-2718 use Guava Library to check overflow when calculating combination
Signed-off-by: Li Yang <liy...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/kylin/repo Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/228b34aa Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/228b34aa Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/228b34aa Branch: refs/heads/pr70 Commit: 228b34aa10f3f89f9024bc2368cbe11a59894d28 Parents: 713e9f7 Author: Zhong <nju_y...@apache.org> Authored: Sat Sep 23 15:11:49 2017 +0800 Committer: Li Yang <liy...@apache.org> Committed: Sat Sep 23 15:12:09 2017 +0800 ---------------------------------------------------------------------- .../kylin/cube/model/AggregationGroup.java | 73 +++++++++++--------- .../org/apache/kylin/cube/model/CubeDesc.java | 2 - .../cube/model/TooManyCuboidException.java | 1 + 3 files changed, 41 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kylin/blob/228b34aa/core-cube/src/main/java/org/apache/kylin/cube/model/AggregationGroup.java ---------------------------------------------------------------------- diff --git a/core-cube/src/main/java/org/apache/kylin/cube/model/AggregationGroup.java b/core-cube/src/main/java/org/apache/kylin/cube/model/AggregationGroup.java index d473858..74ac715 100644 --- a/core-cube/src/main/java/org/apache/kylin/cube/model/AggregationGroup.java +++ b/core-cube/src/main/java/org/apache/kylin/cube/model/AggregationGroup.java @@ -41,6 +41,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.math.LongMath; @SuppressWarnings("serial") @JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE) @@ -300,42 +301,48 @@ public class AggregationGroup implements Serializable { public long calculateCuboidCombination() { long combination = 1; - if (this.getDimCap() > 0) { - CuboidScheduler cuboidScheduler = cubeDesc.getInitialCuboidScheduler(); - combination = cuboidScheduler.calculateCuboidsForAggGroup(this).size(); - } else { - Set<String> includeDims = new TreeSet<>(Arrays.asList(includes)); - Set<String> mandatoryDims = new TreeSet<>(Arrays.asList(selectRule.mandatoryDims)); - - Set<String> hierarchyDims = new TreeSet<>(); - for (String[] ss : selectRule.hierarchyDims) { - hierarchyDims.addAll(Arrays.asList(ss)); - combination = combination * (ss.length + 1); - } - - Set<String> jointDims = new TreeSet<>(); - for (String[] ss : selectRule.jointDims) { - jointDims.addAll(Arrays.asList(ss)); - combination = combination * 2; - } - - Set<String> normalDims = new TreeSet<>(); - normalDims.addAll(includeDims); - normalDims.removeAll(mandatoryDims); - normalDims.removeAll(hierarchyDims); - normalDims.removeAll(jointDims); - - combination = combination * (1L << normalDims.size()); - - if (cubeDesc.getConfig().getCubeAggrGroupIsMandatoryOnlyValid() && !mandatoryDims.isEmpty()) { - combination += 1; + try { + if (this.getDimCap() > 0) { + CuboidScheduler cuboidScheduler = cubeDesc.getInitialCuboidScheduler(); + combination = cuboidScheduler.calculateCuboidsForAggGroup(this).size(); + } else { + Set<String> includeDims = new TreeSet<>(Arrays.asList(includes)); + Set<String> mandatoryDims = new TreeSet<>(Arrays.asList(selectRule.mandatoryDims)); + + Set<String> hierarchyDims = new TreeSet<>(); + for (String[] ss : selectRule.hierarchyDims) { + hierarchyDims.addAll(Arrays.asList(ss)); + combination = LongMath.checkedMultiply(combination, (ss.length + 1)); + } + + Set<String> jointDims = new TreeSet<>(); + for (String[] ss : selectRule.jointDims) { + jointDims.addAll(Arrays.asList(ss)); + } + combination = LongMath.checkedMultiply(combination, (1L << selectRule.jointDims.length)); + + Set<String> normalDims = new TreeSet<>(); + normalDims.addAll(includeDims); + normalDims.removeAll(mandatoryDims); + normalDims.removeAll(hierarchyDims); + normalDims.removeAll(jointDims); + + combination = LongMath.checkedMultiply(combination, (1L << normalDims.size())); + + if (cubeDesc.getConfig().getCubeAggrGroupIsMandatoryOnlyValid() && !mandatoryDims.isEmpty()) { + combination += 1; + } + combination -= 1; // not include cuboid 0 } - combination -= 1; // not include cuboid 0 + + if (combination < 0) + throw new ArithmeticException(); + + } catch (ArithmeticException ae) { + // long overflow, give max value + combination = Long.MAX_VALUE; } - if (combination < 0) { // overflow - combination = Long.MAX_VALUE - 1; - } return combination; } http://git-wip-us.apache.org/repos/asf/kylin/blob/228b34aa/core-cube/src/main/java/org/apache/kylin/cube/model/CubeDesc.java ---------------------------------------------------------------------- diff --git a/core-cube/src/main/java/org/apache/kylin/cube/model/CubeDesc.java b/core-cube/src/main/java/org/apache/kylin/cube/model/CubeDesc.java index 2a902f3..712af43 100644 --- a/core-cube/src/main/java/org/apache/kylin/cube/model/CubeDesc.java +++ b/core-cube/src/main/java/org/apache/kylin/cube/model/CubeDesc.java @@ -660,8 +660,6 @@ public class CubeDesc extends RootPersistentEntity implements IEngineAware { + ". Use 'mandatory'/'hierarchy'/'joint' to optimize; or update 'kylin.cube.aggrgroup.max-combination' to a bigger value."; throw new TooManyCuboidException(msg); } - } catch (TooManyCuboidException tmce) { - throw tmce; } catch (Exception e) { throw new IllegalStateException("Unknown error while calculating cuboid number for " + // "Aggregation group " + index + " of Cube Desc " + this.name, e); http://git-wip-us.apache.org/repos/asf/kylin/blob/228b34aa/core-cube/src/main/java/org/apache/kylin/cube/model/TooManyCuboidException.java ---------------------------------------------------------------------- diff --git a/core-cube/src/main/java/org/apache/kylin/cube/model/TooManyCuboidException.java b/core-cube/src/main/java/org/apache/kylin/cube/model/TooManyCuboidException.java index cfa41e4..57ea6ed 100644 --- a/core-cube/src/main/java/org/apache/kylin/cube/model/TooManyCuboidException.java +++ b/core-cube/src/main/java/org/apache/kylin/cube/model/TooManyCuboidException.java @@ -17,6 +17,7 @@ */ package org.apache.kylin.cube.model; +@SuppressWarnings("serial") public class TooManyCuboidException extends RuntimeException { public TooManyCuboidException(String message) {