Search in sources :

Example 6 with CompiledSelection

use of io.siddhi.core.util.collection.operator.CompiledSelection in project siddhi by wso2.

the class AbstractQueryableRecordTable method compileSelection.

public CompiledSelection compileSelection(Selector selector, List<Attribute> expectedOutputAttributes, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
    selectorForTestOnDemandQuery = selector;
    siddhiQueryContextForTestOnDemandQuery = siddhiQueryContext;
    matchingMetaInfoHolderForTestOnDemandQuery = matchingMetaInfoHolder;
    List<OutputAttribute> outputAttributes = selector.getSelectionList();
    if (outputAttributes.size() == 0) {
        MetaStreamEvent metaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(matchingMetaInfoHolder.getStoreEventIndex());
        List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
        for (Attribute attribute : attributeList) {
            outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
        }
    }
    List<SelectAttributeBuilder> selectAttributeBuilders = new ArrayList<>(outputAttributes.size());
    for (OutputAttribute outputAttribute : outputAttributes) {
        ExpressionBuilder expressionBuilder = new ExpressionBuilder(outputAttribute.getExpression(), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
        selectAttributeBuilders.add(new SelectAttributeBuilder(expressionBuilder, outputAttribute.getRename()));
    }
    MatchingMetaInfoHolder metaInfoHolderAfterSelect = new MatchingMetaInfoHolder(matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getMatchingStreamEventIndex(), matchingMetaInfoHolder.getStoreEventIndex(), matchingMetaInfoHolder.getMatchingStreamDefinition(), matchingMetaInfoHolder.getMatchingStreamDefinition(), matchingMetaInfoHolder.getCurrentState());
    List<ExpressionBuilder> groupByExpressionBuilders = null;
    if (selector.getGroupByList().size() != 0) {
        groupByExpressionBuilders = new ArrayList<>(outputAttributes.size());
        for (Variable variable : selector.getGroupByList()) {
            groupByExpressionBuilders.add(new ExpressionBuilder(variable, metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext));
        }
    }
    ExpressionBuilder havingExpressionBuilder = null;
    if (selector.getHavingExpression() != null) {
        havingExpressionBuilder = new ExpressionBuilder(selector.getHavingExpression(), metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
    }
    List<OrderByAttributeBuilder> orderByAttributeBuilders = null;
    if (selector.getOrderByList().size() != 0) {
        orderByAttributeBuilders = new ArrayList<>(selector.getOrderByList().size());
        for (OrderByAttribute orderByAttribute : selector.getOrderByList()) {
            ExpressionBuilder expressionBuilder = new ExpressionBuilder(orderByAttribute.getVariable(), metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
            orderByAttributeBuilders.add(new OrderByAttributeBuilder(expressionBuilder, orderByAttribute.getOrder()));
        }
    }
    Long limit = null;
    Long offset = null;
    if (selector.getLimit() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), metaInfoHolderAfterSelect.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        limit = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
        if (limit < 0) {
            throw new SiddhiAppCreationException("'limit' cannot have negative value, but found '" + limit + "'", selector, siddhiQueryContext.getSiddhiAppContext());
        }
    }
    if (selector.getOffset() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getOffset(), metaInfoHolderAfterSelect.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        offset = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
        if (offset < 0) {
            throw new SiddhiAppCreationException("'offset' cannot have negative value, but found '" + offset + "'", selector, siddhiQueryContext.getSiddhiAppContext());
        }
    }
    CompiledSelection compiledSelection = compileSelection(selectAttributeBuilders, groupByExpressionBuilders, havingExpressionBuilder, orderByAttributeBuilders, limit, offset);
    Map<String, ExpressionExecutor> expressionExecutorMap = new HashMap<>();
    if (selectAttributeBuilders.size() != 0) {
        for (SelectAttributeBuilder selectAttributeBuilder : selectAttributeBuilders) {
            expressionExecutorMap.putAll(selectAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
        }
    }
    if (groupByExpressionBuilders != null && groupByExpressionBuilders.size() != 0) {
        for (ExpressionBuilder groupByExpressionBuilder : groupByExpressionBuilders) {
            expressionExecutorMap.putAll(groupByExpressionBuilder.getVariableExpressionExecutorMap());
        }
    }
    if (havingExpressionBuilder != null) {
        expressionExecutorMap.putAll(havingExpressionBuilder.getVariableExpressionExecutorMap());
    }
    if (orderByAttributeBuilders != null && orderByAttributeBuilders.size() != 0) {
        for (OrderByAttributeBuilder orderByAttributeBuilder : orderByAttributeBuilders) {
            expressionExecutorMap.putAll(orderByAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
        }
    }
    if (cacheEnabled) {
        CompiledSelectionWithCache compiledSelectionWithCache;
        MetaStateEvent metaStateEventForCacheSelection = matchingMetaInfoHolder.getMetaStateEvent().clone();
        ReturnStream returnStream = new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS);
        int metaPosition = SiddhiConstants.UNKNOWN_STATE;
        List<VariableExpressionExecutor> variableExpressionExecutorsForQuerySelector = new ArrayList<>();
        QuerySelector querySelector = SelectorParser.parse(selector, returnStream, metaStateEventForCacheSelection, tableMap, variableExpressionExecutorsForQuerySelector, metaPosition, ProcessingMode.BATCH, false, siddhiQueryContext);
        if (matchingMetaInfoHolder.getMetaStateEvent().getOutputDataAttributes().size() == 0) {
            for (MetaStateEventAttribute outputDataAttribute : metaStateEventForCacheSelection.getOutputDataAttributes()) {
                matchingMetaInfoHolder.getMetaStateEvent().addOutputDataAllowingDuplicate(outputDataAttribute);
            }
        }
        QueryParserHelper.updateVariablePosition(metaStateEventForCacheSelection, variableExpressionExecutorsForQuerySelector);
        querySelector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(metaStateEventForCacheSelection));
        RecordStoreCompiledSelection recordStoreCompiledSelection = new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
        compiledSelectionWithCache = new CompiledSelectionWithCache(recordStoreCompiledSelection, querySelector, metaStateEventForCacheSelection, matchingMetaInfoHolder.getStoreEventIndex(), variableExpressionExecutorsForQuerySelector);
        return compiledSelectionWithCache;
    } else {
        return new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
    }
}
Also used : Variable(io.siddhi.query.api.expression.Variable) MetaStateEventAttribute(io.siddhi.core.event.state.MetaStateEventAttribute) Attribute(io.siddhi.query.api.definition.Attribute) OrderByAttribute(io.siddhi.query.api.execution.query.selection.OrderByAttribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) MetaStateEventAttribute(io.siddhi.core.event.state.MetaStateEventAttribute) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) QuerySelector(io.siddhi.core.query.selector.QuerySelector) ReturnStream(io.siddhi.query.api.execution.query.output.stream.ReturnStream) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) OrderByAttribute(io.siddhi.query.api.execution.query.selection.OrderByAttribute) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 7 with CompiledSelection

use of io.siddhi.core.util.collection.operator.CompiledSelection in project siddhi by wso2.

the class OnDemandQueryParser method constructOptimizedOnDemandQueryRuntime.

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;
}
Also used : Attribute(io.siddhi.query.api.definition.Attribute) SelectOnDemandQueryRuntime(io.siddhi.core.query.SelectOnDemandQueryRuntime) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) CompiledCondition(io.siddhi.core.util.collection.operator.CompiledCondition) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) FindOnDemandQueryRuntime(io.siddhi.core.query.FindOnDemandQueryRuntime) OnDemandQueryRuntime(io.siddhi.core.query.OnDemandQueryRuntime) UpdateOnDemandQueryRuntime(io.siddhi.core.query.UpdateOnDemandQueryRuntime) UpdateOrInsertOnDemandQueryRuntime(io.siddhi.core.query.UpdateOrInsertOnDemandQueryRuntime) InsertOnDemandQueryRuntime(io.siddhi.core.query.InsertOnDemandQueryRuntime) DeleteOnDemandQueryRuntime(io.siddhi.core.query.DeleteOnDemandQueryRuntime) SelectOnDemandQueryRuntime(io.siddhi.core.query.SelectOnDemandQueryRuntime) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) QueryableProcessor(io.siddhi.core.query.processor.stream.window.QueryableProcessor)

Aggregations

CompiledSelection (io.siddhi.core.util.collection.operator.CompiledSelection)7 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)6 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)6 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)6 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)6 Attribute (io.siddhi.query.api.definition.Attribute)6 ArrayList (java.util.ArrayList)6 QuerySelector (io.siddhi.core.query.selector.QuerySelector)5 CompiledCondition (io.siddhi.core.util.collection.operator.CompiledCondition)5 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)5 AbstractQueryableRecordTable (io.siddhi.core.table.record.AbstractQueryableRecordTable)4 QueryParserHelper (io.siddhi.core.util.parser.helper.QueryParserHelper)4 List (java.util.List)4 Map (java.util.Map)4 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)3 StateEvent (io.siddhi.core.event.state.StateEvent)3 StateEventFactory (io.siddhi.core.event.state.StateEventFactory)3 StateEventPopulatorFactory (io.siddhi.core.event.state.populater.StateEventPopulatorFactory)3 StreamEvent (io.siddhi.core.event.stream.StreamEvent)3 SingleStreamRuntime (io.siddhi.core.query.input.stream.single.SingleStreamRuntime)3