Search in sources :

Example 46 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project beam by apache.

the class JdbcDriverTest method testInternalConnect_setBogusRunner.

@Test
public void testInternalConnect_setBogusRunner() throws Exception {
    thrown.expectMessage("Unknown 'runner' specified 'bogus'");
    CalciteConnection connection = JdbcDriver.connect(BOUNDED_TABLE, PipelineOptionsFactory.create());
    Statement statement = connection.createStatement();
    assertEquals(0, statement.executeUpdate("SET runner = bogus"));
    assertTrue(statement.execute("SELECT * FROM test"));
}
Also used : Statement(java.sql.Statement) CalciteConnection(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 47 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project beam by apache.

the class JdbcDriverTest method testInternalConnect_resetAll.

@Test
public void testInternalConnect_resetAll() throws Exception {
    CalciteConnection connection = JdbcDriver.connect(BOUNDED_TABLE, PipelineOptionsFactory.create());
    Statement statement = connection.createStatement();
    assertEquals(0, statement.executeUpdate("SET runner = bogus"));
    assertEquals(0, statement.executeUpdate("RESET ALL"));
    assertTrue(statement.execute("SELECT * FROM test"));
}
Also used : Statement(java.sql.Statement) CalciteConnection(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 48 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project samza by apache.

the class QueryPlanner method getPlanner.

private Planner getPlanner() {
    Planner planner = null;
    try {
        Connection connection = DriverManager.getConnection("jdbc:calcite:");
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        registerSourceSchemas(rootSchema);
        List<SamzaSqlScalarFunctionImpl> samzaSqlFunctions = udfMetadata.stream().map(SamzaSqlScalarFunctionImpl::new).collect(Collectors.toList());
        final List<RelTraitDef> traitDefs = new ArrayList<>();
        traitDefs.add(ConventionTraitDef.INSTANCE);
        traitDefs.add(RelCollationTraitDef.INSTANCE);
        List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>();
        sqlOperatorTables.add(new SamzaSqlOperatorTable());
        sqlOperatorTables.add(new SamzaSqlUdfOperatorTable(samzaSqlFunctions));
        // TODO: Introduce a pluggable rule factory.
        List<RelOptRule> rules = ImmutableList.of(FilterProjectTransposeRule.INSTANCE, ProjectMergeRule.INSTANCE, new SamzaSqlFilterRemoteJoinRule.SamzaSqlFilterIntoRemoteJoinRule(true, RelFactories.LOGICAL_BUILDER, systemStreamConfigBySource));
        // Using lenient so that !=,%,- are allowed.
        FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.JAVA).setConformance(SqlConformanceEnum.LENIENT).setCaseSensitive(// Make Udfs case insensitive
        false).build()).defaultSchema(rootSchema).operatorTable(new ChainedSqlOperatorTable(sqlOperatorTables)).sqlToRelConverterConfig(SqlToRelConverter.Config.DEFAULT).traitDefs(traitDefs).programs(Programs.hep(rules, true, DefaultRelMetadataProvider.INSTANCE)).build();
        planner = Frameworks.getPlanner(frameworkConfig);
        return planner;
    } catch (Exception e) {
        String errorMsg = "Failed to create planner.";
        LOG.error(errorMsg, e);
        if (planner != null) {
            planner.close();
        }
        throw new SamzaException(errorMsg, e);
    }
}
Also used : Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException) RelOptRule(org.apache.calcite.plan.RelOptRule) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) RelTraitDef(org.apache.calcite.plan.RelTraitDef) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 49 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class CloneSchema method createCloneTable.

private Table createCloneTable(QueryProvider queryProvider, QueryableTable sourceTable, String name) {
    final Queryable<Object> queryable = sourceTable.asQueryable(queryProvider, sourceSchema, name);
    final JavaTypeFactory typeFactory = ((CalciteConnection) queryProvider).getTypeFactory();
    return createCloneTable(typeFactory, Schemas.proto(sourceTable), ImmutableList.<RelCollation>of(), null, queryable);
}
Also used : JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 50 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class ReflectiveSchemaTest method testView.

/**
 * Tests a view.
 */
@Test
public void testView() throws SQLException, ClassNotFoundException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    schema.add("emps_view", ViewTable.viewMacro(schema, "select * from \"hr\".\"emps\" where \"deptno\" = 10", null, Arrays.asList("s", "emps_view"), null));
    rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
    ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from \"s\".\"emps_view\"\n" + "where \"empid\" < 120");
    assertEquals("empid=100; deptno=10; name=Bill; salary=10000.0; commission=1000\n" + "empid=110; deptno=10; name=Theodore; salary=11500.0; commission=250\n", CalciteAssert.toString(resultSet));
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Aggregations

CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)65 Test (org.junit.Test)52 Connection (java.sql.Connection)51 ResultSet (java.sql.ResultSet)42 SchemaPlus (org.apache.calcite.schema.SchemaPlus)42 Statement (java.sql.Statement)32 PreparedStatement (java.sql.PreparedStatement)24 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)22 Properties (java.util.Properties)17 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)17 SQLException (java.sql.SQLException)16 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)12 CalciteConnection (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 TableFunction (org.apache.calcite.schema.TableFunction)10 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)9 org.hsqldb.jdbcDriver (org.hsqldb.jdbcDriver)5 AssertThat (org.apache.calcite.test.CalciteAssert.AssertThat)4 IOException (java.io.IOException)3 ResultSetMetaData (java.sql.ResultSetMetaData)3