Search in sources :

Example 1 with Enumerable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Enumerable in project druid by druid-io.

the class DruidPlanner method planWithBindableConvention.

private PlannerResult planWithBindableConvention(final SqlExplain explain, final RelRoot root) throws RelConversionException {
    BindableRel bindableRel = (BindableRel) planner.transform(Rules.BINDABLE_CONVENTION_RULES, planner.getEmptyTraitSet().replace(BindableConvention.INSTANCE).plus(root.collation), root.rel);
    if (!root.isRefTrivial()) {
        // Add a projection on top to accommodate root.fields.
        final List<RexNode> projects = new ArrayList<>();
        final RexBuilder rexBuilder = bindableRel.getCluster().getRexBuilder();
        for (int field : Pair.left(root.fields)) {
            projects.add(rexBuilder.makeInputRef(bindableRel, field));
        }
        bindableRel = new Bindables.BindableProject(bindableRel.getCluster(), bindableRel.getTraitSet(), bindableRel, projects, root.validatedRowType);
    }
    if (explain != null) {
        return planExplanation(bindableRel, explain);
    } else {
        final BindableRel theRel = bindableRel;
        final DataContext dataContext = plannerContext.createDataContext((JavaTypeFactory) planner.getTypeFactory());
        final Supplier<Sequence<Object[]>> resultsSupplier = new Supplier<Sequence<Object[]>>() {

            @Override
            public Sequence<Object[]> get() {
                final Enumerable enumerable = theRel.bind(dataContext);
                return Sequences.simple(enumerable);
            }
        };
        return new PlannerResult(resultsSupplier, root.validatedRowType);
    }
}
Also used : ArrayList(java.util.ArrayList) BindableRel(org.apache.calcite.interpreter.BindableRel) Bindables(org.apache.calcite.interpreter.Bindables) Sequence(io.druid.java.util.common.guava.Sequence) DataContext(org.apache.calcite.DataContext) RexBuilder(org.apache.calcite.rex.RexBuilder) Enumerable(org.apache.calcite.linq4j.Enumerable) Supplier(com.google.common.base.Supplier) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with Enumerable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Enumerable in project druid by druid-io.

the class DruidPlanner method planWithBindableConvention.

/**
 * Construct a {@link PlannerResult} for a fall-back 'bindable' rel, for things that are not directly translatable
 * to native Druid queries such as system tables and just a general purpose (but definitely not optimized) fall-back.
 *
 * See {@link #planWithDruidConvention} which will handle things which are directly translatable
 * to native Druid queries.
 */
private PlannerResult planWithBindableConvention(final RelRoot root, @Nullable final SqlExplain explain) throws RelConversionException {
    BindableRel bindableRel = (BindableRel) planner.transform(Rules.BINDABLE_CONVENTION_RULES, planner.getEmptyTraitSet().replace(BindableConvention.INSTANCE).plus(root.collation), root.rel);
    if (!root.isRefTrivial()) {
        // Add a projection on top to accommodate root.fields.
        final List<RexNode> projects = new ArrayList<>();
        final RexBuilder rexBuilder = bindableRel.getCluster().getRexBuilder();
        for (int field : Pair.left(root.fields)) {
            projects.add(rexBuilder.makeInputRef(bindableRel, field));
        }
        bindableRel = new Bindables.BindableProject(bindableRel.getCluster(), bindableRel.getTraitSet(), bindableRel, projects, root.validatedRowType);
    }
    if (explain != null) {
        return planExplanation(bindableRel, explain, false);
    } else {
        final BindableRel theRel = bindableRel;
        final DataContext dataContext = plannerContext.createDataContext((JavaTypeFactory) planner.getTypeFactory(), plannerContext.getParameters());
        final Supplier<Sequence<Object[]>> resultsSupplier = () -> {
            final Enumerable<?> enumerable = theRel.bind(dataContext);
            final Enumerator<?> enumerator = enumerable.enumerator();
            return Sequences.withBaggage(new BaseSequence<>(new BaseSequence.IteratorMaker<Object[], EnumeratorIterator<Object[]>>() {

                @Override
                public EnumeratorIterator<Object[]> make() {
                    return new EnumeratorIterator<>(new Iterator<Object[]>() {

                        @Override
                        public boolean hasNext() {
                            return enumerator.moveNext();
                        }

                        @Override
                        public Object[] next() {
                            return (Object[]) enumerator.current();
                        }
                    });
                }

                @Override
                public void cleanup(EnumeratorIterator<Object[]> iterFromMake) {
                }
            }), enumerator::close);
        };
        return new PlannerResult(resultsSupplier, root.validatedRowType);
    }
}
Also used : ArrayList(java.util.ArrayList) BindableRel(org.apache.calcite.interpreter.BindableRel) Bindables(org.apache.calcite.interpreter.Bindables) Sequence(org.apache.druid.java.util.common.guava.Sequence) BaseSequence(org.apache.druid.java.util.common.guava.BaseSequence) BaseSequence(org.apache.druid.java.util.common.guava.BaseSequence) DataContext(org.apache.calcite.DataContext) Enumerator(org.apache.calcite.linq4j.Enumerator) Iterator(java.util.Iterator) RexBuilder(org.apache.calcite.rex.RexBuilder) Enumerable(org.apache.calcite.linq4j.Enumerable) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with Enumerable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Enumerable in project calcite by apache.

the class CalcitePrepareImpl method simplePrepare.

/**
 * Quickly prepares a simple SQL statement, circumventing the usual
 * preparation process.
 */
private <T> CalciteSignature<T> simplePrepare(Context context, String sql) {
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    final RelDataType x = typeFactory.builder().add(SqlUtil.deriveAliasFromOrdinal(0), SqlTypeName.INTEGER).build();
    @SuppressWarnings("unchecked") final List<T> list = (List) ImmutableList.of(1);
    final List<String> origin = null;
    final List<List<String>> origins = Collections.nCopies(x.getFieldCount(), origin);
    final List<ColumnMetaData> columns = getColumnMetaDataList(typeFactory, x, x, origins);
    final Meta.CursorFactory cursorFactory = Meta.CursorFactory.deduce(columns, null);
    return new CalciteSignature<>(sql, ImmutableList.<AvaticaParameter>of(), ImmutableMap.<String, Object>of(), x, columns, cursorFactory, context.getRootSchema(), ImmutableList.<RelCollation>of(), -1, new Bindable<T>() {

        public Enumerable<T> bind(DataContext dataContext) {
            return Linq4j.asEnumerable(list);
        }
    }, Meta.StatementType.SELECT);
}
Also used : Meta(org.apache.calcite.avatica.Meta) RelDataType(org.apache.calcite.rel.type.RelDataType) DataContext(org.apache.calcite.DataContext) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) Enumerable(org.apache.calcite.linq4j.Enumerable) ArrayList(java.util.ArrayList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 4 with Enumerable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Enumerable 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 5 with Enumerable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Enumerable 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

Enumerable (org.apache.calcite.linq4j.Enumerable)7 DataContext (org.apache.calcite.DataContext)4 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)3 Test (org.junit.Test)3 BindableRel (org.apache.calcite.interpreter.BindableRel)2 Bindables (org.apache.calcite.interpreter.Bindables)2 AbstractEnumerable (org.apache.calcite.linq4j.AbstractEnumerable)2 Enumerator (org.apache.calcite.linq4j.Enumerator)2 QueryProvider (org.apache.calcite.linq4j.QueryProvider)2 Function1 (org.apache.calcite.linq4j.function.Function1)2 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)2 RexBuilder (org.apache.calcite.rex.RexBuilder)2 RexNode (org.apache.calcite.rex.RexNode)2 Employee (org.apache.calcite.test.JdbcTest.Employee)2 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 Sequence (io.druid.java.util.common.guava.Sequence)1 PreparedStatement (java.sql.PreparedStatement)1