Search in sources :

Example 11 with NodeRef

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;
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) RelationType(io.trino.sql.analyzer.RelationType) CharType(io.trino.spi.type.CharType) UnknownType(io.trino.type.UnknownType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) LambdaArgumentDeclaration(io.trino.sql.tree.LambdaArgumentDeclaration) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with NodeRef

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);
}
Also used : Cast(io.trino.sql.tree.Cast) NodeRef(io.trino.sql.tree.NodeRef) RelationType(io.trino.sql.analyzer.RelationType) ExpressionAnalyzer.isNumericType(io.trino.sql.analyzer.ExpressionAnalyzer.isNumericType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SelectExpression(io.trino.sql.analyzer.Analysis.SelectExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) LambdaExpression(io.trino.sql.tree.LambdaExpression) Assignments(io.trino.sql.planner.plan.Assignments) ProjectNode(io.trino.sql.planner.plan.ProjectNode)

Example 13 with NodeRef

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);
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) CharType.createCharType(io.trino.spi.type.CharType.createCharType) Type(io.trino.spi.type.Type) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) VarbinaryType(io.trino.spi.type.VarbinaryType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) ExpressionInterpreter(io.trino.sql.planner.ExpressionInterpreter)

Example 14 with NodeRef

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)));
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) Type(io.trino.spi.type.Type) ExpressionInterpreter(io.trino.sql.planner.ExpressionInterpreter)

Example 15 with NodeRef

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);
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) Type(io.trino.spi.type.Type) LiteralEncoder(io.trino.sql.planner.LiteralEncoder) ExpressionInterpreter(io.trino.sql.planner.ExpressionInterpreter)

Aggregations

NodeRef (io.trino.sql.tree.NodeRef)27 Type (io.trino.spi.type.Type)19 Expression (io.trino.sql.tree.Expression)18 List (java.util.List)9 Map (java.util.Map)9 Objects.requireNonNull (java.util.Objects.requireNonNull)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 ImmutableMap (com.google.common.collect.ImmutableMap)8 Optional (java.util.Optional)8 ImmutableList (com.google.common.collect.ImmutableList)7 Session (io.trino.Session)7 PlannerContext (io.trino.sql.PlannerContext)7 ExpressionInterpreter (io.trino.sql.planner.ExpressionInterpreter)7 HashMap (java.util.HashMap)7 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)6 TrinoException (io.trino.spi.TrinoException)5 Parameter (io.trino.sql.tree.Parameter)5 LinkedHashMap (java.util.LinkedHashMap)5 Set (java.util.Set)5 Verify.verify (com.google.common.base.Verify.verify)4