Search in sources :

Example 26 with AbstractSchema

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

the class JdbcTest method testTableMacro.

/**
 * Tests a relation that is accessed via method syntax.
 *
 * <p>The function ({@link Smalls#view(String)} has a return type
 * {@link Table} and the actual returned value implements
 * {@link org.apache.calcite.schema.TranslatableTable}.
 */
@Test
public void testTableMacro() 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 TableMacro tableMacro = TableMacroImpl.create(Smalls.VIEW_METHOD);
    schema.add("View", tableMacro);
    ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from table(\"s\".\"View\"('(10), (20)')) as t(n)\n" + "where n < 15");
    // The call to "View('(10), (2)')" expands to 'values (1), (3), (10), (20)'.
    assertThat(CalciteAssert.toString(resultSet), equalTo("N=1\n" + "N=3\n" + "N=10\n"));
    connection.close();
}
Also used : TableMacro(org.apache.calcite.schema.TableMacro) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 27 with AbstractSchema

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

the class CalciteAssert method addSchema.

public static SchemaPlus addSchema(SchemaPlus rootSchema, SchemaSpec schema) {
    SchemaPlus foodmart;
    SchemaPlus jdbcScott;
    final ConnectionSpec cs;
    final DataSource dataSource;
    switch(schema) {
        case REFLECTIVE_FOODMART:
            return rootSchema.add("foodmart", new ReflectiveSchema(new JdbcTest.FoodmartSchema()));
        case JDBC_SCOTT:
            cs = DatabaseInstance.HSQLDB.scott;
            dataSource = JdbcSchema.dataSource(cs.url, cs.driver, cs.username, cs.password);
            return rootSchema.add("JDBC_SCOTT", JdbcSchema.create(rootSchema, "JDBC_SCOTT", dataSource, cs.catalog, cs.schema));
        case JDBC_FOODMART:
            cs = DB.foodmart;
            dataSource = JdbcSchema.dataSource(cs.url, cs.driver, cs.username, cs.password);
            return rootSchema.add("foodmart", JdbcSchema.create(rootSchema, "foodmart", dataSource, cs.catalog, cs.schema));
        case JDBC_FOODMART_WITH_LATTICE:
            foodmart = rootSchema.getSubSchema("foodmart");
            if (foodmart == null) {
                foodmart = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_FOODMART);
            }
            foodmart.add("lattice", Lattice.create(foodmart.unwrap(CalciteSchema.class), "select 1 from \"foodmart\".\"sales_fact_1997\" as s\n" + "join \"foodmart\".\"time_by_day\" as t using (\"time_id\")\n" + "join \"foodmart\".\"customer\" as c using (\"customer_id\")\n" + "join \"foodmart\".\"product\" as p using (\"product_id\")\n" + "join \"foodmart\".\"product_class\" as pc on p.\"product_class_id\" = pc.\"product_class_id\"", true));
            return foodmart;
        case SCOTT:
            jdbcScott = rootSchema.getSubSchema("jdbc_scott");
            if (jdbcScott == null) {
                jdbcScott = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_SCOTT);
            }
            return rootSchema.add("scott", new CloneSchema(jdbcScott));
        case CLONE_FOODMART:
            foodmart = rootSchema.getSubSchema("foodmart");
            if (foodmart == null) {
                foodmart = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_FOODMART);
            }
            return rootSchema.add("foodmart2", new CloneSchema(foodmart));
        case GEO:
            ModelHandler.addFunctions(rootSchema, null, ImmutableList.<String>of(), GeoFunctions.class.getName(), "*", true);
            final SchemaPlus s = rootSchema.add("GEO", new AbstractSchema());
            ModelHandler.addFunctions(s, "countries", ImmutableList.<String>of(), CountriesTableFunction.class.getName(), null, false);
            final String sql = "select * from table(\"countries\"(true))";
            final ViewTableMacro viewMacro = ViewTable.viewMacro(rootSchema, sql, ImmutableList.of("GEO"), ImmutableList.<String>of(), false);
            s.add("countries", viewMacro);
            return s;
        case HR:
            return rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
        case LINGUAL:
            return rootSchema.add("SALES", new ReflectiveSchema(new JdbcTest.LingualSchema()));
        case BLANK:
            return rootSchema.add("BLANK", new AbstractSchema());
        case ORINOCO:
            final SchemaPlus orinoco = rootSchema.add("ORINOCO", new AbstractSchema());
            orinoco.add("ORDERS", new StreamTest.OrdersHistoryTable(StreamTest.OrdersStreamTableFactory.getRowList()));
            return orinoco;
        case POST:
            final SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
            post.add("EMP", ViewTable.viewMacro(post, "select * from (values\n" + "    ('Jane', 10, 'F'),\n" + "    ('Bob', 10, 'M'),\n" + "    ('Eric', 20, 'M'),\n" + "    ('Susan', 30, 'F'),\n" + "    ('Alice', 30, 'F'),\n" + "    ('Adam', 50, 'M'),\n" + "    ('Eve', 50, 'F'),\n" + "    ('Grace', 60, 'F'),\n" + "    ('Wilma', cast(null as integer), 'F'))\n" + "  as t(ename, deptno, gender)", ImmutableList.<String>of(), ImmutableList.of("POST", "EMP"), null));
            post.add("DEPT", ViewTable.viewMacro(post, "select * from (values\n" + "    (10, 'Sales'),\n" + "    (20, 'Marketing'),\n" + "    (30, 'Engineering'),\n" + "    (40, 'Empty')) as t(deptno, dname)", ImmutableList.<String>of(), ImmutableList.of("POST", "DEPT"), null));
            post.add("EMPS", ViewTable.viewMacro(post, "select * from (values\n" + "    (100, 'Fred',  10, CAST(NULL AS CHAR(1)), CAST(NULL AS VARCHAR(20)), 40,               25, TRUE,    FALSE, DATE '1996-08-03'),\n" + "    (110, 'Eric',  20, 'M',                   'San Francisco',           3,                80, UNKNOWN, FALSE, DATE '2001-01-01'),\n" + "    (110, 'John',  40, 'M',                   'Vancouver',               2, CAST(NULL AS INT), FALSE,   TRUE,  DATE '2002-05-03'),\n" + "    (120, 'Wilma', 20, 'F',                   CAST(NULL AS VARCHAR(20)), 1,                 5, UNKNOWN, TRUE,  DATE '2005-09-07'),\n" + "    (130, 'Alice', 40, 'F',                   'Vancouver',               2, CAST(NULL AS INT), FALSE,   TRUE,  DATE '2007-01-01'))\n" + " as t(empno, name, deptno, gender, city, empid, age, slacker, manager, joinedat)", ImmutableList.<String>of(), ImmutableList.of("POST", "EMPS"), null));
            return post;
        default:
            throw new AssertionError("unknown schema " + schema);
    }
}
Also used : CloneSchema(org.apache.calcite.adapter.clone.CloneSchema) GeoFunctions(org.apache.calcite.runtime.GeoFunctions) ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DataSource(javax.sql.DataSource) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema)

Example 28 with AbstractSchema

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

the class CollectionTypeTest method setupConnectionWithNestedTable.

private Connection setupConnectionWithNestedTable() throws SQLException {
    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("nested", new NestedCollectionTable());
    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) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 29 with AbstractSchema

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

the class ScannableTableTest method newSchema.

protected ConnectionPostProcessor newSchema(final String schemaName, final String tableName, final Table table) {
    return new ConnectionPostProcessor() {

        @Override
        public Connection apply(Connection connection) throws SQLException {
            CalciteConnection con = connection.unwrap(CalciteConnection.class);
            SchemaPlus rootSchema = con.getRootSchema();
            SchemaPlus schema = rootSchema.add(schemaName, new AbstractSchema());
            schema.add(tableName, table);
            connection.setSchema(schemaName);
            return connection;
        }
    };
}
Also used : ConnectionPostProcessor(org.apache.calcite.test.CalciteAssert.ConnectionPostProcessor) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 30 with AbstractSchema

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

the class ScannableTableTest method testPrepared2.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1031">[CALCITE-1031]
 * In prepared statement, CsvScannableTable.scan is called twice</a>.
 */
@Test
public void testPrepared2() throws SQLException {
    final Properties properties = new Properties();
    properties.setProperty("caseSensitive", "true");
    try (final Connection connection = DriverManager.getConnection("jdbc:calcite:", properties)) {
        final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        final AtomicInteger scanCount = new AtomicInteger();
        final AtomicInteger enumerateCount = new AtomicInteger();
        final Schema schema = new AbstractSchema() {

            @Override
            protected Map<String, Table> getTableMap() {
                return ImmutableMap.<String, Table>of("TENS", new SimpleTable() {

                    private Enumerable<Object[]> superScan(DataContext root) {
                        return super.scan(root);
                    }

                    @Override
                    public Enumerable<Object[]> scan(final DataContext root) {
                        scanCount.incrementAndGet();
                        return new AbstractEnumerable<Object[]>() {

                            public Enumerator<Object[]> enumerator() {
                                enumerateCount.incrementAndGet();
                                return superScan(root).enumerator();
                            }
                        };
                    }
                });
            }
        };
        calciteConnection.getRootSchema().add("TEST", schema);
        final String sql = "select * from \"TEST\".\"TENS\" where \"i\" < ?";
        final PreparedStatement statement = calciteConnection.prepareStatement(sql);
        assertThat(scanCount.get(), is(0));
        assertThat(enumerateCount.get(), is(0));
        // First execute
        statement.setInt(1, 20);
        assertThat(scanCount.get(), is(0));
        ResultSet resultSet = statement.executeQuery();
        assertThat(scanCount.get(), is(1));
        assertThat(enumerateCount.get(), is(1));
        assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10"));
        assertThat(scanCount.get(), is(1));
        assertThat(enumerateCount.get(), is(1));
        // Second execute
        resultSet = statement.executeQuery();
        assertThat(scanCount.get(), is(2));
        assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10"));
        assertThat(scanCount.get(), is(2));
        // Third execute
        statement.setInt(1, 30);
        resultSet = statement.executeQuery();
        assertThat(scanCount.get(), is(3));
        assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10", "i=20"));
        assertThat(scanCount.get(), is(3));
    }
}
Also used : ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) Table(org.apache.calcite.schema.Table) FilterableTable(org.apache.calcite.schema.FilterableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) Schema(org.apache.calcite.schema.Schema) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) DataContext(org.apache.calcite.DataContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Enumerator(org.apache.calcite.linq4j.Enumerator) ResultSet(java.sql.ResultSet) Enumerable(org.apache.calcite.linq4j.Enumerable) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) 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