Search in sources :

Example 16 with MetaStateEvent

use of io.siddhi.core.event.state.MetaStateEvent 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 17 with MetaStateEvent

use of io.siddhi.core.event.state.MetaStateEvent in project siddhi by wso2.

the class AbstractQueryableRecordTable method generateCSForSelectAll.

private CompiledSelection generateCSForSelectAll() {
    MetaStreamEvent metaStreamEventForSelectAll = new MetaStreamEvent();
    for (Attribute attribute : tableDefinition.getAttributeList()) {
        metaStreamEventForSelectAll.addOutputData(attribute);
    }
    metaStreamEventForSelectAll.addInputDefinition(tableDefinition);
    MetaStateEvent metaStateEventForSelectAll = new MetaStateEvent(1);
    metaStateEventForSelectAll.addEvent(metaStreamEventForSelectAll);
    MatchingMetaInfoHolder matchingMetaInfoHolderForSlectAll = new MatchingMetaInfoHolder(metaStateEventForSelectAll, -1, 0, tableDefinition, tableDefinition, 0);
    List<OutputAttribute> outputAttributesAll = new ArrayList<>();
    List<Attribute> attributeList = tableDefinition.getAttributeList();
    for (Attribute attribute : attributeList) {
        outputAttributesAll.add(new OutputAttribute(new Variable(attribute.getName())));
    }
    List<SelectAttributeBuilder> selectAttributeBuilders = new ArrayList<>(outputAttributesAll.size());
    List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
    for (OutputAttribute outputAttribute : outputAttributesAll) {
        ExpressionBuilder expressionBuilder = new ExpressionBuilder(outputAttribute.getExpression(), matchingMetaInfoHolderForSlectAll, variableExpressionExecutors, tableMap, null, null, null);
        selectAttributeBuilders.add(new SelectAttributeBuilder(expressionBuilder, outputAttribute.getRename()));
    }
    return compileSelection(selectAttributeBuilders, null, null, null, null, null);
}
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) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 18 with MetaStateEvent

use of io.siddhi.core.event.state.MetaStateEvent in project siddhi by wso2.

the class CacheExpirer method generateExpiryCompiledCondition.

private CompiledCondition generateExpiryCompiledCondition() {
    MetaStreamEvent tableMetaStreamEvent = new MetaStreamEvent();
    tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
    TableDefinition matchingTableDefinition = TableDefinition.id(cacheTable.getTableDefinition().getId());
    for (Attribute attribute : cacheTable.getTableDefinition().getAttributeList()) {
        tableMetaStreamEvent.addOutputData(attribute);
        matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
    }
    tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
    streamEventFactory = new StreamEventFactory(tableMetaStreamEvent);
    Variable rightExpressionForSubtract = new Variable(CACHE_TABLE_TIMESTAMP_ADDED);
    rightExpressionForSubtract.setStreamId(cacheTable.getTableDefinition().getId());
    Expression rightExpressionForCompare = new LongConstant(retentionPeriod);
    Compare.Operator greaterThanOperator = Compare.Operator.GREATER_THAN;
    MetaStreamEvent currentTimeMetaStreamEvent = new MetaStreamEvent();
    currentTimeMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
    Attribute currentTimeAttribute = new Attribute(CACHE_EXPIRE_CURRENT_TIME, Attribute.Type.LONG);
    currentTimeMetaStreamEvent.addOutputData(currentTimeAttribute);
    TableDefinition currentTimeTableDefinition = TableDefinition.id("");
    currentTimeTableDefinition.attribute(CACHE_EXPIRE_CURRENT_TIME, Attribute.Type.LONG);
    currentTimeMetaStreamEvent.addInputDefinition(currentTimeTableDefinition);
    MetaStateEvent metaStateEvent = new MetaStateEvent(2);
    metaStateEvent.addEvent(currentTimeMetaStreamEvent);
    metaStateEvent.addEvent(tableMetaStreamEvent);
    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaStateEvent, 0, cacheTable.getTableDefinition(), 0);
    List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
    Expression leftExpressionForSubtract = new Variable(CACHE_EXPIRE_CURRENT_TIME);
    Expression leftExpressionForCompare = new Subtract(leftExpressionForSubtract, rightExpressionForSubtract);
    Expression deleteCondition = new Compare(leftExpressionForCompare, greaterThanOperator, rightExpressionForCompare);
    SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext, "expiryDeleteQuery");
    return cacheTable.compileCondition(deleteCondition, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
}
Also used : LongConstant(io.siddhi.query.api.expression.constant.LongConstant) Variable(io.siddhi.query.api.expression.Variable) Attribute(io.siddhi.query.api.definition.Attribute) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) Expression(io.siddhi.query.api.expression.Expression) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) TableDefinition(io.siddhi.query.api.definition.TableDefinition) Subtract(io.siddhi.query.api.expression.math.Subtract) Compare(io.siddhi.query.api.expression.condition.Compare) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 19 with MetaStateEvent

use of io.siddhi.core.event.state.MetaStateEvent in project siddhi by wso2.

the class AggregationRuntime method alterMetaInfoHolderForOnDemandQuery.

private static MatchingMetaInfoHolder alterMetaInfoHolderForOnDemandQuery(MetaStreamEvent newMetaStreamEventWithStartEnd, MatchingMetaInfoHolder matchingMetaInfoHolder) {
    MetaStateEvent metaStateEvent = new MetaStateEvent(2);
    MetaStreamEvent incomingMetaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(0);
    metaStateEvent.addEvent(newMetaStreamEventWithStartEnd);
    metaStateEvent.addEvent(incomingMetaStreamEvent);
    return new MatchingMetaInfoHolder(metaStateEvent, 0, 1, newMetaStreamEventWithStartEnd.getLastInputDefinition(), incomingMetaStreamEvent.getLastInputDefinition(), UNKNOWN_STATE);
}
Also used : MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent)

Example 20 with MetaStateEvent

use of io.siddhi.core.event.state.MetaStateEvent in project siddhi by wso2.

the class IncrementalDataPurger method matchingMetaInfoHolder.

/**
 * Building the MatchingMetaInfoHolder for delete records
 */
private MatchingMetaInfoHolder matchingMetaInfoHolder(Table table, Attribute attribute) {
    MetaStateEvent metaStateEvent = new MetaStateEvent(2);
    MetaStreamEvent metaStreamEventWithDeletePara = new MetaStreamEvent();
    MetaStreamEvent metaStreamEventForTable = new MetaStreamEvent();
    TableDefinition deleteTableDefinition = TableDefinition.id("");
    deleteTableDefinition.attribute(attribute.getName(), attribute.getType());
    metaStreamEventWithDeletePara.setEventType(MetaStreamEvent.EventType.TABLE);
    metaStreamEventWithDeletePara.addOutputData(attribute);
    metaStreamEventWithDeletePara.addInputDefinition(deleteTableDefinition);
    metaStreamEventForTable.setEventType(MetaStreamEvent.EventType.TABLE);
    for (Attribute attributes : table.getTableDefinition().getAttributeList()) {
        metaStreamEventForTable.addOutputData(attributes);
    }
    metaStreamEventForTable.addInputDefinition(table.getTableDefinition());
    metaStateEvent.addEvent(metaStreamEventWithDeletePara);
    metaStateEvent.addEvent(metaStreamEventForTable);
    TableDefinition definition = table.getTableDefinition();
    return new MatchingMetaInfoHolder(metaStateEvent, 0, 1, deleteTableDefinition, definition, 0);
}
Also used : 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) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) TableDefinition(io.siddhi.query.api.definition.TableDefinition) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent)

Aggregations

MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)37 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)32 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)16 Attribute (io.siddhi.query.api.definition.Attribute)16 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)13 ArrayList (java.util.ArrayList)12 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)9 Variable (io.siddhi.query.api.expression.Variable)9 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)8 Expression (io.siddhi.query.api.expression.Expression)8 StreamEvent (io.siddhi.core.event.stream.StreamEvent)6 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)6 QuerySelector (io.siddhi.core.query.selector.QuerySelector)6 ReturnStream (io.siddhi.query.api.execution.query.output.stream.ReturnStream)5 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)4 SingleStreamRuntime (io.siddhi.core.query.input.stream.single.SingleStreamRuntime)4 CompiledSelection (io.siddhi.core.util.collection.operator.CompiledSelection)4 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)3 StateEventFactory (io.siddhi.core.event.state.StateEventFactory)3 StateEventPopulatorFactory (io.siddhi.core.event.state.populater.StateEventPopulatorFactory)3