Search in sources :

Example 11 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class TableFunctionTest method getConnectionWithMultiplyFunction.

private Connection getConnectionWithMultiplyFunction() throws ClassNotFoundException, SQLException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableFunction table = TableFunctionImpl.create(Smalls.MULTIPLICATION_TABLE_METHOD);
    schema.add("multiplication", table);
    return connection;
}
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) TableFunction(org.apache.calcite.schema.TableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 12 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class TableFunctionTest method testScannableTableFunction.

/**
 * Tests a table function that implements {@link ScannableTable} and returns
 * a single column.
 */
@Test
public void testScannableTableFunction() 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());
    final TableFunction table = TableFunctionImpl.create(Smalls.MAZE_METHOD);
    schema.add("Maze", table);
    final String sql = "select *\n" + "from table(\"s\".\"Maze\"(5, 3, 1))";
    ResultSet resultSet = connection.createStatement().executeQuery(sql);
    final String result = "S=abcde\n" + "S=xyz\n" + "S=generate(w=5, h=3, s=1)\n";
    assertThat(CalciteAssert.toString(resultSet), is(result));
}
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) TableFunction(org.apache.calcite.schema.TableFunction) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 13 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class TableFunctionTest method testMultipleScannableTableFunctionWithNamedParameters.

/**
 * As {@link #testScannableTableFunction()} but with named parameters.
 */
@Test
public void testMultipleScannableTableFunctionWithNamedParameters() throws SQLException, ClassNotFoundException {
    try (Connection connection = DriverManager.getConnection("jdbc:calcite:");
        Statement statement = connection.createStatement()) {
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
        final TableFunction table1 = TableFunctionImpl.create(Smalls.MAZE_METHOD);
        schema.add("Maze", table1);
        final TableFunction table2 = TableFunctionImpl.create(Smalls.MAZE2_METHOD);
        schema.add("Maze", table2);
        final TableFunction table3 = TableFunctionImpl.create(Smalls.MAZE3_METHOD);
        schema.add("Maze", table3);
        final String sql = "select *\n" + "from table(\"s\".\"Maze\"(5, 3, 1))";
        ResultSet resultSet = statement.executeQuery(sql);
        final String result = "S=abcde\n" + "S=xyz\n";
        assertThat(CalciteAssert.toString(resultSet), is(result + "S=generate(w=5, h=3, s=1)\n"));
        final String sql2 = "select *\n" + "from table(\"s\".\"Maze\"(WIDTH => 5, HEIGHT => 3, SEED => 1))";
        resultSet = statement.executeQuery(sql2);
        assertThat(CalciteAssert.toString(resultSet), is(result + "S=generate2(w=5, h=3, s=1)\n"));
        final String sql3 = "select *\n" + "from table(\"s\".\"Maze\"(HEIGHT => 3, WIDTH => 5))";
        resultSet = statement.executeQuery(sql3);
        assertThat(CalciteAssert.toString(resultSet), is(result + "S=generate2(w=5, h=3, s=null)\n"));
        final String sql4 = "select *\n" + "from table(\"s\".\"Maze\"(FOO => 'a'))";
        resultSet = statement.executeQuery(sql4);
        assertThat(CalciteAssert.toString(resultSet), is(result + "S=generate3(foo=a)\n"));
    }
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) TableFunction(org.apache.calcite.schema.TableFunction) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 14 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class TableFunctionTest method testTableFunctionCursorInputs.

/**
 * Tests a table function that takes cursor input.
 */
@Ignore("CannotPlanException: Node [rel#18:Subset#4.ENUMERABLE.[]] " + "could not be implemented")
@Test
public void testTableFunctionCursorInputs() throws SQLException, ClassNotFoundException {
    try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
        final TableFunction table = TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD);
        schema.add("GenerateStrings", table);
        final TableFunction add = TableFunctionImpl.create(Smalls.PROCESS_CURSOR_METHOD);
        schema.add("process", add);
        final PreparedStatement ps = connection.prepareStatement("select *\n" + "from table(\"s\".\"process\"(2,\n" + "cursor(select * from table(\"s\".\"GenerateStrings\"(?)))\n" + ")) as t(u)\n" + "where u > 3");
        ps.setInt(1, 5);
        ResultSet resultSet = ps.executeQuery();
        // GenerateStrings returns 0..4, then 2 is added (process function),
        // thus 2..6, finally where u > 3 leaves just 4..6
        assertThat(CalciteAssert.toString(resultSet), equalTo("u=4\n" + "u=5\n" + "u=6\n"));
    }
}
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) TableFunction(org.apache.calcite.schema.TableFunction) PreparedStatement(java.sql.PreparedStatement) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class FrameworksTest method testUpdate.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2039">[CALCITE-2039]
 * AssertionError when pushing project to ProjectableFilterableTable</a>
 * using UPDATE via {@link Frameworks}.
 */
@Test
public void testUpdate() throws Exception {
    Table table = new TableImpl();
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    SchemaPlus schema = rootSchema.add("x", new AbstractSchema());
    schema.add("MYTABLE", table);
    List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelDistributionTraitDef.INSTANCE);
    SqlParser.Config parserConfig = SqlParser.configBuilder(SqlParser.Config.DEFAULT).setCaseSensitive(false).build();
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(parserConfig).defaultSchema(schema).traitDefs(traitDefs).ruleSets(RuleSets.ofList(AbstractConverter.ExpandConversionRule.INSTANCE)).programs(Programs.ofRules(Programs.RULE_SET)).build();
    executeQuery(config, " UPDATE MYTABLE set id=7 where id=1", CalcitePrepareImpl.DEBUG);
}
Also used : RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) Table(org.apache.calcite.schema.Table) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) RelTraitDef(org.apache.calcite.plan.RelTraitDef) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SqlParser(org.apache.calcite.sql.parser.SqlParser) Test(org.junit.Test)

Aggregations

AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)33 SchemaPlus (org.apache.calcite.schema.SchemaPlus)30 Connection (java.sql.Connection)22 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)22 Test (org.junit.Test)20 ResultSet (java.sql.ResultSet)15 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)10 TableFunction (org.apache.calcite.schema.TableFunction)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Schema (org.apache.calcite.schema.Schema)6 PreparedStatement (java.sql.PreparedStatement)5 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)5 Statement (java.sql.Statement)4 CloneSchema (org.apache.calcite.adapter.clone.CloneSchema)4 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)3 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)3 Table (org.apache.calcite.schema.Table)3