/** * Creates a LambdaExpression by first constructing a delegate * type, using varargs. */ public static <F extends Function<?>> FunctionExpression<F> lambda( BlockStatement body, ParameterExpression... parameters) { return lambda(body, toList(parameters)); }
/** * Creates an Expression where the delegate type {@code F} is * known at compile time. */ public static <F extends Function<?>> FunctionExpression<F> lambda( Expression body, Iterable<? extends ParameterExpression> parameters) { return lambda(Blocks.toFunctionBlock(body), parameters); }
/** * Creates a LambdaExpression by first constructing a delegate * type, using varargs. * * <p>It can be used when the delegate type is not known at compile time. */ public static <T, F extends Function<? extends T>> FunctionExpression<F> lambda( Class<F> type, BlockStatement body, ParameterExpression... parameters) { return lambda(type, body, toList(parameters)); }
public Expression visit(FunctionExpression functionExpression, BlockStatement body) { return functionExpression.body.equals(body) ? functionExpression : Expressions.lambda(body, functionExpression.parameterList); }
/** * Creates a LambdaExpression by first constructing a delegate * type. * * <p>It can be used when the delegate type is not known at compile time. */ public static <T, F extends Function<? extends T>> FunctionExpression<F> lambda( Class<F> type, Expression body, Iterable<? extends ParameterExpression> parameters) { return lambda(type, Blocks.toFunctionBlock(body), toList(parameters)); }
/** * Creates an Expression where the delegate type {@code F} is * known at compile time, using varargs. */ public static <F extends Function<?>> FunctionExpression<F> lambda( Expression body, ParameterExpression... parameters) { return lambda(Blocks.toFunctionBlock(body), toList(parameters)); }
/** * Creates a LambdaExpression by first constructing a delegate * type, using varargs. * * <p>It can be used when the delegate type is not known at compile time. */ public static <T, F extends Function<? extends T>> FunctionExpression<F> lambda( Class<F> type, Expression body, ParameterExpression... parameters) { return lambda(type, Blocks.toFunctionBlock(body), toList(parameters)); }
/** * Returns elements from a sequence as long as a * specified condition is true. */ public static <T> Queryable<T> takeWhile(Queryable<T> source, FunctionExpression<Predicate1<T>> predicate) { return takeWhileN(source, Expressions.lambda( Functions.toPredicate2(predicate.getFunction()))); }
/** * Bypasses elements in a sequence as long as a * specified condition is true and then returns the remaining * elements. */ public static <T> Queryable<T> skipWhile(Queryable<T> source, FunctionExpression<Predicate1<T>> predicate) { return skipWhileN(source, Expressions.lambda( Functions.toPredicate2(predicate.getFunction()))); }
public Expression generateSelector(final ParameterExpression parameter, final List<Integer> fields, List<Integer> usedFields, JavaRowFormat targetFormat) { final PhysType targetPhysType = project(fields, true, targetFormat); final List<Expression> expressions = new ArrayList<>(); for (Ord<Integer> ord : Ord.zip(fields)) { final Integer field = ord.e; if (usedFields.contains(field)) { expressions.add(fieldReference(parameter, field)); } else { final Primitive primitive = Primitive.of(targetPhysType.fieldClass(ord.i)); expressions.add( Expressions.constant( primitive != null ? primitive.defaultValue : null)); } } for (Integer field : fields) { expressions.add(Expressions.constant(!usedFields.contains(field))); } return Expressions.lambda(Function1.class, targetPhysType.record(expressions), parameter); }
public Expression generateSelector(final ParameterExpression parameter, final List<Integer> fields, List<Integer> usedFields, JavaRowFormat targetFormat) { final PhysType targetPhysType = project(fields, true, targetFormat); final List<Expression> expressions = new ArrayList<>(); for (Ord<Integer> ord : Ord.zip(fields)) { final Integer field = ord.e; if (usedFields.contains(field)) { expressions.add(fieldReference(parameter, field)); } else { final Primitive primitive = Primitive.of(targetPhysType.fieldClass(ord.i)); expressions.add( Expressions.constant( primitive != null ? primitive.defaultValue : null)); } } for (Integer field : fields) { expressions.add(Expressions.constant(!usedFields.contains(field))); } return Expressions.lambda(Function1.class, targetPhysType.record(expressions), parameter); }
public Expression generateSelector( ParameterExpression parameter, List<Integer> fields, JavaRowFormat targetFormat) { // Optimize target format switch (fields.size()) { case 0: targetFormat = JavaRowFormat.LIST; break; case 1: targetFormat = JavaRowFormat.SCALAR; break; } final PhysType targetPhysType = project(fields, targetFormat); switch (format) { case SCALAR: return Expressions.call(BuiltInMethod.IDENTITY_SELECTOR.method); default: return Expressions.lambda(Function1.class, targetPhysType.record(fieldReferences(parameter, fields)), parameter); } }
public Expression generateSelector( ParameterExpression parameter, List<Integer> fields, JavaRowFormat targetFormat) { // Optimize target format switch (fields.size()) { case 0: targetFormat = JavaRowFormat.LIST; break; case 1: targetFormat = JavaRowFormat.SCALAR; break; } final PhysType targetPhysType = project(fields, targetFormat); switch (format) { case SCALAR: return Expressions.call(BuiltInMethod.IDENTITY_SELECTOR.method); default: return Expressions.lambda(Function1.class, targetPhysType.record(fieldReferences(parameter, fields)), parameter); } }
private static Object coerce(Object o, RelDataType type) { if (o == null) { return null; } if (!(type instanceof RelDataTypeFactoryImpl.JavaType)) { return null; } final RelDataTypeFactoryImpl.JavaType javaType = (RelDataTypeFactoryImpl.JavaType) type; final Class clazz = javaType.getJavaClass(); //noinspection unchecked if (clazz.isAssignableFrom(o.getClass())) { return o; } if (clazz == String.class && o instanceof NlsString) { return ((NlsString) o).getValue(); } // We need optimization here for constant folding. // Not all the expressions can be interpreted (e.g. ternary), so // we rely on optimization capabilities to fold non-interpretable // expressions. BlockBuilder bb = new BlockBuilder(); final Expression expr = RexToLixTranslator.convert(Expressions.constant(o), clazz); bb.add(Expressions.return_(null, expr)); final FunctionExpression convert = Expressions.lambda(bb.toBlock(), Collections.emptyList()); return convert.compile().dynamicInvoke(); }
private Expression toRows(PhysType physType, Expression expression) { if (physType.getFormat() == JavaRowFormat.SCALAR && Object[].class.isAssignableFrom(elementType) && getRowType().getFieldCount() == 1 && (table.unwrap(ScannableTable.class) != null || table.unwrap(FilterableTable.class) != null || table.unwrap(ProjectableFilterableTable.class) != null)) { return Expressions.call(BuiltInMethod.SLICE0.method, expression); } JavaRowFormat oldFormat = format(); if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) { return expression; } final ParameterExpression row_ = Expressions.parameter(elementType, "row"); final int fieldCount = table.getRowType().getFieldCount(); List<Expression> expressionList = new ArrayList<>(fieldCount); for (int i = 0; i < fieldCount; i++) { expressionList.add(fieldExpression(row_, i, physType, oldFormat)); } return Expressions.call(expression, BuiltInMethod.SELECT.method, Expressions.lambda(Function1.class, physType.record(expressionList), row_)); }
private Expression toRows(PhysType physType, Expression expression) { if (physType.getFormat() == JavaRowFormat.SCALAR && Object[].class.isAssignableFrom(elementType) && getRowType().getFieldCount() == 1 && (table.unwrap(ScannableTable.class) != null || table.unwrap(FilterableTable.class) != null || table.unwrap(ProjectableFilterableTable.class) != null)) { return Expressions.call(BuiltInMethod.SLICE0.method, expression); } JavaRowFormat oldFormat = format(); if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) { return expression; } final ParameterExpression row_ = Expressions.parameter(elementType, "row"); final int fieldCount = table.getRowType().getFieldCount(); List<Expression> expressionList = new ArrayList<>(fieldCount); for (int i = 0; i < fieldCount; i++) { expressionList.add(fieldExpression(row_, i, physType, oldFormat)); } return Expressions.call(expression, BuiltInMethod.SELECT.method, Expressions.lambda(Function1.class, physType.record(expressionList), row_)); }
@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); }
@Test public void testTableWhere() throws SQLException, ClassNotFoundException { final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect(); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); ParameterExpression c = Expressions.parameter(JdbcTest.Customer.class, "c"); String s = Schemas.queryable(Schemas.createDataContext(connection, rootSchema), rootSchema.getSubSchema("foodmart"), JdbcTest.Customer.class, "customer") .where( Expressions.lambda( Expressions.lessThan( Expressions.field(c, "customer_id"), Expressions.constant(5)), c)) .toList() .toString(); Util.discard(s); } }
@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); }
@Test public void testTableWhere() throws SQLException, ClassNotFoundException { final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect(); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); ParameterExpression c = Expressions.parameter(JdbcTest.Customer.class, "c"); String s = Schemas.queryable(Schemas.createDataContext(connection, rootSchema), rootSchema.getSubSchema("foodmart"), JdbcTest.Customer.class, "customer") .where( Expressions.lambda( Expressions.lessThan( Expressions.field(c, "customer_id"), Expressions.constant(5)), c)) .toList() .toString(); Util.discard(s); } }