Search in sources :

Example 11 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class CoalesceFunctionExecutor method init.

@Override
public void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
    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;
}
Also used : ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) ReturnAttribute(org.ballerinalang.siddhi.annotation.ReturnAttribute) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException)

Example 12 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class IncrementalExecutor method cleanBaseIncrementalValueStore.

private void cleanBaseIncrementalValueStore(long startTimeOfNewAggregates, BaseIncrementalValueStore baseIncrementalValueStore) {
    baseIncrementalValueStore.clearValues();
    baseIncrementalValueStore.setTimestamp(startTimeOfNewAggregates);
    baseIncrementalValueStore.setProcessed(false);
    for (ExpressionExecutor expressionExecutor : baseIncrementalValueStore.getExpressionExecutors()) {
        expressionExecutor.execute(resetEvent);
    }
}
Also used : ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor)

Example 13 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class OrderByEventComparator method compare.

@Override
public int compare(ComplexEvent complexEvent1, ComplexEvent complexEvent2) {
    if (groupByExecutors != null) {
        for (int i = 0, groupByExecutorsLength = groupByExecutors.length; i < groupByExecutorsLength; i++) {
            ExpressionExecutor executor = groupByExecutors[i];
            boolean isAscending = isAscendingArray[i];
            Object value1 = executor.execute(complexEvent1);
            Object value2 = executor.execute(complexEvent2);
            if (value1 != null && value2 != null) {
                int results = 0;
                Attribute.Type type = executor.getReturnType();
                switch(type) {
                    case STRING:
                        results = ((String) value1).compareTo(((String) value2));
                        break;
                    case INT:
                        results = ((Integer) value1).compareTo(((Integer) value2));
                        break;
                    case LONG:
                        results = ((Long) value1).compareTo(((Long) value2));
                        break;
                    case FLOAT:
                        results = ((Float) value1).compareTo(((Float) value2));
                        break;
                    case DOUBLE:
                        results = ((Double) value1).compareTo(((Double) value2));
                        break;
                    case BOOL:
                        results = ((Boolean) value1).compareTo(((Boolean) value2));
                        break;
                    case OBJECT:
                        int hashDiff = value1.hashCode() - value2.hashCode();
                        if (hashDiff < 0) {
                            results = -1;
                        } else if (hashDiff > 0) {
                            results = 1;
                        }
                }
                if (!isAscending) {
                    results = results * -1;
                }
                if (results != 0) {
                    return results;
                }
            } else if (value1 != null) {
                return -1;
            } else if (value2 != null) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
}
Also used : VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) OrderByAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OrderByAttribute) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute)

Example 14 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class AbstractQueryableRecordTable method compileSelection.

public CompiledSelection compileSelection(Selector selector, List<Attribute> expectedOutputAttributes, MatchingMetaInfoHolder matchingMetaInfoHolder, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, String queryName) {
    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, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
        selectAttributeBuilders.add(new SelectAttributeBuilder(expressionBuilder, outputAttribute.getRename()));
    }
    List<ExpressionBuilder> groupByExpressionBuilders = null;
    if (selector.getGroupByList().size() != 0) {
        groupByExpressionBuilders = new ArrayList<>(outputAttributes.size());
        for (Variable variable : selector.getGroupByList()) {
            groupByExpressionBuilders.add(new ExpressionBuilder(variable, matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName));
        }
    }
    ExpressionBuilder havingExpressionBuilder = null;
    if (selector.getHavingExpression() != null) {
        havingExpressionBuilder = new ExpressionBuilder(selector.getHavingExpression(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    }
    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(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
            orderByAttributeBuilders.add(new OrderByAttributeBuilder(expressionBuilder, orderByAttribute.getOrder()));
        }
    }
    Long limit = null;
    if (selector.getLimit() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), matchingMetaInfoHolder.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        limit = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
    }
    CompiledSelection compiledSelection = compileSelection(selectAttributeBuilders, groupByExpressionBuilders, havingExpressionBuilder, orderByAttributeBuilders, limit);
    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());
        }
    }
    return new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
}
Also used : Variable(org.ballerinalang.siddhi.query.api.expression.Variable) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute) OrderByAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OrderByAttribute) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute) CompiledSelection(org.ballerinalang.siddhi.core.util.collection.operator.CompiledSelection) OrderByAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OrderByAttribute) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Example 15 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class AbstractQueryableRecordTable method query.

@Override
public StreamEvent query(StateEvent matchingEvent, CompiledCondition compiledCondition, CompiledSelection compiledSelection) throws ConnectionUnavailableException {
    RecordStoreCompiledSelection recordStoreCompiledSelection = ((RecordStoreCompiledSelection) compiledSelection);
    RecordStoreCompiledCondition 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 (recordTableHandler != null) {
        records = recordTableHandler.query(matchingEvent.getTimestamp(), parameterMap, recordStoreCompiledCondition.compiledCondition, recordStoreCompiledSelection.compiledSelection);
    } else {
        records = query(parameterMap, recordStoreCompiledCondition.compiledCondition, recordStoreCompiledSelection.compiledSelection);
    }
    ComplexEventChunk<StreamEvent> streamEventComplexEventChunk = new ComplexEventChunk<>(true);
    if (records != null) {
        while (records.hasNext()) {
            Object[] record = records.next();
            StreamEvent streamEvent = storeEventPool.borrowEvent();
            System.arraycopy(record, 0, streamEvent.getOutputData(), 0, record.length);
            streamEventComplexEventChunk.add(streamEvent);
        }
    }
    return streamEventComplexEventChunk.getFirst();
}
Also used : ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)46 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)33 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)15 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)14 Map (java.util.Map)13 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)13 HashMap (java.util.HashMap)11 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)11 ArrayList (java.util.ArrayList)10 ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)10 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)10 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)8 StateEvent (org.ballerinalang.siddhi.core.event.state.StateEvent)7 SiddhiAppRuntimeException (org.ballerinalang.siddhi.core.exception.SiddhiAppRuntimeException)7 Expression (org.ballerinalang.siddhi.query.api.expression.Expression)7 AndConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor)5 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)5 OutputAttribute (org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute)5 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)4 ConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor)4