Search in sources :

Example 66 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class TestExpressionInterpreter method assertExpressionAndRowExpressionEquals.

private static void assertExpressionAndRowExpressionEquals(Object expressionResult, Object rowExpressionResult) {
    if (rowExpressionResult instanceof RowExpression) {
        // Cannot be completely evaluated into a constant; compare expressions
        assertTrue(expressionResult instanceof Expression);
        // It is tricky to check the equivalence of an expression and a row expression.
        // We rely on the optimized translator to fill the gap.
        RowExpression translated = TRANSLATOR.translateAndOptimize((Expression) expressionResult, SYMBOL_TYPES);
        assertRowExpressionEvaluationEquals(translated, rowExpressionResult);
    } else {
        // We have constants; directly compare
        assertRowExpressionEvaluationEquals(expressionResult, rowExpressionResult);
    }
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) ExpressionFormatter.formatExpression(com.facebook.presto.sql.ExpressionFormatter.formatExpression) InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Expression(com.facebook.presto.sql.tree.Expression) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Example 67 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class TestCursorProcessorCompiler method testCompilerWithCSE.

@Test
public void testCompilerWithCSE() {
    PageFunctionCompiler functionCompiler = new PageFunctionCompiler(METADATA, 0);
    ExpressionCompiler expressionCompiler = new ExpressionCompiler(METADATA, functionCompiler);
    RowExpression filter = new SpecialFormExpression(AND, BIGINT, ADD_X_Y_GREATER_THAN_2, ADD_X_Y_LESS_THAN_10);
    List<? extends RowExpression> projections = createIfProjectionList(5);
    Supplier<CursorProcessor> cseCursorProcessorSupplier = expressionCompiler.compileCursorProcessor(SESSION.getSqlFunctionProperties(), Optional.of(filter), projections, "key", true);
    Supplier<CursorProcessor> noCseSECursorProcessorSupplier = expressionCompiler.compileCursorProcessor(SESSION.getSqlFunctionProperties(), Optional.of(filter), projections, "key", false);
    Page input = createLongBlockPage(2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    List<Type> types = ImmutableList.of(BIGINT, BIGINT);
    PageBuilder pageBuilder = new PageBuilder(projections.stream().map(RowExpression::getType).collect(toList()));
    RecordSet recordSet = new PageRecordSet(types, input);
    cseCursorProcessorSupplier.get().process(SESSION.getSqlFunctionProperties(), new DriverYieldSignal(), recordSet.cursor(), pageBuilder);
    Page pageFromCSE = pageBuilder.build();
    pageBuilder.reset();
    noCseSECursorProcessorSupplier.get().process(SESSION.getSqlFunctionProperties(), new DriverYieldSignal(), recordSet.cursor(), pageBuilder);
    Page pageFromNoCSE = pageBuilder.build();
    checkPageEqual(pageFromCSE, pageFromNoCSE);
}
Also used : CursorProcessor(com.facebook.presto.operator.project.CursorProcessor) RowExpression(com.facebook.presto.spi.relation.RowExpression) DriverYieldSignal(com.facebook.presto.operator.DriverYieldSignal) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder) PageRecordSet(com.facebook.presto.operator.index.PageRecordSet) Type(com.facebook.presto.common.type.Type) PageRecordSet(com.facebook.presto.operator.index.PageRecordSet) RecordSet(com.facebook.presto.spi.RecordSet) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) Test(org.testng.annotations.Test)

Example 68 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class TestInCodeGenerator method testBigint.

@Test
public void testBigint() {
    FunctionAndTypeManager functionAndTypeManager = createTestMetadataManager().getFunctionAndTypeManager();
    List<RowExpression> values = new ArrayList<>();
    values.add(constant(Integer.MAX_VALUE + 1L, BIGINT));
    values.add(constant(Integer.MIN_VALUE - 1L, BIGINT));
    values.add(constant(3L, BIGINT));
    assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
    values.add(constant(null, BIGINT));
    assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
    values.add(new CallExpression(CAST.name(), functionAndTypeManager.lookupCast(CAST, DOUBLE.getTypeSignature(), BIGINT.getTypeSignature()), BIGINT, Collections.singletonList(constant(12345678901234.0, DOUBLE))));
    assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
    for (long i = 6; i <= 32; ++i) {
        values.add(constant(i, BIGINT));
    }
    assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
    values.add(constant(33L, BIGINT));
    assertEquals(checkSwitchGenerationCase(BIGINT, values), SET_CONTAINS);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) ArrayList(java.util.ArrayList) RowExpression(com.facebook.presto.spi.relation.RowExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 69 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class InCodeGeneratorBenchmark method setup.

@Setup
public void setup() {
    Random random = new Random();
    RowExpression[] arguments = new RowExpression[1 + inListCount];
    switch(type) {
        case StandardTypes.BIGINT:
            prestoType = BIGINT;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant((long) random.nextInt(), BIGINT);
            }
            break;
        case StandardTypes.DOUBLE:
            prestoType = DOUBLE;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant(random.nextDouble(), DOUBLE);
            }
            break;
        case StandardTypes.VARCHAR:
            prestoType = VARCHAR;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant(Slices.utf8Slice(Long.toString(random.nextLong())), VARCHAR);
            }
            break;
        default:
            throw new IllegalStateException();
    }
    arguments[0] = field(0, prestoType);
    RowExpression project = field(0, prestoType);
    PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(prestoType));
    for (int i = 0; i < 10_000; i++) {
        pageBuilder.declarePosition();
        switch(type) {
            case StandardTypes.BIGINT:
                BIGINT.writeLong(pageBuilder.getBlockBuilder(0), random.nextInt());
                break;
            case StandardTypes.DOUBLE:
                DOUBLE.writeDouble(pageBuilder.getBlockBuilder(0), random.nextDouble());
                break;
            case StandardTypes.VARCHAR:
                VARCHAR.writeSlice(pageBuilder.getBlockBuilder(0), Slices.utf8Slice(Long.toString(random.nextLong())));
                break;
        }
    }
    inputPage = pageBuilder.build();
    RowExpression filter = specialForm(IN, BOOLEAN, arguments);
    MetadataManager metadata = MetadataManager.createTestMetadataManager();
    processor = new ExpressionCompiler(metadata, new PageFunctionCompiler(metadata, 0)).compilePageProcessor(SESSION.getSqlFunctionProperties(), Optional.of(filter), ImmutableList.of(project)).get();
}
Also used : MetadataManager(com.facebook.presto.metadata.MetadataManager) Random(java.util.Random) RowExpression(com.facebook.presto.spi.relation.RowExpression) PageBuilder(com.facebook.presto.common.PageBuilder) Setup(org.openjdk.jmh.annotations.Setup)

Example 70 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class TestRowExpressionSerde method assertStringLiteral.

private void assertStringLiteral(@Language("SQL") String sql, String expectedString, Type expectedType) {
    RowExpression roundTrip = getRoundTrip(sql, true);
    assertTrue(roundTrip instanceof ConstantExpression);
    String roundTripValue = ((Slice) ((ConstantExpression) roundTrip).getValue()).toStringUtf8();
    Type roundTripType = roundTrip.getType();
    assertEquals(roundTripValue, expectedString);
    assertEquals(roundTripType, expectedType);
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) RowType(com.facebook.presto.common.type.RowType) Slice(io.airlift.slice.Slice) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Aggregations

RowExpression (com.facebook.presto.spi.relation.RowExpression)237 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)97 Test (org.testng.annotations.Test)87 ImmutableList (com.google.common.collect.ImmutableList)58 CallExpression (com.facebook.presto.spi.relation.CallExpression)52 Map (java.util.Map)49 List (java.util.List)42 Type (com.facebook.presto.common.type.Type)41 PlanNode (com.facebook.presto.spi.plan.PlanNode)41 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)40 ImmutableMap (com.google.common.collect.ImmutableMap)38 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)37 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)35 Optional (java.util.Optional)35 Expression (com.facebook.presto.sql.tree.Expression)31 ColumnHandle (com.facebook.presto.spi.ColumnHandle)27 Objects.requireNonNull (java.util.Objects.requireNonNull)27 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)24 Set (java.util.Set)24 ArrayList (java.util.ArrayList)23