Search in sources :

Example 6 with Selector

use of io.siddhi.query.api.execution.query.selection.Selector in project siddhi by wso2.

the class IncrementalDataPurger method getOnDemandQuery.

private OnDemandQuery getOnDemandQuery(Table table, long timeFrom, long timeTo) {
    List<OutputAttribute> outputAttributes = new ArrayList<>();
    outputAttributes.add(new OutputAttribute(new Variable(AGG_START_TIMESTAMP_COL)));
    Selector selector = Selector.selector().addSelectionList(outputAttributes).groupBy(Expression.variable(AGG_START_TIMESTAMP_COL)).orderBy(Expression.variable(AGG_START_TIMESTAMP_COL), OrderByAttribute.Order.DESC).limit(Expression.value(1));
    InputStore inputStore;
    if (timeTo != 0) {
        inputStore = InputStore.store(table.getTableDefinition().getId()).on(Expression.and(Expression.compare(Expression.variable(AGG_START_TIMESTAMP_COL), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(timeFrom)), Expression.compare(Expression.variable(AGG_START_TIMESTAMP_COL), Compare.Operator.LESS_THAN_EQUAL, Expression.value(timeTo))));
    } else {
        inputStore = InputStore.store(table.getTableDefinition().getId()).on(Expression.compare(Expression.variable(AGG_START_TIMESTAMP_COL), Compare.Operator.LESS_THAN_EQUAL, Expression.value(timeFrom)));
    }
    return OnDemandQuery.query().from(inputStore).select(selector);
}
Also used : Variable(io.siddhi.query.api.expression.Variable) ArrayList(java.util.ArrayList) InputStore(io.siddhi.query.api.execution.query.input.store.InputStore) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) Selector(io.siddhi.query.api.execution.query.selection.Selector)

Example 7 with Selector

use of io.siddhi.query.api.execution.query.selection.Selector 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 8 with Selector

use of io.siddhi.query.api.execution.query.selection.Selector in project siddhi by wso2.

the class SimpleQueryTestCase method test6.

@Test
public void test6() {
    Selector selector = new Selector();
    selector.select("symbol", Expression.isNull(Expression.variable("symbol")));
    selector.select("symbol1", Expression.not(Expression.variable("symbol")));
    selector.select("symbol2", Expression.in(Expression.compare(Expression.variable("symbol"), Compare.Operator.EQUAL, Expression.variable("foo")), "aTable"));
    Selector selector1 = new Selector();
    selector1.select("symbol", Expression.isNull(Expression.variable("symbol")));
    selector1.select("symbol1", Expression.not(Expression.variable("symbol")));
    selector1.select("symbol2", Expression.in(Expression.compare(Expression.variable("symbol"), Compare.Operator.EQUAL, Expression.variable("foo")), "aTable"));
    AssertJUnit.assertTrue(selector.equals(selector1));
    AssertJUnit.assertEquals(selector.hashCode(), selector1.hashCode());
    String selectorString = "Selector{selectionList=[OutputAttribute{rename='symbol', " + "expression=IsNull{streamId='null', streamIndex=null, isInnerStream=false, " + "isFaultStream=false, expression=Variable{id='null', isInnerStream=false, " + "streamIndex=null, functionId='null', functionIndex=null, attributeName='symbol'}}}, " + "OutputAttribute{rename='symbol1', expression=Not{expression=Variable{id='null', " + "isInnerStream=false, streamIndex=null, functionId='null', functionIndex=null, " + "attributeName='symbol'}}}, OutputAttribute{rename='symbol2', " + "expression=In{expression=Compare{rightExpression=Variable{id='null', " + "isInnerStream=false, streamIndex=null, functionId='null', functionIndex=null, " + "attributeName='foo'}, operator=EQUAL, leftExpression=Variable{id='null', " + "isInnerStream=false, streamIndex=null, functionId='null', functionIndex=null, " + "attributeName='symbol'}}, sourceId='aTable'}}], groupByList=[], havingExpression=null, " + "orderByList=[], limit=null, offset=null}";
    AssertJUnit.assertEquals(selector.toString(), selectorString);
}
Also used : Selector(io.siddhi.query.api.execution.query.selection.Selector) Test(org.testng.annotations.Test)

Example 9 with Selector

use of io.siddhi.query.api.execution.query.selection.Selector in project siddhi by wso2.

the class SimpleQueryTestCase method selectortest2.

@Test(expectedExceptions = DuplicateAttributeException.class)
public void selectortest2() {
    Selector selector = new Selector();
    List<OutputAttribute> list = new ArrayList<>();
    list.add(new OutputAttribute(Expression.variable("symbol")));
    selector.select("symbol", Expression.variable("symbol")).orderBy(Expression.variable("avgPrice"), OrderByAttribute.Order.DESC).limit(Expression.value(5)).offset(Expression.value(2)).addSelectionList(list);
}
Also used : ArrayList(java.util.ArrayList) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) Selector(io.siddhi.query.api.execution.query.selection.Selector) Test(org.testng.annotations.Test)

Aggregations

Selector (io.siddhi.query.api.execution.query.selection.Selector)9 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)6 ArrayList (java.util.ArrayList)6 Variable (io.siddhi.query.api.expression.Variable)4 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)2 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)2 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)2 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)2 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)2 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)2 ProcessingMode (io.siddhi.core.query.processor.ProcessingMode)2 GroupByKeyGenerator (io.siddhi.core.query.selector.GroupByKeyGenerator)2 Table (io.siddhi.core.table.Table)2 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)2 Attribute (io.siddhi.query.api.definition.Attribute)2 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)2 Expression (io.siddhi.query.api.expression.Expression)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2