protected override StatementAgentInstanceFactoryResult NewContextInternal(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient) { StopCallback stopCallback; IList <StopCallback> stopCallbacks = new List <StopCallback>(); IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategies; try { stopCallbacks.Add(new ProxyStopCallback(() => _services.InternalEventRouter.RemovePreprocessing(_streamEventType, _desc))); _services.InternalEventRouter.AddPreprocessing(_routerDesc, _onExprView, agentInstanceContext.AgentInstanceLock, !_subSelectStrategyCollection.Subqueries.IsEmpty()); // start subselects subselectStrategies = EPStatementStartMethodHelperSubselect.StartSubselects(_services, _subSelectStrategyCollection, agentInstanceContext, stopCallbacks, isRecoveringResilient); } catch (Exception ex) { stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); StatementAgentInstanceUtil.StopSafe(stopCallback, _statementContext); throw; } StatementAgentInstanceFactoryUpdateResult result = new StatementAgentInstanceFactoryUpdateResult(_onExprView, null, agentInstanceContext, subselectStrategies); if (_statementContext.StatementExtensionServicesContext != null) { _statementContext.StatementExtensionServicesContext.ContributeStopCallback(result, stopCallbacks); } stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); result.StopCallback = stopCallback; return(result); }
public StatementAgentInstanceFactoryResult NewContext(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient) { IList <StopCallback> stopCallbacks = new List <StopCallback>(); View view; IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategies; AggregationService aggregationService; EvalRootState optPatternRoot; IDictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategies; ViewableActivationResult activationResult; StopCallback stopCallback; try { if (Services.SchedulableAgentInstanceDirectory != null) { Services.SchedulableAgentInstanceDirectory.Add(agentInstanceContext.EpStatementAgentInstanceHandle); } OnExprViewResult onExprViewResult = DetermineOnExprView(agentInstanceContext, stopCallbacks); view = onExprViewResult.OnExprView; aggregationService = onExprViewResult.OptionalAggregationService; // attach stream to view activationResult = _activator.Activate(agentInstanceContext, false, isRecoveringResilient); activationResult.Viewable.AddView(view); stopCallbacks.Add(activationResult.StopCallback); optPatternRoot = activationResult.OptionalPatternRoot; // determine final output view view = DetermineFinalOutputView(agentInstanceContext, view); // start subselects subselectStrategies = EPStatementStartMethodHelperSubselect.StartSubselects(Services, _subSelectStrategyCollection, agentInstanceContext, stopCallbacks); // plan table access tableAccessStrategies = EPStatementStartMethodHelperTableAccess.AttachTableAccess(Services, agentInstanceContext, StatementSpec.TableNodes); } catch (Exception) { stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); StatementAgentInstanceUtil.StopSafe(stopCallback, StatementContext); throw; } StatementAgentInstanceFactoryOnTriggerResult onTriggerResult = new StatementAgentInstanceFactoryOnTriggerResult(view, null, agentInstanceContext, aggregationService, subselectStrategies, optPatternRoot, tableAccessStrategies, activationResult); if (StatementContext.StatementExtensionServicesContext != null) { StatementContext.StatementExtensionServicesContext.ContributeStopCallback(onTriggerResult, stopCallbacks); } Log.Debug(".start Statement start completed"); stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); onTriggerResult.StopCallback = stopCallback; return(onTriggerResult); }
protected override StatementAgentInstanceFactoryResult NewContextInternal(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient) { IList <StopCallback> stopCallbacks = new List <StopCallback>(2); Viewable finalView; var viewableActivationResult = new ViewableActivationResult[_eventStreamParentViewableActivators.Length]; IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategies; AggregationService aggregationService; Viewable[] streamViews; Viewable[] eventStreamParentViewable; Viewable[] topViews; IDictionary <ExprPriorNode, ExprPriorEvalStrategy> priorNodeStrategies; IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy> previousNodeStrategies; IDictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategies; RegexExprPreviousEvalStrategy regexExprPreviousEvalStrategy = null; IList <StatementAgentInstancePreload> preloadList = new List <StatementAgentInstancePreload>(); EvalRootState[] patternRoots; StatementAgentInstancePostLoad postLoadJoin = null; var suppressSameEventMatches = false; var discardPartialsOnMatch = false; EvalRootMatchRemover evalRootMatchRemover = null; try { // create root viewables eventStreamParentViewable = new Viewable[_numStreams]; patternRoots = new EvalRootState[_numStreams]; for (var stream = 0; stream < _eventStreamParentViewableActivators.Length; stream++) { var activationResult = _eventStreamParentViewableActivators[stream].Activate(agentInstanceContext, false, isRecoveringResilient); viewableActivationResult[stream] = activationResult; stopCallbacks.Add(activationResult.StopCallback); suppressSameEventMatches = activationResult.IsSuppressSameEventMatches; discardPartialsOnMatch = activationResult.IsDiscardPartialsOnMatch; // add stop callback for any stream-level viewable when applicable if (activationResult.Viewable is StopCallback) { stopCallbacks.Add((StopCallback)activationResult.Viewable); } eventStreamParentViewable[stream] = activationResult.Viewable; patternRoots[stream] = activationResult.OptionalPatternRoot; if (stream == 0) { evalRootMatchRemover = activationResult.OptEvalRootMatchRemover; } if (activationResult.OptionalLock != null) { agentInstanceContext.EpStatementAgentInstanceHandle.StatementAgentInstanceLock = activationResult.OptionalLock; _statementContext.DefaultAgentInstanceLock = activationResult.OptionalLock; } } // compile view factories adding "prior" as necessary var viewFactoryChains = new IList <ViewFactory> [_numStreams]; for (var i = 0; i < _numStreams; i++) { var viewFactoryChain = _unmaterializedViewChain[i].FactoryChain; // add "prior" view factory var hasPrior = _viewResourceDelegate.PerStream[i].PriorRequests != null && !_viewResourceDelegate.PerStream[i].PriorRequests.IsEmpty(); if (hasPrior) { var priorEventViewFactory = EPStatementStartMethodHelperPrior.GetPriorEventViewFactory( agentInstanceContext.StatementContext, i, viewFactoryChain.IsEmpty(), false, -1); viewFactoryChain = new List <ViewFactory>(viewFactoryChain); viewFactoryChain.Add(priorEventViewFactory); } viewFactoryChains[i] = viewFactoryChain; } // create view factory chain context: holds stream-specific services var viewFactoryChainContexts = new AgentInstanceViewFactoryChainContext[_numStreams]; for (var i = 0; i < _numStreams; i++) { viewFactoryChainContexts[i] = AgentInstanceViewFactoryChainContext.Create(viewFactoryChains[i], agentInstanceContext, _viewResourceDelegate.PerStream[i]); } // handle "prior" nodes and their strategies priorNodeStrategies = EPStatementStartMethodHelperPrior.CompilePriorNodeStrategies(_viewResourceDelegate, viewFactoryChainContexts); // handle "previous" nodes and their strategies previousNodeStrategies = EPStatementStartMethodHelperPrevious.CompilePreviousNodeStrategies(_viewResourceDelegate, viewFactoryChainContexts); // materialize views streamViews = new Viewable[_numStreams]; topViews = new Viewable[_numStreams]; for (var i = 0; i < _numStreams; i++) { var hasPreviousNode = _viewResourceDelegate.PerStream[i].PreviousRequests != null && !_viewResourceDelegate.PerStream[i].PreviousRequests.IsEmpty(); var createResult = _services.ViewService.CreateViews(eventStreamParentViewable[i], viewFactoryChains[i], viewFactoryChainContexts[i], hasPreviousNode); topViews[i] = createResult.TopViewable; streamViews[i] = createResult.FinalViewable; var isReuseableView = _eventStreamParentViewableActivators[i] is ViewableActivatorStreamReuseView; if (isReuseableView) { var viewsCreated = createResult.NewViews; var stopCallback = new ProxyStopCallback(() => { ViewServiceHelper.RemoveFirstUnsharedView(viewsCreated); }); stopCallbacks.Add(stopCallback); } // add views to stop callback if applicable AddViewStopCallback(stopCallbacks, createResult.NewViews); } // determine match-recognize "previous"-node strategy (none if not present, or one handling and number of nodes) var matchRecognize = EventRowRegexHelper.RecursiveFindRegexService(topViews[0]); if (matchRecognize != null) { regexExprPreviousEvalStrategy = matchRecognize.PreviousEvaluationStrategy; stopCallbacks.Add(new ProxyStopCallback(matchRecognize.Stop)); } // start subselects subselectStrategies = EPStatementStartMethodHelperSubselect.StartSubselects(_services, _subSelectStrategyCollection, agentInstanceContext, stopCallbacks, isRecoveringResilient); // plan table access tableAccessStrategies = EPStatementStartMethodHelperTableAccess.AttachTableAccess(_services, agentInstanceContext, _statementSpec.TableNodes); // obtain result set processor and aggregation services var processorPair = EPStatementStartMethodHelperUtil.StartResultSetAndAggregation(_resultSetProcessorFactoryDesc, agentInstanceContext, false, null); var resultSetProcessor = processorPair.First; aggregationService = processorPair.Second; stopCallbacks.Add(aggregationService); stopCallbacks.Add(resultSetProcessor); // for just 1 event stream without joins, handle the one-table process separately. JoinPreloadMethod joinPreloadMethod; JoinSetComposerDesc joinSetComposer = null; if (streamViews.Length == 1) { finalView = HandleSimpleSelect(streamViews[0], resultSetProcessor, agentInstanceContext, evalRootMatchRemover, suppressSameEventMatches, discardPartialsOnMatch); joinPreloadMethod = null; } else { var joinPlanResult = HandleJoin(_typeService.StreamNames, streamViews, resultSetProcessor, agentInstanceContext, stopCallbacks, _joinAnalysisResult, isRecoveringResilient); finalView = joinPlanResult.Viewable; joinPreloadMethod = joinPlanResult.PreloadMethod; joinSetComposer = joinPlanResult.JoinSetComposerDesc; } // for stoppable final views, add callback if (finalView is StopCallback) { stopCallbacks.Add((StopCallback)finalView); } // Replay any named window data, for later consumers of named data windows if (_services.EventTableIndexService.AllowInitIndex(isRecoveringResilient)) { var hasNamedWindow = false; var namedWindowPostloadFilters = new FilterSpecCompiled[_statementSpec.StreamSpecs.Length]; var namedWindowTailViews = new NamedWindowTailViewInstance[_statementSpec.StreamSpecs.Length]; var namedWindowFilters = new IList <ExprNode> [_statementSpec.StreamSpecs.Length]; for (var i = 0; i < _statementSpec.StreamSpecs.Length; i++) { var streamNum = i; var streamSpec = _statementSpec.StreamSpecs[i]; if (streamSpec is NamedWindowConsumerStreamSpec) { hasNamedWindow = true; var namedSpec = (NamedWindowConsumerStreamSpec)streamSpec; NamedWindowProcessor processor = _services.NamedWindowMgmtService.GetProcessor(namedSpec.WindowName); var processorInstance = processor.GetProcessorInstance(agentInstanceContext); if (processorInstance != null) { var consumerView = processorInstance.TailViewInstance; namedWindowTailViews[i] = consumerView; var view = (NamedWindowConsumerView)viewableActivationResult[i].Viewable; // determine preload/postload filter for index access if (!namedSpec.FilterExpressions.IsEmpty()) { namedWindowFilters[streamNum] = namedSpec.FilterExpressions; try { var streamName = streamSpec.OptionalStreamName != null ? streamSpec.OptionalStreamName : consumerView.EventType.Name; var types = new StreamTypeServiceImpl(consumerView.EventType, streamName, false, _services.EngineURI); var tagged = new LinkedHashMap <string, Pair <EventType, string> >(); namedWindowPostloadFilters[i] = FilterSpecCompiler.MakeFilterSpec(types.EventTypes[0], types.StreamNames[0], namedSpec.FilterExpressions, null, tagged, tagged, types, null, _statementContext, Collections.SingletonSet(0)); } catch (Exception ex) { Log.Warn("Unexpected exception analyzing filter paths: " + ex.Message, ex); } } // preload view for stream unless the expiry policy is batch window var preload = !consumerView.TailView.IsParentBatchWindow && consumerView.HasFirst(); if (preload) { if (isRecoveringResilient && _numStreams < 2) { preload = false; } } if (preload) { var yesRecoveringResilient = isRecoveringResilient; var preloadFilterSpec = namedWindowPostloadFilters[i]; preloadList.Add(new ProxyStatementAgentInstancePreload() { ProcExecutePreload = () => { var snapshot = consumerView.Snapshot(preloadFilterSpec, _statementContext.Annotations); var eventsInWindow = new List <EventBean>(snapshot.Count); ExprNodeUtility.ApplyFilterExpressionsIterable(snapshot, namedSpec.FilterExpressions, agentInstanceContext, eventsInWindow); EventBean[] newEvents = eventsInWindow.ToArray(); view.Update(newEvents, null); if (!yesRecoveringResilient && joinPreloadMethod != null && !joinPreloadMethod.IsPreloading && agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable != null) { agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable.Execute(); } }, }); } } else { Log.Info("Named window access is out-of-context, the named window '" + namedSpec.WindowName + "' has been declared for a different context then the current statement, the aggregation and join state will not be initialized for statement expression [" + _statementContext.Expression + "]"); } preloadList.Add(new ProxyStatementAgentInstancePreload() { ProcExecutePreload = () => { // in a join, preload indexes, if any if (joinPreloadMethod != null) { joinPreloadMethod.PreloadFromBuffer(streamNum); } else { if (agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable != null) { agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable.Execute(); } } }, }); } } // last, for aggregation we need to send the current join results to the result set processor if ((hasNamedWindow) && (joinPreloadMethod != null) && (!isRecoveringResilient) && _resultSetProcessorFactoryDesc.ResultSetProcessorFactory.HasAggregation) { preloadList.Add(new ProxyStatementAgentInstancePreload() { ProcExecutePreload = () => { joinPreloadMethod.PreloadAggregation(resultSetProcessor); }, }); } if (isRecoveringResilient) { postLoadJoin = new StatementAgentInstancePostLoadSelect(streamViews, joinSetComposer, namedWindowTailViews, namedWindowPostloadFilters, namedWindowFilters, _statementContext.Annotations, agentInstanceContext); } else if (joinSetComposer != null) { postLoadJoin = new StatementAgentInstancePostLoadIndexVisiting(joinSetComposer.JoinSetComposer); } } } catch (Exception) { var stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); StatementAgentInstanceUtil.StopSafe(stopCallback, _statementContext); if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AContextPartitionAllocate(); } throw; } var selectResult = new StatementAgentInstanceFactorySelectResult(finalView, null, agentInstanceContext, aggregationService, subselectStrategies, priorNodeStrategies, previousNodeStrategies, regexExprPreviousEvalStrategy, tableAccessStrategies, preloadList, patternRoots, postLoadJoin, topViews, eventStreamParentViewable, viewableActivationResult); if (_statementContext.StatementExtensionServicesContext != null) { _statementContext.StatementExtensionServicesContext.ContributeStopCallback(selectResult, stopCallbacks); } var stopCallbackX = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); selectResult.StopCallback = stopCallbackX; return(selectResult); }
public override EPStatementStartResult StartInternal(EPServicesContext services, StatementContext statementContext, bool isNewStatement, bool isRecoveringStatement, bool isRecoveringResilient) { // define stop and destroy var stopCallbacks = new List <StopCallback>(); var destroyCallbacks = new EPStatementDestroyCallbackList(); // determine context var contextName = StatementSpec.OptionalContextName; if (contextName != null) { throw new ExprValidationException("Update IStream is not supported in conjunction with a context"); } // First we create streams for subselects, if there are any var subSelectStreamDesc = EPStatementStartMethodHelperSubselect.CreateSubSelectActivation( services, StatementSpec, statementContext, destroyCallbacks); var streamSpec = StatementSpec.StreamSpecs[0]; var updateSpec = StatementSpec.UpdateSpec; String triggereventTypeName; if (streamSpec is FilterStreamSpecCompiled) { var filterStreamSpec = (FilterStreamSpecCompiled)streamSpec; triggereventTypeName = filterStreamSpec.FilterSpec.FilterForEventTypeName; } else if (streamSpec is NamedWindowConsumerStreamSpec) { var namedSpec = (NamedWindowConsumerStreamSpec)streamSpec; triggereventTypeName = namedSpec.WindowName; } 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; } var streamEventType = services.EventAdapterService.GetEventTypeByName(triggereventTypeName); StreamTypeService typeService = new StreamTypeServiceImpl(new EventType[] { streamEventType }, new String[] { streamName }, new bool[] { true }, services.EngineURI, false); // determine subscriber result types var evaluatorContextStmt = new ExprEvaluatorContextStatement(statementContext, false); statementContext.StatementResultService.SetSelectClause(new Type[] { streamEventType.UnderlyingType }, new String[] { "*" }, false, null, evaluatorContextStmt); // Materialize sub-select views var subSelectStrategyCollection = EPStatementStartMethodHelperSubselect.PlanSubSelect(services, statementContext, IsQueryPlanLogging(services), subSelectStreamDesc, new String[] { streamName }, new EventType[] { streamEventType }, new String[] { triggereventTypeName }, StatementSpec.DeclaredExpressions, null); var validationContext = new ExprValidationContext( typeService, statementContext.MethodResolutionService, null, statementContext.SchedulingService, statementContext.VariableService, statementContext.TableService, evaluatorContextStmt, statementContext.EventAdapterService, statementContext.StatementName, statementContext.StatementId, statementContext.Annotations, statementContext.ContextDescriptor, statementContext.ScriptingService, false, false, false, false, null, false); foreach (var assignment in updateSpec.Assignments) { var validated = ExprNodeUtility.GetValidatedAssignment(assignment, validationContext); assignment.Expression = validated; EPStatementStartMethodHelperValidate.ValidateNoAggregations(validated, "Aggregation functions may not be used within an update-clause"); } if (updateSpec.OptionalWhereClause != null) { var validated = ExprNodeUtility.GetValidatedSubtree( ExprNodeOrigin.WHERE, updateSpec.OptionalWhereClause, validationContext); updateSpec.OptionalWhereClause = validated; EPStatementStartMethodHelperValidate.ValidateNoAggregations(validated, "Aggregation functions may not be used within an update-clause"); } // preprocessing view var onExprView = new InternalRoutePreprocessView(streamEventType, statementContext.StatementResultService); // validation var routerDesc = services.InternalEventRouter.GetValidatePreprocessing(onExprView.EventType, updateSpec, statementContext.Annotations); // create context factory var contextFactory = new StatementAgentInstanceFactoryUpdate(statementContext, services, streamEventType, updateSpec, onExprView, routerDesc, subSelectStrategyCollection); // perform start of hook-up to start Viewable finalViewable; EPStatementStopMethod stopStatementMethod; IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategyInstances; // With context - delegate instantiation to context EPStatementStopMethod stopMethod = new EPStatementStopMethodImpl(statementContext, stopCallbacks).Stop; if (StatementSpec.OptionalContextName != null) { // use statement-wide agent-instance-specific subselects var aiRegistryExpr = statementContext.StatementAgentInstanceRegistry.AgentInstanceExprService; subselectStrategyInstances = new Dictionary <ExprSubselectNode, SubSelectStrategyHolder>(); foreach (var node in subSelectStrategyCollection.Subqueries.Keys) { var specificService = aiRegistryExpr.AllocateSubselect(node); node.Strategy = specificService; subselectStrategyInstances.Put(node, new SubSelectStrategyHolder(null, null, null, null, null, null)); } var mergeView = new ContextMergeView(onExprView.EventType); finalViewable = mergeView; var statement = new ContextManagedStatementOnTriggerDesc(StatementSpec, statementContext, mergeView, contextFactory); services.ContextManagementService.AddStatement(StatementSpec.OptionalContextName, statement, isRecoveringResilient); stopStatementMethod = () => { services.ContextManagementService.StoppedStatement(contextName, statementContext.StatementName, statementContext.StatementId); stopMethod.Invoke(); }; destroyCallbacks.AddCallback(EPStatementDestroyCallbackContext.New( services.ContextManagementService, StatementSpec.OptionalContextName, statementContext.StatementName, statementContext.StatementId)); } // Without context - start here else { var agentInstanceContext = GetDefaultAgentInstanceContext(statementContext); var resultOfStart = (StatementAgentInstanceFactoryUpdateResult) contextFactory.NewContext(agentInstanceContext, isRecoveringResilient); finalViewable = resultOfStart.FinalView; stopStatementMethod = new EPStatementStopMethod(() => { resultOfStart.StopCallback.Invoke(); stopMethod.Invoke(); }); subselectStrategyInstances = resultOfStart.SubselectStrategies; } // assign subquery nodes EPStatementStartMethodHelperAssignExpr.AssignSubqueryStrategies(subSelectStrategyCollection, subselectStrategyInstances); return(new EPStatementStartResult(finalViewable, stopStatementMethod, destroyCallbacks.Destroy)); }
/// <summary> /// Validates expression nodes and returns a list of validated nodes. /// </summary> /// <param name="exprNodeOrigin">The expr node origin.</param> /// <param name="exprNodes">is the nodes to validate</param> /// <param name="streamTypeService">is provding type information for each stream</param> /// <param name="statementContext">context</param> /// <param name="taggedEventTypes">pattern tagged types</param> /// <param name="arrayEventTypes">@return list of validated expression nodes</param> /// <returns> /// expr nodes /// </returns> /// <exception cref="ExprValidationException"> /// Failed to validate + EPStatementStartMethodHelperSubselect.GetSubqueryInfoText(count, subselect) + : + ex.Message /// or /// Filter expression not returning a boolean value: ' + ExprNodeUtility.ToExpressionStringMinPrecedenceSafe(validated) + ' /// </exception> /// <throws>ExprValidationException for validation errors</throws> public static IList <ExprNode> ValidateAllowSubquery( ExprNodeOrigin exprNodeOrigin, IList <ExprNode> exprNodes, StreamTypeService streamTypeService, StatementContext statementContext, IDictionary <string, Pair <EventType, string> > taggedEventTypes, IDictionary <string, Pair <EventType, string> > arrayEventTypes) { IList <ExprNode> validatedNodes = new List <ExprNode>(); var evaluatorContextStmt = new ExprEvaluatorContextStatement(statementContext, false); var validationContext = new ExprValidationContext( streamTypeService, statementContext.MethodResolutionService, null, statementContext.TimeProvider, statementContext.VariableService, statementContext.TableService, evaluatorContextStmt, statementContext.EventAdapterService, statementContext.StatementName, statementContext.StatementId, statementContext.Annotations, statementContext.ContextDescriptor, statementContext.ScriptingService, false, false, true, false, null, true); foreach (var node in exprNodes) { // Determine subselects var visitor = new ExprNodeSubselectDeclaredDotVisitor(); node.Accept(visitor); // Compile subselects if (!visitor.Subselects.IsEmpty()) { // The outer event type is the filtered-type itself var subselectStreamNumber = 2048; var count = -1; foreach (var subselect in visitor.Subselects) { count++; subselectStreamNumber++; try { HandleSubselectSelectClauses(subselectStreamNumber, statementContext, subselect, streamTypeService.EventTypes[0], streamTypeService.StreamNames[0], streamTypeService.StreamNames[0], taggedEventTypes, arrayEventTypes); } catch (ExprValidationException ex) { throw new ExprValidationException("Failed to validate " + EPStatementStartMethodHelperSubselect.GetSubqueryInfoText(count, subselect) + ": " + ex.Message, ex); } } } var validated = ExprNodeUtility.GetValidatedSubtree(exprNodeOrigin, node, validationContext); validatedNodes.Add(validated); if ((validated.ExprEvaluator.ReturnType != typeof(bool?)) && ((validated.ExprEvaluator.ReturnType != typeof(bool)))) { throw new ExprValidationException("Filter expression not returning a boolean value: '" + ExprNodeUtility.ToExpressionStringMinPrecedenceSafe(validated) + "'"); } } return(validatedNodes); }
public static EPStatementStartMethodSelectDesc Prepare( StatementSpecCompiled statementSpec, EPServicesContext services, StatementContext statementContext, bool recoveringResilient, AgentInstanceContext defaultAgentInstanceContext, bool queryPlanLogging, ViewableActivatorFactory optionalViewableActivatorFactory, OutputProcessViewCallback optionalOutputProcessViewCallback, SelectExprProcessorDeliveryCallback selectExprProcessorDeliveryCallback) { // define stop and destroy var stopCallbacks = new List <StopCallback>(); var destroyCallbacks = new EPStatementDestroyCallbackList(); // determine context var contextName = statementSpec.OptionalContextName; var contextPropertyRegistry = (contextName != null) ? services.ContextManagementService.GetContextDescriptor(contextName).ContextPropertyRegistry : null; // Determine stream names for each stream - some streams may not have a name given var streamNames = EPStatementStartMethodHelperUtil.DetermineStreamNames(statementSpec.StreamSpecs); var numStreams = streamNames.Length; if (numStreams == 0) { throw new ExprValidationException("The from-clause is required but has not been specified"); } var isJoin = statementSpec.StreamSpecs.Length > 1; var hasContext = statementSpec.OptionalContextName != null; // First we create streams for subselects, if there are any var subSelectStreamDesc = EPStatementStartMethodHelperSubselect.CreateSubSelectActivation(services, statementSpec, statementContext, destroyCallbacks); // Create streams and views var eventStreamParentViewableActivators = new ViewableActivator[numStreams]; var unmaterializedViewChain = new ViewFactoryChain[numStreams]; var eventTypeNames = new string[numStreams]; var isNamedWindow = new bool[numStreams]; var historicalEventViewables = new HistoricalEventViewable[numStreams]; // verify for joins that required views are present var joinAnalysisResult = VerifyJoinViews(statementSpec, statementContext.NamedWindowMgmtService, defaultAgentInstanceContext); var evaluatorContextStmt = new ExprEvaluatorContextStatement(statementContext, false); for (var i = 0; i < statementSpec.StreamSpecs.Length; i++) { var streamSpec = statementSpec.StreamSpecs[i]; var isCanIterateUnbound = streamSpec.ViewSpecs.Length == 0 && (services.ConfigSnapshot.EngineDefaults.ViewResourcesConfig.IsIterableUnbound || AnnotationUtil.FindAttribute(statementSpec.Annotations, typeof(IterableUnboundAttribute)) != null); // Create view factories and parent view based on a filter specification if (streamSpec is FilterStreamSpecCompiled) { var filterStreamSpec = (FilterStreamSpecCompiled)streamSpec; eventTypeNames[i] = filterStreamSpec.FilterSpec.FilterForEventTypeName; // Since only for non-joins we get the existing stream's lock and try to reuse it's views var filterSubselectSameStream = EPStatementStartMethodHelperUtil.DetermineSubquerySameStream(statementSpec, filterStreamSpec); // create activator ViewableActivator activatorDeactivator; if (optionalViewableActivatorFactory != null) { activatorDeactivator = optionalViewableActivatorFactory.CreateActivatorSimple(filterStreamSpec); if (activatorDeactivator == null) { throw new IllegalStateException("Viewable activate is null for " + filterStreamSpec.FilterSpec.FilterForEventType.Name); } } else { if (!hasContext) { activatorDeactivator = services.ViewableActivatorFactory.CreateStreamReuseView(services, statementContext, statementSpec, filterStreamSpec, isJoin, evaluatorContextStmt, filterSubselectSameStream, i, isCanIterateUnbound); } else { InstrumentationAgent instrumentationAgentFilter = null; if (InstrumentationHelper.ENABLED) { var eventTypeName = filterStreamSpec.FilterSpec.FilterForEventType.Name; var streamNumber = i; instrumentationAgentFilter = new ProxyInstrumentationAgent() { ProcIndicateQ = () => { InstrumentationHelper.Get().QFilterActivationStream(eventTypeName, streamNumber); }, ProcIndicateA = () => { InstrumentationHelper.Get().AFilterActivationStream(); }, }; } activatorDeactivator = services.ViewableActivatorFactory.CreateFilterProxy(services, filterStreamSpec.FilterSpec, statementSpec.Annotations, false, instrumentationAgentFilter, isCanIterateUnbound, i); } } eventStreamParentViewableActivators[i] = activatorDeactivator; var resultEventType = filterStreamSpec.FilterSpec.ResultEventType; unmaterializedViewChain[i] = services.ViewService.CreateFactories(i, resultEventType, streamSpec.ViewSpecs, streamSpec.Options, statementContext, false, -1); } // Create view factories and parent view based on a pattern expression else if (streamSpec is PatternStreamSpecCompiled) { var patternStreamSpec = (PatternStreamSpecCompiled)streamSpec; var usedByChildViews = streamSpec.ViewSpecs.Length > 0 || (statementSpec.InsertIntoDesc != null); var patternTypeName = statementContext.StatementId + "_pattern_" + i; var eventType = services.EventAdapterService.CreateSemiAnonymousMapType(patternTypeName, patternStreamSpec.TaggedEventTypes, patternStreamSpec.ArrayEventTypes, usedByChildViews); unmaterializedViewChain[i] = services.ViewService.CreateFactories(i, eventType, streamSpec.ViewSpecs, streamSpec.Options, statementContext, false, -1); var rootFactoryNode = services.PatternNodeFactory.MakeRootNode(patternStreamSpec.EvalFactoryNode); var patternContext = statementContext.PatternContextFactory.CreateContext(statementContext, i, rootFactoryNode, patternStreamSpec.MatchedEventMapMeta, true); // create activator var patternActivator = services.ViewableActivatorFactory.CreatePattern(patternContext, rootFactoryNode, eventType, EPStatementStartMethodHelperUtil.IsConsumingFilters(patternStreamSpec.EvalFactoryNode), patternStreamSpec.IsSuppressSameEventMatches, patternStreamSpec.IsDiscardPartialsOnMatch, isCanIterateUnbound); eventStreamParentViewableActivators[i] = patternActivator; } // Create view factories and parent view based on a database SQL statement else if (streamSpec is DBStatementStreamSpec) { ValidateNoViews(streamSpec, "Historical data"); var sqlStreamSpec = (DBStatementStreamSpec)streamSpec; var typeConversionHook = (SQLColumnTypeConversion)TypeHelper.GetAnnotationHook(statementSpec.Annotations, HookType.SQLCOL, typeof(SQLColumnTypeConversion), statementContext.MethodResolutionService); var outputRowConversionHook = (SQLOutputRowConversion)TypeHelper.GetAnnotationHook(statementSpec.Annotations, HookType.SQLROW, typeof(SQLOutputRowConversion), statementContext.MethodResolutionService); var epStatementAgentInstanceHandle = defaultAgentInstanceContext.EpStatementAgentInstanceHandle; var historicalEventViewable = DatabasePollingViewableFactory.CreateDBStatementView( statementContext.StatementId, i, sqlStreamSpec, services.DatabaseRefService, services.EventAdapterService, epStatementAgentInstanceHandle, statementContext.Annotations, typeConversionHook, outputRowConversionHook, statementContext.ConfigSnapshot.EngineDefaults.LoggingConfig.IsEnableADO, services.DataCacheFactory, statementContext); historicalEventViewables[i] = historicalEventViewable; unmaterializedViewChain[i] = ViewFactoryChain.FromTypeNoViews(historicalEventViewable.EventType); eventStreamParentViewableActivators[i] = services.ViewableActivatorFactory.MakeHistorical(historicalEventViewable); stopCallbacks.Add(historicalEventViewable); } else if (streamSpec is MethodStreamSpec) { ValidateNoViews(streamSpec, "Method data"); var methodStreamSpec = (MethodStreamSpec)streamSpec; var epStatementAgentInstanceHandle = defaultAgentInstanceContext.EpStatementAgentInstanceHandle; var historicalEventViewable = MethodPollingViewableFactory.CreatePollMethodView( i, methodStreamSpec, services.EventAdapterService, epStatementAgentInstanceHandle, statementContext.MethodResolutionService, services.EngineImportService, statementContext.SchedulingService, statementContext.ScheduleBucket, evaluatorContextStmt, statementContext.VariableService, statementContext.ContextName, services.DataCacheFactory, statementContext); historicalEventViewables[i] = historicalEventViewable; unmaterializedViewChain[i] = ViewFactoryChain.FromTypeNoViews(historicalEventViewable.EventType); eventStreamParentViewableActivators[i] = services.ViewableActivatorFactory.MakeHistorical(historicalEventViewable); stopCallbacks.Add(historicalEventViewable); } else if (streamSpec is TableQueryStreamSpec) { ValidateNoViews(streamSpec, "Table data"); var tableStreamSpec = (TableQueryStreamSpec)streamSpec; if (isJoin && tableStreamSpec.FilterExpressions.Count > 0) { throw new ExprValidationException("Joins with tables do not allow table filter expressions, please add table filters to the where-clause instead"); } var metadata = services.TableService.GetTableMetadata(tableStreamSpec.TableName); ExprEvaluator[] tableFilterEvals = null; if (tableStreamSpec.FilterExpressions.Count > 0) { tableFilterEvals = ExprNodeUtility.GetEvaluators(tableStreamSpec.FilterExpressions); } EPLValidationUtil.ValidateContextName(true, metadata.TableName, metadata.ContextName, statementSpec.OptionalContextName, false); eventStreamParentViewableActivators[i] = services.ViewableActivatorFactory.CreateTable(metadata, tableFilterEvals); unmaterializedViewChain[i] = ViewFactoryChain.FromTypeNoViews(metadata.InternalEventType); eventTypeNames[i] = tableStreamSpec.TableName; joinAnalysisResult.SetTablesForStream(i, metadata); if (tableStreamSpec.Options.IsUnidirectional) { throw new ExprValidationException("Tables cannot be marked as unidirectional"); } if (tableStreamSpec.Options.IsRetainIntersection || tableStreamSpec.Options.IsRetainUnion) { throw new ExprValidationException("Tables cannot be marked with retain"); } if (isJoin) { destroyCallbacks.AddCallback(new EPStatementDestroyCallbackTableIdxRef(services.TableService, metadata, statementContext.StatementName)); } services.StatementVariableRefService.AddReferences(statementContext.StatementName, metadata.TableName); } else if (streamSpec is NamedWindowConsumerStreamSpec) { var namedSpec = (NamedWindowConsumerStreamSpec)streamSpec; var processor = services.NamedWindowMgmtService.GetProcessor(namedSpec.WindowName); var namedWindowType = processor.TailView.EventType; if (namedSpec.OptPropertyEvaluator != null) { namedWindowType = namedSpec.OptPropertyEvaluator.FragmentEventType; } eventStreamParentViewableActivators[i] = services.ViewableActivatorFactory.CreateNamedWindow(processor, namedSpec, statementContext); services.NamedWindowConsumerMgmtService.AddConsumer(statementContext, namedSpec); unmaterializedViewChain[i] = services.ViewService.CreateFactories(i, namedWindowType, namedSpec.ViewSpecs, namedSpec.Options, statementContext, false, -1); joinAnalysisResult.SetNamedWindow(i); eventTypeNames[i] = namedSpec.WindowName; isNamedWindow[i] = true; // Consumers to named windows cannot declare a data window view onto the named window to avoid duplicate remove streams EPStatementStartMethodHelperValidate.ValidateNoDataWindowOnNamedWindow(unmaterializedViewChain[i].FactoryChain); } else { throw new ExprValidationException("Unknown stream specification type: " + streamSpec); } } // handle match-recognize pattern if (statementSpec.MatchRecognizeSpec != null) { if (isJoin) { throw new ExprValidationException("Joins are not allowed when using match-recognize"); } if (joinAnalysisResult.TablesPerStream[0] != null) { throw new ExprValidationException("Tables cannot be used with match-recognize"); } var isUnbound = (unmaterializedViewChain[0].FactoryChain.IsEmpty()) && (!(statementSpec.StreamSpecs[0] is NamedWindowConsumerStreamSpec)); var factoryX = services.RegexHandlerFactory.MakeViewFactory(unmaterializedViewChain[0], statementSpec.MatchRecognizeSpec, defaultAgentInstanceContext, isUnbound, statementSpec.Annotations, services.ConfigSnapshot.EngineDefaults.MatchRecognizeConfig); unmaterializedViewChain[0].FactoryChain.Add(factoryX); EPStatementStartMethodHelperAssignExpr.AssignAggregations(factoryX.AggregationService, factoryX.AggregationExpressions); } // Obtain event types from view factory chains var streamEventTypes = new EventType[statementSpec.StreamSpecs.Length]; for (var i = 0; i < unmaterializedViewChain.Length; i++) { streamEventTypes[i] = unmaterializedViewChain[i].EventType; } // Add uniqueness information useful for joins joinAnalysisResult.AddUniquenessInfo(unmaterializedViewChain, statementSpec.Annotations); // Validate sub-select views var subSelectStrategyCollection = EPStatementStartMethodHelperSubselect.PlanSubSelect(services, statementContext, queryPlanLogging, subSelectStreamDesc, streamNames, streamEventTypes, eventTypeNames, statementSpec.DeclaredExpressions, contextPropertyRegistry); // Construct type information per stream StreamTypeService typeService = new StreamTypeServiceImpl(streamEventTypes, streamNames, EPStatementStartMethodHelperUtil.GetHasIStreamOnly(isNamedWindow, unmaterializedViewChain), services.EngineURI, false); var viewResourceDelegateUnverified = new ViewResourceDelegateUnverified(); // Validate views that require validation, specifically streams that don't have // sub-views such as DB SQL joins var historicalViewableDesc = new HistoricalViewableDesc(numStreams); for (var stream = 0; stream < historicalEventViewables.Length; stream++) { var historicalEventViewable = historicalEventViewables[stream]; if (historicalEventViewable == null) { continue; } historicalEventViewable.Validate( services.EngineImportService, typeService, statementContext.MethodResolutionService, statementContext.TimeProvider, statementContext.VariableService, statementContext.TableService, statementContext.ScriptingService, evaluatorContextStmt, services.ConfigSnapshot, services.SchedulingService, services.EngineURI, statementSpec.SqlParameters, statementContext.EventAdapterService, statementContext); historicalViewableDesc.SetHistorical(stream, historicalEventViewable.RequiredStreams); if (historicalEventViewable.RequiredStreams.Contains(stream)) { throw new ExprValidationException("Parameters for historical stream " + stream + " indicate that the stream is subordinate to itself as stream parameters originate in the same stream"); } } // unidirectional is not supported with into-table if (joinAnalysisResult.IsUnidirectional && statementSpec.IntoTableSpec != null) { throw new ExprValidationException("Into-table does not allow unidirectional joins"); } // Construct a processor for results posted by views and joins, which takes care of aggregation if required. // May return null if we don't need to post-process results posted by views or joins. var resultSetProcessorPrototypeDesc = ResultSetProcessorFactoryFactory.GetProcessorPrototype( statementSpec, statementContext, typeService, viewResourceDelegateUnverified, joinAnalysisResult.UnidirectionalInd, true, contextPropertyRegistry, selectExprProcessorDeliveryCallback, services.ConfigSnapshot, services.ResultSetProcessorHelperFactory, false, false); // Validate where-clause filter tree, outer join clause and output limit expression EPStatementStartMethodHelperValidate.ValidateNodes(statementSpec, statementContext, typeService, viewResourceDelegateUnverified); // Handle 'prior' function nodes in terms of view requirements var viewResourceDelegateVerified = EPStatementStartMethodHelperViewResources.VerifyPreviousAndPriorRequirements(unmaterializedViewChain, viewResourceDelegateUnverified); // handle join JoinSetComposerPrototype joinSetComposerPrototype = null; if (numStreams > 1) { var selectsRemoveStream = statementSpec.SelectStreamSelectorEnum.IsSelectsRStream() || statementSpec.OutputLimitSpec != null; var hasAggregations = !resultSetProcessorPrototypeDesc.AggregationServiceFactoryDesc.Expressions.IsEmpty(); joinSetComposerPrototype = JoinSetComposerPrototypeFactory.MakeComposerPrototype( statementContext.StatementName, statementContext.StatementId, statementSpec.OuterJoinDescList, statementSpec.FilterRootNode, typeService.EventTypes, streamNames, joinAnalysisResult, queryPlanLogging, statementContext, historicalViewableDesc, defaultAgentInstanceContext, selectsRemoveStream, hasAggregations, services.TableService, false, services.EventTableIndexService.AllowInitIndex(recoveringResilient)); } // obtain factory for output limiting var outputViewFactory = OutputProcessViewFactoryFactory.Make(statementSpec, services.InternalEventRouter, statementContext, resultSetProcessorPrototypeDesc.ResultSetProcessorFactory.ResultEventType, optionalOutputProcessViewCallback, services.TableService, resultSetProcessorPrototypeDesc.ResultSetProcessorFactory.ResultSetProcessorType, services.ResultSetProcessorHelperFactory, services.StatementVariableRefService); // Factory for statement-context instances var factory = new StatementAgentInstanceFactorySelect( numStreams, eventStreamParentViewableActivators, statementContext, statementSpec, services, typeService, unmaterializedViewChain, resultSetProcessorPrototypeDesc, joinAnalysisResult, recoveringResilient, joinSetComposerPrototype, subSelectStrategyCollection, viewResourceDelegateVerified, outputViewFactory); EPStatementStopMethod stopMethod = new EPStatementStopMethodImpl(statementContext, stopCallbacks); return(new EPStatementStartMethodSelectDesc(factory, subSelectStrategyCollection, viewResourceDelegateUnverified, resultSetProcessorPrototypeDesc, stopMethod, destroyCallbacks)); }