本文整理汇总了Java中org.apache.calcite.linq4j.tree.ConstantExpression类的典型用法代码示例。如果您正苦于以下问题:Java ConstantExpression类的具体用法?Java ConstantExpression怎么用?Java ConstantExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConstantExpression类属于org.apache.calcite.linq4j.tree包,在下文中一共展示了ConstantExpression类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: implement
import org.apache.calcite.linq4j.tree.ConstantExpression; //导入依赖的package包/类
public Expression implement(RexToLixTranslator translator, RexCall call,
List<Expression> translatedOperands) {
final Object value =
((ConstantExpression) translatedOperands.get(0)).value;
SqlTrimFunction.Flag flag = (SqlTrimFunction.Flag) value;
return Expressions.call(
BuiltInMethod.TRIM.method,
Expressions.constant(
flag == SqlTrimFunction.Flag.BOTH
|| flag == SqlTrimFunction.Flag.LEADING),
Expressions.constant(
flag == SqlTrimFunction.Flag.BOTH
|| flag == SqlTrimFunction.Flag.TRAILING),
translatedOperands.get(1),
translatedOperands.get(2));
}
示例2: testAssign2
import org.apache.calcite.linq4j.tree.ConstantExpression; //导入依赖的package包/类
@Test public void testAssign2() {
// long x = 0;
// final long y = System.currentTimeMillis();
// if (System.currentTimeMillis() > 0) {
// x = y;
// }
//
// Make sure we don't fold two calls to System.currentTimeMillis into one.
final ParameterExpression x_ = Expressions.parameter(long.class, "x");
final ParameterExpression y_ = Expressions.parameter(long.class, "y");
final Method mT = Linq4j.getMethod("java.lang.System", "currentTimeMillis");
final ConstantExpression zero = Expressions.constant(0L);
assertThat(
optimize(
Expressions.block(
Expressions.declare(0, x_, zero),
Expressions.declare(Modifier.FINAL, y_, Expressions.call(mT)),
Expressions.ifThen(
Expressions.greaterThan(Expressions.call(mT), zero),
Expressions.statement(Expressions.assign(x_, y_))))),
equalTo("{\n"
+ " long x = 0L;\n"
+ " if (System.currentTimeMillis() > 0L) {\n"
+ " x = System.currentTimeMillis();\n"
+ " }\n"
+ "}\n"));
}
示例3: testApproxConstant
import org.apache.calcite.linq4j.tree.ConstantExpression; //导入依赖的package包/类
/** We use BigDecimal to represent literals of float and double using
* BigDecimal, because we want an exact representation. */
@Test public void testApproxConstant() {
ConstantExpression c;
c = Expressions.constant(new BigDecimal("3.1"), float.class);
assertThat(Expressions.toString(c), equalTo("3.1F"));
c = Expressions.constant(new BigDecimal("-5.156"), float.class);
assertThat(Expressions.toString(c), equalTo("-5.156F"));
c = Expressions.constant(new BigDecimal("-51.6"), Float.class);
assertThat(Expressions.toString(c), equalTo("Float.valueOf(-51.6F)"));
c = Expressions.constant(new BigDecimal(Float.MAX_VALUE), Float.class);
assertThat(Expressions.toString(c),
equalTo("Float.valueOf(Float.intBitsToFloat(2139095039))"));
c = Expressions.constant(new BigDecimal(Float.MIN_VALUE), Float.class);
assertThat(Expressions.toString(c),
equalTo("Float.valueOf(Float.intBitsToFloat(1))"));
c = Expressions.constant(new BigDecimal("3.1"), double.class);
assertThat(Expressions.toString(c), equalTo("3.1D"));
c = Expressions.constant(new BigDecimal("-5.156"), double.class);
assertThat(Expressions.toString(c), equalTo("-5.156D"));
c = Expressions.constant(new BigDecimal("-51.6"), Double.class);
assertThat(Expressions.toString(c), equalTo("Double.valueOf(-51.6D)"));
c = Expressions.constant(new BigDecimal(Double.MAX_VALUE), Double.class);
assertThat(Expressions.toString(c),
equalTo("Double.valueOf(Double.longBitsToDouble(9218868437227405311L))"));
c = Expressions.constant(new BigDecimal(Double.MIN_VALUE), Double.class);
assertThat(Expressions.toString(c),
equalTo("Double.valueOf(Double.longBitsToDouble(1L))"));
}
示例4: handleNull
import org.apache.calcite.linq4j.tree.ConstantExpression; //导入依赖的package包/类
/** Adapts an expression with "normal" result to one that adheres to
* this particular policy. Wraps the result expression into a new
* parameter if need be.
*
* @param input Expression
* @param nullAs If false, if expression is definitely not null at
* runtime. Therefore we can optimize. For example, we can cast to int
* using x.intValue().
* @return Translated expression
*/
public Expression handleNull(Expression input, RexImpTable.NullAs nullAs) {
final Expression nullHandled = nullAs.handle(input);
// If we get ConstantExpression, just return it (i.e. primitive false)
if (nullHandled instanceof ConstantExpression) {
return nullHandled;
}
// if nullHandled expression is the same as "input",
// then we can just reuse it
if (nullHandled == input) {
return input;
}
// If nullHandled is different, then it might be unsafe to compute
// early (i.e. unbox of null value should not happen _before_ ternary).
// Thus we wrap it into brand-new ParameterExpression,
// and we are guaranteed that ParameterExpression will not be shared
String unboxVarName = "v_unboxed";
if (input instanceof ParameterExpression) {
unboxVarName = ((ParameterExpression) input).name + "_unboxed";
}
ParameterExpression unboxed = Expressions.parameter(nullHandled.getType(),
list.newName(unboxVarName));
list.add(Expressions.declare(Modifier.FINAL, unboxed, nullHandled));
return unboxed;
}
示例5: toRex
import org.apache.calcite.linq4j.tree.ConstantExpression; //导入依赖的package包/类
public RexNode toRex(Expression expression) {
switch (expression.getNodeType()) {
case MemberAccess:
// Case-sensitive name match because name was previously resolved.
return rexBuilder.makeFieldAccess(
toRex(
((MemberExpression) expression).expression),
((MemberExpression) expression).field.getName(),
true);
case GreaterThan:
return binary(expression, SqlStdOperatorTable.GREATER_THAN);
case LessThan:
return binary(expression, SqlStdOperatorTable.LESS_THAN);
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(
type(call),
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);
}
}
示例6: testAssign
import org.apache.calcite.linq4j.tree.ConstantExpression; //导入依赖的package包/类
@Test public void testAssign() {
// long x = 0;
// final long y = System.currentTimeMillis();
// if (System.nanoTime() > 0) {
// x = y;
// }
// System.out.println(x);
//
// In bug https://github.com/julianhyde/linq4j/issues/27, this was
// incorrectly optimized to
//
// if (System.nanoTime() > 0L) {
// System.currentTimeMillis();
// }
// System.out.println(0L);
final ParameterExpression x_ = Expressions.parameter(long.class, "x");
final ParameterExpression y_ = Expressions.parameter(long.class, "y");
final Method mT = Linq4j.getMethod("java.lang.System", "currentTimeMillis");
final Method mNano = Linq4j.getMethod("java.lang.System", "nanoTime");
final ConstantExpression zero = Expressions.constant(0L);
assertThat(
optimize(
Expressions.block(
Expressions.declare(0, x_, zero),
Expressions.declare(Modifier.FINAL, y_, Expressions.call(mT)),
Expressions.ifThen(
Expressions.greaterThan(Expressions.call(mNano), zero),
Expressions.statement(Expressions.assign(x_, y_))),
Expressions.statement(
Expressions.call(
Expressions.field(null, System.class, "out"),
"println",
x_)))),
equalTo("{\n"
+ " long x = 0L;\n"
+ " if (System.nanoTime() > 0L) {\n"
+ " x = System.currentTimeMillis();\n"
+ " }\n"
+ " System.out.println(x);\n"
+ "}\n"));
}
示例7: translate
import org.apache.calcite.linq4j.tree.ConstantExpression; //导入依赖的package包/类
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());
}
示例8: visit
import org.apache.calcite.linq4j.tree.ConstantExpression; //导入依赖的package包/类
@Override public Void visit(ConstantExpression constantExpression) {
if (constantExpression.value instanceof Type) {
types.add((Type) constantExpression.value);
}
return super.visit(constantExpression);
}