io.siddhi.query.api.expression.Variable Java Examples

The following examples show how to use io.siddhi.query.api.expression.Variable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SiddhiExecutionPlanner.java    From flink-siddhi with Apache License 2.0 6 votes vote down vote up
private StreamPartition findStreamPartition(SingleInputStream inputStream, Selector selector) {
    // Window Spec
    List<Window> windows = new ArrayList<>();
    for (StreamHandler streamHandler : inputStream.getStreamHandlers()) {
        if (streamHandler instanceof Window) {
            windows.add((Window) streamHandler);
        }
    }

    // Group By Spec
    List<Variable> groupBy = selector.getGroupByList();
    if (windows.size() > 0 || groupBy.size() > 0) {
        return generatePartition(inputStream.getStreamId(), windows, groupBy);
    } else {
        return null;
    }
}
 
Example #2
Source File: SiddhiExecutionPlanner.java    From flink-siddhi with Apache License 2.0 6 votes vote down vote up
private StreamPartition generatePartition(String streamId, List<Window> windows, List<Variable> groupBy) {
    StreamPartition partition = new StreamPartition(streamId);

    if (windows != null && windows.size() > 0) {
        //TODO: sort spec
    }

    if (groupBy != null && groupBy.size() > 0) {
        partition.setGroupByList(groupBy.stream().map(Variable::getAttributeName).collect(Collectors.toList()));
        partition.setType(StreamPartition.Type.GROUPBY);
    } else {
        partition.setType(StreamPartition.Type.SHUFFLE);
    }

    return partition;
}
 
Example #3
Source File: OperatorParser.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private static boolean isTableIndexVariable(MatchingMetaInfoHolder matchingMetaInfoHolder, Expression expression,
                                            String indexAttribute) {
    if (expression instanceof Variable) {
        Variable variable = (Variable) expression;
        if (variable.getStreamId() != null) {
            MetaStreamEvent tableStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent
                    (matchingMetaInfoHolder.getStoreEventIndex());
            if (tableStreamEvent != null) {
                if ((tableStreamEvent.getInputReferenceId() != null && variable.getStreamId().equals
                        (tableStreamEvent.getInputReferenceId())) ||
                        (tableStreamEvent.getLastInputDefinition().getId().equals(variable.getStreamId()))) {
                    if (Arrays.asList(tableStreamEvent.getLastInputDefinition().getAttributeNameArray()).contains
                            (indexAttribute)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example #4
Source File: PolicyExecutionPlannerImpl.java    From eagle with Apache License 2.0 6 votes vote down vote up
private StreamPartition findStreamPartition(SingleInputStream inputStream, Selector selector) {
    // Window Spec
    List<Window> windows = new ArrayList<>();
    for (StreamHandler streamHandler : inputStream.getStreamHandlers()) {
        if (streamHandler instanceof Window) {
            windows.add((Window) streamHandler);
        }
    }

    // Group By Spec
    List<Variable> groupBy = selector.getGroupByList();
    if (windows.size() > 0 || groupBy.size() >= 0) {
        return generatePartition(inputStream.getStreamId(), windows, groupBy);
    } else {
        return null;
    }
}
 
Example #5
Source File: PolicyExecutionPlannerImpl.java    From eagle with Apache License 2.0 6 votes vote down vote up
private StreamPartition generatePartition(String streamId, List<Window> windows, List<Variable> groupBy) {
    StreamPartition partition = new StreamPartition();
    partition.setStreamId(streamId);
    StreamSortSpec sortSpec = null;
    if (windows != null && windows.size() > 0) {
        for (Window window : windows) {
            if (window.getName().equals(WINDOW_EXTERNAL_TIME)) {
                sortSpec = new StreamSortSpec();
                sortSpec.setWindowPeriodMillis(getExternalTimeWindowSize(window));
                sortSpec.setWindowMargin(sortSpec.getWindowPeriodMillis() / 5);
            }
        }
    }
    partition.setSortSpec(sortSpec);
    if (groupBy != null && groupBy.size() > 0) {
        partition.setColumns(groupBy.stream().map(Variable::getAttributeName).collect(Collectors.toList()));
        partition.setType(StreamPartition.Type.GROUPBY);
    } else {
        partition.setType(StreamPartition.Type.SHUFFLE);
    }
    return partition;
}
 
Example #6
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Override
public BasicSelector visitGroup_by_query_selection(@NotNull SiddhiQLParser.Group_by_query_selectionContext ctx) {

    BasicSelector selector = new BasicSelector();

    List<OutputAttribute> attributeList = new ArrayList<OutputAttribute>(ctx.output_attribute().size());
    for (SiddhiQLParser.Output_attributeContext output_attributeContext : ctx.output_attribute()) {
        attributeList.add((OutputAttribute) visit(output_attributeContext));
    }
    selector.addSelectionList(attributeList);

    if (ctx.group_by() != null) {
        selector.addGroupByList((List<Variable>) visit(ctx.group_by()));
    }
    populateQueryContext(selector, ctx);
    return selector;
}
 
Example #7
Source File: CollectionExpressionParser.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private static boolean isCollectionVariable(MatchingMetaInfoHolder matchingMetaInfoHolder, Variable variable) {
    if (variable.getStreamId() != null) {
        MetaStreamEvent collectionStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent
                (matchingMetaInfoHolder.getStoreEventIndex());
        if (collectionStreamEvent != null) {
            if ((collectionStreamEvent.getInputReferenceId() != null && variable.getStreamId().equals
                    (collectionStreamEvent.getInputReferenceId())) ||
                    (collectionStreamEvent.getLastInputDefinition().getId().equals(variable.getStreamId()))) {
                return true;
            }
        }
    } else if (matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length == 1 &&
            matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents()[0].getEventType()
                    != MetaStreamEvent.EventType.DEFAULT) {
        return true;
    } else if (matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length == 2 &&
            matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents()
                    [matchingMetaInfoHolder.getStoreEventIndex()].getEventType()
                    != MetaStreamEvent.EventType.DEFAULT) {
        return true;
    }
    return false;
}
 
Example #8
Source File: CacheTable.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private boolean checkIfVariableMatchesTable(Variable variable, MatchingMetaInfoHolder matchingMetaInfoHolder) {
    if (variable.getStreamId() == null) {
        return false;
    }
    if (variable.getStreamId().equalsIgnoreCase(tableDefinition.getId())) {
        return true;
    }

    for (MetaStreamEvent streamEvent : matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents()) {
        if (streamEvent.getInputReferenceId() != null &&
                streamEvent.getInputReferenceId().equalsIgnoreCase(variable.getStreamId())) {
            if (streamEvent.getInputDefinitions().get(0).getId().equalsIgnoreCase(tableDefinition.getId())) {
                return true;
            }
        }
    }
    return false;
}
 
Example #9
Source File: ExpressionBuilder.java    From siddhi with Apache License 2.0 6 votes vote down vote up
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, false, 0,
                ProcessingMode.BATCH, false, siddhiQueryContext);
        variableExpressionExecutorMap.put(id, variableExpressionExecutor);
    }
    expressionVisitor.endVisitStreamVariable(id, variable.getStreamId(), variable.getAttributeName(), type);

}
 
Example #10
Source File: AggregationExpressionVisitor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
public void addVariableExpression(Expression expression) {
    Variable variable = (Variable) expression;
    String streamId = variable.getStreamId();
    if (streamId == null) {
        if (this.allAttributesList.contains(variable.getAttributeName())) {
            this.conditionOperands.push(expression);
        } else {
            this.conditionOperands.push("true");
        }
    } else {
        if (streamId.equals(inputStreamRefId)) {
            this.conditionOperands.push(expression);
        } else if (this.tableAttributesNameList.contains(variable.getAttributeName())) {
            this.conditionOperands.push(expression);
        } else {
            this.conditionOperands.push("true");
        }
    }

}
 
Example #11
Source File: IncrementalDataPurger.java    From siddhi with Apache License 2.0 6 votes vote down vote up
/**
 * Building the compiled conditions for purge data
 **/
private Map<TimePeriod.Duration, CompiledCondition> createCompileConditions(
        Map<TimePeriod.Duration, Table> aggregationTables, Map<String, Table> tableMap) {
    Map<TimePeriod.Duration, CompiledCondition> compiledConditionMap = new EnumMap<>(TimePeriod.Duration.class);
    CompiledCondition compiledCondition;
    Table table;
    for (Map.Entry<TimePeriod.Duration, Table> entry : aggregationTables.entrySet()) {
        if (!retentionPeriods.get(entry.getKey()).equals(RETAIN_ALL)) {
            table = aggregationTables.get(entry.getKey());
            Variable leftVariable = new Variable(purgingTimestampField);
            leftVariable.setStreamId(entry.getValue().getTableDefinition().getId());
            Compare expression = new Compare(leftVariable,
                    Compare.Operator.LESS_THAN, new Variable(purgingTimestampField));
            compiledCondition = table.compileCondition(expression,
                    matchingMetaInfoHolder(table, aggregatedTimestampAttribute), variableExpressionExecutorList,
                    tableMap, siddhiQueryContext);
            compiledConditionMap.put(entry.getKey(), compiledCondition);
        }
    }
    return compiledConditionMap;
}
 
Example #12
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery10() {
    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.every(
                                    State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(30))))),
                            State.next(
                                    State.next(
                                            State.stream(InputStream.stream("e2", "Stream1").filter(Expression
                                                    .compare(Expression.variable("price").ofStream("e2", Variable
                                                                    .LAST),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.value(20)))),
                                            State.stream(InputStream.stream("e3", "Stream2").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.variable("price").ofStream("e1"))))),
                                    State.stream(InputStream.stream("e4", "Stream3").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.value(74))))
                            )
                    ), Expression.Time.minute(4)
            )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2")))

    );
    query.insertInto("OutputStream");

}
 
Example #13
Source File: CacheExpirer.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private CompiledCondition generateExpiryCompiledCondition() {
    MetaStreamEvent tableMetaStreamEvent = new MetaStreamEvent();
    tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
    TableDefinition matchingTableDefinition = TableDefinition.id(cacheTable.getTableDefinition().getId());
    for (Attribute attribute : cacheTable.getTableDefinition().getAttributeList()) {
        tableMetaStreamEvent.addOutputData(attribute);
        matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
    }
    tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
    streamEventFactory = new StreamEventFactory(tableMetaStreamEvent);

    Variable rightExpressionForSubtract = new Variable(CACHE_TABLE_TIMESTAMP_ADDED);
    rightExpressionForSubtract.setStreamId(cacheTable.getTableDefinition().getId());
    Expression rightExpressionForCompare = new LongConstant(retentionPeriod);
    Compare.Operator greaterThanOperator = Compare.Operator.GREATER_THAN;

    MetaStreamEvent currentTimeMetaStreamEvent = new MetaStreamEvent();
    currentTimeMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
    Attribute currentTimeAttribute = new Attribute(CACHE_EXPIRE_CURRENT_TIME, Attribute.Type.LONG);
    currentTimeMetaStreamEvent.addOutputData(currentTimeAttribute);
    TableDefinition currentTimeTableDefinition = TableDefinition.id("");
    currentTimeTableDefinition.attribute(CACHE_EXPIRE_CURRENT_TIME, Attribute.Type.LONG);
    currentTimeMetaStreamEvent.addInputDefinition(currentTimeTableDefinition);

    MetaStateEvent metaStateEvent = new MetaStateEvent(2);
    metaStateEvent.addEvent(currentTimeMetaStreamEvent);
    metaStateEvent.addEvent(tableMetaStreamEvent);

    MatchingMetaInfoHolder matchingMetaInfoHolder =
            MatcherParser.constructMatchingMetaStateHolder(metaStateEvent, 0,
                    cacheTable.getTableDefinition(), 0);
    List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
    Expression leftExpressionForSubtract = new Variable(CACHE_EXPIRE_CURRENT_TIME);
    Expression leftExpressionForCompare = new Subtract(leftExpressionForSubtract, rightExpressionForSubtract);
    Expression deleteCondition = new Compare(leftExpressionForCompare, greaterThanOperator,
            rightExpressionForCompare);
    SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext, "expiryDeleteQuery");
    return cacheTable.compileCondition(deleteCondition, matchingMetaInfoHolder, variableExpressionExecutors,
            tableMap, siddhiQueryContext);
}
 
Example #14
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery14() {
    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.every(
                                    State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(30))))),
                            State.next(
                                    State.zeroOrMany(
                                            State.stream(InputStream.stream("e2", "Stream1").filter(Expression
                                                    .compare(Expression.variable("price").ofStream("e2", Variable
                                                                    .LAST),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.value(20))))
                                    ),
                                    State.next(
                                            State.stream(InputStream.stream("e3", "Stream2").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.variable("price").ofStream("e1")))),
                                            State.stream(InputStream.stream("e4", "Stream3").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN,
                                                            Expression.value(74)))))
                            )
                    )
            )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2")))
    );
    query.insertInto("OutputStream");
}
 
Example #15
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery15() {
    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.every(
                                    State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(30))))),
                            State.next(
                                    State.zeroOrOne(
                                            State.stream(InputStream.stream("e2", "Stream1").filter(Expression
                                                    .compare(Expression.variable("price").ofStream("e2", Variable
                                                                    .LAST),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.value(20))))
                                    ),
                                    State.next(
                                            State.stream(InputStream.stream("e3", "Stream2").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.variable("price").ofStream("e1")))),
                                            State.stream(InputStream.stream("e4", "Stream3").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN,
                                                            Expression.value(74)))))
                            )
                    )
            )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2")))
    );
    query.insertInto("OutputStream");
}
 
Example #16
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery16() {
    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.every(
                                    State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(30))))),
                            State.next(
                                    State.oneOrMany(
                                            State.stream(InputStream.stream("e2", "Stream1").filter(Expression
                                                    .compare(Expression.variable("price").ofStream("e2", Variable
                                                                    .LAST),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.value(20))))
                                    ),
                                    State.next(
                                            State.stream(InputStream.stream("e3", "Stream2").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.variable("price").ofStream("e1")))),
                                            State.stream(InputStream.stream("e4", "Stream3").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN,
                                                            Expression.value(74)))))
                            )
                    )
            )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2")))
    );
    query.insertInto("OutputStream");
}
 
Example #17
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingFilterQueryWithExpression() {
    Query query = Query.query();

    Variable variable = new Variable("price");
    Variable variable1 = new Variable("symbol");
    variable.setFunctionId("func1");
    variable.setAttributeName("price");
    variable.setFunctionIndex(1);
    variable.setStreamId(true, true, "func");
    variable1.setStreamId(false, true, "func2");

    query.from(
            InputStream.stream("StockStream").
                    filter(Expression.and(Expression.compare(Expression.function("ext",
                            "FooBarCond", Expression.value(7), Expression.value(9.5)),
                            Compare.Operator.GREATER_THAN,
                            Expression.variable(variable.getAttributeName())),
                            Expression.function("ext", "BarCond", Expression.value(100),
                                    Expression.variable("volume")
                            )
                            )
                    ).
                    function("ext", "Foo", Expression.value(67),
                            Expression.value(89)).window("ext", "lengthFirst10", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("ext", "avg",
                            Expression.variable("price")))
    );

    AssertJUnit.assertTrue(variable.isInnerStream());
    AssertJUnit.assertEquals(variable.getFunctionId(), "func1");
    AssertJUnit.assertEquals(Integer.toString(variable.getFunctionIndex()), "1");
}
 
Example #18
Source File: AggregateWindowProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public AggregateWindowProcessor(AggregationRuntime aggregationRuntime, Within within, Expression per,
                                List<Variable> queryGroupByList) {
    this.aggregationRuntime = aggregationRuntime;
    this.within = within;
    this.per = per;
    this.queryGroupByList = queryGroupByList;
}
 
Example #19
Source File: AggregationParser.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private static HashMap<TimePeriod.Duration, Table> initDefaultTables(
        String aggregatorName, List<TimePeriod.Duration> durations,
        StreamDefinition streamDefinition, SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder,
        List<Annotation> annotations, List<Variable> groupByVariableList, boolean isProcessingOnExternalTime,
        boolean enablePartioning) {

    HashMap<TimePeriod.Duration, Table> aggregationTableMap = new HashMap<>();

    // Create annotations for primary key
    Annotation primaryKeyAnnotation = new Annotation(SiddhiConstants.ANNOTATION_PRIMARY_KEY);
    primaryKeyAnnotation.element(null, AGG_START_TIMESTAMP_COL);

    if (enablePartioning) {
        primaryKeyAnnotation.element(null, AGG_SHARD_ID_COL);
    }
    if (isProcessingOnExternalTime) {
        primaryKeyAnnotation.element(null, AGG_EXTERNAL_TIMESTAMP_COL);
    }
    for (Variable groupByVariable : groupByVariableList) {
        primaryKeyAnnotation.element(null, groupByVariable.getAttributeName());
    }
    annotations.add(primaryKeyAnnotation);
    for (TimePeriod.Duration duration : durations) {
        String tableId = aggregatorName + "_" + duration.toString();
        TableDefinition tableDefinition = TableDefinition.id(tableId);
        for (Attribute attribute : streamDefinition.getAttributeList()) {
            tableDefinition.attribute(attribute.getName(), attribute.getType());
        }
        annotations.forEach(tableDefinition::annotation);
        siddhiAppRuntimeBuilder.defineTable(tableDefinition);
        aggregationTableMap.put(duration, siddhiAppRuntimeBuilder.getTableMap().get(tableId));
    }
    return aggregationTableMap;
}
 
Example #20
Source File: JoinInputStreamParser.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private static void setStreamRuntimeProcessorChain(
        MetaStreamEvent metaStreamEvent, SingleStreamRuntime streamRuntime,
        String inputStreamId, Map<String, Table> tableMap, Map<String, Window> windowMap,
        Map<String, AggregationRuntime> aggregationMap,
        List<VariableExpressionExecutor> variableExpressionExecutors, boolean outputExpectsExpiredEvents,
        Within within, Expression per, List<Variable> queryGroupByList, SiddhiQueryContext siddhiQueryContext,
        InputStream inputStream) {
    switch (metaStreamEvent.getEventType()) {

        case TABLE:
            TableWindowProcessor tableWindowProcessor = new TableWindowProcessor(tableMap.get(inputStreamId));
            tableWindowProcessor.initProcessor(metaStreamEvent,
                    new ExpressionExecutor[0], null, outputExpectsExpiredEvents,
                    true, false, inputStream, siddhiQueryContext);
            streamRuntime.setProcessorChain(tableWindowProcessor);
            break;
        case WINDOW:
            WindowWindowProcessor windowWindowProcessor = new WindowWindowProcessor(
                    windowMap.get(inputStreamId));
            windowWindowProcessor.initProcessor(metaStreamEvent,
                    variableExpressionExecutors.toArray(new ExpressionExecutor[0]), null,
                    outputExpectsExpiredEvents, true, false, inputStream, siddhiQueryContext);
            streamRuntime.setProcessorChain(windowWindowProcessor);
            break;
        case AGGREGATE:

            AggregationRuntime aggregationRuntime = aggregationMap.get(inputStreamId);
            AggregateWindowProcessor aggregateWindowProcessor = new AggregateWindowProcessor(
                    aggregationRuntime, within, per, queryGroupByList);
            aggregateWindowProcessor.initProcessor(metaStreamEvent,
                    variableExpressionExecutors.toArray(new ExpressionExecutor[0]), null,
                    outputExpectsExpiredEvents, true, false, inputStream, siddhiQueryContext);
            streamRuntime.setProcessorChain(aggregateWindowProcessor);
            break;
        case DEFAULT:
            break;
    }
}
 
Example #21
Source File: SiddhiExecutionPlanner.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
private String retrieveStreamId(Variable variable, Map<String, SingleInputStream> aliasMap) throws Exception {
    Preconditions.checkNotNull(variable.getStreamId(), "streamId");
    if (inputStreams.containsKey(variable.getStreamId()) && aliasMap.containsKey(variable.getStreamId())) {
        throw new Exception("Duplicated streamId and alias: " + variable.getStreamId());
    } else if (inputStreams.containsKey(variable.getStreamId())) {
        return variable.getStreamId();
    } else if (aliasMap.containsKey(variable.getStreamId())) {
        return aliasMap.get(variable.getStreamId()).getStreamId();
    } else {
        throw new Exception(variable.getStreamId() + " does not exist!");
    }
}
 
Example #22
Source File: SiddhiExecutionPlanner.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
private StreamPartition findStreamPartition(String streamId, ValuePartitionType value){
    StreamPartition partition = new StreamPartition(streamId);
    if(value != null){
        Expression expression = value.getExpression();
        if(expression instanceof Variable){
            String attributeName = ((Variable) expression).getAttributeName();
            partition.setPartitionWithList(Collections.singletonList(attributeName));
            partition.setType(StreamPartition.Type.PARTITIONWITH);
            return partition;
        }
    }
    return null;
}
 
Example #23
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public AggregationDefinition visitDefinition_aggregation(
        @NotNull SiddhiQLParser.Definition_aggregationContext ctx) {
    // Read the name of the aggregation
    String aggregationName = (String) visitAggregation_name(ctx.aggregation_name());

    // Create the aggregation using the extracted aggregation name
    AggregationDefinition aggregationDefinition = AggregationDefinition.id(aggregationName);

    // Get all annotation and populate the aggregation
    for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
        aggregationDefinition.annotation((Annotation) visit(annotationContext));
    }

    // Attach the input stream
    BasicSingleInputStream basicSingleInputStream = (BasicSingleInputStream) visit(ctx.standard_stream());
    aggregationDefinition.from(basicSingleInputStream);

    // Extract the selector and attach it to the new aggregation
    BasicSelector selector = (BasicSelector) visit(ctx.group_by_query_selection());
    aggregationDefinition.select(selector);

    // Get the variable (if available) and aggregate on that variable
    if (ctx.attribute_reference() != null) {
        Variable aggregatedBy = (Variable) visit(ctx.attribute_reference());
        aggregationDefinition.aggregateBy(aggregatedBy);
    }

    // Extract the specified time-durations and attache it to the aggregation definition
    TimePeriod timePeriod = (TimePeriod) visit(ctx.aggregation_time());
    aggregationDefinition.every(timePeriod);
    populateQueryContext(aggregationDefinition, ctx);
    return aggregationDefinition;
}
 
Example #24
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public UpdateSet visitSet_clause(@NotNull SiddhiQLParser.Set_clauseContext ctx) {
    UpdateSet updateSet = new UpdateSet();
    for (SiddhiQLParser.Set_assignmentContext setAssignmentContext : ctx.set_assignment()) {
        updateSet.set(((Variable) visit(setAssignmentContext.attribute_reference())),
                (Expression) visit(setAssignmentContext.expression()));
    }
    populateQueryContext(updateSet, ctx);
    return updateSet;
}
 
Example #25
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 */
@Override
public OrderByAttribute visitOrder_by_reference(SiddhiQLParser.Order_by_referenceContext ctx) {
    Variable variable = (Variable) visit(ctx.attribute_reference());
    if (ctx.order() != null && ctx.order().DESC() != null) {
        return new OrderByAttribute(variable, OrderByAttribute.Order.DESC);
    }
    return new OrderByAttribute(variable);
}
 
Example #26
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public List<Variable> visitGroup_by(@NotNull SiddhiQLParser.Group_byContext ctx) {
    List<Variable> variableList = new ArrayList<Variable>(ctx.attribute_reference().size());
    for (SiddhiQLParser.Attribute_referenceContext attributeReferenceContext : ctx.attribute_reference()) {
        variableList.add((Variable) visit(attributeReferenceContext));
    }
    return variableList;
}
 
Example #27
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
     * {@inheritDoc}
     * <p>The default implementation returns the result of calling
     * {@link #visitChildren} on {@code ctx}.</p>
     *
     * @param ctx
     */
    @Override
    public Selector visitQuery_section(@NotNull SiddhiQLParser.Query_sectionContext ctx) {

//        query_section
//        :(SELECT ('*'| (output_attribute (',' output_attribute)* ))) group_by? having?
//        ;

        Selector selector = new Selector();

        List<OutputAttribute> attributeList = new ArrayList<OutputAttribute>(ctx.output_attribute().size());
        for (SiddhiQLParser.Output_attributeContext output_attributeContext : ctx.output_attribute()) {
            attributeList.add((OutputAttribute) visit(output_attributeContext));
        }
        selector.addSelectionList(attributeList);

        if (ctx.group_by() != null) {
            selector.addGroupByList((List<Variable>) visit(ctx.group_by()));
        }
        if (ctx.having() != null) {
            selector.having((Expression) visit(ctx.having()));
        }
        if (ctx.order_by() != null) {
            selector.addOrderByList((List<OrderByAttribute>) visit(ctx.order_by()));
        }
        if (ctx.limit() != null) {
            selector.limit((Constant) visit(ctx.limit()));
        }
        if (ctx.offset() != null) {
            selector.offset((Constant) visit(ctx.offset()));
        }
        populateQueryContext(selector, ctx);
        return selector;
    }
 
Example #28
Source File: PolicyExecutionPlannerImpl.java    From eagle with Apache License 2.0 5 votes vote down vote up
private String retrieveStreamId(Variable variable, Map<String, List<StreamColumn>> streamMap, Map<String, SingleInputStream> aliasMap) {
    Preconditions.checkNotNull(variable.getStreamId(), "streamId");
    if (streamMap.containsKey(variable.getStreamId()) && aliasMap.containsKey(variable.getStreamId())) {
        throw new DuplicateDefinitionException("Duplicated streamId and alias: " + variable.getStreamId(),
                variable.getQueryContextStartIndex(), variable.getQueryContextEndIndex());
    } else if (streamMap.containsKey(variable.getStreamId())) {
        return variable.getStreamId();
    } else if (aliasMap.containsKey(variable.getStreamId())) {
        return aliasMap.get(variable.getStreamId()).getStreamId();
    } else {
        throw new DefinitionNotExistException(variable.getStreamId());
    }
}
 
Example #29
Source File: AggregationParser.java    From siddhi with Apache License 2.0 4 votes vote down vote up
private static void initIncrementalAttributeAggregator(AbstractDefinition lastInputStreamDefinition,
                                                       AttributeFunction attributeFunction,
                                                       IncrementalAttributeAggregator incrementalAttributeAggregator) {

    String attributeName = null;
    Attribute.Type attributeType = null;
    if (attributeFunction.getParameters() != null && attributeFunction.getParameters()[0] != null) {
        if (attributeFunction.getParameters().length != 1) {
            throw new SiddhiAppCreationException("Incremental aggregator requires only one parameter. "
                    + "Found " + attributeFunction.getParameters().length,
                    attributeFunction.getQueryContextStartIndex(), attributeFunction.getQueryContextEndIndex());
        }
        if (!(attributeFunction.getParameters()[0] instanceof Variable)) {
            throw new SiddhiAppCreationException("Incremental aggregator expected a variable. " +
                    "However a parameter of type " + attributeFunction.getParameters()[0].getClass().getTypeName()
                    + " was found",
                    attributeFunction.getParameters()[0].getQueryContextStartIndex(),
                    attributeFunction.getParameters()[0].getQueryContextEndIndex());
        }
        attributeName = ((Variable) attributeFunction.getParameters()[0]).getAttributeName();
        attributeType = lastInputStreamDefinition.getAttributeType(attributeName);
    }

    incrementalAttributeAggregator.init(attributeName, attributeType);

    Attribute[] baseAttributes = incrementalAttributeAggregator.getBaseAttributes();
    Expression[] baseAttributeInitialValues = incrementalAttributeAggregator
            .getBaseAttributeInitialValues();
    Expression[] baseAggregators = incrementalAttributeAggregator.getBaseAggregators();

    if (baseAttributes.length != baseAggregators.length) {
        throw new SiddhiAppCreationException("Number of baseAggregators '" +
                baseAggregators.length + "' and baseAttributes '" +
                baseAttributes.length + "' is not equal for '" + attributeFunction + "'",
                attributeFunction.getQueryContextStartIndex(), attributeFunction.getQueryContextEndIndex());
    }
    if (baseAttributeInitialValues.length != baseAggregators.length) {
        throw new SiddhiAppCreationException("Number of baseAggregators '" +
                baseAggregators.length + "' and baseAttributeInitialValues '" +
                baseAttributeInitialValues.length + "' is not equal for '" +
                attributeFunction + "'",
                attributeFunction.getQueryContextStartIndex(), attributeFunction.getQueryContextEndIndex());
    }
}
 
Example #30
Source File: AggregationParser.java    From siddhi with Apache License 2.0 4 votes vote down vote up
private static List<ExpressionExecutor> constructProcessExpressionExecutors(
        SiddhiQueryContext siddhiQueryContext, Map<String, Table> tableMap, int baseAggregatorBeginIndex,
        List<Expression> finalBaseExpressions, StreamDefinition incomingOutputStreamDefinition,
        MetaStreamEvent processedMetaStreamEvent,
        List<VariableExpressionExecutor> processVariableExpressionExecutors, boolean isProcessingOnExternalTime,
        TimePeriod.Duration duration, boolean isDistributed, String shardId, boolean isLatestEventColAdded) {

    List<ExpressionExecutor> processExpressionExecutors = new ArrayList<>();
    List<Attribute> attributeList = incomingOutputStreamDefinition.getAttributeList();

    int i = 1;
    //Add timestamp executor
    Attribute attribute = attributeList.get(0);
    VariableExpressionExecutor variableExpressionExecutor = (VariableExpressionExecutor) ExpressionParser
            .parseExpression(new Variable(attribute.getName()), processedMetaStreamEvent, 0, tableMap,
                    processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
    processExpressionExecutors.add(variableExpressionExecutor);

    if (isDistributed) {
        Expression shardIdExpression = Expression.value(shardId);
        ExpressionExecutor shardIdExpressionExecutor = ExpressionParser.parseExpression(shardIdExpression,
                processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, true, 0,
                ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(shardIdExpressionExecutor);
        i++;
    }

    if (isProcessingOnExternalTime) {
        Expression externalTimestampExpression =
                AttributeFunction.function("incrementalAggregator", "getAggregationStartTime",
                        new Variable(AGG_EXTERNAL_TIMESTAMP_COL), new StringConstant(duration.name()));
        ExpressionExecutor externalTimestampExecutor = ExpressionParser.parseExpression(
                externalTimestampExpression, processedMetaStreamEvent, 0, tableMap,
                processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(externalTimestampExecutor);
        i++;
    }

    if (isLatestEventColAdded) {
        baseAggregatorBeginIndex = baseAggregatorBeginIndex - 1;
    }

    for (; i < baseAggregatorBeginIndex; i++) {
        attribute = attributeList.get(i);
        variableExpressionExecutor = (VariableExpressionExecutor) ExpressionParser.parseExpression(
                new Variable(attribute.getName()), processedMetaStreamEvent, 0, tableMap,
                processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(variableExpressionExecutor);
    }

    if (isLatestEventColAdded) {
        Expression lastTimestampExpression =
                AttributeFunction.function("max", new Variable(AGG_LAST_TIMESTAMP_COL));
        ExpressionExecutor latestTimestampExecutor = ExpressionParser.parseExpression(lastTimestampExpression,
                processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, true, 0,
                ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(latestTimestampExecutor);
    }

    for (Expression expression : finalBaseExpressions) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression,
                processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors,
                true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(expressionExecutor);
    }
    return processExpressionExecutors;
}