use of org.wso2.siddhi.query.api.expression.Variable in project siddhi by wso2.
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);
}
use of org.wso2.siddhi.query.api.expression.Variable in project siddhi by wso2.
the class ExpressionBuilder method buildStreamVariableExecutor.
private void buildStreamVariableExecutor(Variable variable, int streamEventChainIndex, ExpressionVisitor expressionVisitor, Attribute.Type type) {
String id = variable.getAttributeName();
if (variable.getStreamId() != null) {
id = variable.getStreamId() + "." + id;
}
expressionVisitor.beginVisitStreamVariable(id, variable.getStreamId(), variable.getAttributeName(), type);
if (!variableExpressionExecutorMap.containsKey(id)) {
ExpressionExecutor variableExpressionExecutor = ExpressionParser.parseExpression(variable, matchingMetaInfoHolder.getMetaStateEvent(), streamEventChainIndex, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
variableExpressionExecutorMap.put(id, variableExpressionExecutor);
}
expressionVisitor.endVisitStreamVariable(id, variable.getStreamId(), variable.getAttributeName(), type);
}
use of org.wso2.siddhi.query.api.expression.Variable in project siddhi by wso2.
the class AggregationParser method parse.
public static AggregationRuntime parse(AggregationDefinition aggregationDefinition, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap, SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder) {
if (aggregationDefinition == null) {
throw new SiddhiAppCreationException("AggregationDefinition instance is null. " + "Hence, can't create the siddhi app '" + siddhiAppContext.getName() + "'");
}
if (aggregationDefinition.getTimePeriod() == null) {
throw new SiddhiAppCreationException("AggregationDefinition '" + aggregationDefinition.getId() + "'s timePeriod is null. " + "Hence, can't create the siddhi app '" + siddhiAppContext.getName() + "'", aggregationDefinition.getQueryContextStartIndex(), aggregationDefinition.getQueryContextEndIndex());
}
if (aggregationDefinition.getSelector() == null) {
throw new SiddhiAppCreationException("AggregationDefinition '" + aggregationDefinition.getId() + "'s selection is not defined. " + "Hence, can't create the siddhi app '" + siddhiAppContext.getName() + "'", aggregationDefinition.getQueryContextStartIndex(), aggregationDefinition.getQueryContextEndIndex());
}
if (streamDefinitionMap.get(aggregationDefinition.getBasicSingleInputStream().getStreamId()) == null) {
throw new SiddhiAppCreationException("Stream " + aggregationDefinition.getBasicSingleInputStream().getStreamId() + " has not been defined");
}
try {
List<VariableExpressionExecutor> incomingVariableExpressionExecutors = new ArrayList<>();
String aggregatorName = aggregationDefinition.getId();
StreamRuntime streamRuntime = InputStreamParser.parse(aggregationDefinition.getBasicSingleInputStream(), siddhiAppContext, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, incomingVariableExpressionExecutors, null, false, aggregatorName);
// Get original meta for later use.
MetaStreamEvent incomingMetaStreamEvent = (MetaStreamEvent) streamRuntime.getMetaComplexEvent();
// Create new meta stream event.
// This must hold the timestamp, group by attributes (if given) and the incremental attributes, in
// onAfterWindowData array
// Example format: AGG_TIMESTAMP, groupByAttribute1, groupByAttribute2, AGG_incAttribute1, AGG_incAttribute2
// AGG_incAttribute1, AGG_incAttribute2 would have the same attribute names as in
// finalListOfIncrementalAttributes
// To enter data as onAfterWindowData
incomingMetaStreamEvent.initializeAfterWindowData();
List<ExpressionExecutor> incomingExpressionExecutors = new ArrayList<>();
List<IncrementalAttributeAggregator> incrementalAttributeAggregators = new ArrayList<>();
List<Variable> groupByVariableList = aggregationDefinition.getSelector().getGroupByList();
boolean isProcessingOnExternalTime = aggregationDefinition.getAggregateAttribute() != null;
// Expressions to get
List<Expression> outputExpressions = new ArrayList<>();
// final aggregate outputs. e.g avg = sum/count
// Expression executors to get
List<ExpressionExecutor> outputExpressionExecutors = new ArrayList<>();
// final aggregate outputs. e.g avg = sum/count
populateIncomingAggregatorsAndExecutors(aggregationDefinition, siddhiAppContext, tableMap, incomingVariableExpressionExecutors, aggregatorName, incomingMetaStreamEvent, incomingExpressionExecutors, incrementalAttributeAggregators, groupByVariableList, outputExpressions);
int baseAggregatorBeginIndex = incomingMetaStreamEvent.getOutputData().size();
List<Expression> finalBaseAggregators = getFinalBaseAggregators(siddhiAppContext, tableMap, incomingVariableExpressionExecutors, aggregatorName, incomingMetaStreamEvent, incomingExpressionExecutors, incrementalAttributeAggregators);
StreamDefinition incomingOutputStreamDefinition = StreamDefinition.id("");
incomingOutputStreamDefinition.setQueryContextStartIndex(aggregationDefinition.getQueryContextStartIndex());
incomingOutputStreamDefinition.setQueryContextEndIndex(aggregationDefinition.getQueryContextEndIndex());
MetaStreamEvent processedMetaStreamEvent = new MetaStreamEvent();
for (Attribute attribute : incomingMetaStreamEvent.getOutputData()) {
incomingOutputStreamDefinition.attribute(attribute.getName(), attribute.getType());
processedMetaStreamEvent.addOutputData(attribute);
}
incomingMetaStreamEvent.setOutputDefinition(incomingOutputStreamDefinition);
processedMetaStreamEvent.addInputDefinition(incomingOutputStreamDefinition);
processedMetaStreamEvent.setOutputDefinition(incomingOutputStreamDefinition);
// Executors of processing meta
List<VariableExpressionExecutor> processVariableExpressionExecutors = new ArrayList<>();
boolean groupBy = aggregationDefinition.getSelector().getGroupByList().size() != 0;
List<ExpressionExecutor> processExpressionExecutors = constructProcessExpressionExecutors(siddhiAppContext, tableMap, aggregatorName, baseAggregatorBeginIndex, finalBaseAggregators, incomingOutputStreamDefinition, processedMetaStreamEvent, processVariableExpressionExecutors, groupBy);
outputExpressionExecutors.addAll(outputExpressions.stream().map(expression -> ExpressionParser.parseExpression(expression, processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, siddhiAppContext, groupBy, 0, aggregatorName)).collect(Collectors.toList()));
// Create group by key generator
GroupByKeyGenerator groupByKeyGenerator = null;
if (groupBy) {
groupByKeyGenerator = new GroupByKeyGenerator(groupByVariableList, processedMetaStreamEvent, SiddhiConstants.UNKNOWN_STATE, tableMap, processVariableExpressionExecutors, siddhiAppContext, aggregatorName);
}
// Create new scheduler
EntryValveExecutor entryValveExecutor = new EntryValveExecutor(siddhiAppContext);
LockWrapper lockWrapper = new LockWrapper(aggregatorName);
lockWrapper.setLock(new ReentrantLock());
Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveExecutor, siddhiAppContext);
scheduler.init(lockWrapper, aggregatorName);
scheduler.setStreamEventPool(new StreamEventPool(processedMetaStreamEvent, 10));
QueryParserHelper.reduceMetaComplexEvent(incomingMetaStreamEvent);
QueryParserHelper.reduceMetaComplexEvent(processedMetaStreamEvent);
QueryParserHelper.updateVariablePosition(incomingMetaStreamEvent, incomingVariableExpressionExecutors);
QueryParserHelper.updateVariablePosition(processedMetaStreamEvent, processVariableExpressionExecutors);
List<TimePeriod.Duration> incrementalDurations = getSortedPeriods(aggregationDefinition.getTimePeriod());
Map<TimePeriod.Duration, Table> aggregationTables = initDefaultTables(aggregatorName, incrementalDurations, processedMetaStreamEvent.getOutputStreamDefinition(), siddhiAppRuntimeBuilder, aggregationDefinition.getAnnotations(), groupByVariableList);
int bufferSize = 0;
Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_BUFFER_SIZE, null, aggregationDefinition.getAnnotations());
if (element != null) {
try {
bufferSize = Integer.parseInt(element.getValue());
} catch (NumberFormatException e) {
throw new SiddhiAppCreationException(e.getMessage() + ": BufferSize must be an integer");
}
}
if (bufferSize > 0) {
TimePeriod.Duration rootDuration = incrementalDurations.get(0);
if (rootDuration == TimePeriod.Duration.MONTHS || rootDuration == TimePeriod.Duration.YEARS) {
throw new SiddhiAppCreationException("A buffer size greater than 0 can be provided, only when the " + "first duration value is seconds, minutes, hours or days");
}
if (!isProcessingOnExternalTime) {
throw new SiddhiAppCreationException("Buffer size cannot be specified when events are aggregated " + "based on event arrival time.");
// Buffer size is used to process out of order events. However, events would never be out of
// order if they are processed based on event arrival time.
}
} else if (bufferSize < 0) {
throw new SiddhiAppCreationException("Expected a positive integer as the buffer size, but found " + bufferSize + " as the provided value");
}
boolean ignoreEventsOlderThanBuffer = false;
element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_IGNORE_EVENTS_OLDER_THAN_BUFFER, null, aggregationDefinition.getAnnotations());
if (element != null) {
if (element.getValue().equalsIgnoreCase("true")) {
ignoreEventsOlderThanBuffer = true;
} else if (!element.getValue().equalsIgnoreCase("false")) {
throw new SiddhiAppCreationException("IgnoreEventsOlderThanBuffer value must " + "be true or false");
}
}
Map<TimePeriod.Duration, IncrementalExecutor> incrementalExecutorMap = buildIncrementalExecutors(isProcessingOnExternalTime, processedMetaStreamEvent, processExpressionExecutors, groupByKeyGenerator, bufferSize, ignoreEventsOlderThanBuffer, incrementalDurations, aggregationTables, siddhiAppContext, aggregatorName);
// Recreate in-memory data from tables
RecreateInMemoryData recreateInMemoryData = new RecreateInMemoryData(incrementalDurations, aggregationTables, incrementalExecutorMap, siddhiAppContext, processedMetaStreamEvent, tableMap, windowMap, aggregationMap);
IncrementalExecutor rootIncrementalExecutor = incrementalExecutorMap.get(incrementalDurations.get(0));
rootIncrementalExecutor.setScheduler(scheduler);
// Connect entry valve to root incremental executor
entryValveExecutor.setNextExecutor(rootIncrementalExecutor);
QueryParserHelper.initStreamRuntime(streamRuntime, incomingMetaStreamEvent, lockWrapper, aggregatorName);
LatencyTracker latencyTrackerFind = null;
LatencyTracker latencyTrackerInsert = null;
ThroughputTracker throughputTrackerFind = null;
ThroughputTracker throughputTrackerInsert = null;
if (siddhiAppContext.getStatisticsManager() != null) {
latencyTrackerFind = QueryParserHelper.createLatencyTracker(siddhiAppContext, aggregationDefinition.getId(), SiddhiConstants.METRIC_INFIX_WINDOWS, SiddhiConstants.METRIC_TYPE_FIND);
latencyTrackerInsert = QueryParserHelper.createLatencyTracker(siddhiAppContext, aggregationDefinition.getId(), SiddhiConstants.METRIC_INFIX_WINDOWS, SiddhiConstants.METRIC_TYPE_INSERT);
throughputTrackerFind = QueryParserHelper.createThroughputTracker(siddhiAppContext, aggregationDefinition.getId(), SiddhiConstants.METRIC_INFIX_WINDOWS, SiddhiConstants.METRIC_TYPE_FIND);
throughputTrackerInsert = QueryParserHelper.createThroughputTracker(siddhiAppContext, aggregationDefinition.getId(), SiddhiConstants.METRIC_INFIX_WINDOWS, SiddhiConstants.METRIC_TYPE_INSERT);
}
streamRuntime.setCommonProcessor(new IncrementalAggregationProcessor(rootIncrementalExecutor, incomingExpressionExecutors, processedMetaStreamEvent, latencyTrackerInsert, throughputTrackerInsert, siddhiAppContext));
List<ExpressionExecutor> baseExecutors = cloneExpressionExecutors(processExpressionExecutors);
ExpressionExecutor timestampExecutor = baseExecutors.remove(0);
return new AggregationRuntime(aggregationDefinition, incrementalExecutorMap, aggregationTables, ((SingleStreamRuntime) streamRuntime), entryValveExecutor, incrementalDurations, siddhiAppContext, baseExecutors, timestampExecutor, processedMetaStreamEvent, outputExpressionExecutors, latencyTrackerFind, throughputTrackerFind, recreateInMemoryData);
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, aggregationDefinition, siddhiAppContext);
throw t;
}
}
use of org.wso2.siddhi.query.api.expression.Variable in project siddhi by wso2.
the class CollectionExpressionParser method parseInternalCollectionExpression.
/**
* Parse the given expression and create the appropriate Executor by recursively traversing the expression.
*
* @param expression Expression to be parsed
* @param matchingMetaInfoHolder matchingMetaInfoHolder
* @param indexedEventHolder indexed event holder
* @return ExpressionExecutor
*/
private static CollectionExpression parseInternalCollectionExpression(Expression expression, MatchingMetaInfoHolder matchingMetaInfoHolder, IndexedEventHolder indexedEventHolder) {
if (expression instanceof And) {
CollectionExpression leftCollectionExpression = parseInternalCollectionExpression(((And) expression).getLeftExpression(), matchingMetaInfoHolder, indexedEventHolder);
CollectionExpression rightCollectionExpression = parseInternalCollectionExpression(((And) expression).getRightExpression(), matchingMetaInfoHolder, indexedEventHolder);
if (leftCollectionExpression.getCollectionScope() == NON && rightCollectionExpression.getCollectionScope() == NON) {
return new BasicCollectionExpression(expression, NON);
} else if ((leftCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PRIMARY_KEY_RESULT_SET || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET) && (rightCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET)) {
Set<String> primaryKeys = new HashSet<>();
primaryKeys.addAll(leftCollectionExpression.getMultiPrimaryKeys());
primaryKeys.addAll(rightCollectionExpression.getMultiPrimaryKeys());
if (indexedEventHolder.getPrimaryKeyReferenceHolders() != null && primaryKeys.size() == indexedEventHolder.getPrimaryKeyReferenceHolders().length) {
return new AndMultiPrimaryKeyCollectionExpression(expression, PRIMARY_KEY_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
} else {
return new AndCollectionExpression(expression, PARTIAL_PRIMARY_KEY_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
}
// TODO support query rewriting to group all PARTIAL_PRIMARY_KEY_RESULT_SETs together such that it can
// build AndMultiPrimaryKeyCollectionExpression.
} else if ((leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET || leftCollectionExpression.getCollectionScope() == NON || leftCollectionExpression.getCollectionScope() == EXHAUSTIVE) && (rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == NON || rightCollectionExpression.getCollectionScope() == EXHAUSTIVE)) {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
} else {
return new AndCollectionExpression(expression, OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
}
} else if (expression instanceof Or) {
CollectionExpression leftCollectionExpression = parseInternalCollectionExpression(((Or) expression).getLeftExpression(), matchingMetaInfoHolder, indexedEventHolder);
CollectionExpression rightCollectionExpression = parseInternalCollectionExpression(((Or) expression).getRightExpression(), matchingMetaInfoHolder, indexedEventHolder);
if (leftCollectionExpression.getCollectionScope() == NON && rightCollectionExpression.getCollectionScope() == NON) {
return new BasicCollectionExpression(expression, NON);
} else if (leftCollectionExpression.getCollectionScope() == EXHAUSTIVE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == EXHAUSTIVE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET) {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
} else {
return new OrCollectionExpression(expression, OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
}
} else if (expression instanceof Not) {
CollectionExpression notCollectionExpression = parseInternalCollectionExpression(((Not) expression).getExpression(), matchingMetaInfoHolder, indexedEventHolder);
switch(notCollectionExpression.getCollectionScope()) {
case NON:
return new BasicCollectionExpression(expression, NON);
case PRIMARY_KEY_ATTRIBUTE:
return new NotCollectionExpression(expression, PRIMARY_KEY_RESULT_SET, notCollectionExpression);
case INDEXED_ATTRIBUTE:
return new NotCollectionExpression(expression, INDEXED_RESULT_SET, notCollectionExpression);
case PRIMARY_KEY_RESULT_SET:
case INDEXED_RESULT_SET:
case OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET:
return new NotCollectionExpression(expression, OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET, notCollectionExpression);
case PARTIAL_PRIMARY_KEY_ATTRIBUTE:
case PARTIAL_PRIMARY_KEY_RESULT_SET:
case EXHAUSTIVE:
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
} else if (expression instanceof Compare) {
CollectionExpression leftCollectionExpression = parseInternalCollectionExpression(((Compare) expression).getLeftExpression(), matchingMetaInfoHolder, indexedEventHolder);
CollectionExpression rightCollectionExpression = parseInternalCollectionExpression(((Compare) expression).getRightExpression(), matchingMetaInfoHolder, indexedEventHolder);
if (leftCollectionExpression.getCollectionScope() == NON && rightCollectionExpression.getCollectionScope() == NON) {
// comparing two stream attributes with O(1) time complexity
return new BasicCollectionExpression(expression, NON);
} else if ((leftCollectionExpression.getCollectionScope() == INDEXED_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE) && rightCollectionExpression.getCollectionScope() == NON) {
switch(leftCollectionExpression.getCollectionScope()) {
case INDEXED_ATTRIBUTE:
return new CompareCollectionExpression((Compare) expression, INDEXED_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
case PRIMARY_KEY_ATTRIBUTE:
return new CompareCollectionExpression((Compare) expression, PRIMARY_KEY_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
case PARTIAL_PRIMARY_KEY_ATTRIBUTE:
return new CompareCollectionExpression((Compare) expression, PARTIAL_PRIMARY_KEY_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
}
} else if (leftCollectionExpression.getCollectionScope() == NON && (rightCollectionExpression.getCollectionScope() == INDEXED_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE)) {
Compare.Operator operator = ((Compare) expression).getOperator();
// moving let to right
switch(operator) {
case LESS_THAN:
operator = Compare.Operator.GREATER_THAN;
break;
case GREATER_THAN:
operator = Compare.Operator.LESS_THAN;
break;
case LESS_THAN_EQUAL:
operator = Compare.Operator.GREATER_THAN_EQUAL;
break;
case GREATER_THAN_EQUAL:
operator = Compare.Operator.LESS_THAN_EQUAL;
break;
case EQUAL:
break;
case NOT_EQUAL:
break;
}
switch(rightCollectionExpression.getCollectionScope()) {
case INDEXED_ATTRIBUTE:
return new CompareCollectionExpression((Compare) expression, INDEXED_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
case PRIMARY_KEY_ATTRIBUTE:
return new CompareCollectionExpression((Compare) expression, PRIMARY_KEY_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
case PARTIAL_PRIMARY_KEY_ATTRIBUTE:
return new CompareCollectionExpression((Compare) expression, PARTIAL_PRIMARY_KEY_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
}
} else {
// comparing non indexed table with stream attributes or another table attribute
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
} else if (expression instanceof Constant) {
return new BasicCollectionExpression(expression, NON);
} else if (expression instanceof Variable) {
if (isCollectionVariable(matchingMetaInfoHolder, (Variable) expression)) {
if (indexedEventHolder.isAttributeIndexed(((Variable) expression).getAttributeName())) {
return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), INDEXED_ATTRIBUTE);
} else if (indexedEventHolder.isMultiPrimaryKeyAttribute(((Variable) expression).getAttributeName())) {
if (indexedEventHolder.getPrimaryKeyReferenceHolders() != null && indexedEventHolder.getPrimaryKeyReferenceHolders().length == 1) {
return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), PRIMARY_KEY_ATTRIBUTE);
} else {
return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), PARTIAL_PRIMARY_KEY_ATTRIBUTE);
}
} else {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
} else {
return new BasicCollectionExpression(expression, NON);
}
} else if (expression instanceof Multiply) {
CollectionExpression left = parseInternalCollectionExpression(((Multiply) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
CollectionExpression right = parseInternalCollectionExpression(((Multiply) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
if (left.getCollectionScope() == NON && right.getCollectionScope() == NON) {
return new BasicCollectionExpression(expression, NON);
} else {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
} else if (expression instanceof Add) {
CollectionExpression left = parseInternalCollectionExpression(((Add) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
CollectionExpression right = parseInternalCollectionExpression(((Add) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
if (left.getCollectionScope() == NON && right.getCollectionScope() == NON) {
return new BasicCollectionExpression(expression, NON);
} else {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
} else if (expression instanceof Subtract) {
CollectionExpression left = parseInternalCollectionExpression(((Subtract) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
CollectionExpression right = parseInternalCollectionExpression(((Subtract) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
if (left.getCollectionScope() == NON && right.getCollectionScope() == NON) {
return new BasicCollectionExpression(expression, NON);
} else {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
} else if (expression instanceof Mod) {
CollectionExpression left = parseInternalCollectionExpression(((Mod) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
CollectionExpression right = parseInternalCollectionExpression(((Mod) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
if (left.getCollectionScope() == NON && right.getCollectionScope() == NON) {
return new BasicCollectionExpression(expression, NON);
} else {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
} else if (expression instanceof Divide) {
CollectionExpression left = parseInternalCollectionExpression(((Divide) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
CollectionExpression right = parseInternalCollectionExpression(((Divide) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
if (left.getCollectionScope() == NON && right.getCollectionScope() == NON) {
return new BasicCollectionExpression(expression, NON);
} else {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
} else if (expression instanceof AttributeFunction) {
Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
for (Expression aExpression : innerExpressions) {
CollectionExpression aCollectionExpression = parseInternalCollectionExpression(aExpression, matchingMetaInfoHolder, indexedEventHolder);
if (aCollectionExpression.getCollectionScope() != NON) {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
}
return new BasicCollectionExpression(expression, NON);
} else if (expression instanceof In) {
CollectionExpression inCollectionExpression = parseInternalCollectionExpression(((In) expression).getExpression(), matchingMetaInfoHolder, indexedEventHolder);
if (inCollectionExpression.getCollectionScope() != NON) {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
return new BasicCollectionExpression(expression, NON);
} else if (expression instanceof IsNull) {
CollectionExpression nullCollectionExpression = parseInternalCollectionExpression(((IsNull) expression).getExpression(), matchingMetaInfoHolder, indexedEventHolder);
if (nullCollectionExpression.getCollectionScope() == NON) {
return new BasicCollectionExpression(expression, NON);
} else if (nullCollectionExpression.getCollectionScope() == INDEXED_ATTRIBUTE) {
return new NullCollectionExpression(expression, INDEXED_RESULT_SET, ((AttributeCollectionExpression) nullCollectionExpression).getAttribute());
} else if (nullCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE) {
return new NullCollectionExpression(expression, PRIMARY_KEY_RESULT_SET, ((AttributeCollectionExpression) nullCollectionExpression).getAttribute());
} else {
return new BasicCollectionExpression(expression, EXHAUSTIVE);
}
}
throw new UnsupportedOperationException(expression.toString() + " not supported!");
}
use of org.wso2.siddhi.query.api.expression.Variable in project siddhi by wso2.
the class ExpressionParser method parseInnerExpression.
/**
* Parse the set of inner expression of AttributeFunctionExtensions and handling all (*) cases
*
* @param innerExpressions InnerExpressions to be parsed
* @param metaEvent Meta Event
* @param currentState Current state number
* @param tableMap Event Table Map
* @param executorList List to hold VariableExpressionExecutors to update after query parsing @return
* @param siddhiAppContext SiddhiAppContext
* @param groupBy is for groupBy expression
* @param defaultStreamEventIndex Default StreamEvent Index
* @return List of expressionExecutors
*/
private static ExpressionExecutor[] parseInnerExpression(Expression[] innerExpressions, MetaComplexEvent metaEvent, int currentState, Map<String, Table> tableMap, List<VariableExpressionExecutor> executorList, SiddhiAppContext siddhiAppContext, boolean groupBy, int defaultStreamEventIndex, String queryName) {
ExpressionExecutor[] innerExpressionExecutors;
if (innerExpressions != null) {
if (innerExpressions.length > 0) {
innerExpressionExecutors = new ExpressionExecutor[innerExpressions.length];
for (int i = 0, innerExpressionsLength = innerExpressions.length; i < innerExpressionsLength; i++) {
innerExpressionExecutors[i] = parseExpression(innerExpressions[i], metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
}
} else {
List<Expression> outputAttributes = new ArrayList<Expression>();
if (metaEvent instanceof MetaStreamEvent) {
List<Attribute> attributeList = ((MetaStreamEvent) metaEvent).getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
outputAttributes.add(new Variable(attribute.getName()));
}
} else {
for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaEvent).getMetaStreamEvents()) {
List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
Expression outputAttribute = new Variable(attribute.getName());
if (!outputAttributes.contains(outputAttribute)) {
outputAttributes.add(outputAttribute);
} else {
List<AbstractDefinition> definitions = new ArrayList<AbstractDefinition>();
for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaEvent).getMetaStreamEvents()) {
definitions.add(aMetaStreamEvent.getLastInputDefinition());
}
throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, attribute.getQueryContextStartIndex(), attribute.getQueryContextEndIndex());
}
}
}
}
innerExpressionExecutors = new ExpressionExecutor[outputAttributes.size()];
for (int i = 0, innerExpressionsLength = outputAttributes.size(); i < innerExpressionsLength; i++) {
innerExpressionExecutors[i] = parseExpression(outputAttributes.get(i), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
}
}
} else {
innerExpressionExecutors = new ExpressionExecutor[0];
}
return innerExpressionExecutors;
}
Aggregations