Search in sources :

Example 1 with ExpressionInterpreter

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);
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) VarcharType(io.prestosql.spi.type.VarcharType) RowExpressionInterpreter(io.prestosql.sql.planner.RowExpressionInterpreter) ExpressionInterpreter(io.prestosql.sql.planner.ExpressionInterpreter)

Example 2 with ExpressionInterpreter

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);
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) Slice(io.airlift.slice.Slice) ExpressionInterpreter(io.prestosql.sql.planner.ExpressionInterpreter) BlockAssertions.createSlicesBlock(io.prestosql.block.BlockAssertions.createSlicesBlock) BlockAssertions.createStringsBlock(io.prestosql.block.BlockAssertions.createStringsBlock) BlockAssertions.createBooleansBlock(io.prestosql.block.BlockAssertions.createBooleansBlock) BlockAssertions.createLongsBlock(io.prestosql.block.BlockAssertions.createLongsBlock) BlockAssertions.createTimestampsWithTimezoneBlock(io.prestosql.block.BlockAssertions.createTimestampsWithTimezoneBlock) BlockAssertions.createRowBlock(io.prestosql.block.BlockAssertions.createRowBlock) Block(io.prestosql.spi.block.Block) BlockAssertions.createIntsBlock(io.prestosql.block.BlockAssertions.createIntsBlock) BlockAssertions.createDoublesBlock(io.prestosql.block.BlockAssertions.createDoublesBlock)

Example 3 with ExpressionInterpreter

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

Example 4 with ExpressionInterpreter

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

Example 5 with ExpressionInterpreter

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);
    });
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) SqlTimestampWithTimeZone(io.prestosql.spi.type.SqlTimestampWithTimeZone) LocalTime(org.joda.time.LocalTime) ExpressionFormatter.formatExpression(io.prestosql.sql.ExpressionFormatter.formatExpression) Expression(io.prestosql.sql.tree.Expression) ExpressionInterpreter(io.prestosql.sql.planner.ExpressionInterpreter) BigInteger(java.math.BigInteger) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime)

Aggregations

Type (io.prestosql.spi.type.Type)7 ExpressionInterpreter (io.prestosql.sql.planner.ExpressionInterpreter)7 NodeRef (io.prestosql.sql.tree.NodeRef)7 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)3 VarbinaryType (io.prestosql.spi.type.VarbinaryType)2 VarcharType.createVarcharType (io.prestosql.spi.type.VarcharType.createVarcharType)2 Expression (io.prestosql.sql.tree.Expression)2 Slice (io.airlift.slice.Slice)1 BlockAssertions.createBooleansBlock (io.prestosql.block.BlockAssertions.createBooleansBlock)1 BlockAssertions.createDoublesBlock (io.prestosql.block.BlockAssertions.createDoublesBlock)1 BlockAssertions.createIntsBlock (io.prestosql.block.BlockAssertions.createIntsBlock)1 BlockAssertions.createLongsBlock (io.prestosql.block.BlockAssertions.createLongsBlock)1 BlockAssertions.createRowBlock (io.prestosql.block.BlockAssertions.createRowBlock)1 BlockAssertions.createSlicesBlock (io.prestosql.block.BlockAssertions.createSlicesBlock)1 BlockAssertions.createStringsBlock (io.prestosql.block.BlockAssertions.createStringsBlock)1 BlockAssertions.createTimestampsWithTimezoneBlock (io.prestosql.block.BlockAssertions.createTimestampsWithTimezoneBlock)1 Block (io.prestosql.spi.block.Block)1 OperatorType (io.prestosql.spi.function.OperatorType)1 RowType (io.prestosql.spi.type.RowType)1 SqlTimestampWithTimeZone (io.prestosql.spi.type.SqlTimestampWithTimeZone)1