Search in sources :

Example 86 with Table

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table in project beam by apache.

the class TableResolutionTest method testResolveWithDots.

/**
 * Unit test for resolving a table with no hierarchy but dots in its actual name.
 */
@Test
public void testResolveWithDots() {
    String tableName = "fake.table";
    when(mockSchemaPlus.getTable(tableName)).thenReturn(mockTable);
    Table table = TableResolution.resolveCalciteTable(mockSchemaPlus, ImmutableList.of(tableName));
    assertThat(table, Matchers.is(mockTable));
}
Also used : Table(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table) Test(org.junit.Test)

Example 87 with Table

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table in project beam by apache.

the class TableScanConverter method convert.

@Override
public RelNode convert(ResolvedTableScan zetaNode, List<RelNode> inputs) {
    List<String> tablePath = getTablePath(zetaNode.getTable());
    SchemaPlus defaultSchemaPlus = getConfig().getDefaultSchema();
    if (defaultSchemaPlus == null) {
        throw new AssertionError("Default schema is null.");
    }
    // TODO: reject incorrect top-level schema
    Table calciteTable = TableResolution.resolveCalciteTable(defaultSchemaPlus, tablePath);
    // we already resolved the table before passing the query to Analyzer, so it should be there
    checkNotNull(calciteTable, "Unable to resolve the table path %s in schema %s", tablePath, defaultSchemaPlus.getName());
    String defaultSchemaName = defaultSchemaPlus.getName();
    final CalciteCatalogReader catalogReader = new CalciteCatalogReader(CalciteSchema.from(defaultSchemaPlus), ImmutableList.of(defaultSchemaName), getCluster().getTypeFactory(), new CalciteConnectionConfigImpl(new Properties()));
    RelOptTableImpl relOptTable = RelOptTableImpl.create(catalogReader, calciteTable.getRowType(getCluster().getTypeFactory()), calciteTable, ImmutableList.<String>builder().add(defaultSchemaName).addAll(tablePath).build());
    if (calciteTable instanceof TranslatableTable) {
        return ((TranslatableTable) calciteTable).toRel(createToRelContext(), relOptTable);
    } else {
        throw new UnsupportedOperationException("Does not support non TranslatableTable type table!");
    }
}
Also used : CalciteConnectionConfigImpl(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfigImpl) RelOptTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptTable) Table(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table) TranslatableTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.TranslatableTable) CalciteCatalogReader(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.CalciteCatalogReader) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) RelOptTableImpl(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.RelOptTableImpl) TranslatableTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.TranslatableTable) Properties(java.util.Properties)

Example 88 with Table

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table 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 89 with Table

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table 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)

Example 90 with Table

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table in project beam by apache.

the class JdbcDriverTest method testInsertIntoCreatedTable.

@Test
public void testInsertIntoCreatedTable() 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'");
    connection.createStatement().executeUpdate("CREATE EXTERNAL TABLE person_src (id BIGINT, name VARCHAR) TYPE 'test'");
    tableProvider.addRows("person_src", row(1L, "aaa"), row(2L, "bbb"));
    connection.createStatement().execute("INSERT INTO person SELECT id, name FROM person_src");
    ResultSet selectResult = connection.createStatement().executeQuery("SELECT id, name FROM person");
    List<Row> resultRows = readResultSet(selectResult).stream().map(resultValues -> resultValues.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)

Aggregations

Table (org.apache.calcite.schema.Table)104 Test (org.junit.Test)43 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)38 RelOptTable (org.apache.calcite.plan.RelOptTable)33 RelDataType (org.apache.calcite.rel.type.RelDataType)22 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)22 SchemaPlus (org.apache.calcite.schema.SchemaPlus)20 ArrayList (java.util.ArrayList)19 List (java.util.List)19 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)17 ScannableTable (org.apache.calcite.schema.ScannableTable)17 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)16 FilterableTable (org.apache.calcite.schema.FilterableTable)15 AbstractTable (org.apache.calcite.schema.impl.AbstractTable)15 ResultSet (java.sql.ResultSet)14 Schema (org.apache.beam.sdk.schemas.Schema)14 Row (org.apache.beam.sdk.values.Row)14 StreamableTable (org.apache.calcite.schema.StreamableTable)14 CalciteConnection (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection)13 Map (java.util.Map)12