Ejemplo n.º 1
0
        private ExprAggMultiFunctionSortedMinMaxByNodeFactory HandleIntoTable(ExprValidationContext validationContext)
        {
            int streamNum;
            var positionalParams = PositionalParams;

            if (positionalParams.Length == 0 ||
                (positionalParams.Length == 1 && positionalParams[0] is ExprWildcard))
            {
                ExprAggMultiFunctionUtil.ValidateWildcardStreamNumbers(
                    validationContext.StreamTypeService, AggregationFunctionName);
                streamNum = 0;
            }
            else if (positionalParams.Length == 1 && positionalParams[0] is ExprStreamUnderlyingNode)
            {
                streamNum = ExprAggMultiFunctionUtil.ValidateStreamWildcardGetStreamNum(positionalParams[0]);
            }
            else if (positionalParams.Length > 0)
            {
                throw new ExprValidationException("When specifying into-table a sort expression cannot be provided");
            }
            else
            {
                streamNum = 0;
            }

            var containedType      = validationContext.StreamTypeService.EventTypes[streamNum];
            var componentType      = containedType.UnderlyingType;
            var accessorResultType = componentType;
            AggregationAccessor accessor;

            if (!_sortedwin)
            {
                accessor = new AggregationAccessorMinMaxByNonTable(_max);
            }
            else
            {
                accessor           = new AggregationAccessorSortedNonTable(_max, componentType);
                accessorResultType = TypeHelper.GetArrayType(accessorResultType);
            }

            AggregationAgent agent = AggregationAgentDefault.INSTANCE;

            if (streamNum != 0)
            {
                agent = new AggregationAgentRewriteStream(streamNum);
            }

            return(new ExprAggMultiFunctionSortedMinMaxByNodeFactory(
                       this, accessor, accessorResultType, containedType, null, null, agent));
        }
 private static string GetErrorPrefix(AggregationStateType stateType)
 {
     return ExprAggMultiFunctionUtil.GetErrorPrefix(stateType.ToString().ToLowerInvariant());
 }
        private LinearAggregationFactoryDesc HandleNonIntoTable(
            ExprNode[] childNodes,
            AggregationStateType stateType,
            ExprValidationContext validationContext)
        {
            var streamTypeService = validationContext.StreamTypeService;
            int streamNum;
            Type resultType;
            ExprEvaluator evaluator;
            ExprNode evaluatorIndex = null;
            bool istreamOnly;
            EventType containedType;
            Type scalarCollectionComponentType = null;

            // validate wildcard use
            var isWildcard = childNodes.Length == 0 || childNodes.Length > 0 && childNodes[0] is ExprWildcard;
            if (isWildcard)
            {
                ExprAggMultiFunctionUtil.ValidateWildcardStreamNumbers(
                    validationContext.StreamTypeService, stateType.ToString().ToLowerInvariant());
                streamNum = 0;
                containedType = streamTypeService.EventTypes[0];
                resultType = containedType.UnderlyingType;
                var tableMetadata = validationContext.TableService.GetTableMetadataFromEventType(containedType);
                evaluator = ExprNodeUtility.MakeUnderlyingEvaluator(0, resultType, tableMetadata);
                istreamOnly = GetIstreamOnly(streamTypeService, 0);
                if (stateType == AggregationStateType.WINDOW && istreamOnly && !streamTypeService.IsOnDemandStreams)
                    throw MakeUnboundValidationEx(stateType);
            }
            else if (childNodes.Length > 0 && childNodes[0] is ExprStreamUnderlyingNode)
            {
                // validate "stream.*"
                streamNum = ExprAggMultiFunctionUtil.ValidateStreamWildcardGetStreamNum(childNodes[0]);
                istreamOnly = GetIstreamOnly(streamTypeService, streamNum);
                if (stateType == AggregationStateType.WINDOW && istreamOnly && !streamTypeService.IsOnDemandStreams)
                    throw MakeUnboundValidationEx(stateType);
                var type = streamTypeService.EventTypes[streamNum];
                containedType = type;
                resultType = type.UnderlyingType;
                var tableMetadata = validationContext.TableService.GetTableMetadataFromEventType(type);
                evaluator = ExprNodeUtility.MakeUnderlyingEvaluator(streamNum, resultType, tableMetadata);
            }
            else
            {
                // validate when neither wildcard nor "stream.*"
                var child = childNodes[0];
                var streams = ExprNodeUtility.GetIdentStreamNumbers(child);
                if (streams.IsEmpty() || streams.Count > 1)
                    throw new ExprValidationException(GetErrorPrefix(stateType) +
                                                      " requires that any child expressions evaluate properties of the same stream; Use 'firstever' or 'lastever' or 'nth' instead");
                streamNum = streams.First();
                istreamOnly = GetIstreamOnly(streamTypeService, streamNum);
                if (stateType == AggregationStateType.WINDOW && istreamOnly && !streamTypeService.IsOnDemandStreams)
                    throw MakeUnboundValidationEx(stateType);
                resultType = childNodes[0].ExprEvaluator.ReturnType;
                evaluator = childNodes[0].ExprEvaluator;
                if (streamNum >= streamTypeService.EventTypes.Length)
                    containedType = streamTypeService.EventTypes[0];
                else
                    containedType = streamTypeService.EventTypes[streamNum];
                scalarCollectionComponentType = resultType;
            }

            if (childNodes.Length > 1)
            {
                if (stateType == AggregationStateType.WINDOW)
                    throw new ExprValidationException(GetErrorPrefix(stateType) +
                                                      " does not accept an index expression; Use 'first' or 'last' instead");
                evaluatorIndex = childNodes[1];
                if (!evaluatorIndex.ExprEvaluator.ReturnType.IsInt32()) {
                    throw new ExprValidationException(
                        GetErrorPrefix(stateType) +
                        " requires an index expression that returns an integer value");
                }
            }

            // determine accessor
            AggregationAccessor accessor;
            if (evaluatorIndex != null)
            {
                var isFirst = stateType == AggregationStateType.FIRST;
                var constant = -1;
                if (evaluatorIndex.IsConstantResult)
                    constant = evaluatorIndex.ExprEvaluator.Evaluate(EvaluateParams.EmptyTrue).AsInt();
                accessor = new AggregationAccessorFirstLastIndexWEval(streamNum, evaluator,
                    evaluatorIndex.ExprEvaluator, constant, isFirst);
            }
            else
            {
                if (stateType == AggregationStateType.FIRST)
                    accessor = new AggregationAccessorFirstWEval(streamNum, evaluator);
                else if (stateType == AggregationStateType.LAST)
                    accessor = new AggregationAccessorLastWEval(streamNum, evaluator);
                else if (stateType == AggregationStateType.WINDOW)
                    accessor = new AggregationAccessorWindowWEval(streamNum, evaluator, resultType);
                else
                    throw new IllegalStateException("Access type is undefined or not known as code '" + stateType +
                                                    "'");
            }

            var accessorResultType = resultType;
            if (stateType == AggregationStateType.WINDOW) accessorResultType = TypeHelper.GetArrayType(resultType);

            var isFafWindow = streamTypeService.IsOnDemandStreams && stateType == AggregationStateType.WINDOW;
            var tableMetadataX = validationContext.TableService.GetTableMetadataFromEventType(containedType);

            var optionalFilter = OptionalFilter;

            if (tableMetadataX == null && !isFafWindow && (istreamOnly || streamTypeService.IsOnDemandStreams))
            {
                if (optionalFilter != null)
                    PositionalParams = ExprNodeUtility.AddExpression(PositionalParams, optionalFilter);
                var factory = validationContext.EngineImportService.AggregationFactoryFactory.MakeLinearUnbounded(
                    validationContext.StatementExtensionSvcContext, this, containedType, accessorResultType, streamNum,
                    optionalFilter != null);
                return new LinearAggregationFactoryDesc(factory, containedType, scalarCollectionComponentType);
            }

            var stateKey = new AggregationStateKeyWStream(
                streamNum, containedType,
                AggregationStateTypeWStream.DATAWINDOWACCESS_LINEAR, 
                new ExprNode[0], optionalFilter);

            var optionalFilterEval = optionalFilter?.ExprEvaluator;
            var stateFactory = validationContext.EngineImportService.AggregationFactoryFactory.MakeLinear(
                validationContext.StatementExtensionSvcContext, this, streamNum, optionalFilterEval);
            var factoryX = new ExprAggMultiFunctionLinearAccessNodeFactoryAccess(this, accessor, accessorResultType,
                containedType,
                stateKey, stateFactory, AggregationAgentDefault.INSTANCE);
            var enumerationType = scalarCollectionComponentType == null ? containedType : null;
            return new LinearAggregationFactoryDesc(factoryX, enumerationType, scalarCollectionComponentType);
        }
Ejemplo n.º 4
0
        private LinearAggregationFactoryDesc HandleNonIntoTable(ExprNode[] childNodes, AggregationStateType stateType, ExprValidationContext validationContext)
        {
            var           streamTypeService = validationContext.StreamTypeService;
            int           streamNum;
            Type          resultType;
            ExprEvaluator evaluator;
            ExprNode      evaluatorIndex = null;
            bool          istreamOnly;
            EventType     containedType;
            Type          scalarCollectionComponentType = null;

            // validate wildcard use
            var isWildcard = childNodes.Length == 0 || childNodes.Length > 0 && childNodes[0] is ExprWildcard;

            if (isWildcard)
            {
                ExprAggMultiFunctionUtil.ValidateWildcardStreamNumbers(validationContext.StreamTypeService, stateType.ToString().ToLower());
                streamNum     = 0;
                containedType = streamTypeService.EventTypes[0];
                resultType    = containedType.UnderlyingType;
                TableMetadata tableMetadata = validationContext.TableService.GetTableMetadataFromEventType(containedType);
                evaluator   = ExprNodeUtility.MakeUnderlyingEvaluator(0, resultType, tableMetadata);
                istreamOnly = GetIstreamOnly(streamTypeService, 0);
                if ((stateType == AggregationStateType.WINDOW) && istreamOnly && !streamTypeService.IsOnDemandStreams)
                {
                    throw MakeUnboundValidationEx(stateType);
                }
            }
            // validate "stream.*"
            else if (childNodes.Length > 0 && childNodes[0] is ExprStreamUnderlyingNode)
            {
                streamNum   = ExprAggMultiFunctionUtil.ValidateStreamWildcardGetStreamNum(childNodes[0]);
                istreamOnly = GetIstreamOnly(streamTypeService, streamNum);
                if ((stateType == AggregationStateType.WINDOW) && istreamOnly && !streamTypeService.IsOnDemandStreams)
                {
                    throw MakeUnboundValidationEx(stateType);
                }
                var type = streamTypeService.EventTypes[streamNum];
                containedType = type;
                resultType    = type.UnderlyingType;
                TableMetadata tableMetadata = validationContext.TableService.GetTableMetadataFromEventType(type);
                evaluator = ExprNodeUtility.MakeUnderlyingEvaluator(streamNum, resultType, tableMetadata);
            }
            // validate when neither wildcard nor "stream.*"
            else
            {
                var child   = childNodes[0];
                var streams = ExprNodeUtility.GetIdentStreamNumbers(child);
                if ((streams.IsEmpty() || (streams.Count > 1)))
                {
                    throw new ExprValidationException(GetErrorPrefix(stateType) + " requires that any child expressions evaluate properties of the same stream; Use 'firstever' or 'lastever' or 'nth' instead");
                }
                streamNum   = streams.First();
                istreamOnly = GetIstreamOnly(streamTypeService, streamNum);
                if ((stateType == AggregationStateType.WINDOW) && istreamOnly && !streamTypeService.IsOnDemandStreams)
                {
                    throw MakeUnboundValidationEx(stateType);
                }
                resultType = childNodes[0].ExprEvaluator.ReturnType;
                evaluator  = childNodes[0].ExprEvaluator;
                if (streamNum >= streamTypeService.EventTypes.Length)
                {
                    containedType = streamTypeService.EventTypes[0];
                }
                else
                {
                    containedType = streamTypeService.EventTypes[streamNum];
                }
                scalarCollectionComponentType = resultType;
            }

            if (childNodes.Length > 1)
            {
                if (stateType == AggregationStateType.WINDOW)
                {
                    throw new ExprValidationException(GetErrorPrefix(stateType) + " does not accept an index expression; Use 'first' or 'last' instead");
                }
                evaluatorIndex = childNodes[1];
                if (evaluatorIndex.ExprEvaluator.ReturnType != typeof(int?))
                {
                    throw new ExprValidationException(GetErrorPrefix(stateType) + " requires an index expression that returns an integer value");
                }
            }

            // determine accessor
            AggregationAccessor accessor;

            if (evaluatorIndex != null)
            {
                var isFirst  = stateType == AggregationStateType.FIRST;
                var constant = -1;
                if (evaluatorIndex.IsConstantResult)
                {
                    constant = evaluatorIndex.ExprEvaluator.Evaluate(new EvaluateParams(null, true, null)).AsInt();
                }
                accessor = new AggregationAccessorFirstLastIndexWEval(streamNum, evaluator, evaluatorIndex.ExprEvaluator, constant, isFirst);
            }
            else
            {
                if (stateType == AggregationStateType.FIRST)
                {
                    accessor = new AggregationAccessorFirstWEval(streamNum, evaluator);
                }
                else if (stateType == AggregationStateType.LAST)
                {
                    accessor = new AggregationAccessorLastWEval(streamNum, evaluator);
                }
                else if (stateType == AggregationStateType.WINDOW)
                {
                    accessor = new AggregationAccessorWindowWEval(streamNum, evaluator, resultType);
                }
                else
                {
                    throw new IllegalStateException("Access type is undefined or not known as code '" + stateType + "'");
                }
            }

            var accessorResultType = resultType;

            if (stateType == AggregationStateType.WINDOW)
            {
                accessorResultType = TypeHelper.GetArrayType(resultType);
            }

            var           isFafWindow    = streamTypeService.IsOnDemandStreams && stateType == AggregationStateType.WINDOW;
            TableMetadata tableMetadataX = validationContext.TableService.GetTableMetadataFromEventType(containedType);

            if (tableMetadataX == null && !isFafWindow && (istreamOnly || streamTypeService.IsOnDemandStreams))
            {
                var factoryX = new ExprAggMultiFunctionLinearAccessNodeFactoryMethod(this, containedType, accessorResultType, streamNum);
                return(new LinearAggregationFactoryDesc(factoryX, containedType, scalarCollectionComponentType));
            }

            var stateKey = new AggregationStateKeyWStream(streamNum, containedType, AggregationStateTypeWStream.DATAWINDOWACCESS_LINEAR, new ExprNode[0]);

            ExprNode me           = this;
            var      theStreamNum = streamNum;
            AggregationStateFactory stateFactory = new ProxyAggregationStateFactory
            {
                ProcCreateAccess = (methodResolutionService, agentInstanceId, groupId, aggregationId, join, groupKey, passThru) =>
                {
                    if (join)
                    {
                        return(methodResolutionService.MakeAccessAggLinearJoin(agentInstanceId, groupId, aggregationId, theStreamNum, passThru));
                    }
                    return(methodResolutionService.MakeAccessAggLinearNonJoin(agentInstanceId, groupId, aggregationId, theStreamNum, passThru));
                },

                ProcAggregationExpression = () => me
            };

            var factory = new ExprAggMultiFunctionLinearAccessNodeFactoryAccess(this, accessor, accessorResultType, containedType,
                                                                                stateKey, stateFactory, AggregationAgentDefault.INSTANCE);
            var enumerationType = scalarCollectionComponentType == null ? containedType : null;

            return(new LinearAggregationFactoryDesc(factory, enumerationType, scalarCollectionComponentType));
        }