Search in sources :

Example 1 with ExpressionInterpreter

use of io.trino.sql.planner.ExpressionInterpreter in project trino by trinodb.

the class ExpressionUtils method constantExpressionEvaluatesSuccessfully.

private static boolean constantExpressionEvaluatesSuccessfully(PlannerContext plannerContext, Session session, Expression constantExpression) {
    Map<NodeRef<Expression>, Type> types = getExpressionTypes(plannerContext, session, constantExpression, TypeProvider.empty());
    ExpressionInterpreter interpreter = new ExpressionInterpreter(constantExpression, plannerContext, session, types);
    Object literalValue = interpreter.optimize(NoOpSymbolResolver.INSTANCE);
    return !(literalValue instanceof Expression);
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) Type(io.trino.spi.type.Type) RowDataType(io.trino.sql.tree.RowDataType) GenericDataType(io.trino.sql.tree.GenericDataType) Expression(io.trino.sql.tree.Expression) LambdaExpression(io.trino.sql.tree.LambdaExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) ExpressionInterpreter(io.trino.sql.planner.ExpressionInterpreter)

Example 2 with ExpressionInterpreter

use of io.trino.sql.planner.ExpressionInterpreter in project trino by trinodb.

the class SimplifyExpressions method rewrite.

public static Expression rewrite(Expression expression, Session session, SymbolAllocator symbolAllocator, PlannerContext plannerContext, TypeAnalyzer typeAnalyzer) {
    requireNonNull(plannerContext, "plannerContext is null");
    requireNonNull(typeAnalyzer, "typeAnalyzer is null");
    if (expression instanceof SymbolReference) {
        return expression;
    }
    Map<NodeRef<Expression>, Type> expressionTypes = typeAnalyzer.getTypes(session, symbolAllocator.getTypes(), expression);
    expression = pushDownNegations(plannerContext.getMetadata(), expression, expressionTypes);
    expression = extractCommonPredicates(plannerContext.getMetadata(), expression);
    expression = normalizeOrExpression(expression);
    expressionTypes = typeAnalyzer.getTypes(session, symbolAllocator.getTypes(), expression);
    ExpressionInterpreter interpreter = new ExpressionInterpreter(expression, plannerContext, session, expressionTypes);
    Object optimized = interpreter.optimize(NoOpSymbolResolver.INSTANCE);
    return new LiteralEncoder(plannerContext).toExpression(session, optimized, expressionTypes.get(NodeRef.of(expression)));
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) Type(io.trino.spi.type.Type) SymbolReference(io.trino.sql.tree.SymbolReference) LiteralEncoder(io.trino.sql.planner.LiteralEncoder) ExpressionInterpreter(io.trino.sql.planner.ExpressionInterpreter)

Example 3 with ExpressionInterpreter

use of io.trino.sql.planner.ExpressionInterpreter 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 4 with ExpressionInterpreter

use of io.trino.sql.planner.ExpressionInterpreter 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 5 with ExpressionInterpreter

use of io.trino.sql.planner.ExpressionInterpreter 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

Type (io.trino.spi.type.Type)7 ExpressionInterpreter (io.trino.sql.planner.ExpressionInterpreter)7 NodeRef (io.trino.sql.tree.NodeRef)7 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)4 CharType.createCharType (io.trino.spi.type.CharType.createCharType)2 VarbinaryType (io.trino.spi.type.VarbinaryType)2 VarcharType.createVarcharType (io.trino.spi.type.VarcharType.createVarcharType)2 LiteralEncoder (io.trino.sql.planner.LiteralEncoder)2 Slice (io.airlift.slice.Slice)1 BlockAssertions.createBooleansBlock (io.trino.block.BlockAssertions.createBooleansBlock)1 BlockAssertions.createDoublesBlock (io.trino.block.BlockAssertions.createDoublesBlock)1 BlockAssertions.createIntsBlock (io.trino.block.BlockAssertions.createIntsBlock)1 BlockAssertions.createLongDecimalsBlock (io.trino.block.BlockAssertions.createLongDecimalsBlock)1 BlockAssertions.createLongsBlock (io.trino.block.BlockAssertions.createLongsBlock)1 BlockAssertions.createRowBlock (io.trino.block.BlockAssertions.createRowBlock)1 BlockAssertions.createShortDecimalsBlock (io.trino.block.BlockAssertions.createShortDecimalsBlock)1 BlockAssertions.createSlicesBlock (io.trino.block.BlockAssertions.createSlicesBlock)1 BlockAssertions.createStringsBlock (io.trino.block.BlockAssertions.createStringsBlock)1 BlockAssertions.createTimestampsWithTimeZoneMillisBlock (io.trino.block.BlockAssertions.createTimestampsWithTimeZoneMillisBlock)1 Block (io.trino.spi.block.Block)1