Search in sources :

Example 6 with TableFunction

use of org.apache.calcite.schema.TableFunction 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 7 with TableFunction

use of org.apache.calcite.schema.TableFunction 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 8 with TableFunction

use of org.apache.calcite.schema.TableFunction in project calcite by apache.

the class LookupOperatorOverloadsTest method test.

@Test
public void test() throws SQLException {
    final String schemaName = "MySchema";
    final String funcName = "MyFUNC";
    final String anotherName = "AnotherFunc";
    try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        SchemaPlus schema = rootSchema.add(schemaName, new AbstractSchema());
        final TableFunction table = TableFunctionImpl.create(Smalls.MAZE_METHOD);
        schema.add(funcName, table);
        schema.add(anotherName, table);
        final TableFunction table2 = TableFunctionImpl.create(Smalls.MAZE3_METHOD);
        schema.add(funcName, table2);
        final CalciteServerStatement statement = connection.createStatement().unwrap(CalciteServerStatement.class);
        final CalcitePrepare.Context prepareContext = statement.createPrepareContext();
        final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
        CalciteCatalogReader reader = new CalciteCatalogReader(prepareContext.getRootSchema(), ImmutableList.<String>of(), typeFactory, prepareContext.config());
        final List<SqlOperator> operatorList = new ArrayList<>();
        SqlIdentifier myFuncIdentifier = new SqlIdentifier(Lists.newArrayList(schemaName, funcName), null, SqlParserPos.ZERO, null);
        reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(2, funcName, operatorList);
        operatorList.clear();
        reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(0, null, operatorList);
        operatorList.clear();
        SqlIdentifier anotherFuncIdentifier = new SqlIdentifier(Lists.newArrayList(schemaName, anotherName), null, SqlParserPos.ZERO, null);
        reader.lookupOperatorOverloads(anotherFuncIdentifier, SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(1, anotherName, operatorList);
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 9 with TableFunction

use of org.apache.calcite.schema.TableFunction in project calcite by apache.

the class ExampleFunctionTest method checkMazeTableFunction.

public void checkMazeTableFunction(Boolean solution, String maze) 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(MAZE_METHOD);
    schema.add("Maze", table);
    final TableFunction table2 = TableFunctionImpl.create(SOLVE_METHOD);
    schema.add("Solve", table2);
    final String sql;
    if (solution) {
        sql = "select *\n" + "from table(\"s\".\"Solve\"(5, 3, 1)) as t(s)";
    } else {
        sql = "select *\n" + "from table(\"s\".\"Maze\"(5, 3, 1)) as t(s)";
    }
    ResultSet resultSet = connection.createStatement().executeQuery(sql);
    final StringBuilder b = new StringBuilder();
    while (resultSet.next()) {
        b.append(resultSet.getString(1)).append("\n");
    }
    assertThat(b.toString(), is(maze));
}
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) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 10 with TableFunction

use of org.apache.calcite.schema.TableFunction in project calcite by apache.

the class TableFunctionTest method testScannableTableFunctionWithNamedParameters.

/**
 * As {@link #testScannableTableFunction()} but with named parameters.
 */
@Test
public void testScannableTableFunctionWithNamedParameters() 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.MAZE2_METHOD);
    schema.add("Maze", table);
    final String sql = "select *\n" + "from table(\"s\".\"Maze\"(5, 3, 1))";
    final Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(sql);
    final String result = "S=abcde\n" + "S=xyz\n";
    assertThat(CalciteAssert.toString(resultSet), is(result + "S=generate2(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"));
    connection.close();
}
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)

Aggregations

TableFunction (org.apache.calcite.schema.TableFunction)12 Connection (java.sql.Connection)10 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)10 SchemaPlus (org.apache.calcite.schema.SchemaPlus)10 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)9 ResultSet (java.sql.ResultSet)8 Test (org.junit.Test)7 PreparedStatement (java.sql.PreparedStatement)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 AggregateFunction (org.apache.calcite.schema.AggregateFunction)2 ScalarFunction (org.apache.calcite.schema.ScalarFunction)2 TableMacro (org.apache.calcite.schema.TableMacro)2 SqlUserDefinedTableFunction (org.apache.calcite.sql.validate.SqlUserDefinedTableFunction)2 Ignore (org.junit.Ignore)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1