private static boolean isEqualRange(TupleDomain<Symbol> tupleDomain, Symbol symbol, long upperBound) { if (tupleDomain.isNone()) { return false; } Domain domain = tupleDomain.getDomains().get().get(symbol); return domain.getValues().equals(ValueSet.ofRanges(Range.lessThanOrEqual(domain.getType(), upperBound))); }
private static <F, T extends Comparable<T>> Domain createDomain(Type type, boolean hasNullValue, RangeStatistics<F> rangeStatistics, Function<F, T> function) { F min = rangeStatistics.getMin(); F max = rangeStatistics.getMax(); if (min != null && max != null) { return Domain.create(ValueSet.ofRanges(Range.range(type, function.apply(min), true, function.apply(max), true)), hasNullValue); } if (max != null) { return Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(type, function.apply(max))), hasNullValue); } if (min != null) { return Domain.create(ValueSet.ofRanges(Range.greaterThanOrEqual(type, function.apply(min))), hasNullValue); } return Domain.create(ValueSet.all(type), hasNullValue); }
private static Domain extractEquatableDomain(ComparisonExpression.Operator comparisonOperator, Type type, Object value, boolean complement) { checkArgument(value != null); switch (comparisonOperator) { case EQUAL: return Domain.create(complementIfNecessary(ValueSet.of(type, value), complement), false); case NOT_EQUAL: return Domain.create(complementIfNecessary(ValueSet.of(type, value).complement(), complement), false); case IS_DISTINCT_FROM: // Need to potential complement the whole domain for IS_DISTINCT_FROM since it is null-aware return complementIfNecessary(Domain.create(ValueSet.of(type, value).complement(), true), complement); default: throw new AssertionError("Unhandled operator: " + comparisonOperator); } }
public boolean isNullableSingleValue() { if (nullAllowed) { return values.isNone(); } else { return values.isSingleValue(); } }
default ValueSet subtract(ValueSet other) { return this.intersect(other.complement()); }
@Test public void testFromInPredicate() { assertPredicateTranslates( in(C_BIGINT, ImmutableList.of(1L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.singleValue(BIGINT, 1L)))); assertPredicateTranslates( in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.singleValue(COLOR, COLOR_VALUE_1)))); assertPredicateTranslates( in(C_BIGINT, ImmutableList.of(1L, 2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false)))); assertPredicateTranslates( in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2), false)))); assertPredicateTranslates( not(in(C_BIGINT, ImmutableList.of(1L, 2L))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.range(BIGINT, 1L, false, 2L, false), Range.greaterThan(BIGINT, 2L)), false)))); assertPredicateTranslates( not(in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2)))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2).complement(), false)))); }
private static OptionalInt extractUpperBound(TupleDomain<Symbol> tupleDomain, Symbol symbol) { if (tupleDomain.isNone()) { return OptionalInt.empty(); } Domain rowNumberDomain = tupleDomain.getDomains().get().get(symbol); if (rowNumberDomain == null) { return OptionalInt.empty(); } ValueSet values = rowNumberDomain.getValues(); if (values.isAll() || values.isNone() || values.getRanges().getRangeCount() <= 0) { return OptionalInt.empty(); } Range span = values.getRanges().getSpan(); if (span.getHigh().isUpperUnbounded()) { return OptionalInt.empty(); } verify(rowNumberDomain.getType().equals(BIGINT)); long upperBound = (Long) span.getHigh().getValue(); if (span.getHigh().getBound() == BELOW) { upperBound--; } if (upperBound > 0 && upperBound <= Integer.MAX_VALUE) { return OptionalInt.of(toIntExact(upperBound)); } return OptionalInt.empty(); }
/** * Reduces the number of discrete components in the Domain if there are too many. */ public Domain simplify() { ValueSet simplifiedValueSet = values.getValuesProcessor().<Optional<ValueSet>>transform( ranges -> { if (ranges.getOrderedRanges().size() <= 32) { return Optional.empty(); } return Optional.of(ValueSet.ofRanges(ranges.getSpan())); }, discreteValues -> { if (discreteValues.getValues().size() <= 32) { return Optional.empty(); } return Optional.of(ValueSet.all(values.getType())); }, allOrNone -> Optional.empty()) .orElse(values); return Domain.create(simplifiedValueSet, nullAllowed); }
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); }
public static Domain all(Type type) { return new Domain(ValueSet.all(type), true); }
return Domain.create(ValueSet.all(type), hasNullValue); return Domain.create(ValueSet.of(type, true), hasNullValue); return Domain.create(ValueSet.of(type, false), hasNullValue); if (longStatistics.genericGetMin() > longStatistics.genericGetMax()) { failWithCorruptionException(failOnCorruptedParquetStatistics, column, id, longStatistics); return Domain.create(ValueSet.all(type), hasNullValue); if (intStatistics.genericGetMin() > intStatistics.genericGetMax()) { failWithCorruptionException(failOnCorruptedParquetStatistics, column, id, intStatistics); return Domain.create(ValueSet.all(type), hasNullValue); return Domain.create(ValueSet.all(type), hasNullValue); if (floatStatistics.genericGetMin() > floatStatistics.genericGetMax()) { failWithCorruptionException(failOnCorruptedParquetStatistics, column, id, floatStatistics); return Domain.create(ValueSet.all(type), hasNullValue); if (doubleStatistics.genericGetMin() > doubleStatistics.genericGetMax()) { failWithCorruptionException(failOnCorruptedParquetStatistics, column, id, doubleStatistics); return Domain.create(ValueSet.all(type), hasNullValue); if (minSlice.compareTo(maxSlice) > 0) { failWithCorruptionException(failOnCorruptedParquetStatistics, column, id, binaryStatistics); return Domain.create(ValueSet.all(type), hasNullValue); if (intStatistics.genericGetMin() > intStatistics.genericGetMax()) {
public static Domain singleValue(Type type, Object value) { return new Domain(ValueSet.of(type, value), false); }
@VisibleForTesting public static Optional<Collection<Object>> extractDiscreteValues(ValueSet valueSet) { return valueSet.getValuesProcessor().transform( ranges -> { ImmutableList.Builder<Object> discreteValues = ImmutableList.builder(); for (Range range : ranges.getOrderedRanges()) { if (!range.isSingleValue()) { return Optional.empty(); } discreteValues.add(range.getSingleValue()); } return Optional.of(discreteValues.build()); }, discreteValues -> Optional.of(discreteValues.getValues()), allOrNone -> allOrNone.isAll() ? Optional.empty() : Optional.of(ImmutableList.of())); }
public boolean isAll() { return values.isAll() && nullAllowed; }
public boolean isNone() { return values.isNone() && !nullAllowed; }
builder.addPredicate(KuduPredicate.newIsNullPredicate(columnSchema)); else if (domain.getValues().isAll() && domain.isNullAllowed()) { builder.addPredicate(KuduPredicate.newIsNotNullPredicate(columnSchema)); ValueSet valueSet = domain.getValues(); if (valueSet instanceof EquatableValueSet) { DiscreteValues discreteValues = valueSet.getDiscreteValues(); KuduPredicate predicate = createInListPredicate(columnSchema, discreteValues); builder.addPredicate(predicate);
private static String createShardPredicate(ImmutableList.Builder<JDBCType> types, ImmutableList.Builder<Object> values, Domain domain, JDBCType jdbcType) { List<Range> ranges = domain.getValues().getRanges().getOrderedRanges(); // only apply predicates if all ranges are single values if (ranges.isEmpty() || !ranges.stream().allMatch(Range::isSingleValue)) { return "true"; } ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder(); ImmutableList.Builder<JDBCType> typesBuilder = ImmutableList.builder(); StringJoiner rangePredicate = new StringJoiner(" OR "); for (Range range : ranges) { Slice uuidText = (Slice) range.getSingleValue(); try { Slice uuidBytes = uuidStringToBytes(uuidText); typesBuilder.add(jdbcType); valuesBuilder.add(uuidBytes); } catch (IllegalArgumentException e) { return "true"; } rangePredicate.add("shard_uuid = ?"); } types.addAll(typesBuilder.build()); values.addAll(valuesBuilder.build()); return rangePredicate.toString(); }
private static ValueSet complementIfNecessary(ValueSet valueSet, boolean complement) { return complement ? valueSet.complement() : valueSet; }
@Test public void testFromInPredicate() { assertPredicateTranslates( in(C_BIGINT, ImmutableList.of(1L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.singleValue(BIGINT, 1L)))); assertPredicateTranslates( in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.singleValue(COLOR, COLOR_VALUE_1)))); assertPredicateTranslates( in(C_BIGINT, ImmutableList.of(1L, 2L)), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false)))); assertPredicateTranslates( in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2), false)))); assertPredicateTranslates( not(in(C_BIGINT, ImmutableList.of(1L, 2L))), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.range(BIGINT, 1L, false, 2L, false), Range.greaterThan(BIGINT, 2L)), false)))); assertPredicateTranslates( not(in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2)))), withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2).complement(), false)))); }