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);
}
}
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);
}
}
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);
}
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);
}
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]);
}
Aggregations