Search in sources :

Example 21 with TestTableProvider

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

the class MongoDbFilterTest method buildUp.

@Before
public void buildUp() {
    TestTableProvider tableProvider = new TestTableProvider();
    Table table = getTable("TEST", PushDownOptions.NONE);
    tableProvider.createTable(table);
    tableProvider.addRows(table.getName(), row(BASIC_SCHEMA, 100, 1, "one", (short) 100, true), row(BASIC_SCHEMA, 200, 2, "two", (short) 200, false));
    sqlEnv = BeamSqlEnv.builder(tableProvider).setPipelineOptions(PipelineOptionsFactory.create()).build();
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Table(org.apache.beam.sdk.extensions.sql.meta.Table) Before(org.junit.Before)

Example 22 with TestTableProvider

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

the class BeamSqlEnvTest method testPlannerClassNotFound.

@Test
public void testPlannerClassNotFound() {
    exceptions.expect(RuntimeException.class);
    exceptions.expectCause(hasMessage(containsString("org.test.ClassNotFound")));
    TestTableProvider root = new TestTableProvider();
    BeamSqlEnv.builder(root).setQueryPlannerClassName("org.test.ClassNotFound").setPipelineOptions(PipelineOptionsFactory.create()).build();
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Test(org.junit.Test)

Example 23 with TestTableProvider

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

the class BeamSqlEnvTest method testCreateExternalTableInNestedTableProvider.

@Test
public void testCreateExternalTableInNestedTableProvider() throws Exception {
    TestTableProvider root = new TestTableProvider();
    TestTableProvider nested = new TestTableProvider();
    TestTableProvider anotherOne = new TestTableProvider();
    BeamSqlEnv env = BeamSqlEnv.builder(root).addSchema("nested", nested).addSchema("anotherOne", anotherOne).setPipelineOptions(PipelineOptionsFactory.create()).build();
    Connection connection = env.connection;
    connection.createStatement().execute("CREATE EXTERNAL TABLE nested.person (id INT) TYPE test");
    connection.createStatement().execute("INSERT INTO nested.person(id) VALUES (1), (2), (6)");
    ResultSet rs = connection.createStatement().executeQuery("SELECT SUM(id) FROM nested.person");
    rs.next();
    assertEquals(9, rs.getInt(1));
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 24 with TestTableProvider

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

the class JdbcDriverTest method testSelectsFromExistingTable.

@Test
public void testSelectsFromExistingTable() throws Exception {
    TestTableProvider tableProvider = new TestTableProvider();
    Connection connection = JdbcDriver.connect(tableProvider, PipelineOptionsFactory.create());
    connection.createStatement().executeUpdate("CREATE EXTERNAL TABLE person (id BIGINT, name VARCHAR) TYPE 'test'");
    tableProvider.addRows("person", row(1L, "aaa"), row(2L, "bbb"));
    ResultSet selectResult = connection.createStatement().executeQuery("SELECT id, name FROM person");
    List<Row> resultRows = readResultSet(selectResult).stream().map(values -> values.stream().collect(toRow(BASIC_SCHEMA))).collect(Collectors.toList());
    assertThat(resultRows, containsInAnyOrder(row(1L, "aaa"), row(2L, "bbb")));
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Connection(java.sql.Connection) Duration(org.joda.time.Duration) DatabaseMetaData(java.sql.DatabaseMetaData) TestBoundedTable(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestBoundedTable) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) ResultSet(java.sql.ResultSet) Locale(java.util.Locale) ReleaseInfo(org.apache.beam.sdk.util.ReleaseInfo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Row(org.apache.beam.sdk.values.Row) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) ISODateTimeFormat(org.joda.time.format.ISODateTimeFormat) ReadOnlyTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.ReadOnlyTableProvider) ImmutableMap(org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableMap) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) ReadableInstant(org.joda.time.ReadableInstant) Test(org.junit.Test) TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) Row.toRow(org.apache.beam.sdk.values.Row.toRow) List(java.util.List) TestUnboundedTable(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestUnboundedTable) Rule(org.junit.Rule) Ignore(org.junit.Ignore) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) Driver(java.sql.Driver) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Statement(java.sql.Statement) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) Matchers.containsString(org.hamcrest.Matchers.containsString) CalciteConnection(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection) Assert.assertEquals(org.junit.Assert.assertEquals) DriverManager(java.sql.DriverManager) Connection(java.sql.Connection) CalciteConnection(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection) ResultSet(java.sql.ResultSet) Row(org.apache.beam.sdk.values.Row) Row.toRow(org.apache.beam.sdk.values.Row.toRow) Test(org.junit.Test)

Example 25 with TestTableProvider

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

the class JdbcDriverTest method testTimestampWithZeroTimezone.

@Test
public void testTimestampWithZeroTimezone() throws Exception {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"), 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) 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