use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.
the class PageProcessorBenchmark method rowExpression.
private RowExpression rowExpression(String value) {
Expression expression = createExpression(value, METADATA, TypeProvider.copyOf(symbolTypes));
Map<NodeRef<Expression>, Type> expressionTypes = TYPE_ANALYZER.getTypes(TEST_SESSION, TypeProvider.copyOf(symbolTypes), expression);
return SqlToRowExpressionTranslator.translate(expression, SCALAR, expressionTypes, sourceLayout, METADATA.getFunctionAndTypeManager(), TEST_SESSION, true);
}
use of io.prestosql.sql.tree.NodeRef 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.tree.NodeRef in project hetu-core by openlookeng.
the class QueryPlanner method coerce.
/**
* Creates a projection with any additional coercions by identity of the provided expressions.
*
* @return the new subplan and a mapping of each expression to the symbol representing the coercion or an existing symbol if a coercion wasn't needed
*/
public static PlanAndMappings coerce(PlanBuilder inputSubPlan, List<Expression> expressions, Analysis analysis, PlanNodeIdAllocator idAllocator, PlanSymbolAllocator planSymbolAllocator, TypeCoercion typeCoercion) {
PlanBuilder subPlan = inputSubPlan;
Assignments.Builder assignments = Assignments.builder();
assignments.putAll(AssignmentUtils.identityAsSymbolReferences(subPlan.getRoot().getOutputSymbols()));
ImmutableMap.Builder<NodeRef<Expression>, Symbol> mappings = ImmutableMap.builder();
for (Expression expression : expressions) {
Type coercion = analysis.getCoercion(expression);
if (coercion != null) {
Type type = analysis.getType(expression);
Symbol symbol = planSymbolAllocator.newSymbol(expression, coercion);
assignments.put(symbol, castToRowExpression(new Cast(subPlan.rewrite(expression), coercion.getTypeSignature().toString(), false, typeCoercion.isTypeOnlyCoercion(type, coercion))));
mappings.put(NodeRef.of(expression), symbol);
} else {
mappings.put(NodeRef.of(expression), subPlan.translate(expression));
}
}
subPlan = subPlan.withNewRoot(new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), assignments.build()));
return new PlanAndMappings(subPlan, mappings.build());
}
use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.
the class ExpressionInterpreter method evaluateConstantExpression.
public static Object evaluateConstantExpression(Expression expression, Type expectedType, Metadata metadata, Session session, List<Expression> parameters) {
ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP);
analyzer.analyze(expression, Scope.create());
Type actualType = analyzer.getExpressionTypes().get(NodeRef.of(expression));
if (!new TypeCoercion(metadata::getType).canCoerce(actualType, expectedType)) {
throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, String.format("Cannot cast type %s to %s", actualType.getTypeSignature(), expectedType.getTypeSignature()));
}
Map<NodeRef<Expression>, Type> coercions = ImmutableMap.<NodeRef<Expression>, Type>builder().putAll(analyzer.getExpressionCoercions()).put(NodeRef.of(expression), expectedType).build();
return evaluateConstantExpression(expression, coercions, analyzer.getTypeOnlyCoercions(), metadata, session, ImmutableSet.of(), parameters);
}
use of io.prestosql.sql.tree.NodeRef 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);
}
Aggregations