use of io.trino.sql.tree.NodeRef in project trino by trinodb.
the class LogicalPlanner method buildLambdaDeclarationToSymbolMap.
private static Map<NodeRef<LambdaArgumentDeclaration>, Symbol> buildLambdaDeclarationToSymbolMap(Analysis analysis, SymbolAllocator symbolAllocator) {
Map<Key, Symbol> allocations = new HashMap<>();
Map<NodeRef<LambdaArgumentDeclaration>, Symbol> result = new LinkedHashMap<>();
for (Entry<NodeRef<Expression>, Type> entry : analysis.getTypes().entrySet()) {
if (!(entry.getKey().getNode() instanceof LambdaArgumentDeclaration)) {
continue;
}
LambdaArgumentDeclaration argument = (LambdaArgumentDeclaration) entry.getKey().getNode();
Key key = new Key(argument, entry.getValue());
// Allocate the same symbol for all lambda argument names with a given type. This is needed to be able to
// properly identify multiple instances of syntactically equal lambda expressions during planning as expressions
// get rewritten via TranslationMap
Symbol symbol = allocations.get(key);
if (symbol == null) {
symbol = symbolAllocator.newSymbol(argument, entry.getValue());
allocations.put(key, symbol);
}
result.put(NodeRef.of(argument), symbol);
}
return result;
}
use of io.trino.sql.tree.NodeRef in project trino by trinodb.
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 subPlan, List<Expression> expressions, Analysis analysis, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator, TypeCoercion typeCoercion) {
Assignments.Builder assignments = Assignments.builder();
assignments.putIdentities(subPlan.getRoot().getOutputSymbols());
Map<NodeRef<Expression>, Symbol> mappings = new HashMap<>();
for (Expression expression : expressions) {
Type coercion = analysis.getCoercion(expression);
// expressions may be repeated, for example, when resolving ordinal references in a GROUP BY clause
if (!mappings.containsKey(NodeRef.of(expression))) {
if (coercion != null) {
Type type = analysis.getType(expression);
Symbol symbol = symbolAllocator.newSymbol(expression, coercion);
assignments.put(symbol, new Cast(subPlan.rewrite(expression), toSqlType(coercion), 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);
}
use of io.trino.sql.tree.NodeRef in project trino by trinodb.
the class TestExpressionInterpreter method optimize.
static Object optimize(Expression parsedExpression) {
Map<NodeRef<Expression>, Type> expressionTypes = getTypes(TEST_SESSION, PLANNER_CONTEXT, SYMBOL_TYPES, parsedExpression);
ExpressionInterpreter interpreter = new ExpressionInterpreter(parsedExpression, PLANNER_CONTEXT, TEST_SESSION, expressionTypes);
return interpreter.optimize(INPUTS);
}
use of io.trino.sql.tree.NodeRef in project trino by trinodb.
the class TestSqlToRowExpressionTranslator method simplifyExpression.
private 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);
ExpressionInterpreter interpreter = new ExpressionInterpreter(expression, PLANNER_CONTEXT, TEST_SESSION, expressionTypes);
Object value = interpreter.optimize(NoOpSymbolResolver.INSTANCE);
return literalEncoder.toExpression(TEST_SESSION, value, expressionTypes.get(NodeRef.of(expression)));
}
use of io.trino.sql.tree.NodeRef in project trino by trinodb.
the class FilterStatsCalculator method simplifyExpression.
private Expression simplifyExpression(Session session, Expression predicate, TypeProvider types) {
// TODO reuse io.trino.sql.planner.iterative.rule.SimplifyExpressions.rewrite
Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(plannerContext, session, predicate, types);
ExpressionInterpreter interpreter = new ExpressionInterpreter(predicate, plannerContext, 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 new LiteralEncoder(plannerContext).toExpression(session, value, BOOLEAN);
}
Aggregations