private Expression translate0(RexNode expr) { if (expr instanceof RexInputRef) { // TODO: multiple inputs, e.g. joins final Expression input = getInput(0); final int index = ((RexInputRef) expr).getIndex(); final List<RelDataTypeField> fields = program.getInputRowType().getFieldList(); final RelDataTypeField field = fields.get(index); if (fields.size() == 1) { return input; } else if (input.getType() == Object[].class) { return Expressions.convert_( Expressions.arrayIndex(input, Expressions.constant(field.getIndex())), Types.box(JavaRules.EnumUtil.javaClass(typeFactory, field.getType()))); } else { return Expressions.field(input, field.getName()); } } if (expr instanceof RexLocalRef) { return translate(program.getExprList().get(((RexLocalRef) expr).getIndex())); } if (expr instanceof RexLiteral) { return Expressions.constant( ((RexLiteral) expr).getValue(), typeFactory.getJavaClass(expr.getType())); } if (expr instanceof RexCall) { final RexCall call = (RexCall) expr; final SqlOperator operator = call.getOperator(); final ExpressionType expressionType = SQL_TO_LINQ_OPERATOR_MAP.get(operator); if (expressionType != null) { switch (operator.getSyntax()) { case Binary: return Expressions.makeBinary( expressionType, translate(call.getOperands()[0]), translate(call.getOperands()[1])); case Postfix: case Prefix: return Expressions.makeUnary(expressionType, translate(call.getOperands()[0])); default: throw new RuntimeException("unknown syntax " + operator.getSyntax()); } } Method method = SQL_OP_TO_JAVA_METHOD_MAP.get(operator); if (method != null) { List<Expression> exprs = translateList(Arrays.asList(call.operands)); return !Modifier.isStatic(method.getModifiers()) ? Expressions.call(exprs.get(0), method, exprs.subList(1, exprs.size())) : Expressions.call(method, exprs); } switch (expr.getKind()) { default: throw new RuntimeException("cannot translate expression " + expr); } } throw new RuntimeException("cannot translate expression " + expr); }
/** Returns the expression to access a table within a schema. */ public static Expression tableExpression( Schema schema, Type elementType, String tableName, Class clazz) { final MethodCallExpression expression; if (Table.class.isAssignableFrom(clazz)) { expression = Expressions.call( schema.getExpression(), BuiltinMethod.SCHEMA_GET_TABLE.method, Expressions.constant(tableName)); } else { expression = Expressions.call( BuiltinMethod.SCHEMAS_QUERYABLE.method, DataContext.ROOT, schema.getExpression(), Expressions.constant(elementType), Expressions.constant(tableName)); } return Types.castIfNecessary(clazz, expression); }
public void testWriteAnonymousClass() { // final List<String> baz = Arrays.asList("foo", "bar"); // new AbstractList<String>() { // public int size() { // return baz.size(); // } // public String get(int index) { // return ((String) baz.get(index)).toUpperCase(); // } // } final ParameterExpression bazParameter = Expressions.parameter(Types.of(List.class, String.class), "baz"); final ParameterExpression indexParameter = Expressions.parameter(Integer.TYPE, "index"); BlockExpression e = Expressions.block( Expressions.declare( Modifier.FINAL, bazParameter, Expressions.call( Arrays.class, "asList", Arrays.<Expression>asList( Expressions.constant("foo"), Expressions.constant("bar")))), Expressions.statement( Expressions.new_( Types.of(AbstractList.class, String.class), Collections.<Expression>emptyList(), Arrays.<MemberDeclaration>asList( Expressions.fieldDecl( Modifier.PUBLIC | Modifier.FINAL, Expressions.parameter(String.class, "qux"), Expressions.constant("xyzzy")), Expressions.methodDecl( Modifier.PUBLIC, Integer.TYPE, "size", Collections.<ParameterExpression>emptyList(), Blocks.toFunctionBlock( Expressions.call( bazParameter, "size", Collections.<Expression>emptyList()))), Expressions.methodDecl( Modifier.PUBLIC, String.class, "get", Arrays.asList(indexParameter), Blocks.toFunctionBlock( Expressions.call( Expressions.convert_( Expressions.call( bazParameter, "get", Arrays.<Expression>asList(indexParameter)), String.class), "toUpperCase", Collections.<Expression>emptyList()))))))); assertEquals( "{\n" + " final java.util.List<String> baz = java.util.Arrays.asList(\"foo\", \"bar\");\n" + " new java.util.AbstractList<String>(){\n" + " public final String qux = \"xyzzy\";\n" + " public int size() {\n" + " return baz.size();\n" + " }\n" + "\n" + " public String get(int index) {\n" + " return ((String) baz.get(index)).toUpperCase();\n" + " }\n" + "\n" + " };\n" + "}\n", Expressions.toString(e)); }