Search in sources :

Example 11 with TestTableProvider

use of org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider in project beam by apache.

the class BeamSqlCliTest method test_time_types.

@Test
public void test_time_types() throws Exception {
    InMemoryMetaStore metaStore = new InMemoryMetaStore();
    TestTableProvider testTableProvider = new TestTableProvider();
    metaStore.registerProvider(testTableProvider);
    BeamSqlCli cli = new BeamSqlCli().metaStore(metaStore);
    cli.execute("CREATE EXTERNAL TABLE test_table (\n" + "f_date DATE, \n" + "f_time TIME, \n" + "f_ts TIMESTAMP" + ") \n" + "TYPE 'test'");
    cli.execute("INSERT INTO test_table VALUES (" + "DATE '2018-11-01', " + "TIME '15:23:59', " + "TIMESTAMP '2018-07-01 21:26:07.123' )");
    Table table = metaStore.getTables().get("test_table");
    assertNotNull(table);
    TestTableProvider.TableWithRows tableWithRows = testTableProvider.tables().get(table.getName());
    assertEquals(1, tableWithRows.getRows().size());
    Row row = tableWithRows.getRows().get(0);
    assertEquals(3, row.getFieldCount());
    // test DATE field
    assertEquals("2018-11-01", row.getLogicalTypeValue("f_date", LocalDate.class).toString());
    // test TIME field
    assertEquals("15:23:59", row.getLogicalTypeValue("f_time", LocalTime.class).toString());
    // test TIMESTAMP field
    assertEquals(parseTimestampWithUTCTimeZone("2018-07-01 21:26:07.123"), row.getDateTime("f_ts"));
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Table(org.apache.beam.sdk.extensions.sql.meta.Table) Row(org.apache.beam.sdk.values.Row) InMemoryMetaStore(org.apache.beam.sdk.extensions.sql.meta.store.InMemoryMetaStore) Test(org.junit.Test)

Example 12 with TestTableProvider

use of org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider in project beam by apache.

the class JdbcDriverTest method testTimestampWithDefaultTimezone.

@Test
public void testTimestampWithDefaultTimezone() throws Exception {
    TestTableProvider tableProvider = new TestTableProvider();
    Connection connection = JdbcDriver.connect(tableProvider, PipelineOptionsFactory.create());
    // A table with one TIMESTAMP column
    Schema schema = Schema.builder().addDateTimeField("ts").build();
    connection.createStatement().executeUpdate("CREATE EXTERNAL TABLE test (ts TIMESTAMP) TYPE 'test'");
    ReadableInstant july1 = ISODateTimeFormat.dateTimeParser().parseDateTime("2018-07-01T01:02:03Z");
    tableProvider.addRows("test", Row.withSchema(schema).addValue(july1).build());
    ResultSet selectResult = connection.createStatement().executeQuery(String.format("SELECT ts FROM test"));
    selectResult.next();
    Timestamp ts = selectResult.getTimestamp(1);
    assertThat(String.format("Wrote %s to a table, but got back %s", ISODateTimeFormat.basicDateTime().print(july1), ISODateTimeFormat.basicDateTime().print(ts.getTime())), ts.getTime(), equalTo(july1.getMillis()));
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) ReadableInstant(org.joda.time.ReadableInstant) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) Schema(org.apache.beam.sdk.schemas.Schema) Connection(java.sql.Connection) CalciteConnection(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection) ResultSet(java.sql.ResultSet) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 13 with TestTableProvider

use of org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider in project beam by apache.

the class JdbcDriverTest method testTimestampWithNonzeroTimezone.

@Test
@Ignore("https://issues.apache.org/jira/browse/CALCITE-2394")
public void testTimestampWithNonzeroTimezone() throws Exception {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tokyo"), Locale.ROOT);
    TestTableProvider tableProvider = new TestTableProvider();
    Connection connection = JdbcDriver.connect(tableProvider, PipelineOptionsFactory.create());
    // A table with one TIMESTAMP column
    Schema schema = Schema.builder().addDateTimeField("ts").build();
    connection.createStatement().executeUpdate("CREATE EXTERNAL TABLE test (ts TIMESTAMP) TYPE 'test'");
    ReadableInstant july1 = ISODateTimeFormat.dateTimeParser().parseDateTime("2018-07-01T01:02:03Z");
    tableProvider.addRows("test", Row.withSchema(schema).addValue(july1).build());
    ResultSet selectResult = connection.createStatement().executeQuery(String.format("SELECT ts FROM test"));
    selectResult.next();
    Timestamp ts = selectResult.getTimestamp(1, cal);
    assertThat(String.format("Wrote %s to a table, but got back %s", ISODateTimeFormat.basicDateTime().print(july1), ISODateTimeFormat.basicDateTime().print(ts.getTime())), ts.getTime(), equalTo(july1.getMillis()));
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) ReadableInstant(org.joda.time.ReadableInstant) Calendar(java.util.Calendar) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) Schema(org.apache.beam.sdk.schemas.Schema) Connection(java.sql.Connection) CalciteConnection(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection) ResultSet(java.sql.ResultSet) Timestamp(java.sql.Timestamp) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with TestTableProvider

use of org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider in project beam by apache.

the class BeamDDLTest method testParseCreateExternalTable_full.

@Test
public void testParseCreateExternalTable_full() throws Exception {
    TestTableProvider tableProvider = new TestTableProvider();
    BeamSqlEnv env = BeamSqlEnv.withTableProvider(tableProvider);
    JSONObject properties = new JSONObject();
    JSONArray hello = new JSONArray();
    hello.add("james");
    hello.add("bond");
    properties.put("hello", hello);
    env.executeDdl("CREATE EXTERNAL TABLE person (\n" + "id int COMMENT 'id', \n" + "name varchar COMMENT 'name') \n" + "TYPE 'text' \n" + "COMMENT 'person table' \n" + "LOCATION '/home/admin/person'\n" + "TBLPROPERTIES '{\"hello\": [\"james\", \"bond\"]}'");
    assertEquals(mockTable("person", "text", "person table", properties), tableProvider.getTables().get("person"));
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) BeamSqlEnv(org.apache.beam.sdk.extensions.sql.impl.BeamSqlEnv) Test(org.junit.Test)

Example 15 with TestTableProvider

use of org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider in project beam by apache.

the class BeamDDLTest method testParseCreateExternalTable_withDatabase.

@Test
public void testParseCreateExternalTable_withDatabase() throws Exception {
    TestTableProvider rootProvider = new TestTableProvider();
    TestTableProvider testProvider = new TestTableProvider();
    BeamSqlEnv env = BeamSqlEnv.builder(rootProvider).addSchema("test", testProvider).setPipelineOptions(PipelineOptionsFactory.create()).build();
    assertNull(testProvider.getTables().get("person"));
    env.executeDdl("CREATE EXTERNAL TABLE test.person (id INT) TYPE text");
    assertNotNull(testProvider.getTables().get("person"));
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) BeamSqlEnv(org.apache.beam.sdk.extensions.sql.impl.BeamSqlEnv) Test(org.junit.Test)

Aggregations

TestTableProvider (org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider)32 Test (org.junit.Test)25 BeamSqlEnv (org.apache.beam.sdk.extensions.sql.impl.BeamSqlEnv)16 Before (org.junit.Before)8 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)7 Table (org.apache.beam.sdk.extensions.sql.meta.Table)7 Timestamp (java.sql.Timestamp)6 Schema (org.apache.beam.sdk.schemas.Schema)6 Row (org.apache.beam.sdk.values.Row)6 CalciteConnection (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection)6 CalciteSchema (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema)6 ReadableInstant (org.joda.time.ReadableInstant)6 JSONObject (com.alibaba.fastjson.JSONObject)5 Calendar (java.util.Calendar)5 BeamRelNode (org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4