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); } }
/** Creates a cluster. */ RelOptCluster( RelOptQuery query, RelOptPlanner planner, RelDataTypeFactory typeFactory, RexBuilder rexBuilder) { assert planner != null; assert typeFactory != null; this.query = query; this.planner = planner; this.typeFactory = typeFactory; this.rexBuilder = rexBuilder; this.originalExpression = rexBuilder.makeLiteral("?"); // set up a default rel metadata provider, // giving the planner first crack at everything metadataProvider = new DefaultRelMetadataProvider(); this.emptyTraitSet = planner.emptyTraitSet(); }