Search in sources :

Example 11 with Expression

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

the class ExpressionVerifier method visitInPredicate.

@Override
protected Boolean visitInPredicate(InPredicate actual, Node expectedExpression) {
    if (expectedExpression instanceof InPredicate) {
        InPredicate expected = (InPredicate) expectedExpression;
        if (actual.getValueList() instanceof InListExpression) {
            return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), expected.getValueList());
        } else {
            checkState(expected.getValueList() instanceof InListExpression, "ExpressionVerifier doesn't support unpacked expected values. Feel free to add support if needed");
            /*
                 * If the expected value is a value list, but the actual is e.g. a SymbolReference,
                 * we need to unpack the value from the list so that when we hit visitSymbolReference, the
                 * expected.toString() call returns something that the symbolAliases actually contains.
                 * For example, InListExpression.toString returns "(onlyitem)" rather than "onlyitem".
                 *
                 * This is required because actual passes through the analyzer, planner, and possibly optimizers,
                 * one of which sometimes takes the liberty of unpacking the InListExpression.
                 *
                 * Since the expected value doesn't go through all of that, we have to deal with the case
                 * of the actual value being unpacked, but the expected value being an InListExpression.
                 */
            List<Expression> values = ((InListExpression) expected.getValueList()).getValues();
            checkState(values.size() == 1, "Multiple expressions in expected value list %s, but actual value is not a list", values, actual.getValue());
            Expression onlyExpectedExpression = values.get(0);
            return process(actual.getValue(), expected.getValue()) && process(actual.getValueList(), onlyExpectedExpression);
        }
    }
    return false;
}
Also used : InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) TryExpression(io.prestosql.sql.tree.TryExpression) Expression(io.prestosql.sql.tree.Expression) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate)

Example 12 with Expression

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

the class FilterMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    FunctionResolution functionResolution = new FunctionResolution(metadata.getFunctionAndTypeManager());
    LogicalRowExpressions logicalRowExpressions = new LogicalRowExpressions(new RowExpressionDeterminismEvaluator(metadata), functionResolution, metadata.getFunctionAndTypeManager());
    FilterNode filterNode = (FilterNode) node;
    if (isExpression(filterNode.getPredicate())) {
        ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);
        if (dynamicFilter.isPresent()) {
            return new MatchResult(verifier.process(castToExpression(filterNode.getPredicate()), combineConjuncts(castToExpression(predicate), castToExpression(dynamicFilter.get()))));
        }
        DynamicFilters.ExtractResult extractResult = extractDynamicFilters(filterNode.getPredicate());
        List<Expression> expressionList = extractResult.getStaticConjuncts().stream().map(OriginalExpressionUtils::castToExpression).collect(Collectors.toList());
        return new MatchResult(verifier.process(combineConjuncts(expressionList), castToExpression(predicate)));
    }
    RowExpressionVerifier verifier = new RowExpressionVerifier(symbolAliases, metadata, session, filterNode.getOutputSymbols());
    if (dynamicFilter.isPresent()) {
        return new MatchResult(verifier.process(combineConjuncts(castToExpression(predicate), castToExpression(dynamicFilter.get())), filterNode.getPredicate()));
    }
    DynamicFilters.ExtractResult extractResult = extractDynamicFilters(filterNode.getPredicate());
    return new MatchResult(verifier.process(castToExpression(predicate), logicalRowExpressions.combineConjuncts(extractResult.getStaticConjuncts())));
}
Also used : RowExpressionDeterminismEvaluator(io.prestosql.sql.relational.RowExpressionDeterminismEvaluator) DynamicFilters.extractDynamicFilters(io.prestosql.sql.DynamicFilters.extractDynamicFilters) DynamicFilters(io.prestosql.sql.DynamicFilters) OriginalExpressionUtils.isExpression(io.prestosql.sql.relational.OriginalExpressionUtils.isExpression) RowExpression(io.prestosql.spi.relation.RowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) Expression(io.prestosql.sql.tree.Expression) LogicalRowExpressions(io.prestosql.expressions.LogicalRowExpressions) FilterNode(io.prestosql.spi.plan.FilterNode) FunctionResolution(io.prestosql.sql.relational.FunctionResolution)

Example 13 with Expression

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

the class JoinMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    JoinNode joinNode = (JoinNode) node;
    if (joinNode.getCriteria().size() != equiCriteria.size()) {
        return NO_MATCH;
    }
    if (filter.isPresent()) {
        if (!joinNode.getFilter().isPresent()) {
            return NO_MATCH;
        }
        RowExpression expression = joinNode.getFilter().get();
        if (isExpression(expression)) {
            if (!new ExpressionVerifier(symbolAliases).process(castToExpression(expression), filter.get())) {
                return NO_MATCH;
            }
        } else {
            if (!new RowExpressionVerifier(symbolAliases, metadata, session, node.getOutputSymbols()).process(filter.get(), expression)) {
                return NO_MATCH;
            }
        }
    } else {
        if (joinNode.getFilter().isPresent()) {
            return NO_MATCH;
        }
    }
    if (distributionType.isPresent() && !distributionType.equals(joinNode.getDistributionType())) {
        return NO_MATCH;
    }
    if (spillable.isPresent() && !spillable.equals(joinNode.isSpillable())) {
        return NO_MATCH;
    }
    /*
         * Have to use order-independent comparison; there are no guarantees what order
         * the equi criteria will have after planning and optimizing.
         */
    Set<JoinNode.EquiJoinClause> actual = ImmutableSet.copyOf(joinNode.getCriteria());
    Set<JoinNode.EquiJoinClause> expected = equiCriteria.stream().map(maker -> maker.getExpectedValue(symbolAliases)).collect(toImmutableSet());
    if (!expected.equals(actual)) {
        return NO_MATCH;
    }
    if (dynamicFilter.isPresent() && !dynamicFilter.get().match(joinNode, symbolAliases).isMatch()) {
        return NO_MATCH;
    }
    return MatchResult.match();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) StatsProvider(io.prestosql.cost.StatsProvider) Set(java.util.Set) PlanNode(io.prestosql.spi.plan.PlanNode) Metadata(io.prestosql.metadata.Metadata) Preconditions.checkState(com.google.common.base.Preconditions.checkState) OriginalExpressionUtils.isExpression(io.prestosql.sql.relational.OriginalExpressionUtils.isExpression) List(java.util.List) DistributionType(io.prestosql.spi.plan.JoinNode.DistributionType) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) RowExpression(io.prestosql.spi.relation.RowExpression) Optional(java.util.Optional) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) NO_MATCH(io.prestosql.sql.planner.assertions.MatchResult.NO_MATCH) Expression(io.prestosql.sql.tree.Expression) JoinNode(io.prestosql.spi.plan.JoinNode) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) JoinNode(io.prestosql.spi.plan.JoinNode) RowExpression(io.prestosql.spi.relation.RowExpression)

Example 14 with Expression

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

the class TestExpressionDomainTranslator method testBigIntType.

private void testBigIntType() {
    mergeAndAssert(true, ExpressionUtils.or(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(5L)), equal(C_BIGINT, bigintLiteral(6L)), equal(C_BIGINT, bigintLiteral(7L)), and(greaterThan(C_BIGINT, bigintLiteral(7L)), lessThanOrEqual(C_BIGINT, bigintLiteral(9L))), between(C_BIGINT, bigintLiteral(10L), bigintLiteral(15L)), between(C_BIGINT, bigintLiteral(25L), bigintLiteral(35L))), between(C_BIGINT, bigintLiteral(2L), bigintLiteral(12L)));
    mergeAndAssert(true, ExpressionUtils.or(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(5L)), equal(C_BIGINT, bigintLiteral(6L)), equal(C_BIGINT, bigintLiteral(7L)), and(greaterThan(C_BIGINT, bigintLiteral(7L)), lessThanOrEqual(C_BIGINT, bigintLiteral(9L))), between(C_BIGINT, bigintLiteral(10L), bigintLiteral(15L)), between(C_BIGINT, bigintLiteral(25L), bigintLiteral(35L))), and(greaterThan(C_BIGINT, bigintLiteral(2L)), lessThan(C_BIGINT, bigintLiteral(12L))));
    mergeAndAssert(true, ExpressionUtils.or(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(5L)), equal(C_BIGINT, bigintLiteral(6L)), equal(C_BIGINT, bigintLiteral(7L)), and(greaterThan(C_BIGINT, bigintLiteral(7L)), lessThanOrEqual(C_BIGINT, bigintLiteral(9L))), between(C_BIGINT, bigintLiteral(10L), bigintLiteral(15L)), between(C_BIGINT, bigintLiteral(25L), bigintLiteral(35L))), or(equal(C_BIGINT, bigintLiteral(2L)), equal(C_BIGINT, bigintLiteral(5L))));
    mergeAndAssert(false, ExpressionUtils.or(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(5L)), equal(C_BIGINT, bigintLiteral(6L)), equal(C_BIGINT, bigintLiteral(7L)), and(greaterThan(C_BIGINT, bigintLiteral(7L)), lessThanOrEqual(C_BIGINT, bigintLiteral(9L))), between(C_BIGINT, bigintLiteral(10L), bigintLiteral(15L)), between(C_BIGINT, bigintLiteral(25L), bigintLiteral(35L))), between(C_BIGINT, bigintLiteral(10L), bigintLiteral(16L)));
    Expression merged = mergeAndAssert(false, ExpressionUtils.or(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(4L)), between(C_BIGINT, bigintLiteral(8L), bigintLiteral(10L))), between(C_BIGINT, bigintLiteral(2L), bigintLiteral(6L)));
    mergeAndAssert(true, ExpressionUtils.or(merged, equal(C_BIGINT, bigintLiteral(5L)), between(C_BIGINT, bigintLiteral(6L), bigintLiteral(7L))), between(C_BIGINT, bigintLiteral(2L), bigintLiteral(10L)));
    mergeAndAssert(true, ExpressionUtils.or(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(5L)), equal(C_BIGINT, bigintLiteral(6L)), equal(C_BIGINT, nullLiteral()), equal(C_BIGINT, bigintLiteral(7L)), and(greaterThan(C_BIGINT, bigintLiteral(7L)), lessThanOrEqual(C_BIGINT, bigintLiteral(9L))), between(C_BIGINT, bigintLiteral(10L), bigintLiteral(15L)), between(C_BIGINT, bigintLiteral(25L), bigintLiteral(35L))), between(C_BIGINT, bigintLiteral(2L), bigintLiteral(12L)));
}
Also used : InListExpression(io.prestosql.sql.tree.InListExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression)

Example 15 with Expression

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

the class TestExpressionDomainTranslator method mergeAndAssert.

private Expression mergeAndAssert(boolean contains, Expression expression, Expression partExpression) {
    CubeRangeCanonicalizer canonicalizer = new CubeRangeCanonicalizer(metadata, TEST_SESSION, TYPES);
    Expression transformed = canonicalizer.mergePredicates(expression);
    ExpressionDomainTranslator.ExtractionResult expressionTD = ExpressionDomainTranslator.fromPredicate(metadata, TEST_SESSION, transformed, TYPES);
    Assert.assertEquals(expressionTD.getRemainingExpression(), BooleanLiteral.TRUE_LITERAL, "Still some part of expression not converted into TupleDomain");
    ExpressionDomainTranslator.ExtractionResult partTD = ExpressionDomainTranslator.fromPredicate(metadata, TEST_SESSION, partExpression, TYPES);
    Assert.assertEquals(partTD.getRemainingExpression(), BooleanLiteral.TRUE_LITERAL, "Still some part of expression not converted into TupleDomain");
    Assert.assertEquals(contains, expressionTD.getTupleDomain().contains(partTD.getTupleDomain()));
    return transformed;
}
Also used : CubeRangeCanonicalizer(io.prestosql.operator.CubeRangeCanonicalizer) ExtractionResult(io.prestosql.sql.planner.ExpressionDomainTranslator.ExtractionResult) InListExpression(io.prestosql.sql.tree.InListExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression)

Aggregations

Expression (io.prestosql.sql.tree.Expression)206 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)114 RowExpression (io.prestosql.spi.relation.RowExpression)80 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)80 InListExpression (io.prestosql.sql.tree.InListExpression)73 Symbol (io.prestosql.spi.plan.Symbol)68 NotExpression (io.prestosql.sql.tree.NotExpression)58 PlanNode (io.prestosql.spi.plan.PlanNode)47 CallExpression (io.prestosql.spi.relation.CallExpression)47 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)43 ArrayList (java.util.ArrayList)42 Test (org.testng.annotations.Test)42 Type (io.prestosql.spi.type.Type)41 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)41 LambdaExpression (io.prestosql.sql.tree.LambdaExpression)41 ImmutableList (com.google.common.collect.ImmutableList)40 List (java.util.List)40 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)38 SymbolReference (io.prestosql.sql.tree.SymbolReference)36 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)35