Search in sources :

Example 1 with DataSource

use of org.apache.storm.sql.runtime.DataSource in project storm by apache.

the class TestPlanCompiler method testUdf.

@Test
public void testUdf() throws Exception {
    String sql = "SELECT MYPLUS(ID, 3)" + "FROM FOO " + "WHERE ID = 2";
    TestCompilerUtils.CalciteState state = TestCompilerUtils.sqlOverNestedTable(sql);
    PlanCompiler compiler = new PlanCompiler(typeFactory);
    AbstractValuesProcessor proc = compiler.compile(state.tree());
    Map<String, DataSource> data = new HashMap<>();
    data.put("FOO", new TestUtils.MockDataSource());
    List<Values> values = new ArrayList<>();
    ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
    proc.initialize(data, h);
    Assert.assertEquals(new Values(5), values.get(0));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Values(org.apache.storm.tuple.Values) AbstractValuesProcessor(org.apache.storm.sql.runtime.AbstractValuesProcessor) ChannelHandler(org.apache.storm.sql.runtime.ChannelHandler) DataSource(org.apache.storm.sql.runtime.DataSource) TestUtils(org.apache.storm.sql.TestUtils) Test(org.junit.Test)

Example 2 with DataSource

use of org.apache.storm.sql.runtime.DataSource in project storm by apache.

the class TestPlanCompiler method testNested.

@Test
public void testNested() throws Exception {
    String sql = "SELECT ID, MAPFIELD['c'], NESTEDMAPFIELD, ARRAYFIELD " + "FROM FOO " + "WHERE NESTEDMAPFIELD['a']['b'] = 2 AND ARRAYFIELD[2] = 200";
    TestCompilerUtils.CalciteState state = TestCompilerUtils.sqlOverNestedTable(sql);
    PlanCompiler compiler = new PlanCompiler(typeFactory);
    AbstractValuesProcessor proc = compiler.compile(state.tree());
    Map<String, DataSource> data = new HashMap<>();
    data.put("FOO", new TestUtils.MockNestedDataSource());
    List<Values> values = new ArrayList<>();
    ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
    proc.initialize(data, h);
    Map<String, Integer> map = ImmutableMap.of("b", 2, "c", 4);
    Map<String, Map<String, Integer>> nestedMap = ImmutableMap.of("a", map);
    Assert.assertEquals(new Values(2, 4, nestedMap, Arrays.asList(100, 200, 300)), values.get(0));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Values(org.apache.storm.tuple.Values) AbstractValuesProcessor(org.apache.storm.sql.runtime.AbstractValuesProcessor) ChannelHandler(org.apache.storm.sql.runtime.ChannelHandler) DataSource(org.apache.storm.sql.runtime.DataSource) TestUtils(org.apache.storm.sql.TestUtils) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with DataSource

use of org.apache.storm.sql.runtime.DataSource in project storm by apache.

the class StormSqlImpl method execute.

@Override
public void execute(Iterable<String> statements, ChannelHandler result) throws Exception {
    Map<String, DataSource> dataSources = new HashMap<>();
    for (String sql : statements) {
        StormParser parser = new StormParser(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 : HashMap(java.util.HashMap) AbstractValuesProcessor(org.apache.storm.sql.runtime.AbstractValuesProcessor) SqlCreateTable(org.apache.storm.sql.parser.SqlCreateTable) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) DataSource(org.apache.storm.sql.runtime.DataSource) PlanCompiler(org.apache.storm.sql.compiler.backends.standalone.PlanCompiler) RelNode(org.apache.calcite.rel.RelNode) SqlCreateFunction(org.apache.storm.sql.parser.SqlCreateFunction) Planner(org.apache.calcite.tools.Planner) QueryPlanner(org.apache.storm.sql.planner.trident.QueryPlanner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) StormParser(org.apache.storm.sql.parser.StormParser) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with DataSource

use of org.apache.storm.sql.runtime.DataSource in project storm by apache.

the class TestPlanCompiler method testLogicalExpr.

@Test
public void testLogicalExpr() throws Exception {
    String sql = "SELECT ID > 0 OR ID < 1, ID > 0 AND ID < 1, NOT (ID > 0 AND ID < 1) FROM FOO WHERE ID > 0 AND ID < 2";
    TestCompilerUtils.CalciteState state = TestCompilerUtils.sqlOverDummyTable(sql);
    PlanCompiler compiler = new PlanCompiler(typeFactory);
    AbstractValuesProcessor proc = compiler.compile(state.tree());
    Map<String, DataSource> data = new HashMap<>();
    data.put("FOO", new TestUtils.MockDataSource());
    List<Values> values = new ArrayList<>();
    ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
    proc.initialize(data, h);
    Assert.assertEquals(new Values(true, false, true), values.get(0));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Values(org.apache.storm.tuple.Values) AbstractValuesProcessor(org.apache.storm.sql.runtime.AbstractValuesProcessor) ChannelHandler(org.apache.storm.sql.runtime.ChannelHandler) DataSource(org.apache.storm.sql.runtime.DataSource) TestUtils(org.apache.storm.sql.TestUtils) Test(org.junit.Test)

Example 5 with DataSource

use of org.apache.storm.sql.runtime.DataSource in project storm by apache.

the class TestPlanCompiler method testCompile.

@Test
public void testCompile() throws Exception {
    String sql = "SELECT ID + 1 FROM FOO WHERE ID > 2";
    TestCompilerUtils.CalciteState state = TestCompilerUtils.sqlOverDummyTable(sql);
    PlanCompiler compiler = new PlanCompiler(typeFactory);
    AbstractValuesProcessor proc = compiler.compile(state.tree());
    Map<String, DataSource> data = new HashMap<>();
    data.put("FOO", new TestUtils.MockDataSource());
    List<Values> values = new ArrayList<>();
    ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
    proc.initialize(data, h);
    Assert.assertArrayEquals(new Values[] { new Values(4), new Values(5) }, values.toArray());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Values(org.apache.storm.tuple.Values) AbstractValuesProcessor(org.apache.storm.sql.runtime.AbstractValuesProcessor) ChannelHandler(org.apache.storm.sql.runtime.ChannelHandler) DataSource(org.apache.storm.sql.runtime.DataSource) TestUtils(org.apache.storm.sql.TestUtils) Test(org.junit.Test)

Aggregations

DataSource (org.apache.storm.sql.runtime.DataSource)7 HashMap (java.util.HashMap)6 AbstractValuesProcessor (org.apache.storm.sql.runtime.AbstractValuesProcessor)6 ArrayList (java.util.ArrayList)5 TestUtils (org.apache.storm.sql.TestUtils)5 ChannelHandler (org.apache.storm.sql.runtime.ChannelHandler)5 Values (org.apache.storm.tuple.Values)5 Test (org.junit.Test)4 PlanCompiler (org.apache.storm.sql.compiler.backends.standalone.PlanCompiler)2 ISqlTridentDataSource (org.apache.storm.sql.runtime.ISqlTridentDataSource)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 RelNode (org.apache.calcite.rel.RelNode)1 SqlNode (org.apache.calcite.sql.SqlNode)1 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)1 Planner (org.apache.calcite.tools.Planner)1 TestCompilerUtils (org.apache.storm.sql.compiler.backends.standalone.TestCompilerUtils)1 SqlCreateFunction (org.apache.storm.sql.parser.SqlCreateFunction)1 SqlCreateTable (org.apache.storm.sql.parser.SqlCreateTable)1 StormParser (org.apache.storm.sql.parser.StormParser)1