Search in sources :

Example 71 with CalciteConnection

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

the class TableFunctionTest method testTableFunctionCursorsInputs.

/**
 * Tests a table function that takes multiple cursor inputs.
 */
@Ignore("CannotPlanException: Node [rel#24:Subset#6.ENUMERABLE.[]] " + "could not be implemented")
@Test
public void testTableFunctionCursorsInputs() throws SQLException, ClassNotFoundException {
    try (Connection connection = getConnectionWithMultiplyFunction()) {
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        SchemaPlus schema = rootSchema.getSubSchema("s");
        final TableFunction table = TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD);
        schema.add("GenerateStrings", table);
        final TableFunction add = TableFunctionImpl.create(Smalls.PROCESS_CURSORS_METHOD);
        schema.add("process", add);
        final PreparedStatement ps = connection.prepareStatement("select *\n" + "from table(\"s\".\"process\"(2,\n" + "cursor(select * from table(\"s\".\"multiplication\"(5,5,0))),\n" + "cursor(select * from table(\"s\".\"GenerateStrings\"(?)))\n" + ")) as t(u)\n" + "where u > 3");
        ps.setInt(1, 5);
        ResultSet resultSet = ps.executeQuery();
        // GenerateStrings produce 0..4
        // multiplication produce 1..5
        // process sums and adds 2
        // sum is 2 + 1..9 == 3..9
        assertThat(CalciteAssert.toString(resultSet), equalTo("u=4\n" + "u=5\n" + "u=6\n" + "u=7\n" + "u=8\n" + "u=9\n"));
    }
}
Also used : 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 72 with CalciteConnection

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

the class TableInRootSchemaTest method testAddingTableInRootSchema.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-85">[CALCITE-85]
 * Adding a table to the root schema causes breakage in
 * CalcitePrepareImpl</a>.
 */
@Test
public void testAddingTableInRootSchema() throws Exception {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    calciteConnection.getRootSchema().add("SAMPLE", new SimpleTable());
    Statement statement = calciteConnection.createStatement();
    ResultSet resultSet = statement.executeQuery("select A, SUM(B) from SAMPLE group by A");
    assertThat(ImmutableMultiset.of("A=foo; EXPR$1=8", "A=bar; EXPR$1=4"), equalTo(CalciteAssert.toSet(resultSet)));
    final ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    assertThat(resultSetMetaData.getColumnName(1), equalTo("A"));
    assertThat(resultSetMetaData.getTableName(1), equalTo("SAMPLE"));
    assertThat(resultSetMetaData.getSchemaName(1), nullValue());
    assertThat(resultSetMetaData.getColumnClassName(1), equalTo("java.lang.String"));
    // Per JDBC, column name should be null. But DBUnit requires every column
    // to have a name, so the driver uses the label.
    assertThat(resultSetMetaData.getColumnName(2), equalTo("EXPR$1"));
    assertThat(resultSetMetaData.getTableName(2), nullValue());
    assertThat(resultSetMetaData.getSchemaName(2), nullValue());
    assertThat(resultSetMetaData.getColumnClassName(2), equalTo("java.lang.Integer"));
    resultSet.close();
    statement.close();
    connection.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 73 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection 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 74 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection 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)

Example 75 with CalciteConnection

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

the class MaterializationService method defineMaterialization.

/**
 * Defines a new materialization. Returns its key.
 */
public MaterializationKey defineMaterialization(final CalciteSchema schema, TileKey tileKey, String viewSql, List<String> viewSchemaPath, String suggestedTableName, TableFactory tableFactory, boolean create, boolean existing) {
    final MaterializationActor.QueryKey queryKey = new MaterializationActor.QueryKey(viewSql, schema, viewSchemaPath);
    final MaterializationKey existingKey = actor.keyBySql.get(queryKey);
    if (existingKey != null) {
        return existingKey;
    }
    if (!create) {
        return null;
    }
    final CalciteConnection connection = CalciteMetaImpl.connect(schema.root(), null);
    CalciteSchema.TableEntry tableEntry;
    // with the name and if none can be found, lookup a view in the schema
    if (existing) {
        tableEntry = schema.getTable(suggestedTableName, true);
        if (tableEntry == null) {
            tableEntry = schema.getTableBasedOnNullaryFunction(suggestedTableName, true);
        }
    } else {
        tableEntry = null;
    }
    if (tableEntry == null) {
        tableEntry = schema.getTableBySql(viewSql);
    }
    RelDataType rowType = null;
    if (tableEntry == null) {
        Table table = tableFactory.createTable(schema, viewSql, viewSchemaPath);
        final String tableName = Schemas.uniqueTableName(schema, Util.first(suggestedTableName, "m"));
        tableEntry = schema.add(tableName, table, ImmutableList.of(viewSql));
        Hook.CREATE_MATERIALIZATION.run(tableName);
        rowType = table.getRowType(connection.getTypeFactory());
    }
    if (rowType == null) {
        // If we didn't validate the SQL by populating a table, validate it now.
        final CalcitePrepare.ParseResult parse = Schemas.parse(connection, schema, viewSchemaPath, viewSql);
        rowType = parse.rowType;
    }
    final MaterializationKey key = new MaterializationKey();
    final MaterializationActor.Materialization materialization = new MaterializationActor.Materialization(key, schema.root(), tableEntry, viewSql, rowType, viewSchemaPath);
    actor.keyMap.put(materialization.key, materialization);
    actor.keyBySql.put(queryKey, materialization.key);
    if (tileKey != null) {
        actor.keyByTile.put(tileKey, materialization.key);
    }
    return key;
}
Also used : Table(org.apache.calcite.schema.Table) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) RelDataType(org.apache.calcite.rel.type.RelDataType) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

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