Search in sources :

Example 1 with ChannelHandler

use of org.apache.storm.sql.runtime.ChannelHandler 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 ChannelHandler

use of org.apache.storm.sql.runtime.ChannelHandler 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 ChannelHandler

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

the class TestStormSql method testAggFnNonSqlReturnType.

@Test
public void testAggFnNonSqlReturnType() throws Exception {
    List<String> stmt = new ArrayList<>();
    stmt.add("CREATE EXTERNAL TABLE FOO (ID INT PRIMARY KEY, SALARY INT, PCT DOUBLE, NAME VARCHAR) LOCATION 'mockgroup:///foo'");
    stmt.add("CREATE FUNCTION TOPN AS 'org.apache.storm.sql.TestUtils$TopN'");
    stmt.add("SELECT STREAM ID, SUM(SALARY), TOPN(1, SALARY) FROM FOO WHERE ID >= 0 GROUP BY (ID) HAVING MAX(SALARY) > 0");
    StormSql sql = StormSql.construct();
    List<Values> values = new ArrayList<>();
    ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
    sql.execute(stmt, h);
    Assert.assertEquals(4, values.size());
    Assert.assertEquals(Collections.singletonList(2), values.get(0).get(2));
    Assert.assertEquals(Collections.singletonList(5), values.get(1).get(2));
    Assert.assertEquals(Collections.singletonList(8), values.get(2).get(2));
    Assert.assertEquals(Collections.singletonList(9), values.get(3).get(2));
}
Also used : ArrayList(java.util.ArrayList) Values(org.apache.storm.tuple.Values) ChannelHandler(org.apache.storm.sql.runtime.ChannelHandler) Test(org.junit.Test)

Example 4 with ChannelHandler

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

the class TestStormSql method testGroupbySameAggregateOnDifferentColumns.

@Test
public void testGroupbySameAggregateOnDifferentColumns() throws Exception {
    List<String> stmt = new ArrayList<>();
    stmt.add("CREATE EXTERNAL TABLE FOO (ID INT PRIMARY KEY, SALARY INT, PCT DOUBLE, NAME VARCHAR) LOCATION 'mockgroup:///foo'");
    stmt.add("SELECT STREAM ID, COUNT(*), AVG(SALARY), AVG(PCT) FROM FOO WHERE ID = 1 GROUP BY (ID)");
    StormSql sql = StormSql.construct();
    List<Values> values = new ArrayList<>();
    ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
    sql.execute(stmt, h);
    Assert.assertEquals(1, values.size());
    Assert.assertEquals(1, values.get(0).get(0));
    Assert.assertEquals(3L, values.get(0).get(1));
    Assert.assertEquals(4, values.get(0).get(2));
    Assert.assertEquals(2.5, values.get(0).get(3));
}
Also used : ArrayList(java.util.ArrayList) Values(org.apache.storm.tuple.Values) ChannelHandler(org.apache.storm.sql.runtime.ChannelHandler) Test(org.junit.Test)

Example 5 with ChannelHandler

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

the class TestStormSql method testExternalNestedNonExistKeyAccess2.

@Test
public void testExternalNestedNonExistKeyAccess2() throws Exception {
    List<String> stmt = new ArrayList<>();
    // this triggers java.lang.RuntimeException: Cannot convert null to int
    stmt.add("CREATE EXTERNAL TABLE FOO (ID INT, MAPFIELD ANY, NESTEDMAPFIELD ANY, ARRAYFIELD ANY) LOCATION 'mocknested:///foo'");
    stmt.add("SELECT STREAM ID, MAPFIELD, NESTEDMAPFIELD, ARRAYFIELD " + "FROM FOO " + "WHERE CAST(NESTEDMAPFIELD['b']['c'] AS INTEGER) = 4");
    StormSql sql = StormSql.construct();
    List<Values> values = new ArrayList<>();
    ChannelHandler h = new TestUtils.CollectDataChannelHandler(values);
    sql.execute(stmt, h);
    Assert.assertEquals(0, values.size());
}
Also used : ArrayList(java.util.ArrayList) Values(org.apache.storm.tuple.Values) ChannelHandler(org.apache.storm.sql.runtime.ChannelHandler) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)25 ChannelHandler (org.apache.storm.sql.runtime.ChannelHandler)25 Values (org.apache.storm.tuple.Values)25 Test (org.junit.Test)24 HashMap (java.util.HashMap)5 TestUtils (org.apache.storm.sql.TestUtils)5 AbstractValuesProcessor (org.apache.storm.sql.runtime.AbstractValuesProcessor)5 DataSource (org.apache.storm.sql.runtime.DataSource)5 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 PlanCompiler (org.apache.storm.sql.compiler.backends.standalone.PlanCompiler)1 TestCompilerUtils (org.apache.storm.sql.compiler.backends.standalone.TestCompilerUtils)1