Search in sources :

Example 46 with RowExpression

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

the class TestEffectivePredicateExtractor method testInnerJoin.

@Test
public void testInnerJoin() {
    ImmutableList.Builder<JoinNode.EquiJoinClause> criteriaBuilder = ImmutableList.builder();
    criteriaBuilder.add(new JoinNode.EquiJoinClause(AV, DV));
    criteriaBuilder.add(new JoinNode.EquiJoinClause(BV, EV));
    List<JoinNode.EquiJoinClause> criteria = criteriaBuilder.build();
    Map<VariableReferenceExpression, ColumnHandle> leftAssignments = Maps.filterKeys(scanAssignments, Predicates.in(ImmutableList.of(AV, BV, CV)));
    TableScanNode leftScan = tableScanNode(leftAssignments);
    Map<VariableReferenceExpression, ColumnHandle> rightAssignments = Maps.filterKeys(scanAssignments, Predicates.in(ImmutableList.of(DV, EV, FV)));
    TableScanNode rightScan = tableScanNode(rightAssignments);
    FilterNode left = filter(leftScan, and(lessThan(BV, AV), lessThan(CV, bigintLiteral(10)), equals(GV, bigintLiteral(10))));
    FilterNode right = filter(rightScan, and(equals(DV, EV), lessThan(FV, bigintLiteral(100))));
    PlanNode node = new JoinNode(Optional.empty(), newId(), JoinNode.Type.INNER, left, right, criteria, ImmutableList.<VariableReferenceExpression>builder().addAll(left.getOutputVariables()).addAll(right.getOutputVariables()).build(), Optional.of(lessThanOrEqual(BV, EV)), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    // All predicates having output symbol should be carried through
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(lessThan(BV, AV), lessThan(CV, bigintLiteral(10)), equals(DV, EV), lessThan(FV, bigintLiteral(100)), equals(AV, DV), equals(BV, EV), lessThanOrEqual(BV, EV)));
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) ImmutableList(com.google.common.collect.ImmutableList) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) FilterNode(com.facebook.presto.spi.plan.FilterNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) PlanNode(com.facebook.presto.spi.plan.PlanNode) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test)

Example 47 with RowExpression

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

the class TestNullabilityAnalyzer method assertNullability.

private void assertNullability(String expression, boolean mayReturnNullForNotNullInput) {
    Expression rawExpression = rewriteIdentifiersToSymbolReferences(new SqlParser().createExpression(expression, new ParsingOptions()));
    Expression desugaredExpression = new TestingDesugarExpressions(TYPES.allVariables()).rewrite(rawExpression);
    RowExpression rowExpression = TRANSLATOR.translate(desugaredExpression, TYPES);
    assertEquals(analyzer.mayReturnNullOnNonNullInput(rowExpression), mayReturnNullForNotNullInput);
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) SqlParser(com.facebook.presto.sql.parser.SqlParser) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Example 48 with RowExpression

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

the class TestRowExpressionPredicateCompiler method test.

@Test
public void test() {
    InputReferenceExpression a = new InputReferenceExpression(Optional.empty(), 0, BIGINT);
    InputReferenceExpression b = new InputReferenceExpression(Optional.empty(), 1, BIGINT);
    Block aBlock = createLongBlock(5, 5, 5, 5, 5);
    Block bBlock = createLongBlock(1, 3, 5, 7, 0);
    // b - a >= 0
    RowExpression sum = call("<", functionResolution.comparisonFunction(GREATER_THAN_OR_EQUAL, BIGINT, BIGINT), BOOLEAN, call("b - a", functionResolution.arithmeticFunction(SUBTRACT, BIGINT, BIGINT), BIGINT, b, a), constant(0L, BIGINT));
    PredicateCompiler compiler = new RowExpressionPredicateCompiler(metadata, 10_000);
    Predicate compiledSum = compiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), sum).get();
    assertEquals(Arrays.asList(1, 0), Ints.asList(compiledSum.getInputChannels()));
    Page page = new Page(bBlock, aBlock);
    assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 0));
    assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 1));
    assertTrue(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 2));
    assertTrue(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 3));
    assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 4));
    // b * 2 < 10
    RowExpression timesTwo = call("=", functionResolution.comparisonFunction(LESS_THAN, BIGINT, BIGINT), BOOLEAN, call("b * 2", functionResolution.arithmeticFunction(MULTIPLY, BIGINT, BIGINT), BIGINT, b, constant(2L, BIGINT)), constant(10L, BIGINT));
    Predicate compiledTimesTwo = compiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), timesTwo).get();
    assertEquals(Arrays.asList(1), Ints.asList(compiledTimesTwo.getInputChannels()));
    page = new Page(bBlock);
    assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 0));
    assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 1));
    assertFalse(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 2));
    assertFalse(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 3));
    assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 4));
}
Also used : InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) Block(com.facebook.presto.common.block.Block) RowExpression(com.facebook.presto.spi.relation.RowExpression) Page(com.facebook.presto.common.Page) PredicateCompiler(com.facebook.presto.spi.relation.PredicateCompiler) Predicate(com.facebook.presto.common.relation.Predicate) Test(org.testng.annotations.Test)

Example 49 with RowExpression

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

the class TestRowExpressionDomainTranslator method testFromOrPredicate.

@Test
public void testFromOrPredicate() {
    RowExpression originalPredicate = or(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)));
    ExtractionResult result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.notNull(BIGINT))));
    originalPredicate = or(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(equal(C_BIGINT, bigintLiteral(2L)), unprocessableExpression2(C_BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));
    // Same unprocessableExpression means that we can do more extraction
    // If both sides are operating on the same single symbol
    originalPredicate = or(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(equal(C_BIGINT, bigintLiteral(2L)), unprocessableExpression1(C_BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), unprocessableExpression1(C_BIGINT));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));
    // And not if they have different symbols
    assertUnsupportedPredicate(or(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(equal(C_DOUBLE, doubleLiteral(2.0)), unprocessableExpression1(C_BIGINT))));
    // We can make another optimization if one side is the super set of the other side
    originalPredicate = or(and(greaterThan(C_BIGINT, bigintLiteral(1L)), greaterThan(C_DOUBLE, doubleLiteral(1.0)), unprocessableExpression1(C_BIGINT)), and(greaterThan(C_BIGINT, bigintLiteral(2L)), greaterThan(C_DOUBLE, doubleLiteral(2.0)), unprocessableExpression1(C_BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), unprocessableExpression1(C_BIGINT));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 1L)), false), C_DOUBLE, Domain.create(ValueSet.ofRanges(Range.greaterThan(DOUBLE, 1.0)), false))));
    // We can't make those inferences if the unprocessableExpressions are non-deterministic
    originalPredicate = or(and(equal(C_BIGINT, bigintLiteral(1L)), randPredicate(C_BIGINT)), and(equal(C_BIGINT, bigintLiteral(2L)), randPredicate(C_BIGINT)));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));
    // Test complements
    originalPredicate = not(or(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)), and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT))));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), and(not(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT))), not(and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));
    assertTrue(result.getTupleDomain().isAll());
    originalPredicate = not(or(not(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT))), not(and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), and(unprocessableExpression1(C_BIGINT), unprocessableExpression2(C_BIGINT)));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, false, 5L, false)), false))));
}
Also used : LiteralEncoder.toRowExpression(com.facebook.presto.sql.planner.LiteralEncoder.toRowExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) ExtractionResult(com.facebook.presto.spi.relation.DomainTranslator.ExtractionResult) Test(org.testng.annotations.Test)

Example 50 with RowExpression

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

the class TestRowExpressionDomainTranslator method testNumericTypeTranslation.

private void testNumericTypeTranslation(NumericValues columnValues, NumericValues literalValues) {
    Type columnType = columnValues.getInput().getType();
    Type literalType = literalValues.getInput().getType();
    Type superType = metadata.getFunctionAndTypeManager().getCommonSuperType(columnType, literalType).orElseThrow(() -> new IllegalArgumentException("incompatible types in test (" + columnType + ", " + literalType + ")"));
    RowExpression max = toRowExpression(literalValues.getMax(), literalType);
    RowExpression min = toRowExpression(literalValues.getMin(), literalType);
    RowExpression integerPositive = toRowExpression(literalValues.getIntegerPositive(), literalType);
    RowExpression integerNegative = toRowExpression(literalValues.getIntegerNegative(), literalType);
    RowExpression fractionalPositive = toRowExpression(literalValues.getFractionalPositive(), literalType);
    RowExpression fractionalNegative = toRowExpression(literalValues.getFractionalNegative(), literalType);
    if (!literalType.equals(superType)) {
        max = cast(max, superType);
        min = cast(min, superType);
        integerPositive = cast(integerPositive, superType);
        integerNegative = cast(integerNegative, superType);
        fractionalPositive = cast(fractionalPositive, superType);
        fractionalNegative = cast(fractionalNegative, superType);
    }
    VariableReferenceExpression columnSymbol = columnValues.getInput();
    RowExpression columnExpression = columnSymbol;
    if (!columnType.equals(superType)) {
        columnExpression = cast(columnExpression, superType);
    }
    // greater than or equal
    testSimpleComparison(greaterThanOrEqual(columnExpression, integerPositive), columnSymbol, Range.greaterThanOrEqual(columnType, columnValues.getIntegerPositive()));
    testSimpleComparison(greaterThanOrEqual(columnExpression, integerNegative), columnSymbol, Range.greaterThanOrEqual(columnType, columnValues.getIntegerNegative()));
    testSimpleComparison(greaterThanOrEqual(columnExpression, max), columnSymbol, Range.greaterThan(columnType, columnValues.getMax()));
    testSimpleComparison(greaterThanOrEqual(columnExpression, min), columnSymbol, Range.greaterThanOrEqual(columnType, columnValues.getMin()));
    if (literalValues.isFractional()) {
        testSimpleComparison(greaterThanOrEqual(columnExpression, fractionalPositive), columnSymbol, Range.greaterThan(columnType, columnValues.getFractionalPositive()));
        testSimpleComparison(greaterThanOrEqual(columnExpression, fractionalNegative), columnSymbol, Range.greaterThan(columnType, columnValues.getFractionalNegative()));
    }
    // greater than
    testSimpleComparison(greaterThan(columnExpression, integerPositive), columnSymbol, Range.greaterThan(columnType, columnValues.getIntegerPositive()));
    testSimpleComparison(greaterThan(columnExpression, integerNegative), columnSymbol, Range.greaterThan(columnType, columnValues.getIntegerNegative()));
    testSimpleComparison(greaterThan(columnExpression, max), columnSymbol, Range.greaterThan(columnType, columnValues.getMax()));
    testSimpleComparison(greaterThan(columnExpression, min), columnSymbol, Range.greaterThanOrEqual(columnType, columnValues.getMin()));
    if (literalValues.isFractional()) {
        testSimpleComparison(greaterThan(columnExpression, fractionalPositive), columnSymbol, Range.greaterThan(columnType, columnValues.getFractionalPositive()));
        testSimpleComparison(greaterThan(columnExpression, fractionalNegative), columnSymbol, Range.greaterThan(columnType, columnValues.getFractionalNegative()));
    }
    // less than or equal
    testSimpleComparison(lessThanOrEqual(columnExpression, integerPositive), columnSymbol, Range.lessThanOrEqual(columnType, columnValues.getIntegerPositive()));
    testSimpleComparison(lessThanOrEqual(columnExpression, integerNegative), columnSymbol, Range.lessThanOrEqual(columnType, columnValues.getIntegerNegative()));
    testSimpleComparison(lessThanOrEqual(columnExpression, max), columnSymbol, Range.lessThanOrEqual(columnType, columnValues.getMax()));
    testSimpleComparison(lessThanOrEqual(columnExpression, min), columnSymbol, Range.lessThan(columnType, columnValues.getMin()));
    if (literalValues.isFractional()) {
        testSimpleComparison(lessThanOrEqual(columnExpression, fractionalPositive), columnSymbol, Range.lessThanOrEqual(columnType, columnValues.getFractionalPositive()));
        testSimpleComparison(lessThanOrEqual(columnExpression, fractionalNegative), columnSymbol, Range.lessThanOrEqual(columnType, columnValues.getFractionalNegative()));
    }
    // less than
    testSimpleComparison(lessThan(columnExpression, integerPositive), columnSymbol, Range.lessThan(columnType, columnValues.getIntegerPositive()));
    testSimpleComparison(lessThan(columnExpression, integerNegative), columnSymbol, Range.lessThan(columnType, columnValues.getIntegerNegative()));
    testSimpleComparison(lessThan(columnExpression, max), columnSymbol, Range.lessThanOrEqual(columnType, columnValues.getMax()));
    testSimpleComparison(lessThan(columnExpression, min), columnSymbol, Range.lessThan(columnType, columnValues.getMin()));
    if (literalValues.isFractional()) {
        testSimpleComparison(lessThan(columnExpression, fractionalPositive), columnSymbol, Range.lessThanOrEqual(columnType, columnValues.getFractionalPositive()));
        testSimpleComparison(lessThan(columnExpression, fractionalNegative), columnSymbol, Range.lessThanOrEqual(columnType, columnValues.getFractionalNegative()));
    }
    // equal
    testSimpleComparison(equal(columnExpression, integerPositive), columnSymbol, Range.equal(columnType, columnValues.getIntegerPositive()));
    testSimpleComparison(equal(columnExpression, integerNegative), columnSymbol, Range.equal(columnType, columnValues.getIntegerNegative()));
    testSimpleComparison(equal(columnExpression, max), columnSymbol, Domain.none(columnType));
    testSimpleComparison(equal(columnExpression, min), columnSymbol, Domain.none(columnType));
    if (literalValues.isFractional()) {
        testSimpleComparison(equal(columnExpression, fractionalPositive), columnSymbol, Domain.none(columnType));
        testSimpleComparison(equal(columnExpression, fractionalNegative), columnSymbol, Domain.none(columnType));
    }
    // not equal
    testSimpleComparison(notEqual(columnExpression, integerPositive), columnSymbol, Domain.create(ValueSet.ofRanges(Range.lessThan(columnType, columnValues.getIntegerPositive()), Range.greaterThan(columnType, columnValues.getIntegerPositive())), false));
    testSimpleComparison(notEqual(columnExpression, integerNegative), columnSymbol, Domain.create(ValueSet.ofRanges(Range.lessThan(columnType, columnValues.getIntegerNegative()), Range.greaterThan(columnType, columnValues.getIntegerNegative())), false));
    testSimpleComparison(notEqual(columnExpression, max), columnSymbol, Domain.notNull(columnType));
    testSimpleComparison(notEqual(columnExpression, min), columnSymbol, Domain.notNull(columnType));
    if (literalValues.isFractional()) {
        testSimpleComparison(notEqual(columnExpression, fractionalPositive), columnSymbol, Domain.notNull(columnType));
        testSimpleComparison(notEqual(columnExpression, fractionalNegative), columnSymbol, Domain.notNull(columnType));
    }
    // is distinct from
    testSimpleComparison(isDistinctFrom(columnExpression, integerPositive), columnSymbol, Domain.create(ValueSet.ofRanges(Range.lessThan(columnType, columnValues.getIntegerPositive()), Range.greaterThan(columnType, columnValues.getIntegerPositive())), true));
    testSimpleComparison(isDistinctFrom(columnExpression, integerNegative), columnSymbol, Domain.create(ValueSet.ofRanges(Range.lessThan(columnType, columnValues.getIntegerNegative()), Range.greaterThan(columnType, columnValues.getIntegerNegative())), true));
    testSimpleComparison(isDistinctFrom(columnExpression, max), columnSymbol, Domain.all(columnType));
    testSimpleComparison(isDistinctFrom(columnExpression, min), columnSymbol, Domain.all(columnType));
    if (literalValues.isFractional()) {
        testSimpleComparison(isDistinctFrom(columnExpression, fractionalPositive), columnSymbol, Domain.all(columnType));
        testSimpleComparison(isDistinctFrom(columnExpression, fractionalNegative), columnSymbol, Domain.all(columnType));
    }
}
Also used : CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) DecimalType(com.facebook.presto.common.type.DecimalType) CastType(com.facebook.presto.metadata.CastType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) LiteralEncoder.toRowExpression(com.facebook.presto.sql.planner.LiteralEncoder.toRowExpression) 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