Search in sources :

Example 1 with SqlCreateFunction

use of com.hortonworks.streamline.streams.sql.parser.SqlCreateFunction in project streamline by hortonworks.

the class StreamlineSqlImpl method handleCreateFunction.

private void handleCreateFunction(SqlCreateFunction sqlCreateFunction) throws ClassNotFoundException {
    if (sqlCreateFunction.jarName() != null) {
        throw new UnsupportedOperationException("UDF 'USING JAR' not implemented");
    }
    Method method;
    Function function;
    if ((method = findMethod(sqlCreateFunction.className(), "evaluate")) != null) {
        function = ScalarFunctionImpl.create(method);
    } else if (findMethod(sqlCreateFunction.className(), "add") != null) {
        function = AggregateFunctionImpl.create(Class.forName(sqlCreateFunction.className()));
    } else {
        throw new RuntimeException("Invalid scalar or aggregate function");
    }
    schema.add(sqlCreateFunction.functionName().toUpperCase(), function);
    hasUdf = true;
}
Also used : SqlCreateFunction(com.hortonworks.streamline.streams.sql.parser.SqlCreateFunction) Function(org.apache.calcite.schema.Function) Method(java.lang.reflect.Method)

Example 2 with SqlCreateFunction

use of com.hortonworks.streamline.streams.sql.parser.SqlCreateFunction in project streamline by hortonworks.

the class StreamlineSqlImpl method execute.

@Override
public void execute(Iterable<String> statements, ChannelHandler result) throws Exception {
    Map<String, DataSource> dataSources = new HashMap<>();
    for (String sql : statements) {
        StreamlineParser parser = new StreamlineParser(sql);
        SqlNode node = parser.impl().parseSqlStmtEof();
        if (node instanceof SqlCreateTable) {
            handleCreateTable((SqlCreateTable) node, dataSources);
        } else if (node instanceof SqlCreateFunction) {
            handleCreateFunction((SqlCreateFunction) node);
        } else {
            FrameworkConfig config = buildFrameWorkConfig();
            Planner planner = Frameworks.getPlanner(config);
            SqlNode parse = planner.parse(sql);
            SqlNode validate = planner.validate(parse);
            RelNode tree = planner.convert(validate);
            PlanCompiler compiler = new PlanCompiler(typeFactory);
            AbstractValuesProcessor proc = compiler.compile(tree);
            proc.initialize(dataSources, result);
        }
    }
}
Also used : StreamlineParser(com.hortonworks.streamline.streams.sql.parser.StreamlineParser) HashMap(java.util.HashMap) AbstractValuesProcessor(com.hortonworks.streamline.streams.sql.runtime.AbstractValuesProcessor) SqlCreateTable(com.hortonworks.streamline.streams.sql.parser.SqlCreateTable) DataSource(com.hortonworks.streamline.streams.sql.runtime.DataSource) PlanCompiler(com.hortonworks.streamline.streams.sql.compiler.PlanCompiler) RelNode(org.apache.calcite.rel.RelNode) SqlCreateFunction(com.hortonworks.streamline.streams.sql.parser.SqlCreateFunction) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlCreateFunction (com.hortonworks.streamline.streams.sql.parser.SqlCreateFunction)2 PlanCompiler (com.hortonworks.streamline.streams.sql.compiler.PlanCompiler)1 SqlCreateTable (com.hortonworks.streamline.streams.sql.parser.SqlCreateTable)1 StreamlineParser (com.hortonworks.streamline.streams.sql.parser.StreamlineParser)1 AbstractValuesProcessor (com.hortonworks.streamline.streams.sql.runtime.AbstractValuesProcessor)1 DataSource (com.hortonworks.streamline.streams.sql.runtime.DataSource)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 RelNode (org.apache.calcite.rel.RelNode)1 Function (org.apache.calcite.schema.Function)1 SqlNode (org.apache.calcite.sql.SqlNode)1 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)1 Planner (org.apache.calcite.tools.Planner)1