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();
}
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);
}
}
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();
}
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);
}
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);
}
Aggregations