Search in sources :

Example 11 with NodeRef

use of io.prestosql.sql.tree.NodeRef 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 12 with NodeRef

use of io.prestosql.sql.tree.NodeRef 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)

Example 13 with NodeRef

use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.

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 = 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) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) ExpressionInterpreter(io.prestosql.sql.planner.ExpressionInterpreter)

Example 14 with NodeRef

use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.

the class TestExpressionInterpreter method evaluate.

private static Object evaluate(Expression expression) {
    Map<NodeRef<Expression>, Type> expressionTypes = TYPE_ANALYZER.getTypes(TEST_SESSION, SYMBOL_TYPES, expression);
    ExpressionInterpreter interpreter = expressionInterpreter(expression, METADATA, TEST_SESSION, expressionTypes);
    return interpreter.evaluate();
}
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) ExpressionInterpreter(io.prestosql.sql.planner.ExpressionInterpreter)

Example 15 with NodeRef

use of io.prestosql.sql.tree.NodeRef in project hetu-core by openlookeng.

the class PlanBuilder method rowExpression.

public RowExpression rowExpression(String sql) {
    Expression expression = expression(sql);
    Map<NodeRef<Expression>, Type> expressionTypes = ExpressionAnalyzer.analyzeExpressions(TEST_SESSION, metadata, new SqlParser(), getTypes(), ImmutableList.of(expression), ImmutableList.of(), WarningCollector.NOOP, false).getExpressionTypes();
    return SqlToRowExpressionTranslator.translate(expression, SCALAR, expressionTypes, ImmutableMap.of(), metadata.getFunctionAndTypeManager(), TEST_SESSION, false);
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) CallExpression(io.prestosql.spi.relation.CallExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) Expression(io.prestosql.sql.tree.Expression) RowExpression(io.prestosql.spi.relation.RowExpression) SqlParser(io.prestosql.sql.parser.SqlParser)

Aggregations

NodeRef (io.prestosql.sql.tree.NodeRef)15 Type (io.prestosql.spi.type.Type)14 Expression (io.prestosql.sql.tree.Expression)10 ExpressionInterpreter (io.prestosql.sql.planner.ExpressionInterpreter)7 RowExpression (io.prestosql.spi.relation.RowExpression)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Symbol (io.prestosql.spi.plan.Symbol)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 OperatorType (io.prestosql.spi.function.OperatorType)3 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)3 LambdaExpression (io.prestosql.sql.tree.LambdaExpression)3 FunctionType (io.prestosql.spi.type.FunctionType)2 RowType (io.prestosql.spi.type.RowType)2 VarbinaryType (io.prestosql.spi.type.VarbinaryType)2 VarcharType (io.prestosql.spi.type.VarcharType)2 VarcharType.createVarcharType (io.prestosql.spi.type.VarcharType.createVarcharType)2 PlanSymbolAllocator (io.prestosql.sql.planner.PlanSymbolAllocator)2 RowExpressionInterpreter (io.prestosql.sql.planner.RowExpressionInterpreter)2 TypeAnalyzer (io.prestosql.sql.planner.TypeAnalyzer)2 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)2