Java Code Examples for io.prestosql.spi.predicate.TupleDomain#isAll()

The following examples show how to use io.prestosql.spi.predicate.TupleDomain#isAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TpchMetadata.java    From presto with Apache License 2.0 6 votes vote down vote up
private Map<TpchColumn<?>, List<Object>> getColumnValuesRestrictions(TpchTable<?> tpchTable, Constraint constraint)
{
    TupleDomain<ColumnHandle> constraintSummary = constraint.getSummary();
    if (constraintSummary.isAll()) {
        return emptyMap();
    }
    else if (constraintSummary.isNone()) {
        Set<TpchColumn<?>> columns = ImmutableSet.copyOf(tpchTable.getColumns());
        return asMap(columns, key -> emptyList());
    }
    else {
        Map<ColumnHandle, Domain> domains = constraintSummary.getDomains().get();
        Optional<Domain> orderStatusDomain = Optional.ofNullable(domains.get(toColumnHandle(ORDER_STATUS)));
        Optional<Map<TpchColumn<?>, List<Object>>> allowedColumnValues = orderStatusDomain.map(domain -> {
            List<Object> allowedValues = ORDER_STATUS_VALUES.stream()
                    .filter(domain::includesNullableValue)
                    .collect(toList());
            return avoidTrivialOrderStatusRestriction(allowedValues);
        });
        return allowedColumnValues.orElse(emptyMap());
    }
}
 
Example 2
Source File: ExpressionConverter.java    From presto with Apache License 2.0 6 votes vote down vote up
public static Expression toIcebergExpression(TupleDomain<IcebergColumnHandle> tupleDomain)
{
    if (tupleDomain.isAll()) {
        return alwaysTrue();
    }
    if (tupleDomain.getDomains().isEmpty()) {
        return alwaysFalse();
    }
    Map<IcebergColumnHandle, Domain> domainMap = tupleDomain.getDomains().get();
    Expression expression = alwaysTrue();
    for (Map.Entry<IcebergColumnHandle, Domain> entry : domainMap.entrySet()) {
        IcebergColumnHandle columnHandle = entry.getKey();
        Domain domain = entry.getValue();
        expression = and(expression, toIcebergExpression(columnHandle.getName(), columnHandle.getType(), domain));
    }
    return expression;
}
 
Example 3
Source File: TableLayoutResult.java    From presto with Apache License 2.0 4 votes vote down vote up
public static TupleDomain<ColumnHandle> computeEnforced(TupleDomain<ColumnHandle> predicate, TupleDomain<ColumnHandle> unenforced)
{
    if (predicate.isNone()) {
        // If the engine requests that the connector provides a layout with a domain of "none". The connector can have two possible reactions, either:
        // 1. The connector can provide an empty table layout.
        //   * There would be no unenforced predicate, i.e., unenforced predicate is TupleDomain.all().
        //   * The predicate was successfully enforced. Enforced predicate would be same as predicate: TupleDomain.none().
        // 2. The connector can't/won't.
        //   * The connector would tell the engine to put a filter on top of the scan, i.e., unenforced predicate is TupleDomain.none().
        //   * The connector didn't successfully enforce anything. Therefore, enforced predicate would be TupleDomain.all().
        if (unenforced.isNone()) {
            return TupleDomain.all();
        }
        if (unenforced.isAll()) {
            return TupleDomain.none();
        }
        throw new IllegalArgumentException();
    }

    // The engine requested the connector provides a layout with a non-none TupleDomain.
    // A TupleDomain is effectively a list of column-Domain pairs.
    // The connector is expected enforce the respective domain entirely on none, some, or all of the columns.
    // 1. When the connector could enforce none of the domains, the unenforced would be equal to predicate;
    // 2. When the connector could enforce some of the domains, the unenforced would contain a subset of the column-Domain pairs;
    // 3. When the connector could enforce all of the domains, the unenforced would be TupleDomain.all().

    // In all 3 cases shown above, the unenforced is not TupleDomain.none().
    checkArgument(!unenforced.isNone());

    Map<ColumnHandle, Domain> predicateDomains = predicate.getDomains().get();
    Map<ColumnHandle, Domain> unenforcedDomains = unenforced.getDomains().get();
    ImmutableMap.Builder<ColumnHandle, Domain> enforcedDomainsBuilder = ImmutableMap.builder();
    for (Map.Entry<ColumnHandle, Domain> entry : predicateDomains.entrySet()) {
        ColumnHandle predicateColumnHandle = entry.getKey();
        if (unenforcedDomains.containsKey(predicateColumnHandle)) {
            checkArgument(
                    entry.getValue().equals(unenforcedDomains.get(predicateColumnHandle)),
                    "Enforced tuple domain cannot be determined. The connector is expected to enforce the respective domain entirely on none, some, or all of the column.");
        }
        else {
            enforcedDomainsBuilder.put(predicateColumnHandle, entry.getValue());
        }
    }
    Map<ColumnHandle, Domain> enforcedDomains = enforcedDomainsBuilder.build();
    checkArgument(
            enforcedDomains.size() + unenforcedDomains.size() == predicateDomains.size(),
            "Enforced tuple domain cannot be determined. Connector returned an unenforced TupleDomain that contains columns not in predicate.");
    return TupleDomain.withColumnDomains(enforcedDomains);
}
 
Example 4
Source File: DomainTranslator.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
protected ExtractionResult visitLogicalBinaryExpression(LogicalBinaryExpression node, Boolean complement)
{
    ExtractionResult leftResult = process(node.getLeft(), complement);
    ExtractionResult rightResult = process(node.getRight(), complement);

    TupleDomain<Symbol> leftTupleDomain = leftResult.getTupleDomain();
    TupleDomain<Symbol> rightTupleDomain = rightResult.getTupleDomain();

    LogicalBinaryExpression.Operator operator = complement ? node.getOperator().flip() : node.getOperator();
    switch (operator) {
        case AND:
            return new ExtractionResult(
                    leftTupleDomain.intersect(rightTupleDomain),
                    combineConjuncts(metadata, leftResult.getRemainingExpression(), rightResult.getRemainingExpression()));

        case OR:
            TupleDomain<Symbol> columnUnionedTupleDomain = TupleDomain.columnWiseUnion(leftTupleDomain, rightTupleDomain);

            // In most cases, the columnUnionedTupleDomain is only a superset of the actual strict union
            // and so we can return the current node as the remainingExpression so that all bounds will be double checked again at execution time.
            Expression remainingExpression = complementIfNecessary(node, complement);

            // However, there are a few cases where the column-wise union is actually equivalent to the strict union, so we if can detect
            // some of these cases, we won't have to double check the bounds unnecessarily at execution time.

            // We can only make inferences if the remaining expressions on both side are equal and deterministic
            if (leftResult.getRemainingExpression().equals(rightResult.getRemainingExpression()) &&
                    DeterminismEvaluator.isDeterministic(leftResult.getRemainingExpression(), metadata)) {
                // The column-wise union is equivalent to the strict union if
                // 1) If both TupleDomains consist of the same exact single column (e.g. left TupleDomain => (a > 0), right TupleDomain => (a < 10))
                // 2) If one TupleDomain is a superset of the other (e.g. left TupleDomain => (a > 0, b > 0 && b < 10), right TupleDomain => (a > 5, b = 5))
                boolean matchingSingleSymbolDomains = !leftTupleDomain.isNone()
                        && !rightTupleDomain.isNone()
                        && leftTupleDomain.getDomains().get().size() == 1
                        && rightTupleDomain.getDomains().get().size() == 1
                        && leftTupleDomain.getDomains().get().keySet().equals(rightTupleDomain.getDomains().get().keySet());
                boolean oneSideIsSuperSet = leftTupleDomain.contains(rightTupleDomain) || rightTupleDomain.contains(leftTupleDomain);

                if (oneSideIsSuperSet) {
                    remainingExpression = leftResult.getRemainingExpression();
                }
                else if (matchingSingleSymbolDomains) {
                    // Types REAL and DOUBLE require special handling because they include NaN value. In this case, we cannot rely on the union of domains.
                    // That is because domains covering the value set partially might union up to a domain covering the whole value set.
                    // While the component domains didn't include NaN, the resulting domain could be further translated to predicate "TRUE" or "a IS NOT NULL",
                    // which is satisfied by NaN. So during domain union, NaN might be implicitly added.
                    // Example: Let 'a' be a column of type DOUBLE.
                    //          Let left TupleDomain => (a > 0) /false for NaN/, right TupleDomain => (a < 10) /false for NaN/.
                    //          Unioned TupleDomain => "is not null" /true for NaN/
                    // To guard against wrong results, the current node is returned as the remainingExpression.
                    Domain leftDomain = getOnlyElement(leftTupleDomain.getDomains().get().values());
                    Domain rightDomain = getOnlyElement(rightTupleDomain.getDomains().get().values());
                    Type type = leftDomain.getType();

                    // A Domain of a floating point type contains NaN in the following cases:
                    // 1. When it contains all the values of the type and null.
                    //    In such case the domain is 'all', and if it is the only domain
                    //    in the TupleDomain, the TupleDomain gets normalized to TupleDomain 'all'.
                    // 2. When it contains all the values of the type and doesn't contain null.
                    //    In such case no normalization on the level of TupleDomain takes place,
                    //    and the check for NaN is done by inspecting the Domain's valueSet.
                    //    NaN is included when the valueSet is 'all'.
                    boolean unionedDomainContainsNaN = columnUnionedTupleDomain.isAll() ||
                            (columnUnionedTupleDomain.getDomains().isPresent() &&
                                    getOnlyElement(columnUnionedTupleDomain.getDomains().get().values()).getValues().isAll());
                    boolean implicitlyAddedNaN = (type instanceof RealType || type instanceof DoubleType) &&
                            !leftDomain.getValues().isAll() &&
                            !rightDomain.getValues().isAll() &&
                            unionedDomainContainsNaN;
                    if (!implicitlyAddedNaN) {
                        remainingExpression = leftResult.getRemainingExpression();
                    }
                }
            }

            return new ExtractionResult(columnUnionedTupleDomain, remainingExpression);

        default:
            throw new AssertionError("Unknown operator: " + node.getOperator());
    }
}