Search in sources :

Example 1 with IsNullPredicate

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

the class TestExpressionUtils method testNormalize.

@Test
public void testNormalize() {
    assertNormalize(new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1")));
    assertNormalize(new IsNullPredicate(name("a")));
    assertNormalize(new NotExpression(new LikePredicate(name("a"), new StringLiteral("x%"), Optional.empty())));
    assertNormalize(new NotExpression(new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1"))), new ComparisonExpression(NOT_EQUAL, name("a"), new LongLiteral("1")));
    assertNormalize(new NotExpression(new ComparisonExpression(NOT_EQUAL, name("a"), new LongLiteral("1"))), new ComparisonExpression(EQUAL, name("a"), new LongLiteral("1")));
    // Cannot normalize IS DISTINCT FROM yet
    assertNormalize(new NotExpression(new ComparisonExpression(IS_DISTINCT_FROM, name("a"), new LongLiteral("1"))));
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) NotExpression(io.prestosql.sql.tree.NotExpression) LikePredicate(io.prestosql.sql.tree.LikePredicate) Test(org.testng.annotations.Test)

Example 2 with IsNullPredicate

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

the class TestCostCalculator method testFilter.

@Test
public void testFilter() {
    TableScanNode tableScan = tableScan("ts", "string");
    IsNullPredicate expression = new IsNullPredicate(new SymbolReference("string"));
    FilterNode filter = new FilterNode(new PlanNodeId("filter"), tableScan, castToRowExpression(expression));
    Map<String, PlanCostEstimate> costs = ImmutableMap.of("ts", cpuCost(1000));
    Map<String, PlanNodeStatsEstimate> stats = ImmutableMap.of("filter", statsEstimate(filter, 4000), "ts", statsEstimate(tableScan, 1000));
    Map<String, Type> types = ImmutableMap.of("string", VARCHAR);
    assertCost(filter, costs, stats, types).cpu(1000 + 1000 * OFFSET_AND_IS_NULL_OVERHEAD).memory(0).network(0);
    assertCostEstimatedExchanges(filter, costs, stats, types).cpu(1000 + 1000 * OFFSET_AND_IS_NULL_OVERHEAD).memory(0).network(0);
    assertCostFragmentedPlan(filter, costs, stats, types).cpu(1000 + 1000 * OFFSET_AND_IS_NULL_OVERHEAD).memory(0).network(0);
    assertCostHasUnknownComponentsForUnknownStats(filter, types);
}
Also used : PlanNodeId(io.prestosql.spi.plan.PlanNodeId) Type(io.prestosql.spi.type.Type) TableScanNode(io.prestosql.spi.plan.TableScanNode) SymbolReference(io.prestosql.sql.tree.SymbolReference) FilterNode(io.prestosql.spi.plan.FilterNode) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) Test(org.testng.annotations.Test)

Example 3 with IsNullPredicate

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

the class TransformCorrelatedInPredicateToJoin method buildInPredicateEquivalent.

private PlanNode buildInPredicateEquivalent(ApplyNode apply, InPredicate inPredicate, Symbol inPredicateOutputSymbol, Decorrelated decorrelated, PlanNodeIdAllocator idAllocator, PlanSymbolAllocator planSymbolAllocator) {
    Expression correlationCondition = and(decorrelated.getCorrelatedPredicates());
    PlanNode decorrelatedBuildSource = decorrelated.getDecorrelatedNode();
    AssignUniqueId probeSide = new AssignUniqueId(idAllocator.getNextId(), apply.getInput(), planSymbolAllocator.newSymbol("unique", BIGINT));
    Symbol buildSideKnownNonNull = planSymbolAllocator.newSymbol("buildSideKnownNonNull", BIGINT);
    ProjectNode buildSide = new ProjectNode(idAllocator.getNextId(), decorrelatedBuildSource, Assignments.builder().putAll(identityAsSymbolReferences(decorrelatedBuildSource.getOutputSymbols())).put(buildSideKnownNonNull, castToRowExpression(bigint(0))).build());
    Symbol probeSideSymbol = SymbolUtils.from(inPredicate.getValue());
    Symbol buildSideSymbol = SymbolUtils.from(inPredicate.getValueList());
    Expression joinExpression = and(or(new IsNullPredicate(toSymbolReference(probeSideSymbol)), new ComparisonExpression(ComparisonExpression.Operator.EQUAL, toSymbolReference(probeSideSymbol), toSymbolReference(buildSideSymbol)), new IsNullPredicate(toSymbolReference(buildSideSymbol))), correlationCondition);
    JoinNode leftOuterJoin = leftOuterJoin(idAllocator, probeSide, buildSide, joinExpression);
    Symbol matchConditionSymbol = planSymbolAllocator.newSymbol("matchConditionSymbol", BOOLEAN);
    Expression matchCondition = and(isNotNull(probeSideSymbol), isNotNull(buildSideSymbol));
    Symbol nullMatchConditionSymbol = planSymbolAllocator.newSymbol("nullMatchConditionSymbol", BOOLEAN);
    Expression nullMatchCondition = and(isNotNull(buildSideKnownNonNull), not(matchCondition));
    ProjectNode preProjection = new ProjectNode(idAllocator.getNextId(), leftOuterJoin, Assignments.builder().putAll(AssignmentUtils.identityAsSymbolReferences(leftOuterJoin.getOutputSymbols())).put(matchConditionSymbol, castToRowExpression(matchCondition)).put(nullMatchConditionSymbol, castToRowExpression(nullMatchCondition)).build());
    Symbol countMatchesSymbol = planSymbolAllocator.newSymbol("countMatches", BIGINT);
    Symbol countNullMatchesSymbol = planSymbolAllocator.newSymbol("countNullMatches", BIGINT);
    AggregationNode aggregation = new AggregationNode(idAllocator.getNextId(), preProjection, ImmutableMap.<Symbol, AggregationNode.Aggregation>builder().put(countMatchesSymbol, countWithFilter(matchConditionSymbol)).put(countNullMatchesSymbol, countWithFilter(nullMatchConditionSymbol)).build(), singleGroupingSet(probeSide.getOutputSymbols()), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty(), AggregationNode.AggregationType.HASH, Optional.empty());
    // TODO since we care only about "some count > 0", we could have specialized node instead of leftOuterJoin that does the job without materializing join results
    SearchedCaseExpression inPredicateEquivalent = new SearchedCaseExpression(ImmutableList.of(new WhenClause(isGreaterThan(countMatchesSymbol, 0), booleanConstant(true)), new WhenClause(isGreaterThan(countNullMatchesSymbol, 0), booleanConstant(null))), Optional.of(booleanConstant(false)));
    return new ProjectNode(idAllocator.getNextId(), aggregation, Assignments.builder().putAll(identityAsSymbolReferences(apply.getInput().getOutputSymbols())).put(inPredicateOutputSymbol, castToRowExpression(inPredicateEquivalent)).build());
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) WhenClause(io.prestosql.sql.tree.WhenClause) PlanNode(io.prestosql.spi.plan.PlanNode) AssignUniqueId(io.prestosql.sql.planner.plan.AssignUniqueId) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) JoinNode(io.prestosql.spi.plan.JoinNode) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) ProjectNode(io.prestosql.spi.plan.ProjectNode) AggregationNode(io.prestosql.spi.plan.AggregationNode)

Example 4 with IsNullPredicate

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

the class ExpressionDomainTranslator method toPredicate.

private Expression toPredicate(Domain domain, SymbolReference reference) {
    if (domain.getValues().isNone()) {
        return domain.isNullAllowed() ? new IsNullPredicate(reference) : FALSE_LITERAL;
    }
    if (domain.getValues().isAll()) {
        return domain.isNullAllowed() ? TRUE_LITERAL : new NotExpression(new IsNullPredicate(reference));
    }
    List<Expression> disjuncts = new ArrayList<>();
    disjuncts.addAll(domain.getValues().getValuesProcessor().transform(ranges -> extractDisjuncts(domain.getType(), ranges, reference), discreteValues -> extractDisjuncts(domain.getType(), discreteValues, reference), allOrNone -> {
        throw new IllegalStateException("Case should not be reachable");
    }));
    // Add nullability disjuncts
    if (domain.isNullAllowed()) {
        disjuncts.add(new IsNullPredicate(reference));
    }
    return combineDisjunctsWithDefault(disjuncts, TRUE_LITERAL);
}
Also used : GREATER_THAN_OR_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL) LESS_THAN_OR_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.LESS_THAN_OR_EQUAL) DiscreteValues(io.prestosql.spi.predicate.DiscreteValues) OperatorNotFoundException(io.prestosql.metadata.OperatorNotFoundException) FALSE_LITERAL(io.prestosql.sql.tree.BooleanLiteral.FALSE_LITERAL) ValueSet(io.prestosql.spi.predicate.ValueSet) NullableValue(io.prestosql.spi.predicate.NullableValue) SqlParser(io.prestosql.sql.parser.SqlParser) PeekingIterator(com.google.common.collect.PeekingIterator) Cast(io.prestosql.sql.tree.Cast) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Slices(io.airlift.slice.Slices) Map(java.util.Map) EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.EQUAL) Type(io.prestosql.spi.type.Type) TypeCoercion(io.prestosql.type.TypeCoercion) SliceUtf8.getCodePointAt(io.airlift.slice.SliceUtf8.getCodePointAt) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) ExpressionUtils.or(io.prestosql.sql.ExpressionUtils.or) ImmutableMap(com.google.common.collect.ImmutableMap) CastType(io.prestosql.metadata.CastType) IsNotNullPredicate(io.prestosql.sql.tree.IsNotNullPredicate) LikeFunctions(io.prestosql.type.LikeFunctions) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) NullLiteral(io.prestosql.sql.tree.NullLiteral) SortedRangeSet(io.prestosql.spi.predicate.SortedRangeSet) Metadata(io.prestosql.metadata.Metadata) NodeRef(io.prestosql.sql.tree.NodeRef) Preconditions.checkState(com.google.common.base.Preconditions.checkState) FunctionHandle(io.prestosql.spi.function.FunctionHandle) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) List(java.util.List) ExpressionUtils(io.prestosql.sql.ExpressionUtils) SymbolReference(io.prestosql.sql.tree.SymbolReference) StringLiteral(io.prestosql.sql.tree.StringLiteral) Domain(io.prestosql.spi.predicate.Domain) Optional(java.util.Optional) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) SymbolUtils.from(io.prestosql.sql.planner.SymbolUtils.from) Utils(io.prestosql.spi.predicate.Utils) InPredicate(io.prestosql.sql.tree.InPredicate) LESS_THAN(io.prestosql.sql.tree.ComparisonExpression.Operator.LESS_THAN) Slice(io.airlift.slice.Slice) InListExpression(io.prestosql.sql.tree.InListExpression) Marker(io.prestosql.spi.predicate.Marker) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) TRUE_LITERAL(io.prestosql.sql.tree.BooleanLiteral.TRUE_LITERAL) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) InterpretedFunctionInvoker(io.prestosql.sql.InterpretedFunctionInvoker) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) Range(io.prestosql.spi.predicate.Range) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) LikePredicate(io.prestosql.sql.tree.LikePredicate) CAST(io.prestosql.metadata.CastType.CAST) Comparator.comparing(java.util.Comparator.comparing) AstVisitor(io.prestosql.sql.tree.AstVisitor) Iterators.peekingIterator(com.google.common.collect.Iterators.peekingIterator) Block(io.prestosql.spi.block.Block) Nullable(javax.annotation.Nullable) GREATER_THAN(io.prestosql.sql.tree.ComparisonExpression.Operator.GREATER_THAN) Symbol(io.prestosql.spi.plan.Symbol) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) ExpressionUtils.combineConjuncts(io.prestosql.sql.ExpressionUtils.combineConjuncts) Ranges(io.prestosql.spi.predicate.Ranges) TupleDomain(io.prestosql.spi.predicate.TupleDomain) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) ExpressionUtils.and(io.prestosql.sql.ExpressionUtils.and) SliceUtf8.setCodePointAt(io.airlift.slice.SliceUtf8.setCodePointAt) ExpressionUtils.combineDisjunctsWithDefault(io.prestosql.sql.ExpressionUtils.combineDisjunctsWithDefault) Collectors.toList(java.util.stream.Collectors.toList) NOT_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.NOT_EQUAL) SliceUtf8.countCodePoints(io.airlift.slice.SliceUtf8.countCodePoints) VarcharType(io.prestosql.spi.type.VarcharType) Expression(io.prestosql.sql.tree.Expression) InListExpression(io.prestosql.sql.tree.InListExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ArrayList(java.util.ArrayList) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) NotExpression(io.prestosql.sql.tree.NotExpression)

Example 5 with IsNullPredicate

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

the class TestEffectivePredicateExtractor method testValues.

@Test
public void testValues() {
    TypeProvider types = TypeProvider.copyOf(ImmutableMap.<Symbol, Type>builder().put(A, BIGINT).put(B, BIGINT).build());
    // one column
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)))), types, typeAnalyzer), new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))));
    // one column with null
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), or(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new IsNullPredicate(AE)));
    // all nulls
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), new IsNullPredicate(AE));
    // many rows
    List<List<RowExpression>> rows = IntStream.range(0, 500).mapToObj(TestEffectivePredicateExtractor::bigintLiteralRowExpression).map(ImmutableList::of).collect(toImmutableList());
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), rows), types, typeAnalyzer), new BetweenPredicate(AE, bigintLiteral(0), bigintLiteral(499)));
    // multiple columns
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), bigintLiteralRowExpression(100)), ImmutableList.of(bigintLiteralRowExpression(2), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new InPredicate(BE, new InListExpression(ImmutableList.of(bigintLiteral(100), bigintLiteral(200))))));
    // multiple columns with null
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString())), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(or(new ComparisonExpression(EQUAL, AE, bigintLiteral(1)), new IsNullPredicate(AE)), or(new ComparisonExpression(EQUAL, BE, bigintLiteral(200)), new IsNullPredicate(BE))));
    // non-deterministic
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()))))), types, typeAnalyzer), new ComparisonExpression(EQUAL, AE, bigintLiteral(1)));
    // non-constant
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(castToRowExpression(BE)))), types, typeAnalyzer), TRUE_LITERAL);
}
Also used : Cast(io.prestosql.sql.tree.Cast) ValuesNode(io.prestosql.spi.plan.ValuesNode) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) Symbol(io.prestosql.spi.plan.Symbol) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) NullLiteral(io.prestosql.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Aggregations

IsNullPredicate (io.prestosql.sql.tree.IsNullPredicate)5 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)4 Symbol (io.prestosql.spi.plan.Symbol)3 Type (io.prestosql.spi.type.Type)3 NotExpression (io.prestosql.sql.tree.NotExpression)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 BetweenPredicate (io.prestosql.sql.tree.BetweenPredicate)2 Cast (io.prestosql.sql.tree.Cast)2 Expression (io.prestosql.sql.tree.Expression)2 InListExpression (io.prestosql.sql.tree.InListExpression)2 InPredicate (io.prestosql.sql.tree.InPredicate)2 NullLiteral (io.prestosql.sql.tree.NullLiteral)2 List (java.util.List)2 Test (org.testng.annotations.Test)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 Iterators.peekingIterator (com.google.common.collect.Iterators.peekingIterator)1