Search in sources :

Example 1 with StreamFunction

use of io.siddhi.query.api.execution.query.input.handler.StreamFunction in project siddhi by wso2.

the class SingleInputStreamParser method generateProcessor.

public static Processor generateProcessor(StreamHandler streamHandler, MetaComplexEvent metaEvent, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, boolean findToBeExecuted, SiddhiQueryContext siddhiQueryContext) {
    Expression[] parameters = streamHandler.getParameters();
    MetaStreamEvent metaStreamEvent;
    int stateIndex = SiddhiConstants.UNKNOWN_STATE;
    if (metaEvent instanceof MetaStateEvent) {
        stateIndex = ((MetaStateEvent) metaEvent).getStreamEventCount() - 1;
        metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(stateIndex);
    } else {
        metaStreamEvent = (MetaStreamEvent) metaEvent;
    }
    if (streamHandler instanceof Window) {
        metaStreamEvent.initializeOnAfterWindowData();
    }
    ExpressionExecutor[] attributeExpressionExecutors;
    if (parameters != null) {
        if (parameters.length > 0) {
            attributeExpressionExecutors = new ExpressionExecutor[parameters.length];
            for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
                attributeExpressionExecutors[i] = ExpressionParser.parseExpression(parameters[i], metaEvent, stateIndex, tableMap, variableExpressionExecutors, false, SiddhiConstants.CURRENT, ProcessingMode.BATCH, false, siddhiQueryContext);
            }
        } else {
            List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
            int parameterSize = attributeList.size();
            attributeExpressionExecutors = new ExpressionExecutor[parameterSize];
            for (int i = 0; i < parameterSize; i++) {
                attributeExpressionExecutors[i] = ExpressionParser.parseExpression(new Variable(attributeList.get(i).getName()), metaEvent, stateIndex, tableMap, variableExpressionExecutors, false, SiddhiConstants.CURRENT, ProcessingMode.BATCH, false, siddhiQueryContext);
            }
        }
    } else {
        attributeExpressionExecutors = new ExpressionExecutor[0];
    }
    ConfigReader configReader;
    if (streamHandler instanceof Filter) {
        return new FilterProcessor(attributeExpressionExecutors[0]);
    } else if (streamHandler instanceof Window) {
        WindowProcessor windowProcessor = (WindowProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, WindowProcessorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
        configReader = siddhiQueryContext.getSiddhiContext().getConfigManager().generateConfigReader(((Window) streamHandler).getNamespace(), ((Window) streamHandler).getName());
        windowProcessor.initProcessor(metaStreamEvent, attributeExpressionExecutors, configReader, outputExpectsExpiredEvents, findToBeExecuted, false, streamHandler, siddhiQueryContext);
        return windowProcessor;
    } else if (streamHandler instanceof StreamFunction) {
        AbstractStreamProcessor abstractStreamProcessor;
        configReader = siddhiQueryContext.getSiddhiContext().getConfigManager().generateConfigReader(((StreamFunction) streamHandler).getNamespace(), ((StreamFunction) streamHandler).getName());
        if (supportsBatchProcessing) {
            try {
                abstractStreamProcessor = (StreamProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamProcessorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
                abstractStreamProcessor.initProcessor(metaStreamEvent, attributeExpressionExecutors, configReader, outputExpectsExpiredEvents, false, false, streamHandler, siddhiQueryContext);
                return abstractStreamProcessor;
            } catch (SiddhiAppCreationException e) {
                if (!e.isClassLoadingIssue()) {
                    ExceptionUtil.populateQueryContext(e, streamHandler, siddhiQueryContext.getSiddhiAppContext(), siddhiQueryContext);
                    throw e;
                }
            }
        }
        abstractStreamProcessor = (StreamFunctionProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamFunctionProcessorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
        abstractStreamProcessor.initProcessor(metaStreamEvent, attributeExpressionExecutors, configReader, outputExpectsExpiredEvents, false, false, streamHandler, siddhiQueryContext);
        return abstractStreamProcessor;
    } else {
        throw new SiddhiAppCreationException(streamHandler.getClass().getName() + " is not supported", streamHandler.getQueryContextStartIndex(), streamHandler.getQueryContextEndIndex());
    }
}
Also used : Window(io.siddhi.query.api.execution.query.input.handler.Window) Variable(io.siddhi.query.api.expression.Variable) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) AbstractStreamProcessor(io.siddhi.core.query.processor.stream.AbstractStreamProcessor) Attribute(io.siddhi.query.api.definition.Attribute) FilterProcessor(io.siddhi.core.query.processor.filter.FilterProcessor) StreamFunction(io.siddhi.query.api.execution.query.input.handler.StreamFunction) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(io.siddhi.core.util.config.ConfigReader) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) Expression(io.siddhi.query.api.expression.Expression) Filter(io.siddhi.query.api.execution.query.input.handler.Filter) WindowProcessor(io.siddhi.core.query.processor.stream.window.WindowProcessor) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 2 with StreamFunction

use of io.siddhi.query.api.execution.query.input.handler.StreamFunction in project siddhi by wso2.

the class AggregationParser method initAggregateQueryExecutor.

private static Map<TimePeriod.Duration, Processor> initAggregateQueryExecutor(List<TimePeriod.Duration> aggregationDurations, Map<TimePeriod.Duration, List<ExpressionExecutor>> processExpressionExecutorsMap, StreamDefinition incomingOutputStreamDefinition, boolean isDistributed, String shardID, boolean isProcessingOnExternalTime, SiddhiQueryContext siddhiQueryContext, AggregationDefinition aggregationDefinition, ConfigManager configManager, Map<TimePeriod.Duration, Table> aggregationTables, List<Variable> groupByVariableList) {
    Map<TimePeriod.Duration, Processor> cudProcessors = new LinkedHashMap<>();
    String datasourceName = AnnotationHelper.getAnnotationElement(SiddhiConstants.NAMESPACE_STORE, "datasource", aggregationDefinition.getAnnotations()).getValue();
    if (datasourceName == null || datasourceName.isEmpty()) {
        throw new SiddhiAppCreationException("Datasource configuration must be provided inorder to use persisted " + "aggregation mode");
    }
    Database databaseType = getDatabaseType(configManager, datasourceName);
    if (log.isDebugEnabled()) {
        log.debug("Database type " + databaseType);
    }
    SiddhiAppContext cudSiddhiAppContext = new SiddhiAppContext();
    SiddhiContext context = new SiddhiContext();
    context.setConfigManager(configManager);
    cudSiddhiAppContext.setSiddhiContext(context);
    StringConstant datasource = new StringConstant(datasourceName);
    ConstantExpressionExecutor datasourceExecutor = new ConstantExpressionExecutor(datasource.getValue(), Attribute.Type.STRING);
    Expression[] streamHandler;
    ExpressionExecutor[] cudStreamProcessorInputVariables;
    if (isProcessingOnExternalTime) {
        streamHandler = new Expression[7];
    } else {
        streamHandler = new Expression[5];
    }
    try {
        DBAggregationQueryConfigurationEntry dbAggregationQueryConfigurationEntry = DBAggregationQueryUtil.lookupCurrentQueryConfigurationEntry(databaseType);
        if (log.isDebugEnabled()) {
            log.debug("CUD queries for aggregation " + aggregationDefinition.getId());
        }
        for (int i = aggregationDurations.size() - 1; i > 0; i--) {
            if (aggregationDurations.get(i).ordinal() >= 3) {
                if (log.isDebugEnabled()) {
                    log.debug(" Initializing cudProcessors for duration " + aggregationDurations.get(i));
                }
                String databaseSelectQuery = generateDatabaseQuery(processExpressionExecutorsMap.get(aggregationDurations.get(i)), dbAggregationQueryConfigurationEntry, incomingOutputStreamDefinition, isDistributed, shardID, isProcessingOnExternalTime, aggregationTables.get(aggregationDurations.get(i)), aggregationTables.get(aggregationDurations.get(i - 1)), groupByVariableList, aggregationDurations.get(i));
                StringConstant selectQuery = new StringConstant(databaseSelectQuery);
                if (log.isDebugEnabled()) {
                    log.debug(selectQuery);
                }
                ConstantExpressionExecutor selectExecutor = new ConstantExpressionExecutor(selectQuery.getValue(), Attribute.Type.STRING);
                Map<Attribute, int[]> cudInputStreamAttributesMap = generateCUDInputStreamAttributes(isProcessingOnExternalTime);
                if (isProcessingOnExternalTime) {
                    cudStreamProcessorInputVariables = new ExpressionExecutor[7];
                } else {
                    cudStreamProcessorInputVariables = new ExpressionExecutor[5];
                }
                cudStreamProcessorInputVariables[0] = datasourceExecutor;
                cudStreamProcessorInputVariables[1] = selectExecutor;
                streamHandler[0] = datasource;
                streamHandler[1] = selectQuery;
                MetaStreamEvent metaStreamEvent = generateCUDMetaStreamEvent(isProcessingOnExternalTime);
                StreamDefinition outputStream = new StreamDefinition();
                VariableExpressionExecutor variableExpressionExecutor;
                int j = 0;
                for (Map.Entry<Attribute, int[]> entry : cudInputStreamAttributesMap.entrySet()) {
                    Attribute attribute = entry.getKey();
                    Variable timestampVariable = new Variable(attribute.getName());
                    for (int position : entry.getValue()) {
                        streamHandler[position + 2] = timestampVariable;
                        variableExpressionExecutor = new VariableExpressionExecutor(attribute, 0, 0);
                        variableExpressionExecutor.setPosition(new int[] { 2, j });
                        cudStreamProcessorInputVariables[position + 2] = variableExpressionExecutor;
                    }
                    outputStream.attribute(attribute.getName(), attribute.getType());
                    j++;
                }
                StreamFunction cudStreamFunction = new StreamFunction(NAMESPACE_RDBMS, FUNCTION_NAME_CUD, streamHandler);
                cudProcessors.put(aggregationDurations.get(i), getCudProcessor(cudStreamFunction, siddhiQueryContext, metaStreamEvent, cudStreamProcessorInputVariables, aggregationDurations.get(i)));
            }
        }
        return cudProcessors;
    } catch (CannotLoadConfigurationException e) {
        throw new SiddhiAppCreationException("Error occurred while initializing the persisted incremental " + "aggregation. Could not load the db quires for database type " + databaseType);
    }
}
Also used : PersistedAggregationResultsProcessor(io.siddhi.core.aggregation.persistedaggregation.config.PersistedAggregationResultsProcessor) QueryableProcessor(io.siddhi.core.query.processor.stream.window.QueryableProcessor) AbstractStreamProcessor(io.siddhi.core.query.processor.stream.AbstractStreamProcessor) QueuedCudStreamProcessor(io.siddhi.core.aggregation.persistedaggregation.QueuedCudStreamProcessor) StreamProcessor(io.siddhi.core.query.processor.stream.StreamProcessor) Processor(io.siddhi.core.query.processor.Processor) IncrementalAggregationProcessor(io.siddhi.core.aggregation.IncrementalAggregationProcessor) Variable(io.siddhi.query.api.expression.Variable) Attribute(io.siddhi.query.api.definition.Attribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) SiddhiContext(io.siddhi.core.config.SiddhiContext) LinkedHashMap(java.util.LinkedHashMap) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) DBAggregationQueryConfigurationEntry(io.siddhi.core.aggregation.persistedaggregation.config.DBAggregationQueryConfigurationEntry) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) StreamFunction(io.siddhi.query.api.execution.query.input.handler.StreamFunction) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) CannotLoadConfigurationException(io.siddhi.core.exception.CannotLoadConfigurationException) Expression(io.siddhi.query.api.expression.Expression) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) StringConstant(io.siddhi.query.api.expression.constant.StringConstant) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 3 with StreamFunction

use of io.siddhi.query.api.execution.query.input.handler.StreamFunction in project siddhi by wso2.

the class SimpleQueryTestCase method testQuery4.

@Test
public void testQuery4() {
    StreamFunction streamFunction = new StreamFunction("function1");
    Query query = Query.query();
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.compare(Expression.value(100), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("volume")))).function(streamFunction).window("length", Expression.value(50)).function("foo", Expression.value(67), Expression.value(89)).filter(Expression.compare(Expression.value(10), Compare.Operator.LESS_THAN_EQUAL, Expression.variable("price"))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("price"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.insertInto("OutStockStream");
}
Also used : Query(io.siddhi.query.api.execution.query.Query) StreamFunction(io.siddhi.query.api.execution.query.input.handler.StreamFunction) Test(org.testng.annotations.Test)

Example 4 with StreamFunction

use of io.siddhi.query.api.execution.query.input.handler.StreamFunction in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitStream_function.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public StreamFunction visitStream_function(@NotNull SiddhiQLParser.Stream_functionContext ctx) {
    AttributeFunction attributeFunction = (AttributeFunction) visit(ctx.function_operation());
    StreamFunction streamFunction = new StreamFunction(attributeFunction.getNamespace(), attributeFunction.getName(), attributeFunction.getParameters());
    populateQueryContext(streamFunction, ctx);
    return streamFunction;
}
Also used : StreamFunction(io.siddhi.query.api.execution.query.input.handler.StreamFunction) AttributeFunction(io.siddhi.query.api.expression.AttributeFunction)

Example 5 with StreamFunction

use of io.siddhi.query.api.execution.query.input.handler.StreamFunction in project siddhi by wso2.

the class SimpleQueryTestCase method testQuery5.

@Test
public void testQuery5() {
    StreamFunction streamFunction = new StreamFunction("funcNameSpace", "function1");
    Query query = Query.query();
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.compare(Expression.value(100), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("volume")))).function(streamFunction).window("length", Expression.value(50)).function("foo", Expression.value(67), Expression.value(89)).filter(Expression.compare(Expression.value(10), Compare.Operator.LESS_THAN_EQUAL, Expression.variable("price"))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("price"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.insertInto("OutStockStream");
}
Also used : Query(io.siddhi.query.api.execution.query.Query) StreamFunction(io.siddhi.query.api.execution.query.input.handler.StreamFunction) Test(org.testng.annotations.Test)

Aggregations

StreamFunction (io.siddhi.query.api.execution.query.input.handler.StreamFunction)5 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)2 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)2 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)2 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)2 AbstractStreamProcessor (io.siddhi.core.query.processor.stream.AbstractStreamProcessor)2 Attribute (io.siddhi.query.api.definition.Attribute)2 Query (io.siddhi.query.api.execution.query.Query)2 Expression (io.siddhi.query.api.expression.Expression)2 Variable (io.siddhi.query.api.expression.Variable)2 Test (org.testng.annotations.Test)2 IncrementalAggregationProcessor (io.siddhi.core.aggregation.IncrementalAggregationProcessor)1 QueuedCudStreamProcessor (io.siddhi.core.aggregation.persistedaggregation.QueuedCudStreamProcessor)1 DBAggregationQueryConfigurationEntry (io.siddhi.core.aggregation.persistedaggregation.config.DBAggregationQueryConfigurationEntry)1 PersistedAggregationResultsProcessor (io.siddhi.core.aggregation.persistedaggregation.config.PersistedAggregationResultsProcessor)1 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 SiddhiContext (io.siddhi.core.config.SiddhiContext)1 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)1 CannotLoadConfigurationException (io.siddhi.core.exception.CannotLoadConfigurationException)1 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)1