use of org.wso2.siddhi.query.api.execution.query.selection.Selector 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.execution.query.selection.Selector in project siddhi by wso2.
the class QuerySelector method clone.
public QuerySelector clone(String key) {
QuerySelector clonedQuerySelector = new QuerySelector(id + key, selector, currentOn, expiredOn, siddhiAppContext);
List<AttributeProcessor> clonedAttributeProcessorList = new ArrayList<AttributeProcessor>();
for (AttributeProcessor attributeProcessor : attributeProcessorList) {
clonedAttributeProcessorList.add(attributeProcessor.cloneProcessor(key));
}
clonedQuerySelector.attributeProcessorList = clonedAttributeProcessorList;
clonedQuerySelector.isGroupBy = isGroupBy;
clonedQuerySelector.containsAggregator = containsAggregator;
clonedQuerySelector.groupByKeyGenerator = groupByKeyGenerator;
clonedQuerySelector.havingConditionExecutor = havingConditionExecutor;
clonedQuerySelector.eventPopulator = eventPopulator;
clonedQuerySelector.batchingEnabled = batchingEnabled;
clonedQuerySelector.isOrderBy = isOrderBy;
clonedQuerySelector.orderByEventComparator = orderByEventComparator;
clonedQuerySelector.limit = limit;
return clonedQuerySelector;
}
use of org.wso2.siddhi.query.api.execution.query.selection.Selector in project siddhi by wso2.
the class SelectorParser method getAttributeProcessors.
/**
* Method to construct AttributeProcessor list for the selector.
*
* @param selector Selector
* @param id stream id
* @param siddhiAppContext siddhi app context
* @param metaComplexEvent meta ComplexEvent
* @param tableMap Table Map
* @param variableExpressionExecutors list of VariableExpressionExecutors
* @param outputStream
* @return list of AttributeProcessors
*/
private static List<AttributeProcessor> getAttributeProcessors(Selector selector, String id, SiddhiAppContext siddhiAppContext, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, OutputStream outputStream, String queryName, int metaPosition) {
List<AttributeProcessor> attributeProcessorList = new ArrayList<>();
StreamDefinition outputDefinition = StreamDefinition.id(id);
outputDefinition.setQueryContextStartIndex(outputStream.getQueryContextStartIndex());
outputDefinition.setQueryContextEndIndex(outputStream.getQueryContextEndIndex());
List<OutputAttribute> outputAttributes = selector.getSelectionList();
if (selector.getSelectionList().size() == 0) {
if (metaComplexEvent instanceof MetaStreamEvent) {
List<Attribute> attributeList = ((MetaStreamEvent) metaComplexEvent).getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
}
} else {
int position = 0;
for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
if (metaPosition == SiddhiConstants.UNKNOWN_STATE || metaPosition == position) {
List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
OutputAttribute outputAttribute = new OutputAttribute(new Variable(attribute.getName()));
if (!outputAttributes.contains(outputAttribute)) {
outputAttributes.add(outputAttribute);
} else {
List<AbstractDefinition> definitions = new ArrayList<>();
for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
definitions.add(aMetaStreamEvent.getLastInputDefinition());
}
throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, outputStream.getQueryContextStartIndex(), outputStream.getQueryContextEndIndex());
}
}
}
++position;
}
}
}
int i = 0;
for (OutputAttribute outputAttribute : outputAttributes) {
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(outputAttribute.getExpression(), metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, !(selector.getGroupByList().isEmpty()), 0, queryName);
if (expressionExecutor instanceof VariableExpressionExecutor) {
// for variables we will directly put
// value at conversion stage
VariableExpressionExecutor executor = ((VariableExpressionExecutor) expressionExecutor);
if (metaComplexEvent instanceof MetaStateEvent) {
((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(new MetaStateEventAttribute(executor.getAttribute(), executor.getPosition()));
} else {
((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(executor.getAttribute());
}
outputDefinition.attribute(outputAttribute.getRename(), ((VariableExpressionExecutor) expressionExecutor).getAttribute().getType());
} else {
// To maintain output variable positions
if (metaComplexEvent instanceof MetaStateEvent) {
((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
} else {
((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
}
AttributeProcessor attributeProcessor = new AttributeProcessor(expressionExecutor);
attributeProcessor.setOutputPosition(i);
attributeProcessorList.add(attributeProcessor);
outputDefinition.attribute(outputAttribute.getRename(), attributeProcessor.getOutputType());
}
i++;
}
metaComplexEvent.setOutputDefinition(outputDefinition);
return attributeProcessorList;
}
use of org.wso2.siddhi.query.api.execution.query.selection.Selector in project siddhi by wso2.
the class StoreQueryParser method populateFindStoreQueryRuntime.
private static void populateFindStoreQueryRuntime(FindStoreQueryRuntime findStoreQueryRuntime, MatchingMetaInfoHolder metaStreamInfoHolder, Selector selector, List<VariableExpressionExecutor> variableExpressionExecutors, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, String queryName, int metaPosition) {
QuerySelector querySelector = SelectorParser.parse(selector, new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS), siddhiAppContext, metaStreamInfoHolder.getMetaStateEvent(), tableMap, variableExpressionExecutors, queryName, metaPosition);
QueryParserHelper.reduceMetaComplexEvent(metaStreamInfoHolder.getMetaStateEvent());
QueryParserHelper.updateVariablePosition(metaStreamInfoHolder.getMetaStateEvent(), variableExpressionExecutors);
querySelector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(metaStreamInfoHolder.getMetaStateEvent()));
findStoreQueryRuntime.setStateEventPool(new StateEventPool(metaStreamInfoHolder.getMetaStateEvent(), 5));
findStoreQueryRuntime.setSelector(querySelector);
findStoreQueryRuntime.setOutputAttributes(metaStreamInfoHolder.getMetaStateEvent().getOutputStreamDefinition().getAttributeList());
}
use of org.wso2.siddhi.query.api.execution.query.selection.Selector in project siddhi by wso2.
the class SiddhiQLBaseVisitorImpl method visitAnonymous_stream.
/**
* {@inheritDoc}
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*
* @param ctx
*/
@Override
public AnonymousInputStream visitAnonymous_stream(@NotNull SiddhiQLParser.Anonymous_streamContext ctx) {
if (ctx.anonymous_stream() != null) {
return (AnonymousInputStream) visit(ctx.anonymous_stream());
}
Set<String> activeStreamsBackup = activeStreams;
try {
activeStreams = new HashSet<String>();
Query query = Query.query().from((InputStream) visit(ctx.query_input()));
if (ctx.query_section() != null) {
query.select((Selector) visit(ctx.query_section()));
}
if (ctx.output_rate() != null) {
query.output((OutputRate) visit(ctx.output_rate()));
}
if (ctx.output_event_type() != null) {
query.outStream(new ReturnStream((OutputStream.OutputEventType) visit(ctx.output_event_type())));
} else {
query.outStream(new ReturnStream());
}
AnonymousInputStream anonymousInputStream = new AnonymousInputStream(query);
populateQueryContext(anonymousInputStream, ctx);
return anonymousInputStream;
} finally {
activeStreams.clear();
activeStreams = activeStreamsBackup;
}
}
Aggregations