Search in sources :

Example 1 with QueryProvider

use of org.apache.calcite.linq4j.QueryProvider 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 2 with QueryProvider

use of org.apache.calcite.linq4j.QueryProvider 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 3 with QueryProvider

use of org.apache.calcite.linq4j.QueryProvider in project calcite by apache.

the class ReflectiveSchemaTest method testQueryProviderSingleColumn.

@Test
public void testQueryProviderSingleColumn() throws Exception {
    Connection connection = CalciteAssert.that(CalciteAssert.Config.REGULAR).connect();
    QueryProvider queryProvider = connection.unwrap(QueryProvider.class);
    ParameterExpression e = Expressions.parameter(Employee.class, "e");
    // "Enumerable<T> asEnumerable(final T[] ts)"
    List<Integer> list = queryProvider.createQuery(Expressions.call(Expressions.call(Types.of(Enumerable.class, Employee.class), null, LINQ4J_AS_ENUMERABLE_METHOD, Expressions.constant(new JdbcTest.HrSchema().emps)), "asQueryable"), Employee.class).select(Expressions.<Function1<Employee, Integer>>lambda(Expressions.field(e, "empid"), e)).toList();
    assertEquals(Arrays.asList(100, 200, 150, 110), list);
}
Also used : QueryProvider(org.apache.calcite.linq4j.QueryProvider) Employee(org.apache.calcite.test.JdbcTest.Employee) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Function1(org.apache.calcite.linq4j.function.Function1) Enumerable(org.apache.calcite.linq4j.Enumerable) Test(org.junit.Test)

Example 4 with QueryProvider

use of org.apache.calcite.linq4j.QueryProvider in project calcite by apache.

the class ReflectiveSchemaTest method testQueryProvider.

/**
 * Test that uses a JDBC connection as a linq4j
 * {@link org.apache.calcite.linq4j.QueryProvider}.
 *
 * @throws Exception on error
 */
@Test
public void testQueryProvider() throws Exception {
    Connection connection = CalciteAssert.that(CalciteAssert.Config.REGULAR).connect();
    QueryProvider queryProvider = connection.unwrap(QueryProvider.class);
    ParameterExpression e = Expressions.parameter(Employee.class, "e");
    // "Enumerable<T> asEnumerable(final T[] ts)"
    List<Object[]> list = queryProvider.createQuery(Expressions.call(Expressions.call(Types.of(Enumerable.class, Employee.class), null, LINQ4J_AS_ENUMERABLE_METHOD, Expressions.constant(new JdbcTest.HrSchema().emps)), "asQueryable"), Employee.class).where(Expressions.<Predicate1<Employee>>lambda(Expressions.lessThan(Expressions.field(e, "empid"), Expressions.constant(160)), e)).where(Expressions.<Predicate1<Employee>>lambda(Expressions.greaterThan(Expressions.field(e, "empid"), Expressions.constant(140)), e)).select(Expressions.<Function1<Employee, Object[]>>lambda(Expressions.new_(Object[].class, Expressions.field(e, "empid"), Expressions.call(Expressions.field(e, "name"), "toUpperCase")), e)).toList();
    assertEquals(1, list.size());
    assertEquals(2, list.get(0).length);
    assertEquals(150, list.get(0)[0]);
    assertEquals("SEBASTIAN", list.get(0)[1]);
}
Also used : QueryProvider(org.apache.calcite.linq4j.QueryProvider) Employee(org.apache.calcite.test.JdbcTest.Employee) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Function1(org.apache.calcite.linq4j.function.Function1) Enumerable(org.apache.calcite.linq4j.Enumerable) Predicate1(org.apache.calcite.linq4j.function.Predicate1) Test(org.junit.Test)

Aggregations

QueryProvider (org.apache.calcite.linq4j.QueryProvider)4 Connection (java.sql.Connection)2 AbstractQueryableTable (org.apache.calcite.adapter.java.AbstractQueryableTable)2 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)2 Enumerable (org.apache.calcite.linq4j.Enumerable)2 Function1 (org.apache.calcite.linq4j.function.Function1)2 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)2 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 Employee (org.apache.calcite.test.JdbcTest.Employee)2 Test (org.junit.Test)2 AbstractList (java.util.AbstractList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BaseQueryable (org.apache.calcite.linq4j.BaseQueryable)1 Queryable (org.apache.calcite.linq4j.Queryable)1 Predicate1 (org.apache.calcite.linq4j.function.Predicate1)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1