use of io.prestosql.sql.planner.ExpressionInterpreter in project hetu-core by openlookeng.
the class FilterStatsCalculator method simplifyExpression.
private Expression simplifyExpression(Session session, Expression predicate, TypeProvider types) {
// TODO reuse io.prestosql.sql.planner.iterative.rule.SimplifyExpressions.rewrite
Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, predicate, types);
ExpressionInterpreter interpreter = ExpressionInterpreter.expressionOptimizer(predicate, metadata, session, expressionTypes);
Object value = interpreter.optimize(NoOpSymbolResolver.INSTANCE);
if (value == null) {
// Expression evaluates to SQL null, which in Filter is equivalent to false. This assumes the expression is a top-level expression (eg. not in NOT).
value = false;
}
return literalEncoder.toExpression(value, BOOLEAN);
}
use of io.prestosql.sql.planner.ExpressionInterpreter in project hetu-core by openlookeng.
the class FunctionAssertions method interpret.
private Object interpret(Expression expression, Type expectedType, Session session) {
Map<NodeRef<Expression>, Type> expressionTypes = typeAnalyzer.getTypes(session, TypeProvider.copyOf(INPUT_TYPES), expression);
ExpressionInterpreter evaluator = ExpressionInterpreter.expressionInterpreter(expression, metadata, session, expressionTypes);
Object result = evaluator.evaluate(symbol -> {
int position = 0;
int channel = INPUT_MAPPING.get(symbol);
Type type = INPUT_TYPES.get(symbol);
Block block = SOURCE_PAGE.getBlock(channel);
if (block.isNull(position)) {
return null;
}
Class<?> javaType = type.getJavaType();
if (javaType == boolean.class) {
return type.getBoolean(block, position);
} else if (javaType == long.class) {
return type.getLong(block, position);
} else if (javaType == double.class) {
return type.getDouble(block, position);
} else if (javaType == Slice.class) {
return type.getSlice(block, position);
} else if (javaType == Block.class) {
return type.getObject(block, position);
} else {
throw new UnsupportedOperationException("not yet implemented");
}
});
// convert result from stack type to Type ObjectValue
Block block = Utils.nativeValueToBlock(expectedType, result);
return expectedType.getObjectValue(session.toConnectorSession(), block, 0);
}
use of io.prestosql.sql.planner.ExpressionInterpreter in project hetu-core by openlookeng.
the class SimplifyExpressions method rewrite.
public static Expression rewrite(Expression inputExpression, Session session, PlanSymbolAllocator planSymbolAllocator, Metadata metadata, LiteralEncoder literalEncoder, TypeAnalyzer typeAnalyzer) {
Expression expression = inputExpression;
requireNonNull(metadata, "metadata is null");
requireNonNull(typeAnalyzer, "typeAnalyzer is null");
if (expression instanceof SymbolReference) {
return expression;
}
expression = pushDownNegations(expression);
expression = extractCommonPredicates(expression);
Map<NodeRef<Expression>, Type> expressionTypes = typeAnalyzer.getTypes(session, planSymbolAllocator.getTypes(), expression);
ExpressionInterpreter interpreter = ExpressionInterpreter.expressionOptimizer(expression, metadata, session, expressionTypes);
Object optimized = interpreter.optimize(NoOpSymbolResolver.INSTANCE);
return literalEncoder.toExpression(optimized, expressionTypes.get(NodeRef.of(expression)));
}
use of io.prestosql.sql.planner.ExpressionInterpreter in project hetu-core by openlookeng.
the class TestingRowExpressionTranslator method simplifyExpression.
Expression simplifyExpression(Expression expression) {
// Testing simplified expressions is important, since simplification may create CASTs or function calls that cannot be simplified by the ExpressionOptimizer
Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(expression, TypeProvider.empty());
ExpressionInterpreter interpreter = ExpressionInterpreter.expressionOptimizer(expression, metadata, TEST_SESSION, expressionTypes);
Object value = interpreter.optimize(NoOpSymbolResolver.INSTANCE);
return literalEncoder.toExpression(value, expressionTypes.get(NodeRef.of(expression)));
}
use of io.prestosql.sql.planner.ExpressionInterpreter in project hetu-core by openlookeng.
the class TestExpressionInterpreter method optimize.
private static Object optimize(@Language("SQL") String expression) {
assertRoundTrip(expression);
Expression parsedExpression = FunctionAssertions.createExpression(expression, METADATA, SYMBOL_TYPES);
Map<NodeRef<Expression>, Type> expressionTypes = TYPE_ANALYZER.getTypes(TEST_SESSION, SYMBOL_TYPES, parsedExpression);
ExpressionInterpreter interpreter = expressionOptimizer(parsedExpression, METADATA, TEST_SESSION, expressionTypes);
return interpreter.optimize(symbol -> {
switch(symbol.getName().toLowerCase(ENGLISH)) {
case "bound_integer":
return 1234L;
case "bound_long":
return 1234L;
case "bound_string":
return utf8Slice("hello");
case "bound_double":
return 12.34;
case "bound_date":
return new LocalDate(2001, 8, 22).toDateMidnight(UTC).getMillis();
case "bound_time":
return new LocalTime(3, 4, 5, 321).toDateTime(new DateTime(0, UTC)).getMillis();
case "bound_timestamp":
return new DateTime(2001, 8, 22, 3, 4, 5, 321, UTC).getMillis();
case "bound_pattern":
return utf8Slice("%el%");
case "bound_timestamp_with_timezone":
return new SqlTimestampWithTimeZone(new DateTime(1970, 1, 1, 1, 0, 0, 999, UTC).getMillis(), getTimeZoneKey("Z"));
case "bound_varbinary":
return Slices.wrappedBuffer((byte) 0xab);
case "bound_decimal_short":
return 12345L;
case "bound_decimal_long":
return Decimals.encodeUnscaledValue(new BigInteger("12345678901234567890123"));
default:
break;
}
return toSymbolReference(symbol);
});
}
Aggregations