use of io.trino.spi.predicate.ValueSet in project trino by trinodb.
the class PushPredicateThroughProjectIntoWindow method extractUpperBound.
private static OptionalInt extractUpperBound(TupleDomain<Symbol> tupleDomain, Symbol symbol) {
if (tupleDomain.isNone()) {
return OptionalInt.empty();
}
Domain rankingDomain = tupleDomain.getDomains().get().get(symbol);
if (rankingDomain == null) {
return OptionalInt.empty();
}
ValueSet values = rankingDomain.getValues();
if (values.isAll() || values.isNone() || values.getRanges().getRangeCount() <= 0) {
return OptionalInt.empty();
}
Range span = values.getRanges().getSpan();
if (span.isHighUnbounded()) {
return OptionalInt.empty();
}
long upperBound = (Long) span.getHighBoundedValue();
if (!span.isHighInclusive()) {
upperBound--;
}
if (upperBound >= Integer.MIN_VALUE && upperBound <= Integer.MAX_VALUE) {
return OptionalInt.of(toIntExact(upperBound));
}
return OptionalInt.empty();
}
use of io.trino.spi.predicate.ValueSet in project trino by trinodb.
the class HiveBucketing method getHiveBucketFilter.
public static Optional<HiveBucketFilter> getHiveBucketFilter(HiveTableHandle hiveTable, TupleDomain<ColumnHandle> effectivePredicate) {
if (hiveTable.getBucketHandle().isEmpty()) {
return Optional.empty();
}
HiveBucketProperty hiveBucketProperty = hiveTable.getBucketHandle().get().toTableBucketProperty();
List<Column> dataColumns = hiveTable.getDataColumns().stream().map(HiveColumnHandle::toMetastoreColumn).collect(toImmutableList());
Optional<Map<ColumnHandle, List<NullableValue>>> bindings = TupleDomain.extractDiscreteValues(effectivePredicate);
if (bindings.isEmpty()) {
return Optional.empty();
}
Optional<Set<Integer>> buckets = getHiveBuckets(hiveBucketProperty, dataColumns, bindings.get());
if (buckets.isPresent()) {
return Optional.of(new HiveBucketFilter(buckets.get()));
}
Optional<Domain> domain = effectivePredicate.getDomains().flatMap(domains -> domains.entrySet().stream().filter(entry -> ((HiveColumnHandle) entry.getKey()).getName().equals(BUCKET_COLUMN_NAME)).findFirst().map(Entry::getValue));
if (domain.isEmpty()) {
return Optional.empty();
}
ValueSet values = domain.get().getValues();
ImmutableSet.Builder<Integer> builder = ImmutableSet.builder();
int bucketCount = hiveBucketProperty.getBucketCount();
for (int i = 0; i < bucketCount; i++) {
if (values.containsValue((long) i)) {
builder.add(i);
}
}
return Optional.of(new HiveBucketFilter(builder.build()));
}
use of io.trino.spi.predicate.ValueSet in project trino by trinodb.
the class KafkaFilterManager method filterRangeByDomain.
@VisibleForTesting
public static Optional<Range> filterRangeByDomain(Domain domain) {
Long low = INVALID_KAFKA_RANGE_INDEX;
Long high = INVALID_KAFKA_RANGE_INDEX;
if (domain.isSingleValue()) {
// still return range for single value case like (_partition_offset=XXX or _timestamp=XXX)
low = (long) domain.getSingleValue();
high = (long) domain.getSingleValue();
} else {
ValueSet valueSet = domain.getValues();
if (valueSet instanceof SortedRangeSet) {
// still return range for single value case like (_partition_offset in (XXX1,XXX2) or _timestamp in XXX1, XXX2)
Ranges ranges = ((SortedRangeSet) valueSet).getRanges();
List<io.trino.spi.predicate.Range> rangeList = ranges.getOrderedRanges();
if (rangeList.stream().allMatch(io.trino.spi.predicate.Range::isSingleValue)) {
List<Long> values = rangeList.stream().map(range -> (Long) range.getSingleValue()).collect(toImmutableList());
low = Collections.min(values);
high = Collections.max(values);
} else {
io.trino.spi.predicate.Range span = ranges.getSpan();
low = getLowIncludedValue(span).orElse(low);
high = getHighIncludedValue(span).orElse(high);
}
}
}
if (high != INVALID_KAFKA_RANGE_INDEX) {
high = high + 1;
}
return Optional.of(new Range(low, high));
}
use of io.trino.spi.predicate.ValueSet in project trino by trinodb.
the class TestPrometheusSplit method testPredicatePushDownSetsUpperBoundOnly.
@Test
public void testPredicatePushDownSetsUpperBoundOnly() {
long predicateHighValue = 1568638171999L;
Range highRange = Range.lessThanOrEqual(TIMESTAMP_COLUMN_TYPE, packDateTimeWithZone(predicateHighValue, UTC_KEY));
ValueSet valueSet = ValueSet.ofRanges(highRange);
Domain testDomain = Domain.create(valueSet, false);
TupleDomain<ColumnHandle> testTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(new PrometheusColumnHandle("timestamp", TIMESTAMP_COLUMN_TYPE, 2), testDomain));
PrometheusTableHandle prometheusTableHandle = new PrometheusTableHandle("schemaName", "tableName").withPredicate(testTupleDomain);
io.airlift.units.Duration maxQueryRangeDuration = new io.airlift.units.Duration(120, TimeUnit.SECONDS);
io.airlift.units.Duration queryChunkSizeDuration = new io.airlift.units.Duration(30, TimeUnit.SECONDS);
Instant now = ofEpochMilli(1568638171999L + 600000L);
List<String> splitTimes = PrometheusSplitManager.generateTimesForSplits(now, maxQueryRangeDuration, queryChunkSizeDuration, prometheusTableHandle);
TemporalAmount expectedMaxQueryAsTime = java.time.Duration.ofMillis(maxQueryRangeDuration.toMillis() + ((splitTimes.size() - 1) * OFFSET_MILLIS));
String lastSplit = splitTimes.get(splitTimes.size() - 1);
Instant lastSplitAsTime = ofEpochMilli(longFromDecimalSecondString(lastSplit));
String earliestSplit = splitTimes.get(0);
Instant earliestSplitAsTime = ofEpochMilli(longFromDecimalSecondString(earliestSplit));
TemporalAmount queryChunkAsTime = java.time.Duration.ofMillis(queryChunkSizeDuration.toMillis());
java.time.Duration actualMaxDuration = Duration.between(earliestSplitAsTime.minus(queryChunkAsTime), lastSplitAsTime);
assertEquals(lastSplitAsTime.toEpochMilli(), 1568638171999L);
assertEquals(actualMaxDuration, expectedMaxQueryAsTime);
}
use of io.trino.spi.predicate.ValueSet in project trino by trinodb.
the class KuduClientSession method addConstraintPredicates.
/**
* translates TupleDomain to KuduPredicates.
*/
private void addConstraintPredicates(KuduTable table, KuduScanToken.KuduScanTokenBuilder builder, TupleDomain<ColumnHandle> constraintSummary) {
verify(!constraintSummary.isNone(), "constraintSummary is none");
if (constraintSummary.isAll()) {
return;
}
Schema schema = table.getSchema();
for (TupleDomain.ColumnDomain<ColumnHandle> columnDomain : constraintSummary.getColumnDomains().get()) {
int position = ((KuduColumnHandle) columnDomain.getColumn()).getOrdinalPosition();
ColumnSchema columnSchema = schema.getColumnByIndex(position);
Domain domain = columnDomain.getDomain();
verify(!domain.isNone(), "Domain is none");
if (domain.isAll()) {
// no restriction
} else if (domain.isOnlyNull()) {
builder.addPredicate(KuduPredicate.newIsNullPredicate(columnSchema));
} else if (!domain.getValues().isNone() && domain.isNullAllowed()) {
// no restriction
} else if (domain.getValues().isAll() && !domain.isNullAllowed()) {
builder.addPredicate(KuduPredicate.newIsNotNullPredicate(columnSchema));
} else if (domain.isSingleValue()) {
KuduPredicate predicate = createEqualsPredicate(columnSchema, domain.getSingleValue());
builder.addPredicate(predicate);
} else {
ValueSet valueSet = domain.getValues();
if (valueSet instanceof EquatableValueSet) {
DiscreteValues discreteValues = valueSet.getDiscreteValues();
KuduPredicate predicate = createInListPredicate(columnSchema, discreteValues);
builder.addPredicate(predicate);
} else if (valueSet instanceof SortedRangeSet) {
Ranges ranges = ((SortedRangeSet) valueSet).getRanges();
List<Range> rangeList = ranges.getOrderedRanges();
if (rangeList.stream().allMatch(Range::isSingleValue)) {
io.trino.spi.type.Type type = TypeHelper.fromKuduColumn(columnSchema);
List<Object> javaValues = rangeList.stream().map(range -> TypeHelper.getJavaValue(type, range.getSingleValue())).collect(toImmutableList());
KuduPredicate predicate = KuduPredicate.newInListPredicate(columnSchema, javaValues);
builder.addPredicate(predicate);
} else {
Range span = ranges.getSpan();
if (!span.isLowUnbounded()) {
KuduPredicate.ComparisonOp op = span.isLowInclusive() ? GREATER_EQUAL : GREATER;
KuduPredicate predicate = createComparisonPredicate(columnSchema, op, span.getLowBoundedValue());
builder.addPredicate(predicate);
}
if (!span.isHighUnbounded()) {
KuduPredicate.ComparisonOp op = span.isHighInclusive() ? LESS_EQUAL : LESS;
KuduPredicate predicate = createComparisonPredicate(columnSchema, op, span.getHighBoundedValue());
builder.addPredicate(predicate);
}
}
} else {
throw new IllegalStateException("Unexpected domain: " + domain);
}
}
}
}
Aggregations