use of org.wso2.siddhi.core.util.collection.operator.CompiledSelection in project siddhi by wso2.
the class SelectStoreQueryRuntime method execute.
public Event[] execute() {
try {
StateEvent stateEvent = new StateEvent(1, 0);
StreamEvent streamEvents = queryableProcessor.query(stateEvent, compiledCondition, compiledSelection);
if (streamEvents == null) {
return null;
} else {
List<Event> events = new ArrayList<Event>();
while (streamEvents != null) {
events.add(new Event(streamEvents.getTimestamp(), streamEvents.getOutputData()));
streamEvents = streamEvents.getNext();
}
return events.toArray(new Event[0]);
}
} catch (Throwable t) {
throw new StoreQueryRuntimeException("Error executing '" + queryName + "', " + t.getMessage(), t);
}
}
use of org.wso2.siddhi.core.util.collection.operator.CompiledSelection 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.core.util.collection.operator.CompiledSelection in project siddhi by wso2.
the class StoreQueryParser method constructStoreQueryRuntime.
private static StoreQueryRuntime constructStoreQueryRuntime(Table table, StoreQuery storeQuery, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, String queryName, int metaPosition, Expression onCondition, MetaStreamEvent metaStreamEvent, List<VariableExpressionExecutor> variableExpressionExecutors) {
metaStreamEvent.setEventType(EventType.TABLE);
initMetaStreamEvent(metaStreamEvent, table.getTableDefinition());
MatchingMetaInfoHolder metaStreamInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, table.getTableDefinition());
CompiledCondition compiledCondition = table.compileCondition(onCondition, metaStreamInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
if (table instanceof QueryableProcessor) {
List<Attribute> expectedOutputAttributes = buildExpectedOutputAttributes(storeQuery, siddhiAppContext, tableMap, queryName, metaPosition, metaStreamInfoHolder);
CompiledSelection compiledSelection = ((QueryableProcessor) table).compileSelection(storeQuery.getSelector(), expectedOutputAttributes, metaStreamInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
SelectStoreQueryRuntime storeQueryRuntime = new SelectStoreQueryRuntime((QueryableProcessor) table, compiledCondition, compiledSelection, expectedOutputAttributes, queryName);
QueryParserHelper.reduceMetaComplexEvent(metaStreamInfoHolder.getMetaStateEvent());
QueryParserHelper.updateVariablePosition(metaStreamInfoHolder.getMetaStateEvent(), variableExpressionExecutors);
return storeQueryRuntime;
} else {
FindStoreQueryRuntime storeQueryRuntime = new FindStoreQueryRuntime(table, compiledCondition, queryName, metaStreamEvent);
populateFindStoreQueryRuntime(storeQueryRuntime, metaStreamInfoHolder, storeQuery.getSelector(), variableExpressionExecutors, siddhiAppContext, tableMap, queryName, metaPosition);
return storeQueryRuntime;
}
}
use of org.wso2.siddhi.core.util.collection.operator.CompiledSelection in project siddhi by wso2.
the class AbstractQueryableRecordTable method query.
@Override
public StreamEvent query(StateEvent matchingEvent, CompiledCondition compiledCondition, CompiledSelection compiledSelection) throws ConnectionUnavailableException {
RecordStoreCompiledSelection recordStoreCompiledSelection = ((RecordStoreCompiledSelection) compiledSelection);
RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
Map<String, Object> parameterMap = new HashMap<>();
for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
}
for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledSelection.variableExpressionExecutorMap.entrySet()) {
parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
}
Iterator<Object[]> records;
if (recordTableHandler != null) {
records = recordTableHandler.query(matchingEvent.getTimestamp(), parameterMap, recordStoreCompiledCondition.compiledCondition, recordStoreCompiledSelection.compiledSelection);
} else {
records = query(parameterMap, recordStoreCompiledCondition.compiledCondition, recordStoreCompiledSelection.compiledSelection);
}
ComplexEventChunk<StreamEvent> streamEventComplexEventChunk = new ComplexEventChunk<>(true);
if (records != null) {
while (records.hasNext()) {
Object[] record = records.next();
StreamEvent streamEvent = storeEventPool.borrowEvent();
System.arraycopy(record, 0, streamEvent.getOutputData(), 0, record.length);
streamEventComplexEventChunk.add(streamEvent);
}
}
return streamEventComplexEventChunk.getFirst();
}
Aggregations