Search in sources :

Example 1 with AggregationDefinition

use of org.ballerinalang.siddhi.query.api.definition.AggregationDefinition in project ballerina by ballerina-lang.

the class DefinitionParserHelper method validateDefinition.

public static void validateDefinition(AbstractDefinition definition, ConcurrentMap<String, AbstractDefinition> streamDefinitionMap, ConcurrentMap<String, AbstractDefinition> tableDefinitionMap, ConcurrentMap<String, AbstractDefinition> windowDefinitionMap, ConcurrentMap<String, AbstractDefinition> aggregationDefinitionMap) {
    AbstractDefinition existingTableDefinition = tableDefinitionMap.get(definition.getId());
    if (existingTableDefinition != null && (!existingTableDefinition.equals(definition) || definition instanceof StreamDefinition)) {
        throw new DuplicateDefinitionException("Table Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingTableDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingStreamDefinition = streamDefinitionMap.get(definition.getId());
    if (existingStreamDefinition != null && (!existingStreamDefinition.equals(definition) || definition instanceof TableDefinition)) {
        throw new DuplicateDefinitionException("Stream Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingStreamDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingWindowDefinition = windowDefinitionMap.get(definition.getId());
    if (existingWindowDefinition != null && (!existingWindowDefinition.equals(definition) || definition instanceof WindowDefinition)) {
        throw new DuplicateDefinitionException("Window Definition with same Window Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingAggregationDefinition = aggregationDefinitionMap.get(definition.getId());
    if (existingAggregationDefinition != null && (!existingAggregationDefinition.equals(definition) || definition instanceof AggregationDefinition)) {
        throw new DuplicateDefinitionException("Aggregation Definition with same Aggregation Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException) AggregationDefinition(org.ballerinalang.siddhi.query.api.definition.AggregationDefinition) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) TableDefinition(org.ballerinalang.siddhi.query.api.definition.TableDefinition) WindowDefinition(org.ballerinalang.siddhi.query.api.definition.WindowDefinition)

Example 2 with AggregationDefinition

use of org.ballerinalang.siddhi.query.api.definition.AggregationDefinition in project ballerina by ballerina-lang.

the class SiddhiQLBaseVisitorImpl method visitDefinition_aggregation.

@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;
}
Also used : SiddhiQLParser(org.ballerinalang.siddhi.query.compiler.SiddhiQLParser) Variable(org.ballerinalang.siddhi.query.api.expression.Variable) AggregationDefinition(org.ballerinalang.siddhi.query.api.definition.AggregationDefinition) TimePeriod(org.ballerinalang.siddhi.query.api.aggregation.TimePeriod) BasicSingleInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) BasicSelector(org.ballerinalang.siddhi.query.api.execution.query.selection.BasicSelector)

Example 3 with AggregationDefinition

use of org.ballerinalang.siddhi.query.api.definition.AggregationDefinition in project ballerina by ballerina-lang.

the class DefineAggregationTestCase method test1.

@Test
public void test1() throws SiddhiParserException {
    AggregationDefinition aggregationDefinitionQuery = SiddhiCompiler.parseAggregationDefinition("define aggregation StockAggregation " + "from StockStream " + "select StockStream.timestamp as timestamp, StockStream.symbol as symbol, " + "       StockStream.price as price " + "   group by StockStream.symbol " + "aggregate by timestamp " + "every seconds ... days ;");
    AggregationDefinition aggregationDefinition = AggregationDefinition.id("StockAggregation").from(InputStream.stream("StockStream")).select(Selector.basicSelector().select("timestamp", Expression.variable("timestamp").ofStream("StockStream")).select("symbol", Expression.variable("symbol").ofStream("StockStream")).select("price", Expression.variable("price").ofStream("StockStream")).groupBy(Expression.variable("symbol").ofStream("StockStream"))).aggregateBy(Expression.variable("timestamp")).every(TimePeriod.range(TimePeriod.Duration.SECONDS, TimePeriod.Duration.DAYS));
    AssertJUnit.assertEquals(aggregationDefinition, aggregationDefinitionQuery);
}
Also used : AggregationDefinition(org.ballerinalang.siddhi.query.api.definition.AggregationDefinition) Test(org.testng.annotations.Test)

Example 4 with AggregationDefinition

use of org.ballerinalang.siddhi.query.api.definition.AggregationDefinition in project ballerina by ballerina-lang.

the class DefineAggregationTestCase method test2.

@Test
public void test2() throws SiddhiParserException {
    AggregationDefinition aggregationDefinitionQuery = SiddhiCompiler.parseAggregationDefinition("define aggregation StockAggregationDefinition " + "from StockStream " + "select StockStream.timestamp, StockStream.symbol as symbol, " + "       StockStream.price as price " + "   group by StockStream.symbol " + "aggregate by timestamp " + "every seconds, minutes, hours ;");
    AggregationDefinition aggregationDefinition = AggregationDefinition.id("StockAggregationDefinition").from(InputStream.stream("StockStream")).select(Selector.basicSelector().select(Expression.variable("timestamp").ofStream("StockStream")).select("symbol", Expression.variable("symbol").ofStream("StockStream")).select("price", Expression.variable("price").ofStream("StockStream")).groupBy(Expression.variable("symbol").ofStream("StockStream"))).aggregateBy(Expression.variable("timestamp")).every(TimePeriod.interval(TimePeriod.Duration.SECONDS, TimePeriod.Duration.MINUTES, TimePeriod.Duration.HOURS));
    AssertJUnit.assertEquals(aggregationDefinition, aggregationDefinitionQuery);
}
Also used : AggregationDefinition(org.ballerinalang.siddhi.query.api.definition.AggregationDefinition) Test(org.testng.annotations.Test)

Example 5 with AggregationDefinition

use of org.ballerinalang.siddhi.query.api.definition.AggregationDefinition in project ballerina by ballerina-lang.

the class SiddhiApp method checkDuplicateDefinition.

private void checkDuplicateDefinition(AbstractDefinition definition) {
    TableDefinition existingTableDefinition = tableDefinitionMap.get(definition.getId());
    if (existingTableDefinition != null && (!existingTableDefinition.equals(definition) || definition instanceof StreamDefinition)) {
        throw new DuplicateDefinitionException("Table Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingTableDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    StreamDefinition existingStreamDefinition = streamDefinitionMap.get(definition.getId());
    if (existingStreamDefinition != null && (!existingStreamDefinition.equals(definition) || definition instanceof TableDefinition)) {
        throw new DuplicateDefinitionException("Stream Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingStreamDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    WindowDefinition existingWindowDefinition = windowDefinitionMap.get(definition.getId());
    if (existingWindowDefinition != null && (!existingWindowDefinition.equals(definition) || definition instanceof WindowDefinition)) {
        throw new DuplicateDefinitionException("Stream Definition with same Window Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AggregationDefinition existingAggregationDefinition = aggregationDefinitionMap.get(definition.getId());
    if (existingAggregationDefinition != null && (!existingAggregationDefinition.equals(definition) || definition instanceof AggregationDefinition)) {
        throw new DuplicateDefinitionException("Aggregate Definition with same Aggregate Id '" + definition.getId() + "' already exist : " + existingAggregationDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException) AggregationDefinition(org.ballerinalang.siddhi.query.api.definition.AggregationDefinition) TableDefinition(org.ballerinalang.siddhi.query.api.definition.TableDefinition) WindowDefinition(org.ballerinalang.siddhi.query.api.definition.WindowDefinition)

Aggregations

AggregationDefinition (org.ballerinalang.siddhi.query.api.definition.AggregationDefinition)6 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)2 TableDefinition (org.ballerinalang.siddhi.query.api.definition.TableDefinition)2 WindowDefinition (org.ballerinalang.siddhi.query.api.definition.WindowDefinition)2 DuplicateDefinitionException (org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException)2 Test (org.testng.annotations.Test)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 TimePeriod (org.ballerinalang.siddhi.query.api.aggregation.TimePeriod)1 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)1 BasicSingleInputStream (org.ballerinalang.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream)1 BasicSelector (org.ballerinalang.siddhi.query.api.execution.query.selection.BasicSelector)1 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)1 SiddhiQLParser (org.ballerinalang.siddhi.query.compiler.SiddhiQLParser)1 SiddhiQLBaseVisitorImpl (org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)1