private int[][] EvaluateChildNodes(GroupByRollupEvalContext context)
        {
            var size = ChildNodes.Count;
            var childIndexes = new int[size][];
            for (var i = 0; i < size; i++) {
                IList<int[]> childIndex = ChildNodes[i].Evaluate(context);
                if (childIndex.Count != 1) {
                    throw new IllegalStateException();
                }

                childIndexes[i] = childIndex[0];
            }

            return childIndexes;
        }
        public override IList<int[]> Evaluate(GroupByRollupEvalContext context)
        {
            var result = new int[expressions.Count];
            for (var i = 0; i < expressions.Count; i++) {
                int index = context.GetIndex(expressions[i]);
                result[i] = index;
            }

            Collections.SortInPlace(result);

            // find dups
            for (var i = 0; i < result.Length - 1; i++) {
                if (result[i] == result[i + 1]) {
                    throw new GroupByRollupDuplicateException(new[] {result[i]});
                }
            }

            return Collections.SingletonList(result);
        }
Example #3
0
        public override IList<int[]> Evaluate(GroupByRollupEvalContext context)
        {
            IList<int[]> rollup = new List<int[]>();
            foreach (var node in ChildNodes) {
                var result = node.Evaluate(context);

                // find dups
                foreach (var row in result) {
                    foreach (var existing in rollup) {
                        if (CompatExtensions.AreEqual(row, existing)) {
                            throw new GroupByRollupDuplicateException(row);
                        }
                    }
                }

                rollup.AddAll(result);
            }

            return rollup;
        }
        public override IList<int[]> Evaluate(GroupByRollupEvalContext context)
        {
            var childIndexes = EvaluateChildNodes(context);

            // find duplicate entries among child expressions
            for (var i = 0; i < childIndexes.Length; i++) {
                for (var j = i + 1; j < childIndexes.Length; j++) {
                    ValidateCompare(childIndexes[i], childIndexes[j]);
                }
            }

            IList<int[]> rollup;
            if (cube) {
                rollup = HandleCube(childIndexes);
            }
            else {
                rollup = HandleRollup(childIndexes);
            }

            rollup.Add(new int[0]);
            return rollup;
        }
        public static GroupByClauseExpressions GetGroupByRollupExpressions(
            IList<GroupByClauseElement> groupByElements,
            SelectClauseSpecRaw selectClauseSpec,
            ExprNode optionalHavingNode,
            IList<OrderByItem> orderByList,
            ExpressionCopier expressionCopier)
        {
            if (groupByElements == null || groupByElements.Count == 0) {
                return null;
            }

            // walk group-by-elements, determine group-by expressions and rollup nodes
            var groupByExpressionInfo = GroupByToRollupNodes(groupByElements);

            // obtain expression nodes, collect unique nodes and assign index
            IList<ExprNode> distinctGroupByExpressions = new List<ExprNode>();
            IDictionary<ExprNode, int> expressionToIndex = new Dictionary<ExprNode, int>();
            foreach (var exprNode in groupByExpressionInfo.Expressions) {
                var found = false;
                for (var i = 0; i < distinctGroupByExpressions.Count; i++) {
                    ExprNode other = distinctGroupByExpressions[i];
                    // find same expression
                    if (ExprNodeUtilityCompare.DeepEquals(exprNode, other, false)) {
                        expressionToIndex.Put(exprNode, i);
                        found = true;
                        break;
                    }
                }

                // not seen before
                if (!found) {
                    expressionToIndex.Put(exprNode, distinctGroupByExpressions.Count);
                    distinctGroupByExpressions.Add(exprNode);
                }
            }

            // determine rollup, validate it is either (not both)
            var hasGroupingSet = false;
            var hasRollup = false;
            foreach (var element in groupByElements) {
                if (element is GroupByClauseElementGroupingSet) {
                    hasGroupingSet = true;
                }

                if (element is GroupByClauseElementRollupOrCube) {
                    hasRollup = true;
                }
            }

            // no-rollup or grouping-sets means simply validate
            ExprNode[] groupByExpressions = distinctGroupByExpressions.ToArray();
            if (!hasRollup && !hasGroupingSet) {
                return new GroupByClauseExpressions(groupByExpressions);
            }

            // evaluate rollup node roots
            var nodes = groupByExpressionInfo.Nodes;
            var perNodeCombinations = new object[nodes.Count][];
            var context = new GroupByRollupEvalContext(expressionToIndex);
            try {
                for (var i = 0; i < nodes.Count; i++) {
                    GroupByRollupNodeBase node = nodes[i];
                    var combinations = node.Evaluate(context);
                    perNodeCombinations[i] = new object[combinations.Count];
                    for (var j = 0; j < combinations.Count; j++) {
                        perNodeCombinations[i][j] = combinations[j];
                    }
                }
            }
            catch (GroupByRollupDuplicateException ex) {
                if (ex.Indexes.Length == 0) {
                    throw new ExprValidationException(
                        "Failed to validate the group-by clause, found duplicate specification of the overall grouping '()'");
                }

                var writer = new StringWriter();
                var delimiter = "";
                for (var i = 0; i < ex.Indexes.Length; i++) {
                    writer.Write(delimiter);
                    writer.Write(
                        ExprNodeUtilityPrint.ToExpressionStringMinPrecedenceSafe(groupByExpressions[ex.Indexes[i]]));
                    delimiter = ", ";
                }

                throw new ExprValidationException(
                    "Failed to validate the group-by clause, found duplicate specification of expressions (" +
                    writer +
                    ")");
            }

            // enumerate combinations building an index list
            var combinationEnumeration = new CombinationEnumeration(perNodeCombinations);
            var combination = new SortedSet<int>();
            var indexList = new LinkedHashSet<MultiKeyArrayInt>();
            while (combinationEnumeration.MoveNext()) {
                combination.Clear();
                object[] combinationOA = combinationEnumeration.Current;
                foreach (var indexes in combinationOA) {
                    var indexarr = (int[]) indexes;
                    foreach (var anIndex in indexarr) {
                        combination.Add(anIndex);
                    }
                }

                var indexArr = CollectionUtil.IntArray(combination);
                indexList.Add(new MultiKeyArrayInt(indexArr));
            }

            // obtain rollup levels
            var rollupLevels = new int[indexList.Count][];
            var count = 0;
            foreach (var mk in indexList) {
                rollupLevels[count++] = mk.Keys;
            }

            var numberOfLevels = rollupLevels.Length;
            if (numberOfLevels == 1 && rollupLevels[0].Length == 0) {
                throw new ExprValidationException(
                    "Failed to validate the group-by clause, the overall grouping '()' cannot be the only grouping");
            }

            // obtain select-expression copies for rewrite
            var expressions = selectClauseSpec.SelectExprList;
            var selects = new ExprNode[numberOfLevels][];
            for (var i = 0; i < numberOfLevels; i++) {
                selects[i] = new ExprNode[expressions.Count];
                for (var j = 0; j < expressions.Count; j++) {
                    SelectClauseElementRaw selectRaw = expressions[j];
                    if (!(selectRaw is SelectClauseExprRawSpec)) {
                        throw new ExprValidationException(
                            "Group-by with rollup requires that the select-clause does not use wildcard");
                    }

                    var compiled = (SelectClauseExprRawSpec) selectRaw;
                    selects[i][j] = CopyVisitExpression(compiled.SelectExpression, expressionCopier);
                }
            }

            // obtain having-expression copies for rewrite
            ExprNode[] optHavingNodeCopy = null;
            if (optionalHavingNode != null) {
                optHavingNodeCopy = new ExprNode[numberOfLevels];
                for (var i = 0; i < numberOfLevels; i++) {
                    optHavingNodeCopy[i] = CopyVisitExpression(optionalHavingNode, expressionCopier);
                }
            }

            // obtain orderby-expression copies for rewrite
            ExprNode[][] optOrderByCopy = null;
            if (orderByList != null && orderByList.Count > 0) {
                optOrderByCopy = new ExprNode[numberOfLevels][];
                for (var i = 0; i < numberOfLevels; i++) {
                    optOrderByCopy[i] = new ExprNode[orderByList.Count];
                    for (var j = 0; j < orderByList.Count; j++) {
                        OrderByItem element = orderByList[j];
                        optOrderByCopy[i][j] = CopyVisitExpression(element.ExprNode, expressionCopier);
                    }
                }
            }

            return new GroupByClauseExpressions(
                groupByExpressions,
                rollupLevels,
                selects,
                optHavingNodeCopy,
                optOrderByCopy);
        }
 public override IList<int[]> Evaluate(GroupByRollupEvalContext context)
 {
     int index = context.GetIndex(expression);
     return Collections.SingletonList(new[] {index});
 }
Example #7
0
 public abstract IList<int[]> Evaluate(GroupByRollupEvalContext context);