Search in sources :

Example 31 with AbstractSchema

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

Example 32 with AbstractSchema

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

the class UdfTest method testArrayUserDefinedFunction.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1834">[CALCITE-1834]
 * User-defined function for Arrays</a>.
 */
@Test
public void testArrayUserDefinedFunction() throws Exception {
    try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
        SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
        post.add("ARRAY_APPEND", new ArrayAppendDoubleFunction());
        post.add("ARRAY_APPEND", new ArrayAppendIntegerFunction());
        final String sql = "select \"empid\" as EMPLOYEE_ID,\n" + "  \"name\" || ' ' || \"name\" as EMPLOYEE_NAME,\n" + "  \"salary\" as EMPLOYEE_SALARY,\n" + "  POST.ARRAY_APPEND(ARRAY[1,2,3], \"deptno\") as DEPARTMENTS\n" + "from \"hr\".\"emps\"";
        final String result = "" + "EMPLOYEE_ID=100; EMPLOYEE_NAME=Bill Bill;" + " EMPLOYEE_SALARY=10000.0; DEPARTMENTS=[1, 2, 3, 10]\n" + "EMPLOYEE_ID=200; EMPLOYEE_NAME=Eric Eric;" + " EMPLOYEE_SALARY=8000.0; DEPARTMENTS=[1, 2, 3, 20]\n" + "EMPLOYEE_ID=150; EMPLOYEE_NAME=Sebastian Sebastian;" + " EMPLOYEE_SALARY=7000.0; DEPARTMENTS=[1, 2, 3, 10]\n" + "EMPLOYEE_ID=110; EMPLOYEE_NAME=Theodore Theodore;" + " EMPLOYEE_SALARY=11500.0; DEPARTMENTS=[1, 2, 3, 10]\n";
        try (Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql)) {
            assertThat(CalciteAssert.toString(resultSet), is(result));
        }
        connection.close();
    }
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Statement(java.sql.Statement) 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)

Example 33 with AbstractSchema

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

the class UdfTest method testUserDefinedFunctionInView.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-937">[CALCITE-937]
 * User-defined function within view</a>.
 */
@Test
public void testUserDefinedFunctionInView() throws Exception {
    Class.forName("org.apache.calcite.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
    SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
    post.add("MY_INCREMENT", ScalarFunctionImpl.create(Smalls.MyIncrement.class, "eval"));
    final String viewSql = "select \"empid\" as EMPLOYEE_ID,\n" + "  \"name\" || ' ' || \"name\" as EMPLOYEE_NAME,\n" + "  \"salary\" as EMPLOYEE_SALARY,\n" + "  POST.MY_INCREMENT(\"empid\", 10) as INCREMENTED_SALARY\n" + "from \"hr\".\"emps\"";
    post.add("V_EMP", ViewTable.viewMacro(post, viewSql, ImmutableList.<String>of(), ImmutableList.of("POST", "V_EMP"), null));
    final String result = "" + "EMPLOYEE_ID=100; EMPLOYEE_NAME=Bill Bill; EMPLOYEE_SALARY=10000.0; INCREMENTED_SALARY=110.0\n" + "EMPLOYEE_ID=200; EMPLOYEE_NAME=Eric Eric; EMPLOYEE_SALARY=8000.0; INCREMENTED_SALARY=220.0\n" + "EMPLOYEE_ID=150; EMPLOYEE_NAME=Sebastian Sebastian; EMPLOYEE_SALARY=7000.0; INCREMENTED_SALARY=165.0\n" + "EMPLOYEE_ID=110; EMPLOYEE_NAME=Theodore Theodore; EMPLOYEE_SALARY=11500.0; INCREMENTED_SALARY=121.0\n";
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(viewSql);
    assertThat(CalciteAssert.toString(resultSet), is(result));
    resultSet.close();
    ResultSet viewResultSet = statement.executeQuery("select * from \"POST\".\"V_EMP\"");
    assertThat(CalciteAssert.toString(viewResultSet), is(result));
    statement.close();
    connection.close();
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Statement(java.sql.Statement) 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

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