Пример #1
0
        private static void ValidateThenSetAssignments(
            IList<OnTriggerSetAssignment> assignments,
            ExprValidationContext validationContext,
            bool allowRHSAggregation)
        {
            if (assignments == null || assignments.IsEmpty()) {
                return;
            }

            foreach (var assign in assignments) {
                ExprNodeUtilityValidate.ValidateAssignment(true, ExprNodeOrigin.UPDATEASSIGN, assign, validationContext);
            }
        }
        protected override void InitExec(
            string aliasName,
            StatementSpecCompiled spec,
            StatementRawInfo statementRawInfo,
            StatementCompileTimeServices services)
        {
            StreamTypeServiceImpl assignmentTypeService = new StreamTypeServiceImpl(
                new EventType[] {
                    processor.EventTypeRspInputEvents, null,
                    processor.EventTypeRspInputEvents
                },
                new string[] {aliasName, "", INITIAL_VALUE_STREAM_NAME},
                new bool[] {true, true, true},
                true,
                false);
            assignmentTypeService.IsStreamZeroUnambigous = true;
            ExprValidationContext validationContext =
                new ExprValidationContextBuilder(assignmentTypeService, statementRawInfo, services)
                    .WithAllowBindingConsumption(true)
                    .Build();

            // validate update expressions
            FireAndForgetSpecUpdate updateSpec = (FireAndForgetSpecUpdate) spec.Raw.FireAndForgetSpec;
            try {
                foreach (OnTriggerSetAssignment assignment in updateSpec.Assignments) {
                    ExprNodeUtilityValidate.ValidateAssignment(
                        false, ExprNodeOrigin.UPDATEASSIGN, assignment, validationContext);
                }
            }
            catch (ExprValidationException e) {
                throw new EPException(e.Message, e);
            }

            // make updater
            try {
                bool copyOnWrite = processor is FireAndForgetProcessorNamedWindowForge;
                updateHelper = EventBeanUpdateHelperForgeFactory.Make(
                    processor.NamedWindowOrTableName,
                    (EventTypeSPI) processor.EventTypeRspInputEvents,
                    updateSpec.Assignments,
                    aliasName,
                    null,
                    copyOnWrite,
                    statementRawInfo.StatementName,
                    services.EventTypeAvroHandler);
            }
            catch (ExprValidationException e) {
                throw new EPException(e.Message, e);
            }
        }
Пример #3
0
        public StmtForgeMethodResult Make(
            string @namespace,
            string classPostfix,
            StatementCompileTimeServices services)
        {
            var statementSpec = _base.StatementSpec;

            // determine context
            var contextName = _base.StatementSpec.Raw.OptionalContextName;
            if (contextName != null) {
                throw new ExprValidationException("Update IStream is not supported in conjunction with a context");
            }

            var streamSpec = statementSpec.StreamSpecs[0];
            var updateSpec = statementSpec.Raw.UpdateDesc;
            string triggereventTypeName;
            EventType streamEventType;
            
            List<StmtClassForgeableFactory> additionalForgeables = new List<StmtClassForgeableFactory>();

            if (streamSpec is FilterStreamSpecCompiled) {
                var filterStreamSpec = (FilterStreamSpecCompiled) streamSpec;
                triggereventTypeName = filterStreamSpec.FilterSpecCompiled.FilterForEventTypeName;
                streamEventType = filterStreamSpec.FilterSpecCompiled.FilterForEventType;
            }
            else if (streamSpec is NamedWindowConsumerStreamSpec) {
                var namedSpec = (NamedWindowConsumerStreamSpec) streamSpec;
                streamEventType = namedSpec.NamedWindow.EventType;
                triggereventTypeName = streamEventType.Name;
            }
            else if (streamSpec is TableQueryStreamSpec) {
                throw new ExprValidationException("Tables cannot be used in an update-istream statement");
            }
            else {
                throw new ExprValidationException("Unknown stream specification streamEventType: " + streamSpec);
            }

            // determine a stream name
            var streamName = triggereventTypeName;
            if (updateSpec.OptionalStreamName != null) {
                streamName = updateSpec.OptionalStreamName;
            }

            StreamTypeService typeService = new StreamTypeServiceImpl(
                new[] {streamEventType},
                new[] {streamName},
                new[] {true},
                false,
                false);

            // create subselect information
            IList<FilterSpecCompiled> filterSpecCompileds = new List<FilterSpecCompiled>();
            IList<NamedWindowConsumerStreamSpec> namedWindowConsumers = new List<NamedWindowConsumerStreamSpec>();
            SubSelectActivationDesc subSelectActivationDesc = SubSelectHelperActivations.CreateSubSelectActivation(
                filterSpecCompileds, namedWindowConsumers, _base, services);
            additionalForgeables.AddAll(subSelectActivationDesc.AdditionalForgeables);
            IDictionary<ExprSubselectNode, SubSelectActivationPlan> subselectActivation = subSelectActivationDesc.Subselects;

            // handle subselects
            SubSelectHelperForgePlan subSelectForgePlan = SubSelectHelperForgePlanner.PlanSubSelect(
                _base, subselectActivation, typeService.StreamNames, typeService.EventTypes, new String[]{triggereventTypeName}, services);
            IDictionary<ExprSubselectNode, SubSelectFactoryForge> subselectForges = subSelectForgePlan.Subselects;
            additionalForgeables.AddAll(subSelectForgePlan.AdditionalForgeables);

            var validationContext =
                new ExprValidationContextBuilder(typeService, _base.StatementRawInfo, services).Build();

            foreach (var assignment in updateSpec.Assignments) {
                ExprNodeUtilityValidate.ValidateAssignment(
                    false, ExprNodeOrigin.UPDATEASSIGN, assignment, validationContext);
            }

            if (updateSpec.OptionalWhereClause != null) {
                var validated = ExprNodeUtilityValidate.GetValidatedSubtree(
                    ExprNodeOrigin.WHERE,
                    updateSpec.OptionalWhereClause,
                    validationContext);
                updateSpec.OptionalWhereClause = validated;
                EPStatementStartMethodHelperValidate.ValidateNoAggregations(
                    validated,
                    "Aggregation functions may not be used within an update-clause");
            }

            // build route information
            var routerDesc = InternalEventRouterDescFactory.GetValidatePreprocessing(
                streamEventType,
                updateSpec,
                _base.StatementRawInfo.Annotations);

            var statementFieldsClassName =
                CodeGenerationIDGenerator.GenerateClassNameSimple(typeof(StatementFields), classPostfix);
            var namespaceScope = new CodegenNamespaceScope(
                @namespace,
                statementFieldsClassName,
                services.IsInstrumented);

            var aiFactoryProviderClassName = CodeGenerationIDGenerator.GenerateClassNameSimple(
                typeof(StatementAIFactoryProvider),
                classPostfix);
            var forge = new StatementAgentInstanceFactoryUpdateForge(routerDesc, subselectForges);
            var aiFactoryForgeable = new StmtClassForgeableAIFactoryProviderUpdate(
                aiFactoryProviderClassName,
                namespaceScope,
                forge);

            var selectSubscriberDescriptor = new SelectSubscriberDescriptor(
                new[] {streamEventType.UnderlyingType},
                new[] {"*"},
                false,
                null,
                null);
            
            var informationals = StatementInformationalsUtil.GetInformationals(
                _base,
                filterSpecCompileds,
                Collections.GetEmptyList<ScheduleHandleCallbackProvider>(),
                Collections.GetEmptyList<NamedWindowConsumerStreamSpec>(),
                false,
                selectSubscriberDescriptor,
                namespaceScope,
                services);
            var statementProviderClassName =
                CodeGenerationIDGenerator.GenerateClassNameSimple(typeof(StatementProvider), classPostfix);
            var stmtProvider = new StmtClassForgeableStmtProvider(
                aiFactoryProviderClassName,
                statementProviderClassName,
                informationals,
                namespaceScope);

            IList<StmtClassForgeable> forgeables = new List<StmtClassForgeable>();
            additionalForgeables.ForEach(_ => forgeables.Add(_.Make(namespaceScope, classPostfix)));
            forgeables.Add(aiFactoryForgeable);
            forgeables.Add(stmtProvider);
            forgeables.Add(new StmtClassForgeableStmtFields(statementFieldsClassName, namespaceScope, 0));
            return new StmtForgeMethodResult(
                forgeables,
                filterSpecCompileds,
                Collections.GetEmptyList<ScheduleHandleCallbackProvider>(),
                namedWindowConsumers,
                Collections.GetEmptyList<FilterSpecParamExprNodeForge>());
        }
Пример #4
0
        public static OnTriggerPlanValidationResult ValidateOnTriggerPlan(
            EventType namedWindowOrTableType,
            OnTriggerWindowDesc onTriggerDesc,
            StreamSpecCompiled streamSpec,
            OnTriggerActivatorDesc activatorResult,
            IDictionary<ExprSubselectNode, SubSelectActivationPlan> subselectActivation,
            StatementBaseInfo @base,
            StatementCompileTimeServices services)
        {
            var zeroStreamAliasName = onTriggerDesc.OptionalAsName;
            if (zeroStreamAliasName == null) {
                zeroStreamAliasName = "stream_0";
            }

            var streamName = streamSpec.OptionalStreamName;
            if (streamName == null) {
                streamName = "stream_1";
            }

            var namedWindowTypeName = onTriggerDesc.WindowName;
            var additionalForgeables = new List<StmtClassForgeableFactory>();

            // Materialize sub-select views
            // 0 - named window stream
            // 1 - arriving stream
            // 2 - initial value before update
            string[] subselectStreamNames = {zeroStreamAliasName, streamSpec.OptionalStreamName};
            EventType[] subselectEventTypes = {namedWindowOrTableType, activatorResult.ActivatorResultEventType};
            string[] subselectEventTypeNames = {namedWindowTypeName, activatorResult.TriggerEventTypeName};

            var subselectForgePlan = SubSelectHelperForgePlanner.PlanSubSelect(
                @base,
                subselectActivation,
                subselectStreamNames,
                subselectEventTypes,
                subselectEventTypeNames,
                services);
            IDictionary<ExprSubselectNode, SubSelectFactoryForge> subselectForges = subselectForgePlan.Subselects;
            additionalForgeables.AddAll(subselectForgePlan.AdditionalForgeables);

            var typeService = new StreamTypeServiceImpl(
                new[] {namedWindowOrTableType, activatorResult.ActivatorResultEventType},
                new[] {zeroStreamAliasName, streamName},
                new[] {false, true},
                true,
                false);

            // allow "initial" as a prefix to properties
            StreamTypeServiceImpl assignmentTypeService;
            if (zeroStreamAliasName.Equals(INITIAL_VALUE_STREAM_NAME) || streamName.Equals(INITIAL_VALUE_STREAM_NAME)) {
                assignmentTypeService = typeService;
            }
            else {
                assignmentTypeService = new StreamTypeServiceImpl(
                    new[] {namedWindowOrTableType, activatorResult.ActivatorResultEventType, namedWindowOrTableType},
                    new[] {zeroStreamAliasName, streamName, INITIAL_VALUE_STREAM_NAME},
                    new[] {false, true, true},
                    false,
                    false);
                assignmentTypeService.IsStreamZeroUnambigous = true;
            }

            if (onTriggerDesc is OnTriggerWindowUpdateDesc) {
                var updateDesc = (OnTriggerWindowUpdateDesc) onTriggerDesc;
                var validationContext = new ExprValidationContextBuilder(
                        assignmentTypeService,
                        @base.StatementRawInfo,
                        services)
                    .WithAllowBindingConsumption(true)
                    .Build();
                foreach (var assignment in updateDesc.Assignments) {
                    ExprNodeUtilityValidate.ValidateAssignment(false, ExprNodeOrigin.UPDATEASSIGN, assignment, validationContext);
                }
            }

            if (onTriggerDesc is OnTriggerMergeDesc) {
                var mergeDesc = (OnTriggerMergeDesc) onTriggerDesc;
                ValidateMergeDesc(
                    mergeDesc,
                    namedWindowOrTableType,
                    zeroStreamAliasName,
                    activatorResult.ActivatorResultEventType,
                    streamName,
                    @base.StatementRawInfo,
                    services);
            }

            // validate join expression
            var validatedJoin = ValidateJoinNamedWindow(
                ExprNodeOrigin.WHERE,
                @base.StatementSpec.Raw.WhereClause,
                namedWindowOrTableType,
                zeroStreamAliasName,
                namedWindowTypeName,
                activatorResult.ActivatorResultEventType,
                streamName,
                activatorResult.TriggerEventTypeName,
                null,
                @base.StatementRawInfo,
                services);

            // validate filter, output rate limiting
            EPStatementStartMethodHelperValidate.ValidateNodes(
                @base.StatementSpec.Raw,
                typeService,
                null,
                @base.StatementRawInfo,
                services);

            // Construct a processor for results; for use in on-select to process selection results
            // Use a wildcard select if the select-clause is empty, such as for on-delete.
            // For on-select the select clause is not empty.
            if (@base.StatementSpec.SelectClauseCompiled.SelectExprList.Length == 0) {
                @base.StatementSpec.SelectClauseCompiled.WithSelectExprList(new SelectClauseElementWildcard());
            }

            var resultSetProcessorPrototype = ResultSetProcessorFactoryFactory.GetProcessorPrototype(
                new ResultSetSpec(@base.StatementSpec),
                typeService,
                null,
                new bool[0],
                true,
                @base.ContextPropertyRegistry,
                false,
                true,
                @base.StatementRawInfo,
                services);
            additionalForgeables.AddAll(resultSetProcessorPrototype.AdditionalForgeables);

            // plan table access
            var tableAccessForges = ExprTableEvalHelperPlan.PlanTableAccess(@base.StatementSpec.TableAccessNodes);

            return new OnTriggerPlanValidationResult(
                subselectForges,
                tableAccessForges,
                resultSetProcessorPrototype,
                validatedJoin,
                zeroStreamAliasName,
                additionalForgeables);
        }
Пример #5
0
        private static void ValidateMergeDesc(
            OnTriggerMergeDesc mergeDesc,
            EventType namedWindowType,
            string namedWindowName,
            EventType triggerStreamType,
            string triggerStreamName,
            StatementRawInfo statementRawInfo,
            StatementCompileTimeServices services)
        {
            var exprNodeErrorMessage = "Aggregation functions may not be used within an merge-clause";

            var dummyTypeNoPropertiesMeta = new EventTypeMetadata(
                "merge_named_window_insert",
                statementRawInfo.ModuleName,
                EventTypeTypeClass.STREAM,
                EventTypeApplicationType.MAP,
                NameAccessModifier.TRANSIENT,
                EventTypeBusModifier.NONBUS,
                false,
                EventTypeIdPair.Unassigned());
            EventType dummyTypeNoProperties = BaseNestableEventUtil.MakeMapTypeCompileTime(
                dummyTypeNoPropertiesMeta,
                Collections.GetEmptyMap<string, object>(),
                null,
                null,
                null,
                null,
                services.BeanEventTypeFactoryPrivate,
                services.EventTypeCompileTimeResolver);
            StreamTypeService insertOnlyTypeSvc = new StreamTypeServiceImpl(
                new[] {dummyTypeNoProperties, triggerStreamType},
                new[] {UuidGenerator.Generate(), triggerStreamName},
                new[] {true, true},
                true,
                false);
            var twoStreamTypeSvc = new StreamTypeServiceImpl(
                new[] {namedWindowType, triggerStreamType},
                new[] {namedWindowName, triggerStreamName},
                new[] {true, true},
                true,
                false);

            foreach (var matchedItem in mergeDesc.Items) {
                // we may provide an additional stream "initial" for the prior value, unless already defined
                StreamTypeServiceImpl assignmentStreamTypeSvc;
                if (namedWindowName.Equals(INITIAL_VALUE_STREAM_NAME) ||
                    triggerStreamName.Equals(INITIAL_VALUE_STREAM_NAME)) {
                    assignmentStreamTypeSvc = twoStreamTypeSvc;
                }
                else {
                    assignmentStreamTypeSvc = new StreamTypeServiceImpl(
                        new[] {namedWindowType, triggerStreamType, namedWindowType},
                        new[] {namedWindowName, triggerStreamName, INITIAL_VALUE_STREAM_NAME},
                        new[] {true, true, true},
                        false,
                        false);
                    assignmentStreamTypeSvc.IsStreamZeroUnambigous = true;
                }

                if (matchedItem.OptionalMatchCond != null) {
                    var matchValidStreams = matchedItem.IsMatchedUnmatched ? twoStreamTypeSvc : insertOnlyTypeSvc;
                    matchedItem.OptionalMatchCond = EPStatementStartMethodHelperValidate.ValidateExprNoAgg(
                        ExprNodeOrigin.MERGEMATCHCOND,
                        matchedItem.OptionalMatchCond,
                        matchValidStreams,
                        exprNodeErrorMessage,
                        true,
                        false,
                        statementRawInfo,
                        services);
                    if (!matchedItem.IsMatchedUnmatched) {
                        EPStatementStartMethodHelperValidate.ValidateSubqueryExcludeOuterStream(
                            matchedItem.OptionalMatchCond);
                    }
                }

                foreach (var item in matchedItem.Actions) {
                    if (item is OnTriggerMergeActionDelete) {
                        var delete = (OnTriggerMergeActionDelete) item;
                        if (delete.OptionalWhereClause != null) {
                            delete.OptionalWhereClause = EPStatementStartMethodHelperValidate.ValidateExprNoAgg(
                                ExprNodeOrigin.MERGEMATCHWHERE,
                                delete.OptionalWhereClause,
                                twoStreamTypeSvc,
                                exprNodeErrorMessage,
                                true,
                                false,
                                statementRawInfo,
                                services);
                        }
                    }
                    else if (item is OnTriggerMergeActionUpdate) {
                        var update = (OnTriggerMergeActionUpdate) item;
                        if (update.OptionalWhereClause != null) {
                            update.OptionalWhereClause = EPStatementStartMethodHelperValidate.ValidateExprNoAgg(
                                ExprNodeOrigin.MERGEMATCHWHERE,
                                update.OptionalWhereClause,
                                twoStreamTypeSvc,
                                exprNodeErrorMessage,
                                true,
                                false,
                                statementRawInfo,
                                services);
                        }

                        foreach (var assignment in update.Assignments) {
                            var validationContext = new ExprValidationContextBuilder(assignmentStreamTypeSvc, statementRawInfo, services)
                                .WithAllowBindingConsumption(true)
                                .WithAllowTableAggReset(true)
                                .Build();
                            ExprNodeUtilityValidate.ValidateAssignment(false, ExprNodeOrigin.UPDATEASSIGN, assignment, validationContext);
                        }
                    }
                    else if (item is OnTriggerMergeActionInsert) {
                        var insert = (OnTriggerMergeActionInsert) item;

                        var insertTypeSvc = GetInsertStreamService(
                            insert.OptionalStreamName,
                            namedWindowName,
                            insertOnlyTypeSvc,
                            twoStreamTypeSvc);

                        if (insert.OptionalWhereClause != null) {
                            insert.OptionalWhereClause = EPStatementStartMethodHelperValidate.ValidateExprNoAgg(
                                ExprNodeOrigin.MERGEMATCHWHERE,
                                insert.OptionalWhereClause,
                                insertTypeSvc,
                                exprNodeErrorMessage,
                                true,
                                false,
                                statementRawInfo,
                                services);
                        }

                        var compiledSelect = ValidateInsertSelect(
                            insert.SelectClause,
                            insertTypeSvc,
                            insert.Columns,
                            statementRawInfo,
                            services);
                        insert.SelectClauseCompiled = compiledSelect;
                    }
                    else {
                        throw new ArgumentException("Unrecognized merge item '" + item.GetType().Name + "'");
                    }
                }
            }

            if (mergeDesc.OptionalInsertNoMatch != null) {
                var insertTypeSvc = GetInsertStreamService(
                    mergeDesc.OptionalInsertNoMatch.OptionalStreamName,
                    namedWindowName,
                    insertOnlyTypeSvc,
                    twoStreamTypeSvc);
                var compiledSelect = ValidateInsertSelect(
                    mergeDesc.OptionalInsertNoMatch.SelectClause,
                    insertTypeSvc,
                    mergeDesc.OptionalInsertNoMatch.Columns,
                    statementRawInfo,
                    services);
                mergeDesc.OptionalInsertNoMatch.SelectClauseCompiled = compiledSelect;
            }
        }
Пример #6
0
        public static OnTriggerSetPlan HandleSetVariable(
            string className,
            CodegenNamespaceScope namespaceScope,
            string classPostfix,
            OnTriggerActivatorDesc activatorResult,
            string optionalStreamName,
            IDictionary<ExprSubselectNode, SubSelectActivationPlan> subselectActivation,
            OnTriggerSetDesc desc,
            StatementBaseInfo @base,
            StatementCompileTimeServices services)
        {
            StreamTypeService typeService = new StreamTypeServiceImpl(
                new[] {activatorResult.ActivatorResultEventType},
                new[] {optionalStreamName},
                new[] {true},
                false,
                false);
            var validationContext = new ExprValidationContextBuilder(typeService, @base.StatementRawInfo, services)
                .WithAllowBindingConsumption(true)
                .Build();

            // handle subselects
            SubSelectHelperForgePlan subSelectForgePlan = SubSelectHelperForgePlanner.PlanSubSelect(
                @base,
                subselectActivation,
                new string[] {optionalStreamName},
                new EventType[] {activatorResult.ActivatorResultEventType},
                new string[] {activatorResult.TriggerEventTypeName},
                services);
            IDictionary<ExprSubselectNode, SubSelectFactoryForge> subselectForges = subSelectForgePlan.Subselects;

            // validate assignments
            foreach (var assignment in desc.Assignments) {
                ExprNodeUtilityValidate.ValidateAssignment(true, ExprNodeOrigin.UPDATEASSIGN, assignment, validationContext);
            }

            // create read-write logic
            VariableReadWritePackageForge variableReadWritePackageForge = new VariableReadWritePackageForge(
                desc.Assignments, @base.StatementName, services);

            // plan table access
            var tableAccessForges = ExprTableEvalHelperPlan.PlanTableAccess(@base.StatementSpec.TableAccessNodes);

            // create output event type
            var eventTypeName = services.EventTypeNameGeneratorStatement.AnonymousTypeName;
            var eventTypeMetadata = new EventTypeMetadata(
                eventTypeName,
                @base.ModuleName,
                EventTypeTypeClass.STATEMENTOUT,
                EventTypeApplicationType.MAP,
                NameAccessModifier.TRANSIENT,
                EventTypeBusModifier.NONBUS,
                false,
                EventTypeIdPair.Unassigned());
            var eventType = BaseNestableEventUtil.MakeMapTypeCompileTime(
                eventTypeMetadata,
                variableReadWritePackageForge.VariableTypes,
                null,
                null,
                null,
                null,
                services.BeanEventTypeFactoryPrivate,
                services.EventTypeCompileTimeResolver);
            services.EventTypeCompileTimeRegistry.NewType(eventType);

            // Handle output format
            var defaultSelectAllSpec = new StatementSpecCompiled();
            defaultSelectAllSpec.SelectClauseCompiled.WithSelectExprList(new SelectClauseElementWildcard());
            defaultSelectAllSpec.Raw.SelectStreamDirEnum = SelectClauseStreamSelectorEnum.RSTREAM_ISTREAM_BOTH;
            StreamTypeService streamTypeService = new StreamTypeServiceImpl(
                new EventType[] {eventType},
                new[] {"trigger_stream"},
                new[] {true},
                false,
                false);
            var resultSetProcessor = ResultSetProcessorFactoryFactory.GetProcessorPrototype(
                new ResultSetSpec(defaultSelectAllSpec),
                streamTypeService,
                null,
                new bool[1],
                false,
                @base.ContextPropertyRegistry,
                false,
                false,
                @base.StatementRawInfo,
                services);
            var classNameRSP = CodeGenerationIDGenerator.GenerateClassNameSimple(
                typeof(ResultSetProcessorFactoryProvider),
                classPostfix);

            var forge = new StatementAgentInstanceFactoryOnTriggerSetForge(
                activatorResult.Activator,
                eventType,
                subselectForges,
                tableAccessForges,
                variableReadWritePackageForge,
                classNameRSP);
            var forgeables = new List<StmtClassForgeable>();
            forgeables.Add(new StmtClassForgeableRSPFactoryProvider(classNameRSP, resultSetProcessor, namespaceScope, @base.StatementRawInfo));

            var onTrigger = new StmtClassForgeableAIFactoryProviderOnTrigger(className, namespaceScope, forge);
            return new OnTriggerSetPlan(
                onTrigger,
                forgeables,
                resultSetProcessor.SelectSubscriberDescriptor,
                subSelectForgePlan.AdditionalForgeables);
        }