Search in sources :

Example 1 with FALSE_LITERAL

use of io.trino.sql.tree.BooleanLiteral.FALSE_LITERAL in project trino by trinodb.

the class DomainTranslator method toPredicate.

private Expression toPredicate(Session session, 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(session, domain.getType(), ranges, reference), discreteValues -> extractDisjuncts(session, 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(plannerContext.getMetadata(), disjuncts, TRUE_LITERAL);
}
Also used : IsNullPredicate(io.trino.sql.tree.IsNullPredicate) TableProceduresPropertyManager(io.trino.metadata.TableProceduresPropertyManager) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) ExpressionUtils.combineDisjunctsWithDefault(io.trino.sql.ExpressionUtils.combineDisjunctsWithDefault) InvocationConvention.simpleConvention(io.trino.spi.function.InvocationConvention.simpleConvention) PeekingIterator(com.google.common.collect.PeekingIterator) InterpretedFunctionInvoker(io.trino.sql.InterpretedFunctionInvoker) TypeUtils.isFloatingPointNaN(io.trino.spi.type.TypeUtils.isFloatingPointNaN) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) ExpressionUtils.or(io.trino.sql.ExpressionUtils.or) Slices(io.airlift.slice.Slices) Map(java.util.Map) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) DiscreteValues(io.trino.spi.predicate.DiscreteValues) SqlParser(io.trino.sql.parser.SqlParser) FunctionCall(io.trino.sql.tree.FunctionCall) BetweenPredicate(io.trino.sql.tree.BetweenPredicate) SliceUtf8.getCodePointAt(io.airlift.slice.SliceUtf8.getCodePointAt) SATURATED_FLOOR_CAST(io.trino.spi.function.OperatorType.SATURATED_FLOOR_CAST) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Range(io.trino.spi.predicate.Range) Ranges(io.trino.spi.predicate.Ranges) ResolvedFunction(io.trino.metadata.ResolvedFunction) Domain(io.trino.spi.predicate.Domain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) FALSE_LITERAL(io.trino.sql.tree.BooleanLiteral.FALSE_LITERAL) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) ValueSet(io.trino.spi.predicate.ValueSet) InPredicate(io.trino.sql.tree.InPredicate) Preconditions.checkState(com.google.common.base.Preconditions.checkState) LESS_THAN_OR_EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN_OR_EQUAL) SessionPropertyManager(io.trino.metadata.SessionPropertyManager) EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.EQUAL) NOT_EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.NOT_EQUAL) List(java.util.List) SymbolReference(io.trino.sql.tree.SymbolReference) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) InListExpression(io.trino.sql.tree.InListExpression) ExpressionUtils.combineConjuncts(io.trino.sql.ExpressionUtils.combineConjuncts) NEVER_NULL(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL) Session(io.trino.Session) PlannerContext(io.trino.sql.PlannerContext) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) AllowAllAccessControl(io.trino.security.AllowAllAccessControl) NullableValue(io.trino.spi.predicate.NullableValue) DoubleType(io.trino.spi.type.DoubleType) LikePredicate(io.trino.sql.tree.LikePredicate) Type(io.trino.spi.type.Type) TypeCoercion(io.trino.type.TypeCoercion) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) ArrayList(java.util.ArrayList) Cast(io.trino.sql.tree.Cast) VarcharType(io.trino.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) IsNotNullPredicate(io.trino.sql.tree.IsNotNullPredicate) NodeRef(io.trino.sql.tree.NodeRef) NotExpression(io.trino.sql.tree.NotExpression) Objects.requireNonNull(java.util.Objects.requireNonNull) NullLiteral(io.trino.sql.tree.NullLiteral) TableProceduresRegistry(io.trino.metadata.TableProceduresRegistry) Iterators.peekingIterator(com.google.common.collect.Iterators.peekingIterator) GREATER_THAN_OR_EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL) Nullable(javax.annotation.Nullable) AstVisitor(io.trino.sql.tree.AstVisitor) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) StringLiteral(io.trino.sql.tree.StringLiteral) OperatorNotFoundException(io.trino.metadata.OperatorNotFoundException) Throwables(com.google.common.base.Throwables) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) LESS_THAN(io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN) TupleDomain(io.trino.spi.predicate.TupleDomain) SliceUtf8.setCodePointAt(io.airlift.slice.SliceUtf8.setCodePointAt) GENERIC_INTERNAL_ERROR(io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) LikeFunctions(io.trino.type.LikeFunctions) Collectors.toList(java.util.stream.Collectors.toList) ExpressionUtils.and(io.trino.sql.ExpressionUtils.and) StatementAnalyzerFactory(io.trino.sql.analyzer.StatementAnalyzerFactory) TablePropertyManager(io.trino.metadata.TablePropertyManager) RealType(io.trino.spi.type.RealType) LogicalExpression(io.trino.sql.tree.LogicalExpression) SliceUtf8.countCodePoints(io.airlift.slice.SliceUtf8.countCodePoints) AnalyzePropertyManager(io.trino.metadata.AnalyzePropertyManager) SortedRangeSet(io.trino.spi.predicate.SortedRangeSet) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Expression(io.trino.sql.tree.Expression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) ArrayList(java.util.ArrayList) IsNullPredicate(io.trino.sql.tree.IsNullPredicate) NotExpression(io.trino.sql.tree.NotExpression)

Example 2 with FALSE_LITERAL

use of io.trino.sql.tree.BooleanLiteral.FALSE_LITERAL in project trino by trinodb.

the class SimplifyFilterPredicate method simplifyFilterExpression.

private Optional<Expression> simplifyFilterExpression(Expression expression) {
    if (expression instanceof IfExpression) {
        IfExpression ifExpression = (IfExpression) expression;
        Expression condition = ifExpression.getCondition();
        Expression trueValue = ifExpression.getTrueValue();
        Optional<Expression> falseValue = ifExpression.getFalseValue();
        if (trueValue.equals(TRUE_LITERAL) && (falseValue.isEmpty() || isNotTrue(falseValue.get()))) {
            return Optional.of(condition);
        }
        if (isNotTrue(trueValue) && falseValue.isPresent() && falseValue.get().equals(TRUE_LITERAL)) {
            return Optional.of(isFalseOrNullPredicate(condition));
        }
        if (falseValue.isPresent() && falseValue.get().equals(trueValue) && isDeterministic(trueValue, metadata)) {
            return Optional.of(trueValue);
        }
        if (isNotTrue(trueValue) && (falseValue.isEmpty() || isNotTrue(falseValue.get()))) {
            return Optional.of(FALSE_LITERAL);
        }
        if (condition.equals(TRUE_LITERAL)) {
            return Optional.of(trueValue);
        }
        if (isNotTrue(condition)) {
            return Optional.of(falseValue.orElse(FALSE_LITERAL));
        }
        return Optional.empty();
    }
    if (expression instanceof NullIfExpression) {
        NullIfExpression nullIfExpression = (NullIfExpression) expression;
        return Optional.of(LogicalExpression.and(nullIfExpression.getFirst(), isFalseOrNullPredicate(nullIfExpression.getSecond())));
    }
    if (expression instanceof SearchedCaseExpression) {
        SearchedCaseExpression caseExpression = (SearchedCaseExpression) expression;
        Optional<Expression> defaultValue = caseExpression.getDefaultValue();
        List<Expression> operands = caseExpression.getWhenClauses().stream().map(WhenClause::getOperand).collect(toImmutableList());
        List<Expression> results = caseExpression.getWhenClauses().stream().map(WhenClause::getResult).collect(toImmutableList());
        long trueResultsCount = results.stream().filter(result -> result.equals(TRUE_LITERAL)).count();
        long notTrueResultsCount = results.stream().filter(SimplifyFilterPredicate::isNotTrue).count();
        // all results true
        if (trueResultsCount == results.size() && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
            return Optional.of(TRUE_LITERAL);
        }
        // all results not true
        if (notTrueResultsCount == results.size() && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
            return Optional.of(FALSE_LITERAL);
        }
        // one result true, and remaining results not true
        if (trueResultsCount == 1 && notTrueResultsCount == results.size() - 1 && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
            ImmutableList.Builder<Expression> builder = ImmutableList.builder();
            for (WhenClause whenClause : caseExpression.getWhenClauses()) {
                Expression operand = whenClause.getOperand();
                Expression result = whenClause.getResult();
                if (isNotTrue(result)) {
                    builder.add(isFalseOrNullPredicate(operand));
                } else {
                    builder.add(operand);
                    return Optional.of(combineConjuncts(metadata, builder.build()));
                }
            }
        }
        // all results not true, and default true
        if (notTrueResultsCount == results.size() && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
            ImmutableList.Builder<Expression> builder = ImmutableList.builder();
            operands.stream().forEach(operand -> builder.add(isFalseOrNullPredicate(operand)));
            return Optional.of(combineConjuncts(metadata, builder.build()));
        }
        // skip clauses with not true conditions
        List<WhenClause> whenClauses = new ArrayList<>();
        for (WhenClause whenClause : caseExpression.getWhenClauses()) {
            Expression operand = whenClause.getOperand();
            if (operand.equals(TRUE_LITERAL)) {
                if (whenClauses.isEmpty()) {
                    return Optional.of(whenClause.getResult());
                }
                return Optional.of(new SearchedCaseExpression(whenClauses, Optional.of(whenClause.getResult())));
            }
            if (!isNotTrue(operand)) {
                whenClauses.add(whenClause);
            }
        }
        if (whenClauses.isEmpty()) {
            return Optional.of(defaultValue.orElse(FALSE_LITERAL));
        }
        if (whenClauses.size() < caseExpression.getWhenClauses().size()) {
            return Optional.of(new SearchedCaseExpression(whenClauses, defaultValue));
        }
        return Optional.empty();
    }
    if (expression instanceof SimpleCaseExpression) {
        SimpleCaseExpression caseExpression = (SimpleCaseExpression) expression;
        Optional<Expression> defaultValue = caseExpression.getDefaultValue();
        if (caseExpression.getOperand() instanceof NullLiteral) {
            return Optional.of(defaultValue.orElse(FALSE_LITERAL));
        }
        List<Expression> results = caseExpression.getWhenClauses().stream().map(WhenClause::getResult).collect(toImmutableList());
        if (results.stream().allMatch(result -> result.equals(TRUE_LITERAL)) && defaultValue.isPresent() && defaultValue.get().equals(TRUE_LITERAL)) {
            return Optional.of(TRUE_LITERAL);
        }
        if (results.stream().allMatch(SimplifyFilterPredicate::isNotTrue) && (defaultValue.isEmpty() || isNotTrue(defaultValue.get()))) {
            return Optional.of(FALSE_LITERAL);
        }
        return Optional.empty();
    }
    return Optional.empty();
}
Also used : IsNullPredicate(io.trino.sql.tree.IsNullPredicate) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) Patterns.filter(io.trino.sql.planner.plan.Patterns.filter) NullIfExpression(io.trino.sql.tree.NullIfExpression) FilterNode(io.trino.sql.planner.plan.FilterNode) ArrayList(java.util.ArrayList) Cast(io.trino.sql.tree.Cast) ImmutableList(com.google.common.collect.ImmutableList) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) DeterminismEvaluator.isDeterministic(io.trino.sql.planner.DeterminismEvaluator.isDeterministic) NotExpression(io.trino.sql.tree.NotExpression) NullLiteral(io.trino.sql.tree.NullLiteral) Rule(io.trino.sql.planner.iterative.Rule) WhenClause(io.trino.sql.tree.WhenClause) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) FALSE_LITERAL(io.trino.sql.tree.BooleanLiteral.FALSE_LITERAL) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) List(java.util.List) Pattern(io.trino.matching.Pattern) IfExpression(io.trino.sql.tree.IfExpression) Captures(io.trino.matching.Captures) LogicalExpression(io.trino.sql.tree.LogicalExpression) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) ExpressionUtils.extractConjuncts(io.trino.sql.ExpressionUtils.extractConjuncts) ExpressionUtils.combineConjuncts(io.trino.sql.ExpressionUtils.combineConjuncts) NullIfExpression(io.trino.sql.tree.NullIfExpression) IfExpression(io.trino.sql.tree.IfExpression) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) WhenClause(io.trino.sql.tree.WhenClause) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) NotExpression(io.trino.sql.tree.NotExpression) IfExpression(io.trino.sql.tree.IfExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) Expression(io.trino.sql.tree.Expression) NullIfExpression(io.trino.sql.tree.NullIfExpression) NullLiteral(io.trino.sql.tree.NullLiteral)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ExpressionUtils.combineConjuncts (io.trino.sql.ExpressionUtils.combineConjuncts)2 FALSE_LITERAL (io.trino.sql.tree.BooleanLiteral.FALSE_LITERAL)2 TRUE_LITERAL (io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL)2 Cast (io.trino.sql.tree.Cast)2 Expression (io.trino.sql.tree.Expression)2 IsNullPredicate (io.trino.sql.tree.IsNullPredicate)2 LogicalExpression (io.trino.sql.tree.LogicalExpression)2 NotExpression (io.trino.sql.tree.NotExpression)2 NullLiteral (io.trino.sql.tree.NullLiteral)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Throwables (com.google.common.base.Throwables)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1