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());
}
}
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;
}
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);
}
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);
}
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());
}
}
Aggregations