Search in sources :

Example 1 with FunctionExpression

use of org.apache.calcite.linq4j.tree.FunctionExpression in project calcite by apache.

the class ExpressionTest method testLambdaCallsTwoArgMethod.

@Test
public void testLambdaCallsTwoArgMethod() throws NoSuchMethodException {
    // A parameter for the lambda expression.
    ParameterExpression paramS = Expressions.parameter(String.class, "s");
    ParameterExpression paramBegin = Expressions.parameter(Integer.TYPE, "begin");
    ParameterExpression paramEnd = Expressions.parameter(Integer.TYPE, "end");
    // This expression represents a lambda expression
    // that adds 1 to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.call(paramS, String.class.getMethod("substring", Integer.TYPE, Integer.TYPE), paramBegin, paramEnd), paramS, paramBegin, paramEnd);
    // Compile and run the lambda expression.
    String s = (String) lambdaExpr.compile().dynamicInvoke("hello world", 3, 7);
    assertEquals("lo w", s);
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Example 2 with FunctionExpression

use of org.apache.calcite.linq4j.tree.FunctionExpression in project calcite by apache.

the class SqlUserDefinedTableMacro method coerce.

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.<ParameterExpression>emptyList());
    return convert.compile().dynamicInvoke();
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) Expression(org.apache.calcite.linq4j.tree.Expression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RelDataTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeFactoryImpl) NlsString(org.apache.calcite.util.NlsString) NlsString(org.apache.calcite.util.NlsString) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 3 with FunctionExpression

use of org.apache.calcite.linq4j.tree.FunctionExpression in project calcite by apache.

the class LixToRelTranslator method translate.

public RelNode translate(Expression expression) {
    if (expression instanceof MethodCallExpression) {
        final MethodCallExpression call = (MethodCallExpression) expression;
        BuiltInMethod method = BuiltInMethod.MAP.get(call.method);
        if (method == null) {
            throw new UnsupportedOperationException("unknown method " + call.method);
        }
        RelNode input;
        switch(method) {
            case SELECT:
                input = translate(call.targetExpression);
                return LogicalProject.create(input, toRex(input, (FunctionExpression) call.expressions.get(0)), (List<String>) null);
            case WHERE:
                input = translate(call.targetExpression);
                return LogicalFilter.create(input, toRex((FunctionExpression) call.expressions.get(0), input));
            case AS_QUERYABLE:
                return LogicalTableScan.create(cluster, RelOptTableImpl.create(null, typeFactory.createJavaType(Types.toClass(Types.getElementType(call.targetExpression.getType()))), ImmutableList.<String>of(), call.targetExpression));
            case SCHEMA_GET_TABLE:
                return LogicalTableScan.create(cluster, RelOptTableImpl.create(null, typeFactory.createJavaType((Class) ((ConstantExpression) call.expressions.get(1)).value), ImmutableList.<String>of(), call.targetExpression));
            default:
                throw new UnsupportedOperationException("unknown method " + call.method);
        }
    }
    throw new UnsupportedOperationException("unknown expression type " + expression.getNodeType());
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) RelNode(org.apache.calcite.rel.RelNode) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression)

Example 4 with FunctionExpression

use of org.apache.calcite.linq4j.tree.FunctionExpression in project calcite by apache.

the class ExpressionTest method testLambdaPrimitiveTwoArgs.

@Test
public void testLambdaPrimitiveTwoArgs() {
    // Parameters for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(int.class, "key");
    ParameterExpression param2Expr = Expressions.parameter(int.class, "key2");
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.block((Type) null, Expressions.return_(null, paramExpr)), Arrays.asList(paramExpr, param2Expr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function2() {\n" + "  public int apply(int key, int key2) {\n" + "    return key;\n" + "  }\n" + "  public Integer apply(Integer key, Integer key2) {\n" + "    return apply(\n" + "      key.intValue(),\n" + "      key2.intValue());\n" + "  }\n" + "  public Integer apply(Object key, Object key2) {\n" + "    return apply(\n" + "      (Integer) key,\n" + "      (Integer) key2);\n" + "  }\n" + "}\n", s);
}
Also used : Type(java.lang.reflect.Type) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Example 5 with FunctionExpression

use of org.apache.calcite.linq4j.tree.FunctionExpression in project calcite by apache.

the class ExpressionTest method testLambdaCallsBinaryOp.

@Test
public void testLambdaCallsBinaryOp() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Double.TYPE, "arg");
    // This expression represents a lambda expression
    // that adds 1 to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2d)), Arrays.asList(paramExpr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + "  public double apply(double arg) {\n" + "    return arg + 2.0D;\n" + "  }\n" + "  public Object apply(Double arg) {\n" + "    return apply(\n" + "      arg.doubleValue());\n" + "  }\n" + "  public Object apply(Object arg) {\n" + "    return apply(\n" + "      (Double) arg);\n" + "  }\n" + "}\n", s);
    // Compile and run the lambda expression.
    // The value of the parameter is 1.5.
    double n = (Double) lambdaExpr.compile().dynamicInvoke(1.5d);
    // This code example produces the following output:
    // 
    // arg => (arg +2)
    // 3
    assertEquals(3.5D, n, 0d);
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Aggregations

FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)5 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)4 Test (org.junit.Test)3 Type (java.lang.reflect.Type)1 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)1 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)1 Expression (org.apache.calcite.linq4j.tree.Expression)1 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)1 RelNode (org.apache.calcite.rel.RelNode)1 RelDataTypeFactoryImpl (org.apache.calcite.rel.type.RelDataTypeFactoryImpl)1 BuiltInMethod (org.apache.calcite.util.BuiltInMethod)1 NlsString (org.apache.calcite.util.NlsString)1