Search in sources :

Example 26 with ExpressionExecutor

use of io.siddhi.core.executor.ExpressionExecutor in project siddhi by wso2.

the class CoalesceFunctionExecutor method init.

@Override
public StateFactory init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiQueryContext siddhiQueryContext) {
    if (attributeExpressionExecutors.length == 0) {
        throw new SiddhiAppValidationException("Coalesce must have at least one parameter");
    }
    Attribute.Type type = attributeExpressionExecutors[0].getReturnType();
    for (ExpressionExecutor expressionExecutor : attributeExpressionExecutors) {
        if (type != expressionExecutor.getReturnType()) {
            throw new SiddhiAppValidationException("Coalesce cannot have parameters with different type");
        }
    }
    returnType = type;
    return null;
}
Also used : ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) Attribute(io.siddhi.query.api.definition.Attribute) ReturnAttribute(io.siddhi.annotation.ReturnAttribute) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException)

Example 27 with ExpressionExecutor

use of io.siddhi.core.executor.ExpressionExecutor 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 28 with ExpressionExecutor

use of io.siddhi.core.executor.ExpressionExecutor in project siddhi by wso2.

the class AbstractQueryableRecordTable method compileCondition.

@Override
public CompiledCondition compileCondition(Expression condition, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
    ExpressionExecutor inMemoryCompiledCondition = ExpressionParser.parseExpression(condition, matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
    ExpressionBuilder expressionBuilder = new ExpressionBuilder(condition, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, new UpdateOrInsertReducer(inMemoryCompiledCondition, matchingMetaInfoHolder), null, siddhiQueryContext);
    CompiledCondition compileCondition = compileCondition(expressionBuilder);
    Map<String, ExpressionExecutor> expressionExecutorMap = expressionBuilder.getVariableExpressionExecutorMap();
    if (cacheEnabled) {
        CompiledCondition compiledConditionWithCache = new CompiledConditionWithCache(compileCondition, ((CacheTable) cacheTable).generateCacheCompileCondition(condition, matchingMetaInfoHolder, siddhiQueryContext, variableExpressionExecutors), siddhiQueryContext);
        return new RecordStoreCompiledCondition(expressionExecutorMap, compiledConditionWithCache, siddhiQueryContext);
    } else {
        return new RecordStoreCompiledCondition(expressionExecutorMap, compileCondition, siddhiQueryContext);
    }
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) CompiledCondition(io.siddhi.core.util.collection.operator.CompiledCondition)

Example 29 with ExpressionExecutor

use of io.siddhi.core.executor.ExpressionExecutor in project siddhi by wso2.

the class AbstractQueryableRecordTable method query.

@Override
public StreamEvent query(StateEvent matchingEvent, CompiledCondition compiledCondition, CompiledSelection compiledSelection, Attribute[] outputAttributes) throws ConnectionUnavailableException {
    findMatchingEvent = matchingEvent;
    updateStoreTableSize();
    // handle condition type convs
    ComplexEventChunk<StreamEvent> streamEventComplexEventChunk = new ComplexEventChunk<>();
    RecordStoreCompiledCondition recordStoreCompiledCondition;
    RecordStoreCompiledSelection recordStoreCompiledSelection;
    CompiledConditionWithCache compiledConditionWithCache = null;
    CompiledSelectionWithCache compiledSelectionWithCache = null;
    StreamEvent cacheResults;
    if (cacheEnabled) {
        RecordStoreCompiledCondition compiledConditionTemp = (RecordStoreCompiledCondition) compiledCondition;
        compiledConditionWithCache = (CompiledConditionWithCache) compiledConditionTemp.getCompiledCondition();
        recordStoreCompiledCondition = new RecordStoreCompiledCondition(compiledConditionTemp.variableExpressionExecutorMap, compiledConditionWithCache.getStoreCompileCondition(), compiledConditionTemp.getSiddhiQueryContext());
        compiledSelectionWithCache = (CompiledSelectionWithCache) compiledSelection;
        recordStoreCompiledSelection = compiledSelectionWithCache.recordStoreCompiledSelection;
    } else {
        recordStoreCompiledSelection = ((RecordStoreCompiledSelection) compiledSelection);
        recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    }
    Map<String, Object> parameterMap = new HashMap<>();
    for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
        parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
    }
    for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledSelection.variableExpressionExecutorMap.entrySet()) {
        parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
    }
    Iterator<Object[]> records;
    if (cacheEnabled) {
        readWriteLock.writeLock().lock();
        try {
            // when store is smaller than max cache size
            if (storeTableSize <= maxCacheSize && !queryStoreWithoutCheckingCache.get()) {
                // return results from cache
                cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                if (log.isDebugEnabled()) {
                    log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store table size is smaller than max cache. Sending results from cache");
                }
                if (cacheResults == null) {
                    return null;
                }
                return executeSelectorOnCacheResults(compiledSelectionWithCache, cacheResults, matchingEvent.getStreamEvent(0));
            } else {
                // when store is bigger than max cache size
                if (log.isDebugEnabled() && !queryStoreWithoutCheckingCache.get()) {
                    log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store table size is bigger than cache.");
                }
                if (compiledConditionWithCache.isRouteToCache() && !queryStoreWithoutCheckingCache.get()) {
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache constraints satisfied. Checking cache");
                    }
                    // if query conrains all primary keys and has == only for them
                    cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                    if (cacheResults != null) {
                        if (log.isDebugEnabled()) {
                            log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache hit. Sending results from cache");
                        }
                        return executeSelectorOnCacheResults(compiledSelectionWithCache, cacheResults, matchingEvent.getStreamEvent(0));
                    }
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache miss. Loading from store");
                    }
                    // read all fields of missed entry from store
                    Iterator<Object[]> recordsFromSelectAll;
                    if (recordTableHandler != null) {
                        recordsFromSelectAll = recordTableHandler.query(matchingEvent.getTimestamp(), parameterMap, recordStoreCompiledCondition.getCompiledCondition(), compiledSelectionForSelectAll, outputAttributes);
                    } else {
                        recordsFromSelectAll = query(parameterMap, recordStoreCompiledCondition.getCompiledCondition(), compiledSelectionForSelectAll, outputAttributes);
                    }
                    if (recordsFromSelectAll == null || !recordsFromSelectAll.hasNext()) {
                        if (log.isDebugEnabled()) {
                            log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store also miss. sending null");
                        }
                        return null;
                    }
                    Object[] recordSelectAll = recordsFromSelectAll.next();
                    StreamEvent streamEvent = storeEventPool.newInstance();
                    streamEvent.setOutputData(new Object[outputAttributes.length]);
                    System.arraycopy(recordSelectAll, 0, streamEvent.getOutputData(), 0, recordSelectAll.length);
                    if (cacheTable.size() == maxCacheSize) {
                        ((CacheTable) cacheTable).deleteOneEntryUsingCachePolicy();
                    }
                    ((CacheTable) cacheTable).addStreamEventUptoMaxSize(streamEvent);
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": sending results from cache after loading from store");
                    }
                    cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                    return executeSelectorOnCacheResults(compiledSelectionWithCache, cacheResults, matchingEvent.getStreamEvent(0));
                }
            }
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
    if (log.isDebugEnabled() && !queryStoreWithoutCheckingCache.get()) {
        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": sending results from store");
    }
    // query conditions are not satisfied check from store/ cache not enabled
    if (recordTableHandler != null) {
        records = recordTableHandler.query(matchingEvent.getTimestamp(), parameterMap, recordStoreCompiledCondition.getCompiledCondition(), recordStoreCompiledSelection.compiledSelection, outputAttributes);
    } else {
        records = query(parameterMap, recordStoreCompiledCondition.getCompiledCondition(), recordStoreCompiledSelection.compiledSelection, outputAttributes);
    }
    addStreamEventToChunk(outputAttributes, streamEventComplexEventChunk, records);
    return streamEventComplexEventChunk.getFirst();
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) HashMap(java.util.HashMap) OnDemandQueryParser.generateMatchingMetaInfoHolderForCacheTable(io.siddhi.core.util.parser.OnDemandQueryParser.generateMatchingMetaInfoHolderForCacheTable) CacheTable(io.siddhi.core.table.CacheTable) OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent(io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Map(java.util.Map) HashMap(java.util.HashMap)

Example 30 with ExpressionExecutor

use of io.siddhi.core.executor.ExpressionExecutor in project siddhi by wso2.

the class InMemoryTable method compileUpdateSet.

@Override
public CompiledUpdateSet compileUpdateSet(UpdateSet updateSet, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
    Map<Integer, ExpressionExecutor> expressionExecutorMap = new HashMap<>();
    for (UpdateSet.SetAttribute setAttribute : updateSet.getSetAttributeList()) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        int attributePosition = tableDefinition.getAttributePosition(setAttribute.getTableVariable().getAttributeName());
        expressionExecutorMap.put(attributePosition, expressionExecutor);
    }
    return new InMemoryCompiledUpdateSet(expressionExecutorMap);
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) UpdateSet(io.siddhi.query.api.execution.query.output.stream.UpdateSet)

Aggregations

ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)53 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)43 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)26 Map (java.util.Map)22 Attribute (io.siddhi.query.api.definition.Attribute)21 StreamEvent (io.siddhi.core.event.stream.StreamEvent)20 HashMap (java.util.HashMap)20 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)19 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)14 ArrayList (java.util.ArrayList)13 StateEvent (io.siddhi.core.event.state.StateEvent)12 Variable (io.siddhi.query.api.expression.Variable)11 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)10 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)10 Expression (io.siddhi.query.api.expression.Expression)10 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)8 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)8 AndConditionExpressionExecutor (io.siddhi.core.executor.condition.AndConditionExpressionExecutor)5 ConditionExpressionExecutor (io.siddhi.core.executor.condition.ConditionExpressionExecutor)5 QueryableProcessor (io.siddhi.core.query.processor.stream.window.QueryableProcessor)4