public void testClassDecl() { final NewExpression newExpression = Expressions.new_( Object.class, Collections.<Expression>emptyList(), Arrays.<MemberDeclaration>asList( new FieldDeclaration( Modifier.PUBLIC | Modifier.FINAL, new ParameterExpression(String.class, "foo"), Expressions.constant("bar")), new ClassDeclaration( Modifier.PUBLIC | Modifier.STATIC, "MyClass", null, Collections.<Type>emptyList(), Arrays.<MemberDeclaration>asList( new FieldDeclaration( 0, new ParameterExpression(int.class, "x"), Expressions.constant(0)))), new FieldDeclaration(0, new ParameterExpression(int.class, "i"), null))); assertEquals( "new Object(){\n" + " public final String foo = \"bar\";\n" + " public static class MyClass {\n" + " int x = 0;\n" + " }\n" + " int i;\n" + "}", Expressions.toString(newExpression)); }
public void testLambdaCallsBinaryOp() { // A parameter for the lambda expression. ParameterExpression paramExpr = Expressions.parameter(Integer.TYPE, "arg"); // This expression represents a lambda expression // that adds 1 to the parameter value. FunctionExpression lambdaExpr = Expressions.lambda( Expressions.add(paramExpr, Expressions.constant(2)), Arrays.asList(paramExpr)); // Print out the expression. String s = Expressions.toString(lambdaExpr); assertEquals( "new net.hydromatic.linq4j.function.Function1() {\n" + " public int apply(Integer arg) {\n" + " return arg + 2;\n" + " }\n" + " public Object apply(Object arg) {\n" + " return apply(\n" + " (Integer) arg);\n" + " }\n" + "}\n", s); // Compile and run the lambda expression. // The value of the parameter is 1. int n = (Integer) lambdaExpr.compile().dynamicInvoke(1); // This code example produces the following output: // // arg => (arg +2) // 3 assertEquals(3, n); }
/** * Gets the expression for an input and counts it. * * @param index Input ordinal * @return Expression to which an input should be translated */ private Expression getInput(int index) { Slot slot = inputSlots.get(index); if (list == null) { slot.count++; } else { if (slot.count > 1 && slot.parameterExpression == null) { slot.parameterExpression = Expressions.parameter(slot.expression.type, "current" + index); list.add(Expressions.declare(Modifier.FINAL, slot.parameterExpression, slot.expression)); } } return slot.parameterExpression != null ? slot.parameterExpression : slot.expression; }
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 for a sub-schema. */ public static Expression subSchemaExpression(Schema schema, String name, Class type) { // (Type) schemaExpression.getSubSchema("name") Expression call = Expressions.call( schema.getExpression(), BuiltinMethod.SCHEMA_GET_SUB_SCHEMA.method, Expressions.constant(name)); // CHECKSTYLE: IGNORE 2 //noinspection unchecked if (false && type != null && !type.isAssignableFrom(Schema.class)) { return unwrap(call, type); } return call; }
@Override protected PreparedExecution implement( RelDataType rowType, RelNode rootRel, SqlKind sqlKind, ClassDeclaration decl, Argument[] args) { RelDataType resultType = rootRel.getRowType(); boolean isDml = sqlKind.belongsTo(SqlKind.DML); javaCompiler = createCompiler(); EnumerableRelImplementor relImplementor = getRelImplementor(rootRel.getCluster().getRexBuilder()); BlockExpression expr = relImplementor.implementRoot((EnumerableRel) rootRel); ParameterExpression root0 = Expressions.parameter(DataContext.class, "root0"); String s = Expressions.toString( Blocks.create( Expressions.declare( Modifier.FINAL, (ParameterExpression) schema.getExpression(), root0), expr), false); final Executable executable; try { executable = (Executable) ExpressionEvaluator.createFastScriptEvaluator( s, Executable.class, new String[] {root0.name}); } catch (Exception e) { throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n" + s, e); } if (timingTracer != null) { timingTracer.traceTime("end codegen"); } if (timingTracer != null) { timingTracer.traceTime("end compilation"); } return new PreparedExecution( null, rootRel, resultType, isDml, mapTableModOp(isDml, sqlKind), null) { public Object execute() { return executable.execute(schema); } }; }
public void testCompile() throws NoSuchMethodException { // Creating a parameter for the expression tree. ParameterExpression param = Expressions.parameter(String.class); // Creating an expression for the method call and specifying its // parameter. MethodCallExpression methodCall = Expressions.call(Integer.class, "valueOf", Collections.<Expression>singletonList(param)); // The following statement first creates an expression tree, // then compiles it, and then runs it. int x = Expressions.<Function1<String, Integer>>lambda( methodCall, new ParameterExpression[] {param}) .getFunction() .apply("1234"); assertEquals(1234, x); }
public void testWriteArray() { assertEquals( "1 + integers[2 + index]", Expressions.toString( Expressions.add( Expressions.constant(1), Expressions.arrayIndex( Expressions.variable(int[].class, "integers"), Expressions.add( Expressions.constant(2), Expressions.variable(int.class, "index")))))); }
private Result( SqlNode node, Collection<Clause> clauses, String neededAlias, List<Pair<String, RelDataType>> aliases) { this.node = node; this.neededAlias = neededAlias; this.aliases = aliases; this.clauses = Expressions.list(clauses); }
/** Converts a call to an aggregate function to an expression. */ public SqlNode toSql(AggregateCall aggCall) { SqlOperator op = (SqlAggFunction) aggCall.getAggregation(); final List<SqlNode> operands = Expressions.list(); for (int arg : aggCall.getArgList()) { operands.add(field(arg)); } return op.createCall( aggCall.isDistinct() ? SqlSelectKeyword.DISTINCT.symbol(POS) : null, POS, operands.toArray(new SqlNode[operands.size()])); }
/** 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); }
private Expression translate(RexNode expr) { Slot slot = map.get(expr); if (slot == null) { Expression expression = translate0(expr); assert expression != null; final ParameterExpression parameter; if (!inlineRexSet.contains(expr) && !(expr instanceof RexLocalRef)) { parameter = Expressions.parameter(expression.getType(), "v" + map.size()); } else { parameter = null; } slot = new Slot(parameter, expression); if (parameter != null && list != null) { list.add(Expressions.declare(Modifier.FINAL, slot.parameterExpression, slot.expression)); } map.put(expr, slot); } slot.count++; return slot.parameterExpression != null ? slot.parameterExpression : slot.expression; }
public void testWriteConstant() { // primitives assertEquals( "new int[] {\n" + " 1,\n" + " 2,\n" + " -1}", Expressions.toString(Expressions.constant(new int[] {1, 2, -1}))); // objects and nulls assertEquals( "new String[] {\n" + " \"foo\",\n" + " null}", Expressions.toString(Expressions.constant(new String[] {"foo", null}))); // automatically call constructor if it matches fields assertEquals( "new net.hydromatic.linq4j.test.Linq4jTest.Employee[] {\n" + " new net.hydromatic.linq4j.test.Linq4jTest.Employee(\n" + " 100,\n" + " \"Fred\",\n" + " 10),\n" + " new net.hydromatic.linq4j.test.Linq4jTest.Employee(\n" + " 110,\n" + " \"Bill\",\n" + " 30),\n" + " new net.hydromatic.linq4j.test.Linq4jTest.Employee(\n" + " 120,\n" + " \"Eric\",\n" + " 10),\n" + " new net.hydromatic.linq4j.test.Linq4jTest.Employee(\n" + " 130,\n" + " \"Janet\",\n" + " 10)}", Expressions.toString(Expressions.constant(Linq4jTest.emps))); }
public Result implement(EnumerableRelImplementor implementor, Prefer pref) { logger.debug("implementing enumerable"); final DrillImplementor drillImplementor = new DrillImplementor(this.client == null); DrillRel input = (DrillRel) getChild(); drillImplementor.go(input); String plan = drillImplementor.getJsonString(); Hook.LOGICAL_PLAN.run(plan); // not quite sure where this list was supposed to be set earlier, leaving it null got me back // the full result set final List<String> fieldNameList = rowType.getFieldNames(); // final List<String> fieldNameList = null; BlockStatement expr = new BlockBuilder() .append( Expressions.call( OF_METHOD, Expressions.constant(plan), // Expressions.call( Arrays.class, "asList", // Expressions.newArrayInit( String.class, Functions.apply(fieldNameList, TO_LITERAL)) // ), Expressions.constant(Object.class), // Expressions.variable(DataContext.class, "root"))) .toBlock(); final PhysType physType = PhysTypeImpl.of( implementor.getTypeFactory(), getRowType(), pref.prefer(JavaRowFormat.ARRAY)); return new Result(expr, physType, JavaRowFormat.ARRAY); }
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); }
public RexNode toRex(Expression expression) { switch (expression.getNodeType()) { case MemberAccess: return rexBuilder.makeFieldAccess( toRex(((MemberExpression) expression).expression), ((MemberExpression) expression).field.getName()); case GreaterThan: return binary(expression, SqlStdOperatorTable.greaterThanOperator); case LessThan: return binary(expression, SqlStdOperatorTable.lessThanOperator); case Parameter: return parameter((ParameterExpression) expression); case Call: MethodCallExpression call = (MethodCallExpression) expression; SqlOperator operator = RexToLixTranslator.JAVA_TO_SQL_METHOD_MAP.get(call.method); if (operator != null) { return rexBuilder.makeCall( operator, toRex( Expressions.<Expression>list() .appendIfNotNull(call.targetExpression) .appendAll(call.expressions))); } throw new RuntimeException("Could translate call to method " + call.method); case Constant: final ConstantExpression constant = (ConstantExpression) expression; Object value = constant.value; if (value instanceof Number) { Number number = (Number) value; if (value instanceof Double || value instanceof Float) { return rexBuilder.makeApproxLiteral(BigDecimal.valueOf(number.doubleValue())); } else if (value instanceof BigDecimal) { return rexBuilder.makeExactLiteral((BigDecimal) value); } else { return rexBuilder.makeExactLiteral(BigDecimal.valueOf(number.longValue())); } } else if (value instanceof Boolean) { return rexBuilder.makeLiteral((Boolean) value); } else { return rexBuilder.makeLiteral(constant.toString()); } default: throw new UnsupportedOperationException( "unknown expression type " + expression.getNodeType() + " " + expression); } }
public void testBlockBuilder() { BlockBuilder statements = new BlockBuilder(); Expression one = statements.append("one", Expressions.constant(1)); Expression two = statements.append("two", Expressions.constant(2)); Expression three = statements.append("three", Expressions.add(one, two)); Expression six = statements.append("six", Expressions.multiply(three, two)); Expression eighteen = statements.append("eighteen", Expressions.multiply(three, six)); statements.add(Expressions.return_(null, eighteen)); assertEquals( "{\n" + " final int three = 1 + 2;\n" + " final int six = three * 2;\n" + " final int eighteen = three * six;\n" + " return eighteen;\n" + "}\n", Expressions.toString(statements.toBlock())); }
public void testBlockBuilder2() { BlockBuilder statements = new BlockBuilder(); Expression element = statements.append("element", Expressions.constant(null)); Expression comparator = statements.append("comparator", Expressions.constant(null, Comparator.class)); Expression treeSet = statements.append("treeSet", Expressions.new_(TreeSet.class, Arrays.asList(comparator))); statements.add(Expressions.return_(null, Expressions.call(treeSet, "add", element))); assertEquals( "{\n" + " final java.util.Comparator comparator = null;\n" + " final java.util.TreeSet treeSet = new java.util.TreeSet(\n" + " comparator);\n" + " return treeSet.add(null);\n" + "}\n", Expressions.toString(statements.toBlock())); }
/** * Once you have a Result of implementing a child relational expression, call this method to * create a Builder to implement the current relational expression by adding additional clauses * to the SQL query. * * <p>You need to declare which clauses you intend to add. If the clauses are "later", you can * add to the same query. For example, "GROUP BY" comes after "WHERE". But if they are the same * or earlier, this method will start a new SELECT that wraps the previous result. * * <p>When you have called {@link Builder#setSelect(org.eigenbase.sql.SqlNodeList)}, {@link * Builder#setWhere(org.eigenbase.sql.SqlNode)} etc. call {@link * Builder#result(org.eigenbase.sql.SqlNode, java.util.Collection, org.eigenbase.rel.RelNode)} * to fix the new query. * * @param rel Relational expression being implemented * @param clauses Clauses that will be generated to implement current relational expression * @return A builder */ public Builder builder(JdbcRel rel, Clause... clauses) { final Clause maxClause = maxClause(); boolean needNew = false; for (Clause clause : clauses) { if (maxClause.ordinal() >= clause.ordinal()) { needNew = true; } } SqlSelect select; Expressions.FluentList<Clause> clauseList = Expressions.list(); if (needNew) { select = subSelect(); } else { select = asSelect(); clauseList.addAll(this.clauses); } clauseList.addAll(Arrays.asList(clauses)); Context newContext; final SqlNodeList selectList = select.getSelectList(); if (selectList != null) { newContext = new Context(selectList.size()) { @Override public SqlNode field(int ordinal) { final SqlNode selectItem = selectList.get(ordinal); switch (selectItem.getKind()) { case AS: return ((SqlCall) selectItem).operand(0); } return selectItem; } }; } else { newContext = new AliasContext(aliases, aliases.size() > 1); } return new Builder(rel, clauseList, select, newContext); }
public void testWriteWhile() { DeclarationExpression xDecl, yDecl; Node node = Expressions.block( xDecl = Expressions.declare(0, "x", Expressions.constant(10)), yDecl = Expressions.declare(0, "y", Expressions.constant(0)), Expressions.while_( Expressions.lessThan(xDecl.parameter, Expressions.constant(5)), Expressions.statement(Expressions.preIncrementAssign(yDecl.parameter)))); assertEquals( "{\n" + " int x = 10;\n" + " int y = 0;\n" + " while (x < 5) {\n" + " ++y;\n" + " }\n" + "}\n", Expressions.toString(node)); }
@Override public Expression getExpression(SchemaPlus parentSchema, String name) { return Expressions.call(DataContext.ROOT, BuiltinMethod.DATA_CONTEXT_GET_ROOT_SCHEMA.method); }
/** Converts an expression from {@link RexNode} to {@link SqlNode} format. */ SqlNode toSql(RexProgram program, RexNode rex) { if (rex instanceof RexLocalRef) { final int index = ((RexLocalRef) rex).getIndex(); return toSql(program, program.getExprList().get(index)); } else if (rex instanceof RexInputRef) { return field(((RexInputRef) rex).getIndex()); } else if (rex instanceof RexLiteral) { final RexLiteral literal = (RexLiteral) rex; switch (literal.getTypeName().getFamily()) { case CHARACTER: return SqlLiteral.createCharString((String) literal.getValue2(), POS); case NUMERIC: case EXACT_NUMERIC: return SqlLiteral.createExactNumeric(literal.getValue().toString(), POS); case APPROXIMATE_NUMERIC: return SqlLiteral.createApproxNumeric(literal.getValue().toString(), POS); case BOOLEAN: return SqlLiteral.createBoolean((Boolean) literal.getValue(), POS); case DATE: return SqlLiteral.createDate((Calendar) literal.getValue(), POS); case TIME: return SqlLiteral.createTime( (Calendar) literal.getValue(), literal.getType().getPrecision(), POS); case TIMESTAMP: return SqlLiteral.createTimestamp( (Calendar) literal.getValue(), literal.getType().getPrecision(), POS); case ANY: switch (literal.getTypeName()) { case NULL: return SqlLiteral.createNull(POS); // fall through } default: throw new AssertionError(literal + ": " + literal.getTypeName()); } } else if (rex instanceof RexCall) { final RexCall call = (RexCall) rex; final SqlOperator op = call.getOperator(); final List<SqlNode> nodeList = toSql(program, call.getOperands()); if (op == SqlStdOperatorTable.CAST) { RelDataType type = call.getType(); if (type.getSqlTypeName() == SqlTypeName.VARCHAR && dialect.getDatabaseProduct() == SqlDialect.DatabaseProduct.MYSQL) { // MySQL doesn't have a VARCHAR type, only CHAR. nodeList.add( new SqlDataTypeSpec( new SqlIdentifier("CHAR", POS), type.getPrecision(), -1, null, null, POS)); } else { nodeList.add(toSql(type)); } } if (op == SqlStdOperatorTable.CASE) { final SqlNode valueNode; final List<SqlNode> whenList = Expressions.list(); final List<SqlNode> thenList = Expressions.list(); final SqlNode elseNode; if (nodeList.size() % 2 == 0) { // switched: // "case x when v1 then t1 when v2 then t2 ... else e end" valueNode = nodeList.get(0); for (int i = 1; i < nodeList.size() - 1; i += 2) { whenList.add(nodeList.get(i)); thenList.add(nodeList.get(i + 1)); } } else { // other: "case when w1 then t1 when w2 then t2 ... else e end" valueNode = null; for (int i = 0; i < nodeList.size() - 1; i += 2) { whenList.add(nodeList.get(i)); thenList.add(nodeList.get(i + 1)); } } elseNode = nodeList.get(nodeList.size() - 1); return op.createCall( POS, valueNode, new SqlNodeList(whenList, POS), new SqlNodeList(thenList, POS), elseNode); } if (op instanceof SqlBinaryOperator && nodeList.size() > 2) { // In RexNode trees, OR and AND have any number of children; // SqlCall requires exactly 2. So, convert to a left-deep binary tree. return createLeftCall(op, nodeList); } return op.createCall(new SqlNodeList(nodeList, POS)); } else { throw new AssertionError(rex); } }
/** * Converts a schema expression to a given type by calling the {@link * net.hydromatic.optiq.SchemaPlus#unwrap(Class)} method. */ public static Expression unwrap(Expression call, Class type) { return Expressions.convert_( Expressions.call(call, BuiltinMethod.SCHEMA_PLUS_UNWRAP.method, Expressions.constant(type)), type); }
public void testWrite() { assertEquals( "1 + 2.0F + 3L + Long.valueOf(4L)", Expressions.toString( Expressions.add( Expressions.add( Expressions.add(Expressions.constant(1), Expressions.constant(2F, Float.TYPE)), Expressions.constant(3L, Long.TYPE)), Expressions.constant(4L, Long.class)))); assertEquals( "new java.math.BigDecimal(31415926L, 7)", Expressions.toString(Expressions.constant(BigDecimal.valueOf(314159260, 8)))); // Parentheses needed, to override the left-associativity of +. assertEquals( "1 + (2 + 3)", Expressions.toString( Expressions.add( Expressions.constant(1), Expressions.add(Expressions.constant(2), Expressions.constant(3))))); // No parentheses needed; higher precedence of * achieves the desired // effect. assertEquals( "1 + 2 * 3", Expressions.toString( Expressions.add( Expressions.constant(1), Expressions.multiply(Expressions.constant(2), Expressions.constant(3))))); assertEquals( "1 * (2 + 3)", Expressions.toString( Expressions.multiply( Expressions.constant(1), Expressions.add(Expressions.constant(2), Expressions.constant(3))))); // Parentheses needed, to overcome right-associativity of =. assertEquals( "(1 = 2) = 3", Expressions.toString( Expressions.assign( Expressions.assign(Expressions.constant(1), Expressions.constant(2)), Expressions.constant(3)))); // Ternary operator. assertEquals( "1 < 2 ? (3 < 4 ? 5 : 6) : 7 < 8 ? 9 : 10", Expressions.toString( Expressions.condition( Expressions.lessThan(Expressions.constant(1), Expressions.constant(2)), Expressions.condition( Expressions.lessThan(Expressions.constant(3), Expressions.constant(4)), Expressions.constant(5), Expressions.constant(6)), Expressions.condition( Expressions.lessThan(Expressions.constant(7), Expressions.constant(8)), Expressions.constant(9), Expressions.constant(10))))); assertEquals( "0 + (double) (2 + 3)", Expressions.toString( Expressions.add( Expressions.constant(0), Expressions.convert_( Expressions.add(Expressions.constant(2), Expressions.constant(3)), Double.TYPE)))); assertEquals( "a.empno", Expressions.toString( Expressions.field(Expressions.parameter(Linq4jTest.Employee.class, "a"), "empno"))); assertEquals( "java.util.Collections.EMPTY_LIST", Expressions.toString(Expressions.field(null, Collections.class, "EMPTY_LIST"))); final ParameterExpression paramX = Expressions.parameter(String.class, "x"); assertEquals( "new net.hydromatic.linq4j.function.Function1() {\n" + " public int apply(String x) {\n" + " return x.length();\n" + " }\n" + " public Object apply(Object x) {\n" + " return apply(\n" + " (String) x);\n" + " }\n" + "}\n", Expressions.toString( Expressions.lambda( Function1.class, Expressions.call(paramX, "length", Collections.<Expression>emptyList()), Arrays.asList(paramX)))); assertEquals( "new String[] {\n" + " \"foo\",\n" + " null,\n" + " \"bar\\\"baz\"}", Expressions.toString( Expressions.newArrayInit( String.class, Arrays.<Expression>asList( Expressions.constant("foo"), Expressions.constant(null), Expressions.constant("bar\"baz"))))); assertEquals( "(int) ((String) (Object) \"foo\").length()", Expressions.toString( Expressions.convert_( Expressions.call( Expressions.convert_( Expressions.convert_(Expressions.constant("foo"), Object.class), String.class), "length", Collections.<Expression>emptyList()), Integer.TYPE))); // resolving a static method assertEquals( "Integer.valueOf(\"0123\")", Expressions.toString( Expressions.call( Integer.class, "valueOf", Collections.<Expression>singletonList(Expressions.constant("0123"))))); // precedence of not and instanceof assertEquals( "!(o instanceof String)", Expressions.toString( Expressions.not( Expressions.typeIs(Expressions.parameter(Object.class, "o"), String.class)))); // not not assertEquals( "!!(o instanceof String)", Expressions.toString( Expressions.not( Expressions.not( Expressions.typeIs(Expressions.parameter(Object.class, "o"), String.class))))); }
public void testReturn() { assertEquals( "if (true) {\n" + " return;\n" + "}\n", Expressions.toString( Expressions.ifThen(Expressions.constant(true), Expressions.return_(null)))); }
/** * Creates a result based on a join. (Each join could contain one or more relational expressions.) */ public Result result(SqlNode join, Result leftResult, Result rightResult) { final List<Pair<String, RelDataType>> list = new ArrayList<Pair<String, RelDataType>>(); list.addAll(leftResult.aliases); list.addAll(rightResult.aliases); return new Result(join, Expressions.list(Clause.FROM), null, list); }
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)); }
@Override public Expression apply(String a0) { return Expressions.constant(a0); }