Search in sources :

Example 41 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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 42 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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 43 with SchemaPlus

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

the class Smalls method multiplicationTable.

/**
 * A function that generates multiplication table of {@code ncol} columns x
 * {@code nrow} rows.
 */
public static QueryableTable multiplicationTable(final int ncol, final int nrow, Integer offset) {
    final int offs = offset == null ? 0 : offset;
    return new AbstractQueryableTable(Object[].class) {

        public RelDataType getRowType(RelDataTypeFactory typeFactory) {
            final RelDataTypeFactory.Builder builder = typeFactory.builder();
            builder.add("row_name", typeFactory.createJavaType(String.class));
            final RelDataType int_ = typeFactory.createJavaType(int.class);
            for (int i = 1; i <= ncol; i++) {
                builder.add("c" + i, int_);
            }
            return builder.build();
        }

        public Queryable<Object[]> asQueryable(QueryProvider queryProvider, SchemaPlus schema, String tableName) {
            final List<Object[]> table = new AbstractList<Object[]>() {

                @Override
                public Object[] get(int index) {
                    Object[] cur = new Object[ncol + 1];
                    cur[0] = "row " + index;
                    for (int j = 1; j <= ncol; j++) {
                        cur[j] = j * (index + 1) + offs;
                    }
                    return cur;
                }

                @Override
                public int size() {
                    return nrow;
                }
            };
            return Linq4j.asEnumerable(table).asQueryable();
        }
    };
}
Also used : AbstractQueryableTable(org.apache.calcite.adapter.java.AbstractQueryableTable) AbstractList(java.util.AbstractList) QueryProvider(org.apache.calcite.linq4j.QueryProvider) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 44 with SchemaPlus

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

the class Smalls method oneThreePlus.

private static QueryableTable oneThreePlus(String s) {
    List<Integer> items;
    // Then the engine calls a function with null arguments to get getRowType.
    if (s == null) {
        items = ImmutableList.of();
    } else {
        Integer latest = Integer.parseInt(s.substring(1, s.length() - 1));
        items = ImmutableList.of(1, 3, latest);
    }
    final Enumerable<Integer> enumerable = Linq4j.asEnumerable(items);
    return new AbstractQueryableTable(Integer.class) {

        public <E> Queryable<E> asQueryable(QueryProvider queryProvider, SchemaPlus schema, String tableName) {
            // noinspection unchecked
            return (Queryable<E>) enumerable.asQueryable();
        }

        public RelDataType getRowType(RelDataTypeFactory typeFactory) {
            return typeFactory.builder().add("c", SqlTypeName.INTEGER).build();
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractQueryableTable(org.apache.calcite.adapter.java.AbstractQueryableTable) QueryProvider(org.apache.calcite.linq4j.QueryProvider) Queryable(org.apache.calcite.linq4j.Queryable) BaseQueryable(org.apache.calcite.linq4j.BaseQueryable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory)

Example 45 with SchemaPlus

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

the class FrameworksTest method testOptimize.

@Test
public void testOptimize() {
    RelNode x = Frameworks.withPlanner(new Frameworks.PlannerAction<RelNode>() {

        public RelNode apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
            final Table table = new AbstractTable() {

                public RelDataType getRowType(RelDataTypeFactory typeFactory) {
                    final RelDataType stringType = typeFactory.createJavaType(String.class);
                    final RelDataType integerType = typeFactory.createJavaType(Integer.class);
                    return typeFactory.builder().add("s", stringType).add("i", integerType).build();
                }
            };
            // "SELECT * FROM myTable"
            final RelOptAbstractTable relOptTable = new RelOptAbstractTable(relOptSchema, "myTable", table.getRowType(typeFactory)) {
            };
            final EnumerableTableScan tableRel = EnumerableTableScan.create(cluster, relOptTable);
            // "WHERE i > 1"
            final RexBuilder rexBuilder = cluster.getRexBuilder();
            final RexNode condition = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeFieldAccess(rexBuilder.makeRangeReference(tableRel), "i", true), rexBuilder.makeExactLiteral(BigDecimal.ONE));
            final LogicalFilter filter = LogicalFilter.create(tableRel, condition);
            // Specify that the result should be in Enumerable convention.
            final RelNode rootRel = filter;
            final RelOptPlanner planner = cluster.getPlanner();
            RelTraitSet desiredTraits = cluster.traitSet().replace(EnumerableConvention.INSTANCE);
            final RelNode rootRel2 = planner.changeTraits(rootRel, desiredTraits);
            planner.setRoot(rootRel2);
            // Now, plan.
            return planner.findBestExp();
        }
    });
    String s = RelOptUtil.dumpPlan("", x, SqlExplainFormat.TEXT, SqlExplainLevel.DIGEST_ATTRIBUTES);
    assertThat(Util.toLinux(s), equalTo("EnumerableFilter(condition=[>($1, 1)])\n" + "  EnumerableTableScan(table=[[myTable]])\n"));
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelOptSchema(org.apache.calcite.plan.RelOptSchema) 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) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) RelNode(org.apache.calcite.rel.RelNode) EnumerableTableScan(org.apache.calcite.adapter.enumerable.EnumerableTableScan) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Aggregations

SchemaPlus (org.apache.calcite.schema.SchemaPlus)182 Test (org.junit.Test)57 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)42 Connection (java.sql.Connection)39 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)33 ResultSet (java.sql.ResultSet)26 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)24 RelNode (org.apache.calcite.rel.RelNode)21 Table (org.apache.calcite.schema.Table)20 SqlNode (org.apache.calcite.sql.SqlNode)19 IOException (java.io.IOException)17 Statement (java.sql.Statement)17 PreparedStatement (java.sql.PreparedStatement)16 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)15 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)15 ArrayList (java.util.ArrayList)14 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)14 SQLException (java.sql.SQLException)13 Properties (java.util.Properties)13 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)13