Search in sources :

Example 1 with OutputStream

use of io.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class SelectorParser method parse.

/**
 * Parse Selector portion of a query and return corresponding QuerySelector.
 *
 * @param selector                    selector to be parsed
 * @param outputStream                output stream
 * @param metaComplexEvent            Meta event used to collect execution info of stream associated with query
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors variable expression executors
 * @param metaPosition                helps to identify the meta position of aggregates
 * @param processingMode              processing mode of the query
 * @param outputExpectsExpiredEvents  is expired events sent as output
 * @param siddhiQueryContext          current siddhi query context
 * @return QuerySelector
 */
public static QuerySelector parse(Selector selector, OutputStream outputStream, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, int metaPosition, ProcessingMode processingMode, boolean outputExpectsExpiredEvents, SiddhiQueryContext siddhiQueryContext) {
    boolean currentOn = false;
    boolean expiredOn = false;
    String id = null;
    if (outputStream.getOutputEventType() == OutputStream.OutputEventType.CURRENT_EVENTS || outputStream.getOutputEventType() == OutputStream.OutputEventType.ALL_EVENTS) {
        currentOn = true;
    }
    if (outputStream.getOutputEventType() == OutputStream.OutputEventType.EXPIRED_EVENTS || outputStream.getOutputEventType() == OutputStream.OutputEventType.ALL_EVENTS) {
        expiredOn = true;
    }
    boolean groupBy = !selector.getGroupByList().isEmpty();
    id = outputStream.getId();
    containsAggregatorThreadLocal.remove();
    QuerySelector querySelector = new QuerySelector(id, selector, currentOn, expiredOn, siddhiQueryContext);
    List<AttributeProcessor> attributeProcessors = getAttributeProcessors(selector, id, metaComplexEvent, tableMap, variableExpressionExecutors, outputStream, metaPosition, processingMode, outputExpectsExpiredEvents, groupBy, siddhiQueryContext);
    querySelector.setAttributeProcessorList(attributeProcessors, "true".equals(containsAggregatorThreadLocal.get()));
    containsAggregatorThreadLocal.remove();
    ConditionExpressionExecutor havingCondition = generateHavingExecutor(selector.getHavingExpression(), metaComplexEvent, tableMap, variableExpressionExecutors, siddhiQueryContext);
    querySelector.setHavingConditionExecutor(havingCondition, "true".equals(containsAggregatorThreadLocal.get()));
    containsAggregatorThreadLocal.remove();
    if (!selector.getGroupByList().isEmpty()) {
        List<Expression> groupByExpressionList = selector.getGroupByList().stream().map(groupByVariable -> (Expression) groupByVariable).collect(Collectors.toList());
        querySelector.setGroupByKeyGenerator(new GroupByKeyGenerator(groupByExpressionList, metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, null, variableExpressionExecutors, siddhiQueryContext));
    }
    if (!selector.getOrderByList().isEmpty()) {
        querySelector.setOrderByEventComparator(new OrderByEventComparator(selector.getOrderByList(), metaComplexEvent, SiddhiConstants.HAVING_STATE, null, variableExpressionExecutors, siddhiQueryContext));
    }
    if (selector.getLimit() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), metaComplexEvent, SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        containsAggregatorThreadLocal.remove();
        querySelector.setLimit(((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue());
    }
    if (selector.getOffset() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getOffset(), metaComplexEvent, SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        containsAggregatorThreadLocal.remove();
        querySelector.setOffset(((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue());
    }
    return querySelector;
}
Also used : Selector(io.siddhi.query.api.execution.query.selection.Selector) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) GroupByKeyGenerator(io.siddhi.core.query.selector.GroupByKeyGenerator) Variable(io.siddhi.query.api.expression.Variable) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) SiddhiConstants(io.siddhi.core.util.SiddhiConstants) QuerySelector(io.siddhi.core.query.selector.QuerySelector) Expression(io.siddhi.query.api.expression.Expression) ArrayList(java.util.ArrayList) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor) Table(io.siddhi.core.table.Table) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaComplexEvent(io.siddhi.core.event.MetaComplexEvent) MetaStateEventAttribute(io.siddhi.core.event.state.MetaStateEventAttribute) Map(java.util.Map) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ProcessingMode(io.siddhi.core.query.processor.ProcessingMode) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) OrderByEventComparator(io.siddhi.core.query.selector.OrderByEventComparator) ConditionExpressionExecutor(io.siddhi.core.executor.condition.ConditionExpressionExecutor) Attribute(io.siddhi.query.api.definition.Attribute) DuplicateAttributeException(io.siddhi.query.api.exception.DuplicateAttributeException) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) Collectors(java.util.stream.Collectors) List(java.util.List) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ConditionExpressionExecutor(io.siddhi.core.executor.condition.ConditionExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) Expression(io.siddhi.query.api.expression.Expression) GroupByKeyGenerator(io.siddhi.core.query.selector.GroupByKeyGenerator) QuerySelector(io.siddhi.core.query.selector.QuerySelector) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor) ConditionExpressionExecutor(io.siddhi.core.executor.condition.ConditionExpressionExecutor) OrderByEventComparator(io.siddhi.core.query.selector.OrderByEventComparator)

Example 2 with OutputStream

use of io.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitStore_query.

@Override
public Object visitStore_query(SiddhiQLParser.Store_queryContext ctx) {
    OutputStream outputStream;
    OnDemandQuery onDemandQuery = OnDemandQuery.query();
    if (ctx.FROM() != null) {
        onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.FIND);
        onDemandQuery.from((InputStore) visit(ctx.store_input()));
        if (ctx.query_section() != null) {
            onDemandQuery = onDemandQuery.select((Selector) visit(ctx.query_section()));
        }
    } else if (ctx.query_section() != null) {
        onDemandQuery.select((Selector) visit(ctx.query_section()));
        if (ctx.UPDATE() != null && ctx.OR() != null) {
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.UPDATE_OR_INSERT);
            Source source = (Source) visit(ctx.target());
            if (source.isInnerStream || source.isFaultStream) {
                throw newSiddhiParserException(ctx, "UPDATE OR INTO INSERT can be only used with Tables!");
            }
            if (ctx.set_clause() != null) {
                outputStream = new UpdateOrInsertStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
            } else {
                outputStream = new UpdateOrInsertStream(source.streamId, (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
            }
            onDemandQuery.outStream(outputStream);
        } else if (ctx.INSERT() != null) {
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.INSERT);
            Source source = (Source) visit(ctx.target());
            if (source.isInnerStream || source.isFaultStream) {
                throw newSiddhiParserException(ctx, "INSERT can be only used with Tables!");
            }
            outputStream = new InsertIntoStream(source.streamId);
            populateQueryContext(outputStream, ctx);
            onDemandQuery.outStream(outputStream);
        } else if (ctx.store_query_output() != null) {
            outputStream = (OutputStream) visit(ctx.store_query_output());
            if (outputStream instanceof DeleteStream) {
                onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.DELETE);
            } else if (outputStream instanceof UpdateStream) {
                onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.UPDATE);
            }
            onDemandQuery.outStream(outputStream);
        }
    } else if (ctx.store_query_output() != null) {
        outputStream = (OutputStream) visit(ctx.store_query_output());
        if (outputStream instanceof DeleteStream) {
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.DELETE);
        } else if (outputStream instanceof UpdateStream) {
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.UPDATE);
        }
        onDemandQuery.outStream(outputStream);
    }
    populateQueryContext(onDemandQuery, ctx);
    return onDemandQuery;
}
Also used : UpdateOrInsertStream(io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) OnDemandQuery(io.siddhi.query.api.execution.query.OnDemandQuery) BasicSelector(io.siddhi.query.api.execution.query.selection.BasicSelector) Selector(io.siddhi.query.api.execution.query.selection.Selector)

Example 3 with OutputStream

use of io.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitQuery_output.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public OutputStream visitQuery_output(@NotNull SiddhiQLParser.Query_outputContext ctx) {
    if (ctx.INSERT() != null) {
        Source source = (Source) visit(ctx.target());
        if (ctx.UPDATE() != null && ctx.OR() != null) {
            if (source.isInnerStream || source.isFaultStream) {
                throw newSiddhiParserException(ctx, "UPDATE OR INTO INSERT be only used with Tables!");
            }
            if (ctx.output_event_type() != null) {
                if (ctx.set_clause() != null) {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                } else {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                }
            } else {
                if (ctx.set_clause() != null) {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                } else {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                }
            }
        } else {
            if (ctx.output_event_type() != null) {
                OutputStream outputStream = new InsertIntoStream(source.streamId, source.isInnerStream, source.isFaultStream, (OutputStream.OutputEventType) visit(ctx.output_event_type()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new InsertIntoStream(source.streamId, source.isInnerStream, source.isFaultStream);
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        }
    } else if (ctx.DELETE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream || source.isFaultStream) {
            throw newSiddhiParserException(ctx, "DELETE can be only used with Tables!");
        }
        Expression expression = null;
        if (ctx.expression() != null) {
            expression = (Expression) visit(ctx.expression());
        }
        if (ctx.output_event_type() != null) {
            OutputStream outputStream = new DeleteStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), expression);
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new DeleteStream(source.streamId, expression);
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else if (ctx.UPDATE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream || source.isFaultStream) {
            throw newSiddhiParserException(ctx, "UPDATE can be only used with Tables!");
        }
        if (ctx.output_event_type() != null) {
            if (ctx.set_clause() != null) {
                OutputStream outputStream = new UpdateStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new UpdateStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        } else {
            if (ctx.set_clause() != null) {
                OutputStream outputStream = new UpdateStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new UpdateStream(source.streamId, (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        }
    } else if (ctx.RETURN() != null) {
        if (ctx.output_event_type() != null) {
            OutputStream outputStream = new ReturnStream((OutputStream.OutputEventType) visit(ctx.output_event_type()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new ReturnStream();
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else {
        throw newSiddhiParserException(ctx);
    }
}
Also used : UpdateOrInsertStream(io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) Expression(io.siddhi.query.api.expression.Expression) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) ReturnStream(io.siddhi.query.api.execution.query.output.stream.ReturnStream)

Example 4 with OutputStream

use of io.siddhi.query.api.execution.query.output.stream.OutputStream in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitStore_query_output.

/**
 * {@inheritDoc}
 * <p>
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 */
@Override
public OutputStream visitStore_query_output(@NotNull SiddhiQLParser.Store_query_outputContext ctx) {
    if (ctx.DELETE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream || source.isFaultStream) {
            throw newSiddhiParserException(ctx, "DELETE can be only used with Tables!");
        }
        Expression expression = null;
        if (ctx.expression() != null) {
            expression = (Expression) visit(ctx.expression());
        }
        OutputStream outputStream = new DeleteStream(source.streamId, expression);
        populateQueryContext(outputStream, ctx);
        return outputStream;
    } else if (ctx.UPDATE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream || source.isFaultStream) {
            throw newSiddhiParserException(ctx, "DELETE can be only used with Tables!");
        }
        if (ctx.set_clause() != null) {
            OutputStream outputStream = new UpdateStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new UpdateStream(source.streamId, (Expression) visit(ctx.expression()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else {
        throw newSiddhiParserException(ctx);
    }
}
Also used : Expression(io.siddhi.query.api.expression.Expression) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) UpdateSet(io.siddhi.query.api.execution.query.output.stream.UpdateSet)

Aggregations

OutputStream (io.siddhi.query.api.execution.query.output.stream.OutputStream)4 DeleteStream (io.siddhi.query.api.execution.query.output.stream.DeleteStream)3 UpdateStream (io.siddhi.query.api.execution.query.output.stream.UpdateStream)3 Expression (io.siddhi.query.api.expression.Expression)3 InsertIntoStream (io.siddhi.query.api.execution.query.output.stream.InsertIntoStream)2 UpdateOrInsertStream (io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream)2 Selector (io.siddhi.query.api.execution.query.selection.Selector)2 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)1 MetaComplexEvent (io.siddhi.core.event.MetaComplexEvent)1 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)1 MetaStateEventAttribute (io.siddhi.core.event.state.MetaStateEventAttribute)1 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)1 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)1 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)1 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)1 ConditionExpressionExecutor (io.siddhi.core.executor.condition.ConditionExpressionExecutor)1 ProcessingMode (io.siddhi.core.query.processor.ProcessingMode)1 GroupByKeyGenerator (io.siddhi.core.query.selector.GroupByKeyGenerator)1 OrderByEventComparator (io.siddhi.core.query.selector.OrderByEventComparator)1 QuerySelector (io.siddhi.core.query.selector.QuerySelector)1