Java Code Examples for io.prestosql.spi.predicate.Range#isSingleValue()

The following examples show how to use io.prestosql.spi.predicate.Range#isSingleValue() . 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: InformationSchemaMetadata.java    From presto with Apache License 2.0 5 votes vote down vote up
private <T> Optional<Set<String>> filterString(TupleDomain<T> constraint, T column)
{
    if (constraint.isNone()) {
        return Optional.of(ImmutableSet.of());
    }

    Domain domain = constraint.getDomains().get().get(column);
    if (domain == null) {
        return Optional.empty();
    }

    if (domain.isSingleValue()) {
        return Optional.of(ImmutableSet.of(((Slice) domain.getSingleValue()).toStringUtf8()));
    }
    if (domain.getValues() instanceof EquatableValueSet) {
        Collection<Object> values = ((EquatableValueSet) domain.getValues()).getValues();
        return Optional.of(values.stream()
                .map(Slice.class::cast)
                .map(Slice::toStringUtf8)
                .collect(toImmutableSet()));
    }
    if (domain.getValues() instanceof SortedRangeSet) {
        ImmutableSet.Builder<String> result = ImmutableSet.builder();
        for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
            checkState(!range.isAll()); // Already checked
            if (!range.isSingleValue()) {
                return Optional.empty();
            }

            result.add(((Slice) range.getSingleValue()).toStringUtf8());
        }

        return Optional.of(result.build());
    }
    return Optional.empty();
}
 
Example 2
Source File: QueryBuilder.java    From presto with Apache License 2.0 4 votes vote down vote up
private String toPredicate(String columnName, Domain domain, JdbcColumnHandle column, List<TypeAndValue> accumulator)
{
    if (domain.getValues().isNone()) {
        return domain.isNullAllowed() ? quote(columnName) + " IS NULL" : ALWAYS_FALSE;
    }

    if (domain.getValues().isAll()) {
        return domain.isNullAllowed() ? ALWAYS_TRUE : quote(columnName) + " IS NOT NULL";
    }

    List<String> disjuncts = new ArrayList<>();
    List<Object> singleValues = new ArrayList<>();
    for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
        checkState(!range.isAll()); // Already checked
        if (range.isSingleValue()) {
            singleValues.add(range.getLow().getValue());
        }
        else {
            List<String> rangeConjuncts = new ArrayList<>();
            if (!range.getLow().isLowerUnbounded()) {
                switch (range.getLow().getBound()) {
                    case ABOVE:
                        rangeConjuncts.add(toPredicate(columnName, ">", range.getLow().getValue(), column, accumulator));
                        break;
                    case EXACTLY:
                        rangeConjuncts.add(toPredicate(columnName, ">=", range.getLow().getValue(), column, accumulator));
                        break;
                    case BELOW:
                        throw new IllegalArgumentException("Low marker should never use BELOW bound");
                    default:
                        throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
                }
            }
            if (!range.getHigh().isUpperUnbounded()) {
                switch (range.getHigh().getBound()) {
                    case ABOVE:
                        throw new IllegalArgumentException("High marker should never use ABOVE bound");
                    case EXACTLY:
                        rangeConjuncts.add(toPredicate(columnName, "<=", range.getHigh().getValue(), column, accumulator));
                        break;
                    case BELOW:
                        rangeConjuncts.add(toPredicate(columnName, "<", range.getHigh().getValue(), column, accumulator));
                        break;
                    default:
                        throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
                }
            }
            // If rangeConjuncts is null, then the range was ALL, which should already have been checked for
            checkState(!rangeConjuncts.isEmpty());
            disjuncts.add("(" + Joiner.on(" AND ").join(rangeConjuncts) + ")");
        }
    }

    // Add back all of the possible single values either as an equality or an IN predicate
    if (singleValues.size() == 1) {
        disjuncts.add(toPredicate(columnName, "=", getOnlyElement(singleValues), column, accumulator));
    }
    else if (singleValues.size() > 1) {
        for (Object value : singleValues) {
            bindValue(value, column, accumulator);
        }
        String values = Joiner.on(",").join(nCopies(singleValues.size(), "?"));
        disjuncts.add(quote(columnName) + " IN (" + values + ")");
    }

    // Add nullability disjuncts
    checkState(!disjuncts.isEmpty());
    if (domain.isNullAllowed()) {
        disjuncts.add(quote(columnName) + " IS NULL");
    }

    return "(" + Joiner.on(" OR ").join(disjuncts) + ")";
}
 
Example 3
Source File: ShardPredicate.java    From presto with Apache License 2.0 4 votes vote down vote up
public static ShardPredicate create(TupleDomain<RaptorColumnHandle> tupleDomain)
{
    StringJoiner predicate = new StringJoiner(" AND ").setEmptyValue("true");
    ImmutableList.Builder<JDBCType> types = ImmutableList.builder();
    ImmutableList.Builder<Object> values = ImmutableList.builder();

    for (Entry<RaptorColumnHandle, Domain> entry : tupleDomain.getDomains().get().entrySet()) {
        Domain domain = entry.getValue();
        if (domain.isNullAllowed() || domain.isAll()) {
            continue;
        }
        RaptorColumnHandle handle = entry.getKey();
        Type type = handle.getColumnType();

        JDBCType jdbcType = jdbcType(type);
        if (jdbcType == null) {
            continue;
        }

        if (handle.isShardUuid()) {
            predicate.add(createShardPredicate(types, values, domain, jdbcType));
            continue;
        }

        if (!domain.getType().isOrderable()) {
            continue;
        }

        StringJoiner columnPredicate = new StringJoiner(" OR ", "(", ")").setEmptyValue("true");
        Ranges ranges = domain.getValues().getRanges();

        // prevent generating complicated metadata queries
        if (ranges.getRangeCount() > MAX_RANGE_COUNT) {
            continue;
        }

        for (Range range : ranges.getOrderedRanges()) {
            Object minValue = null;
            Object maxValue = null;
            if (range.isSingleValue()) {
                minValue = range.getSingleValue();
                maxValue = range.getSingleValue();
            }
            else {
                if (!range.getLow().isLowerUnbounded()) {
                    minValue = range.getLow().getValue();
                }
                if (!range.getHigh().isUpperUnbounded()) {
                    maxValue = range.getHigh().getValue();
                }
            }

            String min;
            String max;
            if (handle.isBucketNumber()) {
                min = "bucket_number";
                max = "bucket_number";
            }
            else {
                min = minColumn(handle.getColumnId());
                max = maxColumn(handle.getColumnId());
            }

            StringJoiner rangePredicate = new StringJoiner(" AND ", "(", ")").setEmptyValue("true");
            if (minValue != null) {
                rangePredicate.add(format("(%s >= ? OR %s IS NULL)", max, max));
                types.add(jdbcType);
                values.add(minValue);
            }
            if (maxValue != null) {
                rangePredicate.add(format("(%s <= ? OR %s IS NULL)", min, min));
                types.add(jdbcType);
                values.add(maxValue);
            }
            columnPredicate.add(rangePredicate.toString());
        }
        predicate.add(columnPredicate.toString());
    }
    return new ShardPredicate(predicate.toString(), types.build(), values.build());
}
 
Example 4
Source File: ElasticsearchQueryBuilder.java    From presto with Apache License 2.0 4 votes vote down vote up
private static QueryBuilder buildTermQuery(BoolQueryBuilder queryBuilder, ConnectorSession session, String columnName, Domain domain, Type type)
{
    for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
        BoolQueryBuilder rangeQueryBuilder = new BoolQueryBuilder();
        Set<Object> valuesToInclude = new HashSet<>();
        checkState(!range.isAll(), "Invalid range for column: " + columnName);
        if (range.isSingleValue()) {
            valuesToInclude.add(range.getLow().getValue());
        }
        else {
            if (!range.getLow().isLowerUnbounded()) {
                switch (range.getLow().getBound()) {
                    case ABOVE:
                        rangeQueryBuilder.filter(new RangeQueryBuilder(columnName).gt(getValue(session, type, range.getLow().getValue())));
                        break;
                    case EXACTLY:
                        rangeQueryBuilder.filter(new RangeQueryBuilder(columnName).gte(getValue(session, type, range.getLow().getValue())));
                        break;
                    case BELOW:
                        throw new IllegalArgumentException("Low marker should never use BELOW bound");
                    default:
                        throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
                }
            }
            if (!range.getHigh().isUpperUnbounded()) {
                switch (range.getHigh().getBound()) {
                    case EXACTLY:
                        rangeQueryBuilder.filter(new RangeQueryBuilder(columnName).lte(getValue(session, type, range.getHigh().getValue())));
                        break;
                    case BELOW:
                        rangeQueryBuilder.filter(new RangeQueryBuilder(columnName).lt(getValue(session, type, range.getHigh().getValue())));
                        break;
                    case ABOVE:
                        throw new IllegalArgumentException("High marker should never use ABOVE bound");
                    default:
                        throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
                }
            }
        }

        if (valuesToInclude.size() == 1) {
            rangeQueryBuilder.filter(new TermQueryBuilder(columnName, getValue(session, type, getOnlyElement(valuesToInclude))));
        }
        queryBuilder.should(rangeQueryBuilder);
    }
    return queryBuilder;
}
 
Example 5
Source File: BigQueryFilterQueryBuilder.java    From presto with Apache License 2.0 4 votes vote down vote up
private String toPredicate(String columnName, Domain domain, BigQueryColumnHandle column)
{
    if (domain.getValues().isNone()) {
        return domain.isNullAllowed() ? quote(columnName) + " IS NULL" : "FALSE";
    }

    if (domain.getValues().isAll()) {
        return domain.isNullAllowed() ? "TRUE" : quote(columnName) + " IS NOT NULL";
    }

    List<String> disjuncts = new ArrayList<>();
    List<Object> singleValues = new ArrayList<>();
    for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
        checkState(!range.isAll()); // Already checked
        if (range.isSingleValue()) {
            singleValues.add(range.getLow().getValue());
        }
        else {
            List<String> rangeConjuncts = new ArrayList<>();
            if (!range.getLow().isLowerUnbounded()) {
                switch (range.getLow().getBound()) {
                    case ABOVE:
                        rangeConjuncts.add(toPredicate(columnName, ">", range.getLow().getValue(), column));
                        break;
                    case EXACTLY:
                        rangeConjuncts.add(toPredicate(columnName, ">=", range.getLow().getValue(), column));
                        break;
                    case BELOW:
                        throw new IllegalArgumentException("Low marker should never use BELOW bound");
                    default:
                        throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
                }
            }
            if (!range.getHigh().isUpperUnbounded()) {
                switch (range.getHigh().getBound()) {
                    case ABOVE:
                        throw new IllegalArgumentException("High marker should never use ABOVE bound");
                    case EXACTLY:
                        rangeConjuncts.add(toPredicate(columnName, "<=", range.getHigh().getValue(), column));
                        break;
                    case BELOW:
                        rangeConjuncts.add(toPredicate(columnName, "<", range.getHigh().getValue(), column));
                        break;
                    default:
                        throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
                }
            }
            // If rangeConjuncts is null, then the range was ALL, which should already have been checked for
            checkState(!rangeConjuncts.isEmpty());
            disjuncts.add("(" + concat(rangeConjuncts) + ")");
        }
    }

    // Add back all of the possible single values either as an equality or an IN predicate
    if (singleValues.size() == 1) {
        disjuncts.add(toPredicate(columnName, "=", getOnlyElement(singleValues), column));
    }
    else if (singleValues.size() > 1) {
        String values = singleValues.stream()
                .map(column.getBigQueryType()::convertToString)
                .collect(joining(","));
        disjuncts.add(quote(columnName) + " IN (" + values + ")");
    }

    // Add nullability disjuncts
    checkState(!disjuncts.isEmpty());
    if (domain.isNullAllowed()) {
        disjuncts.add(quote(columnName) + " IS NULL");
    }

    return "(" + String.join(" OR ", disjuncts) + ")";
}
 
Example 6
Source File: IonSqlQueryBuilder.java    From presto with Apache License 2.0 4 votes vote down vote up
private String toPredicate(Domain domain, Type type, int position)
{
    checkArgument(domain.getType().isOrderable(), "Domain type must be orderable");

    if (domain.getValues().isNone()) {
        if (domain.isNullAllowed()) {
            return format("s._%d", position + 1) + " = '' ";
        }
        return "FALSE";
    }

    if (domain.getValues().isAll()) {
        if (domain.isNullAllowed()) {
            return "TRUE";
        }
        return format("s._%d", position + 1) + " <> '' ";
    }

    List<String> disjuncts = new ArrayList<>();
    List<Object> singleValues = new ArrayList<>();
    for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
        checkState(!range.isAll());
        if (range.isSingleValue()) {
            singleValues.add(range.getLow().getValue());
            continue;
        }
        List<String> rangeConjuncts = new ArrayList<>();
        if (!range.getLow().isLowerUnbounded()) {
            switch (range.getLow().getBound()) {
                case ABOVE:
                    rangeConjuncts.add(toPredicate(">", range.getLow().getValue(), type, position));
                    break;
                case EXACTLY:
                    rangeConjuncts.add(toPredicate(">=", range.getLow().getValue(), type, position));
                    break;
                case BELOW:
                    throw new IllegalArgumentException("Low marker should never use BELOW bound");
                default:
                    throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
            }
        }
        if (!range.getHigh().isUpperUnbounded()) {
            switch (range.getHigh().getBound()) {
                case ABOVE:
                    throw new IllegalArgumentException("High marker should never use ABOVE bound");
                case EXACTLY:
                    rangeConjuncts.add(toPredicate("<=", range.getHigh().getValue(), type, position));
                    break;
                case BELOW:
                    rangeConjuncts.add(toPredicate("<", range.getHigh().getValue(), type, position));
                    break;
                default:
                    throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
            }
        }
        // If rangeConjuncts is null, then the range was ALL, which should already have been checked for
        checkState(!rangeConjuncts.isEmpty());
        disjuncts.add("(" + Joiner.on(" AND ").join(rangeConjuncts) + ")");
    }

    // Add back all of the possible single values either as an equality or an IN predicate
    if (singleValues.size() == 1) {
        disjuncts.add(toPredicate("=", getOnlyElement(singleValues), type, position));
    }
    else if (singleValues.size() > 1) {
        List<String> values = new ArrayList<>();
        for (Object value : singleValues) {
            checkType(type);
            values.add(valueToQuery(type, value));
        }
        disjuncts.add(createColumn(type, position) + " IN (" + Joiner.on(",").join(values) + ")");
    }

    // Add nullability disjuncts
    checkState(!disjuncts.isEmpty());
    if (domain.isNullAllowed()) {
        disjuncts.add(format("s._%d", position + 1) + " = '' ");
    }

    return "(" + Joiner.on(" OR ").join(disjuncts) + ")";
}
 
Example 7
Source File: PinotQueryBuilder.java    From presto with Apache License 2.0 4 votes vote down vote up
private static String toPredicate(String columnName, Domain domain)
{
    List<String> disjuncts = new ArrayList<>();
    List<Object> singleValues = new ArrayList<>();
    for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
        checkState(!range.isAll()); // Already checked
        if (range.isSingleValue()) {
            singleValues.add(range.getLow().getValue());
        }
        else {
            List<String> rangeConjuncts = new ArrayList<>();
            if (!range.getLow().isLowerUnbounded()) {
                switch (range.getLow().getBound()) {
                    case ABOVE:
                        rangeConjuncts.add(toConjunct(columnName, ">", range.getLow().getValue()));
                        break;
                    case EXACTLY:
                        rangeConjuncts.add(toConjunct(columnName, ">=", range.getLow().getValue()));
                        break;
                    case BELOW:
                        throw new IllegalArgumentException("Low marker should never use BELOW bound");
                    default:
                        throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
                }
            }
            if (!range.getHigh().isUpperUnbounded()) {
                switch (range.getHigh().getBound()) {
                    case ABOVE:
                        throw new IllegalArgumentException("High marker should never use ABOVE bound");
                    case EXACTLY:
                        rangeConjuncts.add(toConjunct(columnName, "<=", range.getHigh().getValue()));
                        break;
                    case BELOW:
                        rangeConjuncts.add(toConjunct(columnName, "<", range.getHigh().getValue()));
                        break;
                    default:
                        throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
                }
            }
            // If rangeConjuncts is null, then the range was ALL, which is not supported in pql
            checkState(!rangeConjuncts.isEmpty());
            disjuncts.add("(" + Joiner.on(" AND ").join(rangeConjuncts) + ")");
        }
        // Add back all of the possible single values either as an equality or an IN predicate
        if (singleValues.size() == 1) {
            disjuncts.add(toConjunct(columnName, "=", getOnlyElement(singleValues)));
        }
        else if (singleValues.size() > 1) {
            disjuncts.add(format("%s IN (%s)", inClauseValues(columnName, singleValues)));
        }
    }
    return "(" + Joiner.on(" OR ").join(disjuncts) + ")";
}
 
Example 8
Source File: HBaseSplitManager.java    From presto-hbase-connector with Apache License 2.0 4 votes vote down vote up
/**
 * Find all available constraint from Sql.
 * Beware, Query conditions on both sides of 'OR' cannot be obtained.
 *
 * @param constraint constraint
 * @return all available constraint from Sql
 */
private List<ConditionInfo> findConditionFromConstraint(TupleDomain<ColumnHandle> constraint) {
    List<ConditionInfo> handles = new ArrayList<>();
    if (!constraint.getDomains().isPresent()) {
        return handles;
    }

    Map<ColumnHandle, Domain> domainMap = constraint.getDomains().orElse(null);
    Set<ColumnHandle> keySet = domainMap.keySet();

    for (ColumnHandle ch : keySet) {
        HBaseColumnHandle hch = (HBaseColumnHandle) ch;

        Domain domain = domainMap.get(hch);
        if (domain == null) {
            continue;
        }

        if (domain.isSingleValue()) {
            Object value = domain.getNullableSingleValue();
            // =
            if (value instanceof Slice) {
                handles.add(new ConditionInfo(hch.getColumnName(), CONDITION_OPER.EQ,
                        value, domain.getType()));
            } else {
                handles.add(new ConditionInfo(hch.getColumnName(), CONDITION_OPER.EQ,
                        value, domain.getType()));
            }
        } else {
            for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
                if (range.isSingleValue()) {
                    handles.add(new ConditionInfo(hch.getColumnName(), CONDITION_OPER.EQ,
                            range.getSingleValue(), domain.getType()));
                } else {
                    if (!range.getLow().isLowerUnbounded()) {
                        switch (range.getLow().getBound()) {
                            // >
                            // != part 1
                            case ABOVE:
                                handles.add(new ConditionInfo(hch.getColumnName(), CONDITION_OPER.GT,
                                        range.getLow().getValue(), domain.getType()));
                                break;
                            // >=
                            case EXACTLY:
                                handles.add(new ConditionInfo(hch.getColumnName(), CONDITION_OPER.GE,
                                        range.getLow().getValue(), domain.getType()));
                                break;
                            case BELOW:
                                throw new IllegalArgumentException("Low Marker should never use BELOW bound: " + range);
                            default:
                                throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
                        }
                    }
                    if (!range.getHigh().isUpperUnbounded()) {
                        switch (range.getHigh().getBound()) {
                            case ABOVE:
                                throw new IllegalArgumentException("High Marker should never use ABOVE bound: " + range);
                                // <=
                            case EXACTLY:
                                handles.add(new ConditionInfo(hch.getColumnName(), CONDITION_OPER.LE,
                                        range.getHigh().getValue(), domain.getType()));
                                break;
                            // <
                            // !=
                            case BELOW:
                                handles.add(new ConditionInfo(hch.getColumnName(), CONDITION_OPER.LT,
                                        range.getHigh().getValue(), domain.getType()));
                                break;
                            default:
                                throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
                        }
                    }
                    // If rangeConjuncts is null, then the range was ALL, which should already have been checked for
                }
            } // end of for domain.getValues().getRanges().getOrderedRanges())
        } // else of if (domain.isSingleValue)

    } // end of for domainMap.keySet

    return handles;
}