private static OnDemandQueryRuntime constructOptimizedOnDemandQueryRuntime(Table table,
                                                                               OnDemandQuery onDemandQuery,
                                                                               Map<String, Table> tableMap,
                                                                               int metaPosition, Expression onCondition,
                                                                               MetaStreamEvent metaStreamEvent,
                                                                               List<VariableExpressionExecutor>
                                                                                       variableExpressionExecutors,
                                                                               SiddhiQueryContext siddhiQueryContext) {
        MatchingMetaInfoHolder matchingMetaInfoHolder;

        initMetaStreamEvent(metaStreamEvent, table.getTableDefinition());
        matchingMetaInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, table.getTableDefinition());
        CompiledCondition compiledCondition = table.compileCondition(onCondition, matchingMetaInfoHolder,
                variableExpressionExecutors, tableMap, siddhiQueryContext);
        List<Attribute> expectedOutputAttributes = buildExpectedOutputAttributes(onDemandQuery,
                tableMap, metaPosition, matchingMetaInfoHolder, siddhiQueryContext);

//        MatchingMetaInfoHolder matchingMetaInfoHolderForSelection = generateMatchingMetaInfoHolder(
//                metaStreamEvent, generateTableDefinitionFromOnDemandQuery(onDemandQuery, expectedOutputAttributes),
//                table.getTableDefinition());
        CompiledSelection compiledSelection = ((QueryableProcessor) table).compileSelection(
                onDemandQuery.getSelector(), expectedOutputAttributes, matchingMetaInfoHolder,
                variableExpressionExecutors, tableMap, siddhiQueryContext);
        OnDemandQueryRuntime onDemandQueryRuntime =
                new SelectOnDemandQueryRuntime((QueryableProcessor) table, compiledCondition,
                        compiledSelection, expectedOutputAttributes, siddhiQueryContext.getName());
        try {
            AbstractQueryableRecordTable.CompiledSelectionWithCache compiledSelectionWithCache =
                    (AbstractQueryableRecordTable.CompiledSelectionWithCache) compiledSelection;
            onDemandQueryRuntime.setSelector(compiledSelectionWithCache.getQuerySelector());
            onDemandQueryRuntime.setMetaStreamEvent(metaStreamEvent);
            onDemandQueryRuntime.setStateEventFactory(new StateEventFactory(
                    matchingMetaInfoHolder.getMetaStateEvent()));
        } catch (ClassCastException ignored) {

        }

        QueryParserHelper.reduceMetaComplexEvent(matchingMetaInfoHolder.getMetaStateEvent());
        QueryParserHelper.updateVariablePosition(matchingMetaInfoHolder.getMetaStateEvent(),
                variableExpressionExecutors);
        return onDemandQueryRuntime;
    }
Parse a onDemandQuery and return corresponding OnDemandQueryRuntime.

@param onDemandQuery       onDemandQuery to be parsed.
@param siddhiQueryContext associated Siddhi query context.
@param tableMap         keyvalue containing tables.
@param windowMap        keyvalue containing windows.
@param aggregationMap   keyvalue containing aggregation runtimes.
@return OnDemandQueryRuntime
modules/siddhi-core/src/main/java/io/siddhi/core/util/parser/OnDemandQueryParser.java