Exemple #1
0
        public override bool Equals(object o)
        {
            if (this == o) return true;
            if (o == null || GetType() != o.GetType()) return false;

            AggregationStateKeyWStream that = (AggregationStateKeyWStream) o;

            if (streamNum != that.streamNum) return false;
            if (stateType != that.stateType) return false;
            if (!ExprNodeUtilityCompare.DeepEquals(criteraExprNodes, that.criteraExprNodes, false)) return false;
            if (eventType != null) {
                if (that.eventType == null) {
                    return false;
                }

                if (!EventTypeUtility.IsTypeOrSubTypeOf(that.eventType, eventType)) return false;
            }

            if (filterExprNode == null) {
                return that.filterExprNode == null;
            }

            return that.filterExprNode != null &&
                   ExprNodeUtilityCompare.DeepEquals(filterExprNode, that.filterExprNode, false);
        }
Exemple #2
0
        public override bool EqualsNode(
            ExprNode node,
            bool ignoreStreamPrefix)
        {
            if (!(node is ExprAppDotMethodImpl)) {
                return false;
            }

            var other = (ExprAppDotMethodImpl) node;
            if (!Desc.LhsName.Equals(other.Desc.LhsName)) {
                return false;
            }

            if (!Desc.DotMethodName.Equals(other.Desc.DotMethodName)) {
                return false;
            }

            if (!Desc.RhsName.Equals(other.Desc.RhsName)) {
                return false;
            }

            if (!ExprNodeUtilityCompare.DeepEquals(Desc.Lhs, other.Desc.Lhs, false)) {
                return false;
            }

            return ExprNodeUtilityCompare.DeepEquals(Desc.Rhs, other.Desc.Rhs, false);
        }
Exemple #3
0
        public void AddCustom(
            ExprNode[] indexExpressions,
            string operationName,
            int expressionPosition,
            ExprNode expression)
        {
            // find existing custom-entry for same index expressions
            QueryGraphValueEntryCustomForge found = null;
            foreach (var desc in Items) {
                if (desc.Entry is QueryGraphValueEntryCustomForge) {
                    if (ExprNodeUtilityCompare.DeepEquals(desc.IndexExprs, indexExpressions, true)) {
                        found = (QueryGraphValueEntryCustomForge) desc.Entry;
                        break;
                    }
                }
            }

            if (found == null) {
                found = new QueryGraphValueEntryCustomForge();
                Items.Add(new QueryGraphValueDescForge(indexExpressions, found));
            }

            // find/create operation against the indexed fields
            var key = new QueryGraphValueEntryCustomKeyForge(operationName, indexExpressions);
            QueryGraphValueEntryCustomOperationForge op = found.Operations.Get(key);
            if (op == null) {
                op = new QueryGraphValueEntryCustomOperationForge();
                found.Operations.Put(key, op);
            }

            op.PositionalExpressions.Put(expressionPosition, expression);
        }
        public bool EqualsFilter(FilterSpecPlanPathTripletForge other)
        {
            if (!ExprNodeUtilityCompare.DeepEqualsNullChecked(TripletConfirm, other.TripletConfirm, true)) {
                return false;
            }

            return Param.Equals(other.Param);
        }
Exemple #5
0
        private QueryGraphValueDescForge FindEntry(ExprNode[] search)
        {
            foreach (var desc in Items) {
                if (ExprNodeUtilityCompare.DeepEquals(search, desc.IndexExprs, true)) {
                    return desc;
                }
            }

            return null;
        }
        private static void CompareTableLookupPlan(
            int streamNum,
            int numInstruction,
            TableLookupPlanForge expectedPlan,
            TableLookupPlanForge actualPlan,
            IDictionary<TableLookupIndexReqKey, TableLookupIndexReqKey> indexNameMapping)
        {
            var message = "Failed at stream " + streamNum + " and instruction " + numInstruction;
            Assert.AreEqual(expectedPlan.IndexedStream, actualPlan.IndexedStream, message);
            Assert.AreEqual(expectedPlan.LookupStream, actualPlan.LookupStream, message);
            Assert.AreEqual(expectedPlan.GetType().FullName, actualPlan.GetType().FullName, message);

            // assert index mapping
            Assert.AreEqual(expectedPlan.IndexNum.Length, actualPlan.IndexNum.Length, message);
            for (var i = 0; i < expectedPlan.IndexNum.Length; i++) {
                var expectedIndexKey = expectedPlan.IndexNum[i];
                var actualIndexKey = actualPlan.IndexNum[i];
                Assert.AreEqual(expectedIndexKey, indexNameMapping.Get(actualIndexKey), message);
            }

            if (expectedPlan is FullTableScanLookupPlanForge && actualPlan is FullTableScanLookupPlanForge) {
            }
            else if (expectedPlan is IndexedTableLookupPlanHashedOnlyForge &&
                     actualPlan is IndexedTableLookupPlanHashedOnlyForge) {
                var singleActual = (IndexedTableLookupPlanHashedOnlyForge) actualPlan;
                var singleExpected = (IndexedTableLookupPlanHashedOnlyForge) expectedPlan;
                CompareIndexDesc(singleExpected.KeyDescriptor, singleActual.KeyDescriptor);
            }
            else if (expectedPlan is InKeywordTableLookupPlanMultiIdxForge &&
                     actualPlan is InKeywordTableLookupPlanMultiIdxForge) {
                var inExpected = (InKeywordTableLookupPlanMultiIdxForge) expectedPlan;
                var inActual = (InKeywordTableLookupPlanMultiIdxForge) actualPlan;
                Assert.IsTrue(ExprNodeUtilityCompare.DeepEquals(inExpected.KeyExpr, inActual.KeyExpr, false));
            }
            else if (expectedPlan is InKeywordTableLookupPlanSingleIdxForge &&
                     actualPlan is InKeywordTableLookupPlanSingleIdxForge) {
                var inExpected = (InKeywordTableLookupPlanSingleIdxForge) expectedPlan;
                var inActual = (InKeywordTableLookupPlanSingleIdxForge) actualPlan;
                Assert.IsTrue(ExprNodeUtilityCompare.DeepEquals(inExpected.Expressions, inActual.Expressions, false));
            }
            else if (expectedPlan is SortedTableLookupPlanForge && actualPlan is SortedTableLookupPlanForge) {
                var inExpected = (SortedTableLookupPlanForge) expectedPlan;
                var inActual = (SortedTableLookupPlanForge) actualPlan;
                Assert.AreEqual(inExpected.LookupStream, inActual.LookupStream);
                Assert.IsTrue(
                    ExprNodeUtilityCompare.DeepEquals(
                        inExpected.RangeKeyPair.Expressions,
                        inActual.RangeKeyPair.Expressions,
                        false));
            }
            else {
                Assert.Fail("Failed to compare plan for stream " + streamNum + ", found type " + actualPlan.GetType());
            }
        }
Exemple #7
0
        public override bool EqualsNode(
            ExprNode node,
            bool ignoreStreamPrefix)
        {
            if (!(node is ExprDeclaredNodeImpl)) {
                return false;
            }

            var otherExprCaseNode = (ExprDeclaredNodeImpl) node;
            return ExprNodeUtilityCompare.DeepEquals(ExpressionBodyCopy, otherExprCaseNode, false);
        }
Exemple #8
0
        private static void AddEquivalent(
            ExprAggregateNode aggNodeToAdd,
            IList<AggregationServiceAggExpressionDesc> equivalencyList,
            bool intoTableNonRollup)
        {
            // Check any same aggregation nodes among all aggregation clauses
            var foundEquivalent = false;
            foreach (var existing in equivalencyList) {
                var aggNode = existing.AggregationNode;

                // we have equivalence when:
                // (a) equals on node returns true
                // (b) positional parameters are the same
                // (c) non-positional (group-by over, if present, are the same ignoring duplicates)
                if (!aggNode.EqualsNode(aggNodeToAdd, false)) {
                    continue;
                }

                if (!ExprNodeUtilityCompare.DeepEquals(
                    aggNode.PositionalParams,
                    aggNodeToAdd.PositionalParams,
                    false)) {
                    continue;
                }

                if (!ExprNodeUtilityCompare.DeepEqualsNullChecked(
                    aggNode.OptionalFilter,
                    aggNodeToAdd.OptionalFilter,
                    false)) {
                    continue;
                }

                if (aggNode.OptionalLocalGroupBy != null || aggNodeToAdd.OptionalLocalGroupBy != null) {
                    if (aggNode.OptionalLocalGroupBy == null && aggNodeToAdd.OptionalLocalGroupBy != null ||
                        aggNode.OptionalLocalGroupBy != null && aggNodeToAdd.OptionalLocalGroupBy == null) {
                        continue;
                    }

                    if (!ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(
                        aggNode.OptionalLocalGroupBy.PartitionExpressions,
                        aggNodeToAdd.OptionalLocalGroupBy.PartitionExpressions)) {
                        continue;
                    }
                }

                existing.AddEquivalent(aggNodeToAdd);
                foundEquivalent = true;
                break;
            }

            if (!foundEquivalent || intoTableNonRollup) {
                equivalencyList.Add(new AggregationServiceAggExpressionDesc(aggNodeToAdd, aggNodeToAdd.Factory));
            }
        }
Exemple #9
0
        protected override bool EqualsNodeInternal(ExprTableAccessNode other)
        {
            var that = (ExprTableAccessNodeSubpropAccessor) other;
            if (!SubpropName.Equals(that.SubpropName)) {
                return false;
            }

            return ExprNodeUtilityCompare.DeepEquals(
                aggregateAccessMultiValueNode,
                that.aggregateAccessMultiValueNode,
                false);
        }
Exemple #10
0
        private static IList<AggregationServiceAggExpressionDesc> FindPartition(
            IList<AggregationGroupByLocalGroupLevel> partitions,
            ExprNode[] partitionExpressions)
        {
            foreach (var level in partitions) {
                if (ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(level.PartitionExpr, partitionExpressions)) {
                    return level.Expressions;
                }
            }

            return null;
        }
Exemple #11
0
        public override bool Equals(object obj)
        {
            if (this == obj) {
                return true;
            }

            if (!(obj is FilterForEvalConstRuntimeExprForge)) {
                return false;
            }

            var other = (FilterForEvalConstRuntimeExprForge) obj;
            return ExprNodeUtilityCompare.DeepEquals(other._runtimeConstant, _runtimeConstant, true);
        }
Exemple #12
0
        public override bool Equals(object o)
        {
            if (this == o) {
                return true;
            }

            if (o == null || GetType() != o.GetType()) {
                return false;
            }

            var that = (ChainableArray) o;
            return EqualsChainable(that) && ExprNodeUtilityCompare.DeepEquals(Indexes, that.Indexes);
        }
Exemple #13
0
        private static AggregationGroupByLocalGroupDesc AnalyzeLocalGroupBy(
            IList<AggregationServiceAggExpressionDesc> aggregations,
            ExprNode[] groupByNodes,
            AggregationGroupByRollupDescForge groupByRollupDesc,
            IntoTableSpec intoTableSpec)
        {
            var hasOver = false;
            foreach (var desc in aggregations) {
                if (desc.AggregationNode.OptionalLocalGroupBy != null) {
                    hasOver = true;
                    break;
                }
            }

            if (!hasOver) {
                return null;
            }

            if (groupByRollupDesc != null) {
                throw new ExprValidationException("Roll-up and group-by parameters cannot be combined");
            }

            if (intoTableSpec != null) {
                throw new ExprValidationException("Into-table and group-by parameters cannot be combined");
            }

            IList<AggregationGroupByLocalGroupLevel> partitions = new List<AggregationGroupByLocalGroupLevel>();
            foreach (var desc in aggregations) {
                var localGroupBy = desc.AggregationNode.OptionalLocalGroupBy;

                var partitionExpressions = localGroupBy == null ? groupByNodes : localGroupBy.PartitionExpressions;
                var found = FindPartition(partitions, partitionExpressions);
                if (found == null) {
                    found = new List<AggregationServiceAggExpressionDesc>();
                    var level = new AggregationGroupByLocalGroupLevel(partitionExpressions, found);
                    partitions.Add(level);
                }

                found.Add(desc);
            }

            // check single group-by partition and it matches the group-by clause
            if (partitions.Count == 1 &&
                ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(partitions[0].PartitionExpr, groupByNodes)) {
                return null;
            }

            return new AggregationGroupByLocalGroupDesc(aggregations.Count, partitions.ToArray());
        }
Exemple #14
0
        public override bool Equals(object o)
        {
            if (this == o) {
                return true;
            }

            if (o == null || GetType() != o.GetType()) {
                return false;
            }

            var that = (ChainableCall) o;
            return EqualsChainable(that) 
                   && Name.Equals(that.Name)
                   && ExprNodeUtilityCompare.DeepEquals(Parameters, that.Parameters);
        }
Exemple #15
0
        private static void VerifyGroups(IList<ViewFactoryForge> forges)
        {
            GroupByViewFactoryForge group = null;
            MergeViewFactoryForge merge = null;
            var numDataWindows = 0;
            foreach (var forge in forges) {
                if (forge is GroupByViewFactoryForge) {
                    if (group == null) {
                        group = (GroupByViewFactoryForge) forge;
                    }
                    else {
                        throw new ViewProcessingException("Multiple groupwin-declarations are not supported");
                    }
                }

                if (forge is MergeViewFactoryForge) {
                    if (merge == null) {
                        merge = (MergeViewFactoryForge) forge;
                    }
                    else {
                        throw new ViewProcessingException("Multiple merge-declarations are not supported");
                    }
                }

                numDataWindows += forge is DataWindowViewForge ? 1 : 0;
            }

            if (group != null && group != forges[0]) {
                throw new ViewProcessingException("The 'groupwin' declaration must occur in the first position");
            }

            if (merge != null) {
                if (numDataWindows > 1) {
                    throw new ViewProcessingException(
                        "The 'merge' declaration cannot be used in conjunction with multiple data windows");
                }

                if (group == null) {
                    throw new ViewProcessingException(
                        "The 'merge' declaration cannot be used in without a 'group' declaration");
                }

                if (!ExprNodeUtilityCompare.DeepEquals(group.ViewParameters, merge.ViewParameters)) {
                    throw new ViewProcessingException("Mismatching parameters between 'group' and 'merge'");
                }
            }
        }
Exemple #16
0
        public override bool Equals(object o)
        {
            if (this == o) {
                return true;
            }

            if (o == null || GetType() != o.GetType()) {
                return false;
            }

            var that = (ScriptCodegenFieldSharable) o;
            if (!(ExprNodeUtilityCompare.DeepEquals(_parameters, that._parameters, false))) {
                return false;
            }
            
            return _scriptName.Equals(that._scriptName);
        }
        public override bool Equals(object o)
        {
            if (this == o) {
                return true;
            }

            if (o == null || GetType() != o.GetType()) {
                return false;
            }

            var that = (QueryGraphValueEntryCustomKeyForge) o;

            if (!OperationName.Equals(that.OperationName)) {
                return false;
            }

            return ExprNodeUtilityCompare.DeepEquals(ExprNodes, that.ExprNodes, true);
        }
        private static bool IsExprExistsInAllEqualsChildNodes(
            ExprNode[] childNodes,
            ExprNode search)
        {
            foreach (var child in childNodes) {
                var lhs = child.ChildNodes[0];
                var rhs = child.ChildNodes[1];
                if (!ExprNodeUtilityCompare.DeepEquals(lhs, search, false) && !ExprNodeUtilityCompare.DeepEquals(rhs, search, false)) {
                    return false;
                }

                if (ExprNodeUtilityCompare.DeepEquals(lhs, rhs, false)) {
                    return false;
                }
            }

            return true;
        }
Exemple #19
0
        public bool EqualsFilter(FilterSpecPlanPathForge other)
        {
            if (Triplets.Length != other.Triplets.Length) {
                return false;
            }

            for (var i = 0; i < Triplets.Length; i++) {
                var mytriplet = Triplets[i];
                var othertriplet = other.Triplets[i];
                if (!mytriplet.EqualsFilter(othertriplet)) {
                    return false;
                }
            }

            if (!ExprNodeUtilityCompare.DeepEqualsNullChecked(PathNegate, other.PathNegate, true)) {
                return false;
            }

            return true;
        }
Exemple #20
0
        public bool EqualsFilter(FilterSpecPlanForge other)
        {
            if (Paths.Length != other.Paths.Length) {
                return false;
            }

            for (var i = 0; i < Paths.Length; i++) {
                var myPath = Paths[i];
                var otherPath = other.Paths[i];
                if (!myPath.EqualsFilter(otherPath)) {
                    return false;
                }
            }

            if (!ExprNodeUtilityCompare.DeepEqualsNullChecked(FilterConfirm, other.FilterConfirm, true)) {
                return false;
            }

            return ExprNodeUtilityCompare.DeepEqualsNullChecked(FilterNegate, other.FilterNegate, true);
        }
Exemple #21
0
        public override bool EqualsNode(
            ExprNode node,
            bool ignoreStreamPrefix)
        {
            if (this == node) {
                return true;
            }

            if (node == null || GetType() != node.GetType()) {
                return false;
            }

            var that = (ExprNodeScript) node;

            if (!Script?.Equals(that.Script) ?? that.Script != null) {
                return false;
            }

            return ExprNodeUtilityCompare.DeepEquals(Parameters, that.Parameters);
        }
        private static void ValidateOrderByAggregates(
            IList<ExprAggregateNode> selectAggNodes,
            IList<ExprAggregateNode> orderAggNodes)
        {
            // Check that the order-by clause doesn't contain
            // any aggregate functions not in the select expression
            foreach (var orderAgg in orderAggNodes) {
                var inSelect = false;
                foreach (var selectAgg in selectAggNodes) {
                    if (ExprNodeUtilityCompare.DeepEquals(selectAgg, orderAgg, false)) {
                        inSelect = true;
                        break;
                    }
                }

                if (!inSelect) {
                    throw new ExprValidationException(
                        "Aggregate functions in the order-by clause must also occur in the select expression");
                }
            }
        }
Exemple #23
0
        public override bool Equals(object otherObject)
        {
            if (otherObject == this) {
                return true;
            }

            if (otherObject == null) {
                return false;
            }

            if (GetType() != otherObject.GetType()) {
                return false;
            }

            var other = (ObjectSpec) otherObject;
            if (!ObjectName.Equals(other.ObjectName)) {
                return false;
            }

            if (ObjectParameters.Count != other.ObjectParameters.Count) {
                return false;
            }

            // Compare object parameter by object parameter
            var index = 0;
            foreach (var thisParam in ObjectParameters) {
                var otherParam = other.ObjectParameters[index];
                index++;

                if (!ExprNodeUtilityCompare.DeepEquals(thisParam, otherParam, false)) {
                    return false;
                }
            }

            return true;
        }
        private static AggregationMFIdentifier FindExisting(
            Deque<AggregationMFIdentifier> accessProviderSlots,
            AggregationMultiFunctionStateKey providerKey,
            ExprAggregateLocalGroupByDesc optionalOver,
            ExprNode[] groupByNodes)
        {
            foreach (var ident in accessProviderSlots) {
                if (!providerKey.Equals(ident.AggregationStateKey)) {
                    continue;
                }

                // if there is no local-group by, but there is group-by-clause, and the ident-over matches, use that
                if (optionalOver == null &&
                    groupByNodes.Length > 0 &&
                    ident.OptionalLocalGroupBy != null &&
                    ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(
                        groupByNodes,
                        ident.OptionalLocalGroupBy.PartitionExpressions)) {
                    return ident;
                }

                if (optionalOver == null && ident.OptionalLocalGroupBy == null) {
                    return ident;
                }

                if (optionalOver != null &&
                    ident.OptionalLocalGroupBy != null &&
                    ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(
                        optionalOver.PartitionExpressions,
                        ident.OptionalLocalGroupBy.PartitionExpressions)) {
                    return ident;
                }
            }

            return null;
        }
        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 static ExprNode RewriteOrToInIfApplicable(
            ExprNode constituent,
            bool rewriteRegardlessOfLookupable)
        {
            if (!(constituent is ExprOrNode) || constituent.ChildNodes.Length < 2) {
                return constituent;
            }

            // check eligibility
            var childNodes = constituent.ChildNodes;
            foreach (var child in childNodes) {
                if (!(child is ExprEqualsNode)) {
                    return constituent;
                }

                var equalsNode = (ExprEqualsNode) child;
                if (equalsNode.IsIs || equalsNode.IsNotEquals) {
                    return constituent;
                }
            }

            // find common-expression node
            ExprNode commonExpressionNode;
            var lhs = childNodes[0].ChildNodes[0];
            var rhs = childNodes[0].ChildNodes[1];
            if (ExprNodeUtilityCompare.DeepEquals(lhs, rhs, false)) {
                return constituent;
            }

            if (IsExprExistsInAllEqualsChildNodes(childNodes, lhs)) {
                commonExpressionNode = lhs;
            }
            else if (IsExprExistsInAllEqualsChildNodes(childNodes, rhs)) {
                commonExpressionNode = rhs;
            }
            else {
                return constituent;
            }

            // if the common expression doesn't reference an event property, no need to rewrite
            if (!rewriteRegardlessOfLookupable) {
                var lookupableVisitor = new FilterSpecExprNodeVisitorLookupableLimitedExpr();
                commonExpressionNode.Accept(lookupableVisitor);
                if (!lookupableVisitor.HasStreamZeroReference || !lookupableVisitor.IsLimited) {
                    return constituent;
                }
            }

            // build node
            var @in = new ExprInNodeImpl(false);
            @in.AddChildNode(commonExpressionNode);
            for (var i = 0; i < constituent.ChildNodes.Length; i++) {
                var child = constituent.ChildNodes[i];
                var nodeindex = ExprNodeUtilityCompare.DeepEquals(commonExpressionNode, childNodes[i].ChildNodes[0], false) ? 1 : 0;
                @in.AddChildNode(child.ChildNodes[nodeindex]);
            }

            // validate
            try {
                @in.ValidateWithoutContext();
            }
            catch (ExprValidationException) {
                return constituent;
            }

            return @in;
        }
Exemple #27
0
        public static AggregationLocalGroupByPlanForge Analyze(
            ExprForge[][] methodForges,
            AggregationForgeFactory[] methodFactories,
            AggregationStateFactoryForge[] accessAggregations,
            AggregationGroupByLocalGroupDesc localGroupDesc,
            ExprNode[] groupByExpressions,
            AggregationAccessorSlotPairForge[] accessors,
            ImportService importService,
            bool fireAndForget,
            string statementName)
        {
            if (groupByExpressions == null) {
                groupByExpressions = ExprNodeUtilityQuery.EMPTY_EXPR_ARRAY;
            }

            var columns = new AggregationLocalGroupByColumnForge[localGroupDesc.NumColumns];
            IList<AggregationLocalGroupByLevelForge> levelsList = new List<AggregationLocalGroupByLevelForge>();
            AggregationLocalGroupByLevelForge optionalTopLevel = null;

            // determine optional top level (level number is -1)
            for (var i = 0; i < localGroupDesc.Levels.Length; i++) {
                var levelDesc = localGroupDesc.Levels[i];
                if (levelDesc.PartitionExpr.Length == 0) {
                    optionalTopLevel = GetLevel(
                        -1,
                        levelDesc,
                        methodForges,
                        methodFactories,
                        accessAggregations,
                        columns,
                        groupByExpressions.Length == 0,
                        accessors,
                        importService,
                        fireAndForget,
                        statementName);
                }
            }

            // determine default (same as group-by) level, if any, assign level number 0
            var levelNumber = 0;
            for (var i = 0; i < localGroupDesc.Levels.Length; i++) {
                var levelDesc = localGroupDesc.Levels[i];
                if (levelDesc.PartitionExpr.Length == 0) {
                    continue;
                }

                var isDefaultLevel = groupByExpressions != null &&
                                     ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(
                                         groupByExpressions,
                                         levelDesc.PartitionExpr);
                if (isDefaultLevel) {
                    var level = GetLevel(
                        0,
                        levelDesc,
                        methodForges,
                        methodFactories,
                        accessAggregations,
                        columns,
                        isDefaultLevel,
                        accessors,
                        importService,
                        fireAndForget,
                        statementName);
                    levelsList.Add(level);
                    levelNumber++;
                    break;
                }
            }

            // determine all other levels, assign level numbers
            for (var i = 0; i < localGroupDesc.Levels.Length; i++) {
                var levelDesc = localGroupDesc.Levels[i];
                if (levelDesc.PartitionExpr.Length == 0) {
                    continue;
                }

                var isDefaultLevel = groupByExpressions != null &&
                                     ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(
                                         groupByExpressions,
                                         levelDesc.PartitionExpr);
                if (isDefaultLevel) {
                    continue;
                }

                var level = GetLevel(
                    levelNumber,
                    levelDesc,
                    methodForges,
                    methodFactories,
                    accessAggregations,
                    columns,
                    isDefaultLevel,
                    accessors,
                    importService,
                    fireAndForget,
                    statementName);
                levelsList.Add(level);
                levelNumber++;
            }

            // totals
            var numMethods = 0;
            var numAccesses = 0;
            if (optionalTopLevel != null) {
                numMethods += optionalTopLevel.MethodFactories.Length;
                numAccesses += optionalTopLevel.AccessStateForges.Length;
            }

            foreach (var level in levelsList) {
                numMethods += level.MethodFactories.Length;
                numAccesses += level.AccessStateForges.Length;
            }

            AggregationLocalGroupByLevelForge[] levels = levelsList.ToArray();
            return new AggregationLocalGroupByPlanForge(numMethods, numAccesses, columns, optionalTopLevel, levels);
        }
Exemple #28
0
        public static ExprNode RewriteOrToInIfApplicable(ExprNode constituent)
        {
            if (!(constituent is ExprOrNode) || constituent.ChildNodes.Length < 2) {
                return constituent;
            }

            // check eligibility
            var childNodes = constituent.ChildNodes;
            foreach (var child in childNodes) {
                if (!(child is ExprEqualsNode)) {
                    return constituent;
                }

                var equalsNode = (ExprEqualsNode) child;
                if (equalsNode.IsIs || equalsNode.IsNotEquals) {
                    return constituent;
                }
            }

            // find common-expression node
            ExprNode commonExpressionNode;
            var lhs = childNodes[0].ChildNodes[0];
            var rhs = childNodes[0].ChildNodes[1];
            if (ExprNodeUtilityCompare.DeepEquals(lhs, rhs, false)) {
                return constituent;
            }

            if (IsExprExistsInAllEqualsChildNodes(childNodes, lhs)) {
                commonExpressionNode = lhs;
            }
            else if (IsExprExistsInAllEqualsChildNodes(childNodes, rhs)) {
                commonExpressionNode = rhs;
            }
            else {
                return constituent;
            }

            // build node
            var @in = new ExprInNodeImpl(false);
            @in.AddChildNode(commonExpressionNode);
            for (var i = 0; i < constituent.ChildNodes.Length; i++) {
                var child = constituent.ChildNodes[i];
                var nodeindex = ExprNodeUtilityCompare.DeepEquals(
                    commonExpressionNode,
                    childNodes[i].ChildNodes[0],
                    false)
                    ? 1
                    : 0;
                @in.AddChildNode(child.ChildNodes[nodeindex]);
            }

            // validate
            try {
                @in.ValidateWithoutContext();
            }
            catch (ExprValidationException) {
                return constituent;
            }

            return @in;
        }
        public static AggregationLocalGroupByPlanDesc Analyze(
            ExprForge[][] methodForges,
            AggregationForgeFactory[] methodFactories,
            AggregationStateFactoryForge[] accessAggregations,
            AggregationGroupByLocalGroupDesc localGroupDesc,
            ExprNode[] groupByExpressions,
            MultiKeyClassRef groupByMultiKey,
            AggregationAccessorSlotPairForge[] accessors,
            StatementRawInfo raw,
            SerdeCompileTimeResolver serdeResolver)
        {
            if (groupByExpressions == null) {
                groupByExpressions = ExprNodeUtilityQuery.EMPTY_EXPR_ARRAY;
            }

            var columns = new AggregationLocalGroupByColumnForge[localGroupDesc.NumColumns];
            var levelsList = new List<AggregationLocalGroupByLevelForge>();
            AggregationLocalGroupByLevelForge optionalTopLevel = null;
            var additionalForgeables = new List<StmtClassForgeableFactory>();

            // determine optional top level (level number is -1)
            for (var i = 0; i < localGroupDesc.Levels.Length; i++) {
                var levelDesc = localGroupDesc.Levels[i];
                if (levelDesc.PartitionExpr.Length == 0) {
                    AggregationGroupByLocalGroupLevelDesc top = GetLevel(
                        -1,
                        levelDesc,
                        methodForges,
                        methodFactories,
                        accessAggregations,
                        columns,
                        groupByExpressions.Length == 0,
                        accessors,
                        groupByExpressions,
                        groupByMultiKey,
                        raw,
                        serdeResolver);
                    optionalTopLevel = top.Forge;
                    additionalForgeables.AddRange(top.AdditionalForgeables);
                }
            }

            // determine default (same as group-by) level, if any, assign level number 0
            var levelNumber = 0;
            for (var i = 0; i < localGroupDesc.Levels.Length; i++) {
                var levelDesc = localGroupDesc.Levels[i];
                if (levelDesc.PartitionExpr.Length == 0) {
                    continue;
                }

                var isDefaultLevel = groupByExpressions != null &&
                                     ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(
                                         groupByExpressions,
                                         levelDesc.PartitionExpr);

                if (isDefaultLevel) {
                    AggregationGroupByLocalGroupLevelDesc level = GetLevel(
                        0,
                        levelDesc,
                        methodForges,
                        methodFactories,
                        accessAggregations,
                        columns,
                        isDefaultLevel,
                        accessors,
                        groupByExpressions,
                        groupByMultiKey,
                        raw,
                        serdeResolver);
                    additionalForgeables.AddRange(level.AdditionalForgeables);
                    levelsList.Add(level.Forge);
                    levelNumber++;
                    break;
                }
            }

            // determine all other levels, assign level numbers
            for (var i = 0; i < localGroupDesc.Levels.Length; i++) {
                var levelDesc = localGroupDesc.Levels[i];
                if (levelDesc.PartitionExpr.Length == 0) {
                    continue;
                }

                var isDefaultLevel = groupByExpressions != null &&
                                     ExprNodeUtilityCompare.DeepEqualsIgnoreDupAndOrder(
                                         groupByExpressions,
                                         levelDesc.PartitionExpr);
                if (isDefaultLevel) {
                    continue;
                }

                AggregationGroupByLocalGroupLevelDesc level = GetLevel(
                    levelNumber,
                    levelDesc,
                    methodForges,
                    methodFactories,
                    accessAggregations,
                    columns,
                    isDefaultLevel,
                    accessors,
                    groupByExpressions,
                    groupByMultiKey,
                    raw,
                    serdeResolver);
                levelsList.Add(level.Forge);
                additionalForgeables.AddRange(level.AdditionalForgeables);
                levelNumber++;
            }

            // totals
            var numMethods = 0;
            var numAccesses = 0;
            if (optionalTopLevel != null) {
                numMethods += optionalTopLevel.MethodFactories.Length;
                numAccesses += optionalTopLevel.AccessStateForges.Length;
            }

            foreach (var level in levelsList) {
                numMethods += level.MethodFactories.Length;
                numAccesses += level.AccessStateForges.Length;
            }

            AggregationLocalGroupByLevelForge[] levels = levelsList.ToArray();
            AggregationLocalGroupByPlanForge forge = new AggregationLocalGroupByPlanForge(numMethods, numAccesses, columns, optionalTopLevel, levels);
            return new AggregationLocalGroupByPlanDesc(forge, additionalForgeables);
        }
        internal static FilterSpecPlanForge PlanRemainingNodesWithConditions(
            FilterSpecParaForgeMap overallExpressions,
            FilterSpecCompilerArgs args,
            int filterServiceMaxFilterWidth,
            ExprNode topLevelNegator)
        {
            var unassigned = overallExpressions.UnassignedExpressions;
            var orNodes    = new List <ExprOrNode>(unassigned.Count);

            foreach (var node in unassigned)
            {
                if (node is ExprOrNode)
                {
                    orNodes.Add((ExprOrNode)node);
                }
            }

            var expressionsWithoutOr = new FilterSpecParaForgeMap();

            expressionsWithoutOr.Add(overallExpressions);

            // first dimension: or-node index
            // second dimension: or child node index
            var countOr        = 0;
            var sizeFactorized = 1;
            var sizePerOr      = new int[orNodes.Count];
            var orChildNodes   = new OrChildNode[orNodes.Count][];
            var hasControl     = false;

            foreach (var orNode in orNodes)
            {
                expressionsWithoutOr.RemoveNode(orNode);

                // get value-nodes and non-value nodes
                var nonValueNodes = GetNonValueChildNodes(orNode);
                var valueNodes    = new List <ExprNode>(Arrays.AsList(orNode.ChildNodes));
                valueNodes.RemoveAll(nonValueNodes);
                ExprNode singleValueNode = ExprNodeUtilityMake.ConnectExpressionsByLogicalOrWhenNeeded(valueNodes);

                // get all child nodes; last one is confirm if present
                IList <ExprNode> allChildNodes = new List <ExprNode>(nonValueNodes);
                if (singleValueNode != null)
                {
                    allChildNodes.Add(singleValueNode);
                }

                var len = allChildNodes.Count;
                orChildNodes[countOr] = new OrChildNode[len];

                for (var i = 0; i < len; i++)
                {
                    var child = allChildNodes[i];
                    if (child == singleValueNode)
                    {
                        hasControl = true;
                        orChildNodes[countOr][i] = new OrChildNodeV(singleValueNode);
                    }
                    else
                    {
                        var map     = new FilterSpecParaForgeMap();
                        var nodes   = Collections.SingletonList(child);
                        var confirm = DecomposePopulateConsolidate(map, true, nodes, args);
                        if (confirm == null)
                        {
                            orChildNodes[countOr][i] = new OrChildNodeNV(child, map);
                        }
                        else
                        {
                            hasControl = true;
                            orChildNodes[countOr][i] = new OrChildNodeNVNegated(child, map, confirm);
                        }
                    }
                }

                sizePerOr[countOr] = len;
                sizeFactorized     = sizeFactorized * len;
                countOr++;
            }

            // compute permutations
            var permutations           = new CombPermutationTriplets[sizeFactorized];
            var combinationEnumeration = CombinationEnumeration.FromZeroBasedRanges(sizePerOr);
            var count = 0;

            foreach (var permutation in combinationEnumeration)
            {
                permutations[count] = ComputePermutation(expressionsWithoutOr, permutation, orChildNodes, hasControl, args);
                count++;
            }

            // Remove any permutations that only have a control-confirm
            var result             = new List <FilterSpecPlanPathForge>(sizeFactorized);
            var pathControlConfirm = new List <ExprNode>();

            foreach (var permutation in permutations)
            {
                if (permutation.Triplets.Length > 0)
                {
                    result.Add(new FilterSpecPlanPathForge(permutation.Triplets, permutation.NegateCondition));
                }
                else
                {
                    pathControlConfirm.Add(permutation.NegateCondition);
                }
            }

            if (result.Count > filterServiceMaxFilterWidth)
            {
                return(null);
            }

            var      pathArray         = result.ToArray();
            ExprNode topLevelConfirmer = ExprNodeUtilityMake.ConnectExpressionsByLogicalOrWhenNeeded(pathControlConfirm);

            // determine when the path-negate condition is the same as the root confirm-expression
            if (topLevelConfirmer != null)
            {
                var not = new ExprNotNode();
                not.AddChildNode(topLevelConfirmer);
                foreach (var path in pathArray)
                {
                    if (ExprNodeUtilityCompare.DeepEquals(not, path.PathNegate, true))
                    {
                        path.PathNegate = null;
                    }
                }
            }

            var convertor = new MatchedEventConvertorForge(
                args.taggedEventTypes,
                args.arrayEventTypes,
                args.allTagNamesOrdered,
                null,
                true);

            return(new FilterSpecPlanForge(pathArray, topLevelConfirmer, topLevelNegator, convertor));
        }