Search in sources :

Example 26 with TestTableProvider

use of org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider 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)

Example 27 with TestTableProvider

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

the class ThreeTablesSchema method testSystemNotReorderingWithoutRules.

@Test
public void testSystemNotReorderingWithoutRules() {
    TestTableProvider tableProvider = new TestTableProvider();
    createThreeTables(tableProvider);
    List<RelOptRule> ruleSet = BeamRuleSets.getRuleSets().stream().flatMap(rules -> StreamSupport.stream(rules.spliterator(), false)).filter(rule -> !(rule instanceof BeamJoinPushThroughJoinRule)).filter(rule -> !(rule instanceof BeamJoinAssociateRule)).filter(rule -> !(rule instanceof JoinCommuteRule)).collect(Collectors.toList());
    BeamSqlEnv env = BeamSqlEnv.builder(tableProvider).setPipelineOptions(PipelineOptionsFactory.create()).setRuleSets(ImmutableList.of(RuleSets.ofList(ruleSet))).build();
    // This is Join(Join(medium, large), small) which should be converted to a join that large table
    // is on the top.
    BeamRelNode parsedQuery = env.parseQuery("select * from medium_table " + " JOIN large_table on large_table.medium_key = medium_table.large_key " + " JOIN small_table on medium_table.small_key = small_table.medium_key ");
    assertTopTableInJoins(parsedQuery, "small_table");
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Linq4j(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Linq4j) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) Arrays(java.util.Arrays) AbstractTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.impl.AbstractTable) Enumerable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Enumerable) BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) BeamRuleSets(org.apache.beam.sdk.extensions.sql.impl.planner.BeamRuleSets) Frameworks(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Frameworks) RelFieldCollation(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation) ImmutableBitSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet) Planner(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner) Map(java.util.Map) EnumerableConvention(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.adapter.enumerable.EnumerableConvention) JoinCommuteRule(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.rules.JoinCommuteRule) RelRoot(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot) ImmutableMap(org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableMap) TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Collectors(java.util.stream.Collectors) Table(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table) TableScan(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.TableScan) Statistic(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Statistic) List(java.util.List) ImmutableList(org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList) RelCollationTraitDef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollationTraitDef) AbstractSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.impl.AbstractSchema) RuleSets(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSets) RuleSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet) Programs(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Programs) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) FrameworkConfig(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig) RelCollations(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollations) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Statistics(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Statistics) RelOptRule(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptRule) Join(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Join) ScannableTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.ScannableTable) StreamSupport(java.util.stream.StreamSupport) BeamSqlEnv(org.apache.beam.sdk.extensions.sql.impl.BeamSqlEnv) Row(org.apache.beam.sdk.values.Row) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) SqlParser(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser) EnumerableRules(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.adapter.enumerable.EnumerableRules) Test(org.junit.Test) ConventionTraitDef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.ConventionTraitDef) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet) DataContext(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.DataContext) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) Assert(org.junit.Assert) CoreRules(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.rules.CoreRules) BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) BeamSqlEnv(org.apache.beam.sdk.extensions.sql.impl.BeamSqlEnv) JoinCommuteRule(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.rules.JoinCommuteRule) RelOptRule(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptRule) Test(org.junit.Test)

Example 28 with TestTableProvider

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

the class ThreeTablesSchema method testSystemReorderingMediumLargeSmall.

@Test
public void testSystemReorderingMediumLargeSmall() {
    TestTableProvider tableProvider = new TestTableProvider();
    createThreeTables(tableProvider);
    BeamSqlEnv env = BeamSqlEnv.withTableProvider(tableProvider);
    // This is Join(Join(medium, large), small) which should be converted to a join that large table
    // is on the top.
    BeamRelNode parsedQuery = env.parseQuery("select * from medium_table " + " JOIN large_table on large_table.medium_key = medium_table.large_key " + " JOIN small_table on medium_table.small_key = small_table.medium_key ");
    assertTopTableInJoins(parsedQuery, "large_table");
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) BeamSqlEnv(org.apache.beam.sdk.extensions.sql.impl.BeamSqlEnv) Test(org.junit.Test)

Example 29 with TestTableProvider

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

the class ThreeTablesSchema method testTableSizes.

@Test
public void testTableSizes() {
    TestTableProvider tableProvider = new TestTableProvider();
    createThreeTables(tableProvider);
    Assert.assertEquals(1d, tableProvider.buildBeamSqlTable(tableProvider.getTable("small_table")).getTableStatistics(null).getRowCount(), 0.01);
    Assert.assertEquals(3d, tableProvider.buildBeamSqlTable(tableProvider.getTable("medium_table")).getTableStatistics(null).getRowCount(), 0.01);
    Assert.assertEquals(100d, tableProvider.buildBeamSqlTable(tableProvider.getTable("large_table")).getTableStatistics(null).getRowCount(), 0.01);
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) Test(org.junit.Test)

Example 30 with TestTableProvider

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

the class ThreeTablesSchema method testSystemNotReorderingSmallMediumLarge.

@Test
public void testSystemNotReorderingSmallMediumLarge() {
    TestTableProvider tableProvider = new TestTableProvider();
    createThreeTables(tableProvider);
    BeamSqlEnv env = BeamSqlEnv.withTableProvider(tableProvider);
    // This is a correct ordered join because large table is on the top. It should not change that.
    BeamRelNode parsedQuery = env.parseQuery("select * from small_table " + " JOIN medium_table on medium_table.small_key = small_table.medium_key " + " JOIN large_table on large_table.medium_key = medium_table.large_key ");
    assertTopTableInJoins(parsedQuery, "large_table");
}
Also used : TestTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider) BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) 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