Search in sources :

Example 16 with ConstantExpressionExecutor

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

the class AggregationParser method generateDatabaseQuery.

private static String generateDatabaseQuery(List<ExpressionExecutor> expressionExecutors, DBAggregationQueryConfigurationEntry dbAggregationQueryConfigurationEntry, StreamDefinition incomingOutputStreamDefinition, boolean isDistributed, String shardID, boolean isProcessingOnExternalTime, Table aggregationTable, Table parentAggregationTable, List<Variable> groupByVariableList, TimePeriod.Duration duration) {
    DBAggregationSelectFunctionTemplate dbAggregationSelectFunctionTemplates = dbAggregationQueryConfigurationEntry.getRdbmsSelectFunctionTemplate();
    DBAggregationSelectQueryTemplate dbAggregationSelectQueryTemplate = dbAggregationQueryConfigurationEntry.getRdbmsSelectQueryTemplate();
    DBAggregationTimeConversionDurationMapping dbAggregationTimeConversionDurationMapping = dbAggregationQueryConfigurationEntry.getDbAggregationTimeConversionDurationMapping();
    List<Attribute> attributeList = incomingOutputStreamDefinition.getAttributeList();
    List<String> groupByColumnNames = new ArrayList<>();
    StringJoiner outerSelectColumnJoiner = new StringJoiner(", ");
    StringJoiner subSelectT1ColumnJoiner = new StringJoiner(", ", SQL_SELECT, " ");
    StringJoiner subSelectT2ColumnJoiner = new StringJoiner(", ");
    StringJoiner innerSelectT2ColumnJoiner = new StringJoiner(", ", SQL_SELECT, " ");
    StringJoiner onConditionBuilder = new StringJoiner(SQL_AND);
    StringJoiner subSelectT2OnConditionBuilder = new StringJoiner(SQL_AND);
    StringJoiner groupByQueryBuilder = new StringJoiner(", ");
    StringJoiner groupByT3QueryBuilder = new StringJoiner(", ");
    StringJoiner finalSelectQuery = new StringJoiner(" ");
    StringJoiner completeQuery = new StringJoiner(" ");
    StringJoiner insertIntoColumns = new StringJoiner(", ");
    StringBuilder filterQueryBuilder = new StringBuilder();
    StringBuilder insertIntoQueryBuilder = new StringBuilder();
    String innerFromClause = SQL_FROM + parentAggregationTable.getTableDefinition().getId();
    String innerWhereFilterClause;
    String groupByClause;
    String innerT2WhereCondition;
    StringJoiner innerT2Query = new StringJoiner(" ");
    StringJoiner subQueryT1 = new StringJoiner(" ");
    StringJoiner subQueryT2 = new StringJoiner(" ");
    attributeList.stream().forEach(attribute -> insertIntoColumns.add(attribute.getName()));
    int i = 0;
    insertIntoQueryBuilder.append(dbAggregationSelectQueryTemplate.getRecordInsertQuery().replace(PLACEHOLDER_TABLE_NAME, aggregationTable.getTableDefinition().getId()).replace(PLACEHOLDER_COLUMNS, insertIntoColumns.toString()));
    filterQueryBuilder.append(" (").append(AGG_START_TIMESTAMP_COL).append(" >= ?").append(" AND ").append(AGG_START_TIMESTAMP_COL).append(" < ? ").append(") ");
    if (isDistributed) {
        filterQueryBuilder.append(" AND ").append(AGG_SHARD_ID_COL).append(" = '").append(shardID).append("' ");
        groupByQueryBuilder.add(AGG_SHARD_ID_COL);
        groupByT3QueryBuilder.add(INNER_SELECT_QUERY_REF_T3 + "." + AGG_SHARD_ID_COL);
        innerSelectT2ColumnJoiner.add(AGG_SHARD_ID_COL);
        subSelectT2OnConditionBuilder.add(parentAggregationTable.getTableDefinition().getId() + "." + AGG_SHARD_ID_COL + EQUALS + INNER_SELECT_QUERY_REF_T3 + "." + AGG_SHARD_ID_COL);
        if (isProcessingOnExternalTime) {
            subSelectT1ColumnJoiner.add(AGG_SHARD_ID_COL);
        }
    }
    if (isProcessingOnExternalTime) {
        groupByVariableList.stream().forEach(variable -> {
            groupByColumnNames.add(variable.getAttributeName());
            groupByQueryBuilder.add(variable.getAttributeName());
            groupByT3QueryBuilder.add(INNER_SELECT_QUERY_REF_T3 + "." + variable.getAttributeName());
            onConditionBuilder.add(SUB_SELECT_QUERY_REF_T1 + "." + variable.getAttributeName() + EQUALS + SUB_SELECT_QUERY_REF_T2 + "." + variable.getAttributeName());
            subSelectT2OnConditionBuilder.add(parentAggregationTable.getTableDefinition().getId() + "." + variable.getAttributeName() + EQUALS + INNER_SELECT_QUERY_REF_T3 + "." + variable.getAttributeName());
        });
        innerT2WhereCondition = INNER_SELECT_QUERY_REF_T3 + "." + groupByVariableList.get(0).getAttributeName() + SQL_NOT_NULL;
        for (ExpressionExecutor expressionExecutor : expressionExecutors) {
            if (expressionExecutor instanceof VariableExpressionExecutor) {
                VariableExpressionExecutor variableExpressionExecutor = (VariableExpressionExecutor) expressionExecutor;
                if (variableExpressionExecutor.getAttribute().getName().equals(AGG_START_TIMESTAMP_COL)) {
                    outerSelectColumnJoiner.add(" ? " + SQL_AS + variableExpressionExecutor.getAttribute().getName());
                } else if (!variableExpressionExecutor.getAttribute().getName().equals(AGG_EXTERNAL_TIMESTAMP_COL)) {
                    if (groupByColumnNames.contains(variableExpressionExecutor.getAttribute().getName())) {
                        subSelectT2ColumnJoiner.add(INNER_SELECT_QUERY_REF_T3 + "." + variableExpressionExecutor.getAttribute().getName() + SQL_AS + variableExpressionExecutor.getAttribute().getName());
                        outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + variableExpressionExecutor.getAttribute().getName() + SQL_AS + attributeList.get(i).getName());
                        subSelectT1ColumnJoiner.add(variableExpressionExecutor.getAttribute().getName());
                        innerSelectT2ColumnJoiner.add(variableExpressionExecutor.getAttribute().getName());
                    } else {
                        subSelectT2ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, variableExpressionExecutor.getAttribute().getName()) + SQL_AS + variableExpressionExecutor.getAttribute().getName());
                        outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T2 + "." + variableExpressionExecutor.getAttribute().getName() + SQL_AS + attributeList.get(i).getName());
                    }
                }
            } else if (expressionExecutor instanceof IncrementalAggregateBaseTimeFunctionExecutor) {
                if (attributeList.get(i).getName().equals(AGG_EXTERNAL_TIMESTAMP_COL)) {
                    outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + AGG_EXTERNAL_TIMESTAMP_COL + SQL_AS + AGG_EXTERNAL_TIMESTAMP_COL);
                    subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getTimeConversionFunction().replace(PLACEHOLDER_COLUMN, AGG_EXTERNAL_TIMESTAMP_COL).replace(PLACEHOLDER_DURATION, dbAggregationTimeConversionDurationMapping.getDurationMapping(duration)) + SQL_AS + AGG_EXTERNAL_TIMESTAMP_COL);
                    subSelectT2ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getTimeConversionFunction().replace(PLACEHOLDER_COLUMN, AGG_EXTERNAL_TIMESTAMP_COL).replace(PLACEHOLDER_DURATION, dbAggregationTimeConversionDurationMapping.getDurationMapping(duration)) + SQL_AS + AGG_EXTERNAL_TIMESTAMP_COL);
                    onConditionBuilder.add(SUB_SELECT_QUERY_REF_T1 + "." + AGG_EXTERNAL_TIMESTAMP_COL + EQUALS + SUB_SELECT_QUERY_REF_T2 + "." + AGG_EXTERNAL_TIMESTAMP_COL);
                } else {
                    outerSelectColumnJoiner.add(" ? " + SQL_AS + attributeList.get(i).getName());
                }
            } else if (expressionExecutor instanceof MaxAttributeAggregatorExecutor) {
                if (attributeList.get(i).getName().equals(AGG_LAST_TIMESTAMP_COL)) {
                    innerSelectT2ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
                    subSelectT2ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, INNER_SELECT_QUERY_REF_T3 + "." + attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
                    outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T2 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
                    subSelectT2OnConditionBuilder.add(parentAggregationTable.getTableDefinition().getId() + "." + attributeList.get(i).getName() + EQUALS + INNER_SELECT_QUERY_REF_T3 + "." + attributeList.get(i).getName());
                } else {
                    outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
                    subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
                }
            } else if (expressionExecutor instanceof MinAttributeAggregatorExecutor) {
                outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
                subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMinFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
            } else if (expressionExecutor instanceof ConstantExpressionExecutor) {
                outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
            } else if (expressionExecutor instanceof SumAttributeAggregatorExecutor) {
                outerSelectColumnJoiner.add(SUB_SELECT_QUERY_REF_T1 + "." + attributeList.get(i).getName() + SQL_AS + attributeList.get(i).getName());
                subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getSumFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()) + SQL_AS + attributeList.get(i).getName());
            }
            i++;
        }
        groupByQueryBuilder.add(dbAggregationSelectFunctionTemplates.getTimeConversionFunction().replace(PLACEHOLDER_COLUMN, AGG_EXTERNAL_TIMESTAMP_COL).replace(PLACEHOLDER_DURATION, dbAggregationTimeConversionDurationMapping.getDurationMapping(duration)));
        groupByT3QueryBuilder.add(dbAggregationSelectFunctionTemplates.getTimeConversionFunction().replace(PLACEHOLDER_COLUMN, AGG_EXTERNAL_TIMESTAMP_COL).replace(PLACEHOLDER_DURATION, dbAggregationTimeConversionDurationMapping.getDurationMapping(duration)));
        groupByClause = dbAggregationSelectQueryTemplate.getGroupByClause().replace(PLACEHOLDER_COLUMNS, groupByQueryBuilder.toString());
        innerWhereFilterClause = dbAggregationSelectQueryTemplate.getWhereClause().replace(PLACEHOLDER_CONDITION, filterQueryBuilder.toString());
        innerT2Query.add(innerSelectT2ColumnJoiner.toString()).add(innerFromClause).add(innerWhereFilterClause).add(groupByClause);
        subQueryT1.add(subSelectT1ColumnJoiner.toString()).add(innerFromClause).add(innerWhereFilterClause).add(groupByClause);
        subQueryT2.add(dbAggregationSelectQueryTemplate.getSelectQueryWithInnerSelect().replace(PLACEHOLDER_SELECTORS, subSelectT2ColumnJoiner.toString()).replace(PLACEHOLDER_TABLE_NAME, parentAggregationTable.getTableDefinition().getId()).replace(PLACEHOLDER_INNER_QUERY_2, innerT2Query.toString()).replace(PLACEHOLDER_ON_CONDITION, subSelectT2OnConditionBuilder.toString()).replace(PLACEHOLDER_CONDITION, innerT2WhereCondition).replace(PLACEHOLDER_COLUMNS, groupByT3QueryBuilder.toString()));
        finalSelectQuery.add(dbAggregationSelectQueryTemplate.getJoinClause().replace(PLACEHOLDER_SELECTORS, outerSelectColumnJoiner.toString()).replace(PLACEHOLDER_FROM_CONDITION, subQueryT1.toString()).replace(PLACEHOLDER_INNER_QUERY_1, subQueryT2.toString()).replace(PLACEHOLDER_CONDITION, onConditionBuilder.toString()));
        completeQuery.add(insertIntoQueryBuilder.toString()).add(finalSelectQuery.toString());
    } else {
        for (ExpressionExecutor executor : expressionExecutors) {
            if (executor instanceof VariableExpressionExecutor) {
                VariableExpressionExecutor variableExpressionExecutor = (VariableExpressionExecutor) executor;
                if (variableExpressionExecutor.getAttribute().getName().equals(AGG_START_TIMESTAMP_COL)) {
                    subSelectT1ColumnJoiner.add("? " + SQL_AS + variableExpressionExecutor.getAttribute().getName());
                } else {
                    subSelectT1ColumnJoiner.add(variableExpressionExecutor.getAttribute().getName());
                    groupByQueryBuilder.add(variableExpressionExecutor.getAttribute().getName());
                }
            } else if (executor instanceof ConstantExpressionExecutor) {
                if (((ConstantExpressionExecutor) executor).getValue() != null) {
                    subSelectT1ColumnJoiner.add("'" + ((ConstantExpressionExecutor) executor).getValue() + "' " + SQL_AS + attributeList.get(i).getName());
                } else {
                    subSelectT1ColumnJoiner.add(attributeList.get(i).getName());
                }
            } else if (executor instanceof SumAttributeAggregatorExecutor) {
                subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getSumFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()).concat(SQL_AS).concat(attributeList.get(i).getName()));
            } else if (executor instanceof MinAttributeAggregatorExecutor) {
                subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMinFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()).concat(SQL_AS).concat(attributeList.get(i).getName()));
            } else if (executor instanceof MaxAttributeAggregatorExecutor) {
                subSelectT1ColumnJoiner.add(dbAggregationSelectFunctionTemplates.getMaxFunction().replace(PLACEHOLDER_COLUMN, attributeList.get(i).getName()).concat(SQL_AS).concat(attributeList.get(i).getName()));
            }
            i++;
        }
        completeQuery.add(insertIntoQueryBuilder.toString()).add(subSelectT1ColumnJoiner.toString()).add(innerFromClause).add(SQL_WHERE + filterQueryBuilder).add(dbAggregationSelectQueryTemplate.getGroupByClause().replace(PLACEHOLDER_COLUMNS, groupByQueryBuilder.toString()));
    }
    return completeQuery.toString();
}
Also used : MaxAttributeAggregatorExecutor(io.siddhi.core.query.selector.attribute.aggregator.MaxAttributeAggregatorExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) DBAggregationSelectFunctionTemplate(io.siddhi.core.aggregation.persistedaggregation.config.DBAggregationSelectFunctionTemplate) Attribute(io.siddhi.query.api.definition.Attribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) SumAttributeAggregatorExecutor(io.siddhi.core.query.selector.attribute.aggregator.SumAttributeAggregatorExecutor) DBAggregationTimeConversionDurationMapping(io.siddhi.core.aggregation.persistedaggregation.config.DBAggregationTimeConversionDurationMapping) MinAttributeAggregatorExecutor(io.siddhi.core.query.selector.attribute.aggregator.MinAttributeAggregatorExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) IncrementalAggregateBaseTimeFunctionExecutor(io.siddhi.core.executor.incremental.IncrementalAggregateBaseTimeFunctionExecutor) DBAggregationSelectQueryTemplate(io.siddhi.core.aggregation.persistedaggregation.config.DBAggregationSelectQueryTemplate) StringJoiner(java.util.StringJoiner)

Example 17 with ConstantExpressionExecutor

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

the class AggregationParser method populateIncomingAggregatorsAndExecutors.

private static void populateIncomingAggregatorsAndExecutors(AggregationDefinition aggregationDefinition, SiddhiQueryContext siddhiQueryContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> incomingVariableExpressionExecutors, MetaStreamEvent incomingMetaStreamEvent, List<ExpressionExecutor> incomingExpressionExecutors, List<IncrementalAttributeAggregator> incrementalAttributeAggregators, List<Variable> groupByVariableList, List<Expression> outputExpressions, boolean isProcessingOnExternalTime, boolean isDistributed, String shardId) {
    boolean isLatestEventAdded = false;
    ExpressionExecutor timestampExecutor = getTimeStampExecutor(siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent);
    Attribute timestampAttribute = new Attribute(AGG_START_TIMESTAMP_COL, Attribute.Type.LONG);
    incomingMetaStreamEvent.addOutputData(timestampAttribute);
    incomingExpressionExecutors.add(timestampExecutor);
    if (isDistributed) {
        ExpressionExecutor nodeIdExpExecutor = new ConstantExpressionExecutor(shardId, Attribute.Type.STRING);
        incomingExpressionExecutors.add(nodeIdExpExecutor);
        incomingMetaStreamEvent.addOutputData(new Attribute(AGG_SHARD_ID_COL, Attribute.Type.STRING));
    }
    ExpressionExecutor externalTimestampExecutor = null;
    if (isProcessingOnExternalTime) {
        Expression externalTimestampExpression = aggregationDefinition.getAggregateAttribute();
        externalTimestampExecutor = ExpressionParser.parseExpression(externalTimestampExpression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        if (externalTimestampExecutor.getReturnType() == Attribute.Type.STRING) {
            Expression expression = AttributeFunction.function("incrementalAggregator", "timestampInMilliseconds", externalTimestampExpression);
            externalTimestampExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        } else if (externalTimestampExecutor.getReturnType() != Attribute.Type.LONG) {
            throw new SiddhiAppCreationException("Aggregation Definition '" + aggregationDefinition.getId() + "'s timestamp attribute expects " + "long or string, but found " + externalTimestampExecutor.getReturnType() + ". Hence, can't " + "create the siddhi app '" + siddhiQueryContext.getSiddhiAppContext().getName() + "'", externalTimestampExpression.getQueryContextStartIndex(), externalTimestampExpression.getQueryContextEndIndex());
        }
        Attribute externalTimestampAttribute = new Attribute(AGG_EXTERNAL_TIMESTAMP_COL, Attribute.Type.LONG);
        incomingMetaStreamEvent.addOutputData(externalTimestampAttribute);
        incomingExpressionExecutors.add(externalTimestampExecutor);
    }
    AbstractDefinition incomingLastInputStreamDefinition = incomingMetaStreamEvent.getLastInputDefinition();
    for (Variable groupByVariable : groupByVariableList) {
        incomingMetaStreamEvent.addOutputData(incomingLastInputStreamDefinition.getAttributeList().get(incomingLastInputStreamDefinition.getAttributePosition(groupByVariable.getAttributeName())));
        incomingExpressionExecutors.add(ExpressionParser.parseExpression(groupByVariable, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext));
    }
    // Add AGG_TIMESTAMP to output as well
    aggregationDefinition.getAttributeList().add(timestampAttribute);
    // check and set whether the aggregation in happened on an external timestamp
    if (isProcessingOnExternalTime) {
        outputExpressions.add(Expression.variable(AGG_EXTERNAL_TIMESTAMP_COL));
    } else {
        outputExpressions.add(Expression.variable(AGG_START_TIMESTAMP_COL));
    }
    for (OutputAttribute outputAttribute : aggregationDefinition.getSelector().getSelectionList()) {
        Expression expression = outputAttribute.getExpression();
        // If the select contains the aggregation function expression type will be AttributeFunction
        if (expression instanceof AttributeFunction) {
            IncrementalAttributeAggregator incrementalAggregator = null;
            try {
                incrementalAggregator = (IncrementalAttributeAggregator) SiddhiClassLoader.loadExtensionImplementation(new AttributeFunction("incrementalAggregator", ((AttributeFunction) expression).getName(), ((AttributeFunction) expression).getParameters()), IncrementalAttributeAggregatorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
            } catch (SiddhiAppCreationException ex) {
                try {
                    SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
                    processAggregationSelectors(aggregationDefinition, siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent, incomingExpressionExecutors, outputExpressions, outputAttribute, expression);
                } catch (SiddhiAppCreationException e) {
                    throw new SiddhiAppCreationException("'" + ((AttributeFunction) expression).getName() + "' is neither a incremental attribute aggregator extension or a function" + " extension", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                }
            }
            if (incrementalAggregator != null) {
                initIncrementalAttributeAggregator(incomingLastInputStreamDefinition, (AttributeFunction) expression, incrementalAggregator);
                incrementalAttributeAggregators.add(incrementalAggregator);
                aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), incrementalAggregator.getReturnType()));
                outputExpressions.add(incrementalAggregator.aggregate());
            }
        } else {
            if (expression instanceof Variable && groupByVariableList.contains(expression)) {
                Attribute groupByAttribute = null;
                for (Attribute attribute : incomingMetaStreamEvent.getOutputData()) {
                    if (attribute.getName().equals(((Variable) expression).getAttributeName())) {
                        groupByAttribute = attribute;
                        break;
                    }
                }
                if (groupByAttribute == null) {
                    throw new SiddhiAppCreationException("Expected GroupBy attribute '" + ((Variable) expression).getAttributeName() + "' not used in aggregation '" + siddhiQueryContext.getName() + "' processing.", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                }
                aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), groupByAttribute.getType()));
                outputExpressions.add(Expression.variable(groupByAttribute.getName()));
            } else {
                isLatestEventAdded = true;
                processAggregationSelectors(aggregationDefinition, siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent, incomingExpressionExecutors, outputExpressions, outputAttribute, expression);
            }
        }
    }
    if (isProcessingOnExternalTime && isLatestEventAdded) {
        Attribute lastEventTimeStamp = new Attribute(AGG_LAST_TIMESTAMP_COL, Attribute.Type.LONG);
        incomingMetaStreamEvent.addOutputData(lastEventTimeStamp);
        incomingExpressionExecutors.add(externalTimestampExecutor);
    }
}
Also used : Variable(io.siddhi.query.api.expression.Variable) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) Attribute(io.siddhi.query.api.definition.Attribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) Expression(io.siddhi.query.api.expression.Expression) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) IncrementalAttributeAggregator(io.siddhi.core.query.selector.attribute.aggregator.incremental.IncrementalAttributeAggregator) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) AttributeFunction(io.siddhi.query.api.expression.AttributeFunction) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor)

Example 18 with ConstantExpressionExecutor

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

the class SortWindowProcessor method init.

@Override
protected StateFactory init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiQueryContext siddhiQueryContext) {
    if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
        lengthToKeep = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue()));
    } else {
        throw new UnsupportedOperationException("The first parameter should be an integer");
    }
    parameterInfo = new ArrayList<Object[]>();
    eventComparator = new EventComparator();
    for (int i = 1, parametersLength = attributeExpressionExecutors.length; i < parametersLength; i++) {
        if (!(attributeExpressionExecutors[i] instanceof VariableExpressionExecutor)) {
            throw new UnsupportedOperationException("Required a variable, but found a string parameter");
        } else {
            ExpressionExecutor variableExpressionExecutor = attributeExpressionExecutors[i];
            int order;
            String nextParameter;
            if (i + 1 < parametersLength && attributeExpressionExecutors[i + 1].getReturnType() == Attribute.Type.STRING) {
                nextParameter = (String) ((ConstantExpressionExecutor) attributeExpressionExecutors[i + 1]).getValue();
                if (nextParameter.equalsIgnoreCase(DESC)) {
                    order = -1;
                    i++;
                } else if (nextParameter.equalsIgnoreCase(ASC)) {
                    order = 1;
                    i++;
                } else {
                    throw new UnsupportedOperationException("Parameter string literals should only be \"asc\" or " + "\"desc\"");
                }
            } else {
                // assigning the default order: "asc"
                order = 1;
            }
            parameterInfo.add(new Object[] { variableExpressionExecutor, order });
        }
    }
    return () -> new WindowState();
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor)

Example 19 with ConstantExpressionExecutor

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

the class TimeBatchWindowProcessor method init.

@Override
protected StateFactory init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, StreamEventClonerHolder streamEventClonerHolder, boolean outputExpectsExpiredEvents, boolean findToBeExecuted, SiddhiQueryContext siddhiQueryContext) {
    this.outputExpectsExpiredEvents = outputExpectsExpiredEvents;
    this.siddhiQueryContext = siddhiQueryContext;
    if (attributeExpressionExecutors.length == 1) {
        initTimeParameter(attributeExpressionExecutors[0]);
    } else if (attributeExpressionExecutors.length == 2) {
        initTimeParameter(attributeExpressionExecutors[0]);
        if (!(attributeExpressionExecutors[1] instanceof ConstantExpressionExecutor)) {
            throw new SiddhiAppValidationException("TimeBatch window's window.time (2nd) parameter should be " + "constant but found a dynamic attribute " + attributeExpressionExecutors[1].getClass().getCanonicalName());
        }
        // start time
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
            isStartTimeEnabled = true;
            startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        } else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.LONG) {
            isStartTimeEnabled = true;
            startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        } else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.BOOL) {
            isStreamCurrentEvents = Boolean.valueOf(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        } else {
            throw new SiddhiAppValidationException("TimeBatch window's 2nd parameter " + "should be 'start.time' which is int or long, or 'stream.current.event' which is bool " + " but found " + attributeExpressionExecutors[1].getReturnType());
        }
    } else if (attributeExpressionExecutors.length == 3) {
        initTimeParameter(attributeExpressionExecutors[0]);
        if (!(attributeExpressionExecutors[1] instanceof ConstantExpressionExecutor)) {
            throw new SiddhiAppValidationException("TimeBatch window's window.time (2nd) parameter 'start.time' " + "should be a constant but found a dynamic attribute " + attributeExpressionExecutors[1].getClass().getCanonicalName());
        }
        // start time
        isStartTimeEnabled = true;
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
            startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        } else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.LONG) {
            startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        } else {
            throw new SiddhiAppValidationException("TimeBatch window's 2nd parameter " + "should be 'start.time' which is int or long, " + " but found " + attributeExpressionExecutors[1].getReturnType());
        }
        if (!(attributeExpressionExecutors[2] instanceof ConstantExpressionExecutor)) {
            throw new SiddhiAppValidationException("TimeBatch window's window.time (3rd) parameter " + "'stream.current.event' should be a constant but found a dynamic attribute " + attributeExpressionExecutors[2].getClass().getCanonicalName());
        }
        if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.BOOL) {
            isStreamCurrentEvents = Boolean.valueOf(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[2]).getValue()));
        } else {
            throw new SiddhiAppValidationException("TimeBatch window's 3rd parameter " + "should be 'stream.current.event' which is bool " + " but found " + attributeExpressionExecutors[2].getReturnType());
        }
    } else {
        throw new SiddhiAppValidationException("Time window should only have one or two parameters. " + "(<int|long|time> windowTime), but found " + attributeExpressionExecutors.length + " input " + "attributes");
    }
    return () -> new WindowState(streamEventClonerHolder, outputExpectsExpiredEvents, findToBeExecuted);
}
Also used : SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor)

Example 20 with ConstantExpressionExecutor

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

the class EventTestCase method testConditionExpressionExecutors.

@Test
public void testConditionExpressionExecutors() {
    // StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute
    // .Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    VariableExpressionExecutor priceVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("price", Attribute.Type.FLOAT), 0, 0);
    priceVariableExpressionExecutor.setPosition(new int[] { 0, SiddhiConstants.UNKNOWN_STATE, SiddhiConstants.OUTPUT_DATA_INDEX, 1 });
    VariableExpressionExecutor volumeVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("volume", Attribute.Type.INT), 0, 0);
    volumeVariableExpressionExecutor.setPosition(new int[] { 0, SiddhiConstants.UNKNOWN_STATE, SiddhiConstants.OUTPUT_DATA_INDEX, 2 });
    ExpressionExecutor compareLessThanExecutor = new LessThanCompareConditionExpressionExecutorFloatFloat(new ConstantExpressionExecutor(10f, Attribute.Type.FLOAT), priceVariableExpressionExecutor);
    ExpressionExecutor compareGreaterThanExecutor = new GreaterThanCompareConditionExpressionExecutorIntInt(new ConstantExpressionExecutor(10, Attribute.Type.INT), volumeVariableExpressionExecutor);
    ExpressionExecutor andExecutor = new AndConditionExpressionExecutor(compareLessThanExecutor, compareGreaterThanExecutor);
    int count = 0;
    for (int i = 0; i < 3; i++) {
        StreamEvent event = new StreamEvent(0, 0, 3);
        event.setOutputData(new Object[] { "WSO2", i * 11f, 5 });
        if ((Boolean) andExecutor.execute(event)) {
            count++;
        }
    }
    AssertJUnit.assertEquals("Two events should pass through executor", 2, count);
}
Also used : LessThanCompareConditionExpressionExecutorFloatFloat(io.siddhi.core.executor.condition.compare.lessthan.LessThanCompareConditionExpressionExecutorFloatFloat) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) AndConditionExpressionExecutor(io.siddhi.core.executor.condition.AndConditionExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) Attribute(io.siddhi.query.api.definition.Attribute) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) GreaterThanCompareConditionExpressionExecutorIntInt(io.siddhi.core.executor.condition.compare.greaterthan.GreaterThanCompareConditionExpressionExecutorIntInt) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) AndConditionExpressionExecutor(io.siddhi.core.executor.condition.AndConditionExpressionExecutor) Test(org.testng.annotations.Test)

Aggregations

ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)21 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)14 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)14 Attribute (io.siddhi.query.api.definition.Attribute)14 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)8 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)7 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)6 Variable (io.siddhi.query.api.expression.Variable)6 ArrayList (java.util.ArrayList)6 SiddhiAppValidationException (io.siddhi.query.api.exception.SiddhiAppValidationException)5 Expression (io.siddhi.query.api.expression.Expression)5 HashMap (java.util.HashMap)5 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)4 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)4 AndConditionExpressionExecutor (io.siddhi.core.executor.condition.AndConditionExpressionExecutor)3 QueryableProcessor (io.siddhi.core.query.processor.stream.window.QueryableProcessor)3 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)3 Map (java.util.Map)3 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)2 MetaStateEventAttribute (io.siddhi.core.event.state.MetaStateEventAttribute)2