private ICollection <EventBean> GetStreamFilterSnapshot(int streamNum, ContextPartitionSelector contextPartitionSelector)
        {
            var streamSpec = _statementSpec.StreamSpecs[streamNum];
            IList <ExprNode> filterExpressions = Collections.GetEmptyList <ExprNode>();

            if (streamSpec is NamedWindowConsumerStreamSpec)
            {
                var namedSpec = (NamedWindowConsumerStreamSpec)streamSpec;
                filterExpressions = namedSpec.FilterExpressions;
            }
            else
            {
                var tableSpec = (TableQueryStreamSpec)streamSpec;
                filterExpressions = tableSpec.FilterExpressions;
            }

            var fireAndForgetProcessor = _processors[streamNum];

            // handle the case of a single or matching agent instance
            var processorInstance = fireAndForgetProcessor.GetProcessorInstance(_agentInstanceContext);

            if (processorInstance != null)
            {
                EPPreparedExecuteTableHelper.AssignTableAccessStrategies(_services, _statementSpec.TableNodes, _agentInstanceContext);
                return(GetStreamSnapshotInstance(streamNum, filterExpressions, processorInstance));
            }

            // context partition runtime query
            var contextPartitions = EPPreparedExecuteMethodHelper.GetAgentInstanceIds(fireAndForgetProcessor, contextPartitionSelector, _services.ContextManagementService, fireAndForgetProcessor.ContextName);

            // collect events
            var events = new ArrayDeque <EventBean>();

            foreach (int agentInstanceId in contextPartitions)
            {
                processorInstance = fireAndForgetProcessor.GetProcessorInstanceContextById(agentInstanceId);
                if (processorInstance != null)
                {
                    var coll = processorInstance.SnapshotBestEffort(this, _filters[streamNum], _statementSpec.Annotations);
                    events.AddAll(coll);
                }
            }
            return(events);
        }
        /// <summary>
        /// Executes the prepared query.
        /// </summary>
        /// <returns>query results</returns>
        public EPPreparedQueryResult Execute(ContextPartitionSelector[] contextPartitionSelectors)
        {
            try {
                var numStreams = _processors.Length;

                if (contextPartitionSelectors != null && contextPartitionSelectors.Length != numStreams)
                {
                    throw new ArgumentException("Number of context partition selectors does not match the number of named windows in the from-clause");
                }

                // handle non-context case
                if (_statementSpec.OptionalContextName == null)
                {
                    ICollection <EventBean>[] snapshots = new ICollection <EventBean> [numStreams];
                    for (var i = 0; i < numStreams; i++)
                    {
                        var selector = contextPartitionSelectors == null ? null : contextPartitionSelectors[i];
                        snapshots[i] = GetStreamFilterSnapshot(i, selector);
                    }

                    _resultSetProcessor.Clear();
                    return(Process(snapshots));
                }

                IList <ContextPartitionResult> contextPartitionResults = new List <ContextPartitionResult>();
                var singleSelector = contextPartitionSelectors != null && contextPartitionSelectors.Length > 0 ? contextPartitionSelectors[0] : null;

                // context partition runtime query
                ICollection <int> agentInstanceIds = EPPreparedExecuteMethodHelper.GetAgentInstanceIds(_processors[0], singleSelector, _services.ContextManagementService, _statementSpec.OptionalContextName);

                // collect events and agent instances
                foreach (int agentInstanceId in agentInstanceIds)
                {
                    var processorInstance = _processors[0].GetProcessorInstanceContextById(agentInstanceId);
                    if (processorInstance != null)
                    {
                        EPPreparedExecuteTableHelper.AssignTableAccessStrategies(_services, _statementSpec.TableNodes, processorInstance.AgentInstanceContext);
                        var coll = processorInstance.SnapshotBestEffort(this, _filters[0], _statementSpec.Annotations);
                        contextPartitionResults.Add(new ContextPartitionResult(coll, processorInstance.AgentInstanceContext));
                    }
                }

                // process context partitions
                var events = new ArrayDeque <EventBean[]>();
                foreach (var contextPartitionResult in contextPartitionResults)
                {
                    var snapshot = contextPartitionResult.Events;
                    if (_statementSpec.FilterRootNode != null)
                    {
                        snapshot = GetFiltered(snapshot, Collections.SingletonList(_statementSpec.FilterRootNode));
                    }
                    EventBean[] rows = snapshot.ToArray();
                    _resultSetProcessor.AgentInstanceContext = contextPartitionResult.Context;
                    var results = _resultSetProcessor.ProcessViewResult(rows, null, true);
                    if (results != null && results.First != null && results.First.Length > 0)
                    {
                        events.Add(results.First);
                    }
                }
                return(new EPPreparedQueryResult(_resultSetProcessor.ResultEventType, EventBeanUtility.Flatten(events)));
            }
            finally {
                if (_hasTableAccess)
                {
                    _services.TableService.TableExprEvaluatorContext.ReleaseAcquiredLocks();
                }
            }
        }
        /// <summary>
        /// Executes the prepared query.
        /// </summary>
        /// <returns>query results</returns>
        public EPPreparedQueryResult Execute(ContextPartitionSelector[] contextPartitionSelectors)
        {
            try {
                if (contextPartitionSelectors != null && contextPartitionSelectors.Length != 1)
                {
                    throw new ArgumentException("Number of context partition selectors must be one");
                }
                var optionalSingleSelector = contextPartitionSelectors != null && contextPartitionSelectors.Length > 0 ? contextPartitionSelectors[0] : null;

                // validate context
                if (Processor.ContextName != null &&
                    StatementSpec.OptionalContextName != null &&
                    !Processor.ContextName.Equals(StatementSpec.OptionalContextName))
                {
                    throw new EPException("Context for named window is '" + Processor.ContextName + "' and query specifies context '" + StatementSpec.OptionalContextName + "'");
                }

                // handle non-specified context
                if (StatementSpec.OptionalContextName == null)
                {
                    FireAndForgetInstance processorInstance = Processor.GetProcessorInstanceNoContext();
                    if (processorInstance != null)
                    {
                        var rows = Executor.Execute(processorInstance);
                        if (rows != null && rows.Length > 0)
                        {
                            Dispatch();
                        }
                        return(new EPPreparedQueryResult(Processor.EventTypePublic, rows));
                    }
                }

                // context partition runtime query
                var agentInstanceIds = EPPreparedExecuteMethodHelper.GetAgentInstanceIds(Processor, optionalSingleSelector, Services.ContextManagementService, Processor.ContextName);

                // collect events and agent instances
                if (agentInstanceIds.IsEmpty())
                {
                    return(new EPPreparedQueryResult(Processor.EventTypeResultSetProcessor, CollectionUtil.EVENTBEANARRAY_EMPTY));
                }

                if (agentInstanceIds.Count == 1)
                {
                    int agentInstanceId   = agentInstanceIds.First();
                    var processorInstance = Processor.GetProcessorInstanceContextById(agentInstanceId);
                    var rows = Executor.Execute(processorInstance);
                    if (rows.Length > 0)
                    {
                        Dispatch();
                    }
                    return(new EPPreparedQueryResult(Processor.EventTypeResultSetProcessor, rows));
                }

                var allRows = new ArrayDeque <EventBean>();
                foreach (int agentInstanceId in agentInstanceIds)
                {
                    var processorInstance = Processor.GetProcessorInstanceContextById(agentInstanceId);
                    if (processorInstance != null)
                    {
                        var rows = Executor.Execute(processorInstance);
                        allRows.AddAll(rows);
                    }
                }
                if (allRows.Count > 0)
                {
                    Dispatch();
                }
                return(new EPPreparedQueryResult(Processor.EventTypeResultSetProcessor, allRows.ToArray()));
            }
            finally {
                if (HasTableAccess)
                {
                    Services.TableService.TableExprEvaluatorContext.ReleaseAcquiredLocks();
                }
            }
        }