Search in sources :

Example 51 with SamzaSqlValidator

use of org.apache.samza.sql.planner.SamzaSqlValidator in project samza by apache.

the class TestSamzaSqlEndToEnd method testEndToEndStreamTableRightJoin.

@Test
public void testEndToEndStreamTableRightJoin() throws Exception {
    int numMessages = 20;
    TestAvroSystemFactory.messages.clear();
    Map<String, String> staticConfigs = SamzaSqlTestConfig.fetchStaticConfigsWithFactories(Collections.emptyMap(), numMessages, true);
    String sql = "Insert into testavro.enrichedPageViewTopic " + "select pv.pageKey as __key__, pv.pageKey as pageKey, p.name as companyName, p.name as profileName," + "       p.address as profileAddress " + "from testavro.PROFILE.`$table` as p " + "right join testavro.PAGEVIEW as pv " + " on p.id = pv.profileId";
    List<String> sqlStmts = Arrays.asList(sql);
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMTS_JSON, JsonUtil.toJson(sqlStmts));
    Config config = new MapConfig(staticConfigs);
    new SamzaSqlValidator(config).validate(sqlStmts);
    runApplication(config);
    List<String> outMessages = TestAvroSystemFactory.messages.stream().map(x -> ((GenericRecord) x.getMessage()).get("pageKey").toString() + "," + (((GenericRecord) x.getMessage()).get("profileName") == null ? "null" : ((GenericRecord) x.getMessage()).get("profileName").toString())).collect(Collectors.toList());
    Assert.assertEquals(numMessages, outMessages.size());
    List<String> expectedOutMessages = TestAvroSystemFactory.getPageKeyProfileNameOuterJoinWithNullForeignKeys(numMessages);
    Assert.assertEquals(expectedOutMessages, outMessages);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) TestAvroSystemFactory(org.apache.samza.sql.system.TestAvroSystemFactory) SamzaSqlValidatorException(org.apache.samza.sql.planner.SamzaSqlValidatorException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MyTestUdf(org.apache.samza.sql.util.MyTestUdf) HashSet(java.util.HashSet) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) GenericRecord(org.apache.avro.generic.GenericRecord) Logger(org.slf4j.Logger) SampleRelConverterFactory(org.apache.samza.sql.util.SampleRelConverterFactory) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) Set(java.util.Set) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) SamzaSqlValidator(org.apache.samza.sql.planner.SamzaSqlValidator) Test(org.junit.Test) JsonUtil(org.apache.samza.sql.util.JsonUtil) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Ignore(org.junit.Ignore) Optional(java.util.Optional) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) Collections(java.util.Collections) SamzaSqlValidator(org.apache.samza.sql.planner.SamzaSqlValidator) MapConfig(org.apache.samza.config.MapConfig) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 52 with SamzaSqlValidator

use of org.apache.samza.sql.planner.SamzaSqlValidator in project samza by apache.

the class TestSamzaSqlEndToEnd method testRegexMatchUdfInWhereClause.

@Test
public void testRegexMatchUdfInWhereClause() throws Exception {
    int numMessages = 20;
    TestAvroSystemFactory.messages.clear();
    Map<String, String> staticConfigs = SamzaSqlTestConfig.fetchStaticConfigsWithFactories(numMessages);
    String sql1 = "Insert into testavro.outputTopic(id, bool_value) " + "select id, NOT(id = 5) as bool_value " + "from testavro.SIMPLE1 " + "where RegexMatch('.*4', name)";
    List<String> sqlStmts = Collections.singletonList(sql1);
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMTS_JSON, JsonUtil.toJson(sqlStmts));
    Config config = new MapConfig(staticConfigs);
    new SamzaSqlValidator(config).validate(sqlStmts);
    runApplication(config);
    LOG.info("output Messages " + TestAvroSystemFactory.messages);
    // There should be two messages that contain "4"
    Assert.assertEquals(TestAvroSystemFactory.messages.size(), 2);
}
Also used : SamzaSqlValidator(org.apache.samza.sql.planner.SamzaSqlValidator) MapConfig(org.apache.samza.config.MapConfig) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) Test(org.junit.Test)

Example 53 with SamzaSqlValidator

use of org.apache.samza.sql.planner.SamzaSqlValidator in project samza by apache.

the class TestSamzaSqlEndToEnd method testEndToEndWithProjectionWithCase.

@Test
public void testEndToEndWithProjectionWithCase() throws Exception {
    int numMessages = 20;
    TestAvroSystemFactory.messages.clear();
    Map<String, String> staticConfigs = SamzaSqlTestConfig.fetchStaticConfigsWithFactories(numMessages);
    String sql1 = "Insert into testavro.outputTopic(id, long_value) " + " select id, NOT(id = 5) as bool_value, CASE WHEN id IN (5, 6, 7) THEN CAST('foo' AS VARCHAR) WHEN id < 5 THEN CAST('bars' AS VARCHAR) ELSE NULL END as string_value from testavro.SIMPLE1";
    List<String> sqlStmts = Arrays.asList(sql1);
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMTS_JSON, JsonUtil.toJson(sqlStmts));
    Config config = new MapConfig(staticConfigs);
    new SamzaSqlValidator(config).validate(sqlStmts);
    runApplication(config);
    List<Integer> outMessages = TestAvroSystemFactory.messages.stream().map(x -> Integer.valueOf(((GenericRecord) x.getMessage()).get("id").toString())).sorted().collect(Collectors.toList());
    Assert.assertEquals(numMessages, outMessages.size());
    Assert.assertTrue(IntStream.range(0, numMessages).boxed().collect(Collectors.toList()).equals(outMessages));
}
Also used : SamzaSqlValidator(org.apache.samza.sql.planner.SamzaSqlValidator) MapConfig(org.apache.samza.config.MapConfig) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) Test(org.junit.Test)

Example 54 with SamzaSqlValidator

use of org.apache.samza.sql.planner.SamzaSqlValidator in project samza by apache.

the class TestSamzaSqlEndToEnd method testEndToEndStreamTableInnerJoinWithNullForeignKeys.

@Test
public void testEndToEndStreamTableInnerJoinWithNullForeignKeys() throws Exception {
    int numMessages = 20;
    TestAvroSystemFactory.messages.clear();
    Map<String, String> staticConfigs = SamzaSqlTestConfig.fetchStaticConfigsWithFactories(Collections.emptyMap(), numMessages, true);
    String sql = "Insert into testavro.enrichedPageViewTopic " + "select pv.pageKey as __key__, pv.pageKey as pageKey, p.name as companyName, p.name as profileName," + "       p.address as profileAddress " + "from testavro.PAGEVIEW as pv " + "join testavro.PROFILE.`$table` as p " + " on pv.profileId = p.id";
    List<String> sqlStmts = Arrays.asList(sql);
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMTS_JSON, JsonUtil.toJson(sqlStmts));
    Config config = new MapConfig(staticConfigs);
    new SamzaSqlValidator(config).validate(sqlStmts);
    runApplication(config);
    List<String> outMessages = TestAvroSystemFactory.messages.stream().map(x -> ((GenericRecord) x.getMessage()).get("pageKey").toString() + "," + (((GenericRecord) x.getMessage()).get("profileName") == null ? "null" : ((GenericRecord) x.getMessage()).get("profileName").toString())).collect(Collectors.toList());
    // Half the foreign keys are null.
    Assert.assertEquals(numMessages / 2, outMessages.size());
    List<String> expectedOutMessages = TestAvroSystemFactory.getPageKeyProfileNameJoinWithNullForeignKeys(numMessages);
    Assert.assertEquals(expectedOutMessages, outMessages);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) TestAvroSystemFactory(org.apache.samza.sql.system.TestAvroSystemFactory) SamzaSqlValidatorException(org.apache.samza.sql.planner.SamzaSqlValidatorException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MyTestUdf(org.apache.samza.sql.util.MyTestUdf) HashSet(java.util.HashSet) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) GenericRecord(org.apache.avro.generic.GenericRecord) Logger(org.slf4j.Logger) SampleRelConverterFactory(org.apache.samza.sql.util.SampleRelConverterFactory) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) Set(java.util.Set) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) SamzaSqlValidator(org.apache.samza.sql.planner.SamzaSqlValidator) Test(org.junit.Test) JsonUtil(org.apache.samza.sql.util.JsonUtil) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Ignore(org.junit.Ignore) Optional(java.util.Optional) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) Collections(java.util.Collections) SamzaSqlValidator(org.apache.samza.sql.planner.SamzaSqlValidator) MapConfig(org.apache.samza.config.MapConfig) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 55 with SamzaSqlValidator

use of org.apache.samza.sql.planner.SamzaSqlValidator in project samza by apache.

the class TestSamzaSqlEndToEnd method testEndToEndStreamTableInnerJoinWithFilter.

@Test
public void testEndToEndStreamTableInnerJoinWithFilter() throws Exception {
    int numMessages = 20;
    TestAvroSystemFactory.messages.clear();
    Map<String, String> staticConfigs = SamzaSqlTestConfig.fetchStaticConfigsWithFactories(numMessages);
    String sql = "Insert into testavro.enrichedPageViewTopic " + "select pv.pageKey as __key__, pv.pageKey as pageKey, p.name as companyName, p.name as profileName," + "       p.address as profileAddress " + "from testavro.PROFILE.`$table` as p " + "join testavro.PAGEVIEW as pv " + " on p.id = pv.profileId " + "where p.name = 'Mike'";
    List<String> sqlStmts = Arrays.asList(sql);
    staticConfigs.put(SamzaSqlApplicationConfig.CFG_SQL_STMTS_JSON, JsonUtil.toJson(sqlStmts));
    Config config = new MapConfig(staticConfigs);
    new SamzaSqlValidator(config).validate(sqlStmts);
    runApplication(config);
    List<String> outMessages = TestAvroSystemFactory.messages.stream().map(x -> ((GenericRecord) x.getMessage()).get("pageKey").toString() + "," + (((GenericRecord) x.getMessage()).get("profileName") == null ? "null" : ((GenericRecord) x.getMessage()).get("profileName").toString())).collect(Collectors.toList());
    Assert.assertEquals(4, outMessages.size());
    List<String> expectedOutMessages = TestAvroSystemFactory.getPageKeyProfileNameJoin(numMessages).stream().filter(msg -> msg.endsWith("Mike")).collect(Collectors.toList());
    Assert.assertEquals(expectedOutMessages, outMessages);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) TestAvroSystemFactory(org.apache.samza.sql.system.TestAvroSystemFactory) SamzaSqlValidatorException(org.apache.samza.sql.planner.SamzaSqlValidatorException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MyTestUdf(org.apache.samza.sql.util.MyTestUdf) HashSet(java.util.HashSet) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) GenericRecord(org.apache.avro.generic.GenericRecord) Logger(org.slf4j.Logger) SampleRelConverterFactory(org.apache.samza.sql.util.SampleRelConverterFactory) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) Set(java.util.Set) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) SamzaSqlValidator(org.apache.samza.sql.planner.SamzaSqlValidator) Test(org.junit.Test) JsonUtil(org.apache.samza.sql.util.JsonUtil) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Ignore(org.junit.Ignore) Optional(java.util.Optional) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) Collections(java.util.Collections) SamzaSqlValidator(org.apache.samza.sql.planner.SamzaSqlValidator) MapConfig(org.apache.samza.config.MapConfig) SamzaSqlTestConfig(org.apache.samza.sql.util.SamzaSqlTestConfig) SamzaSqlApplicationConfig(org.apache.samza.sql.runner.SamzaSqlApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Aggregations

Config (org.apache.samza.config.Config)55 MapConfig (org.apache.samza.config.MapConfig)55 SamzaSqlValidator (org.apache.samza.sql.planner.SamzaSqlValidator)55 SamzaSqlApplicationConfig (org.apache.samza.sql.runner.SamzaSqlApplicationConfig)55 SamzaSqlTestConfig (org.apache.samza.sql.util.SamzaSqlTestConfig)55 Test (org.junit.Test)55 Ignore (org.junit.Ignore)28 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 Arrays (java.util.Arrays)24 List (java.util.List)24 Map (java.util.Map)24 Collectors (java.util.stream.Collectors)24 GenericRecord (org.apache.avro.generic.GenericRecord)24 SamzaSqlValidatorException (org.apache.samza.sql.planner.SamzaSqlValidatorException)24 TestAvroSystemFactory (org.apache.samza.sql.system.TestAvroSystemFactory)24 JsonUtil (org.apache.samza.sql.util.JsonUtil)24 Assert (org.junit.Assert)24 OutgoingMessageEnvelope (org.apache.samza.system.OutgoingMessageEnvelope)23 HashSet (java.util.HashSet)22