use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class TestDomainTranslator method testFromSingleBooleanReference.
@Test
public void testFromSingleBooleanReference() {
Expression originalPredicate = C_BOOLEAN.toSymbolReference();
ExtractionResult result = fromPredicate(originalPredicate);
assertEquals(result.getTupleDomain(), tupleDomain(C_BOOLEAN, Domain.create(ValueSet.ofRanges(Range.equal(BOOLEAN, true)), false)));
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
originalPredicate = not(C_BOOLEAN.toSymbolReference());
result = fromPredicate(originalPredicate);
assertEquals(result.getTupleDomain(), tupleDomain(C_BOOLEAN, Domain.create(ValueSet.ofRanges(Range.equal(BOOLEAN, true)).complement(), false)));
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
originalPredicate = and(C_BOOLEAN.toSymbolReference(), C_BOOLEAN_1.toSymbolReference());
result = fromPredicate(originalPredicate);
Domain domain = Domain.create(ValueSet.ofRanges(Range.equal(BOOLEAN, true)), false);
assertEquals(result.getTupleDomain(), tupleDomain(C_BOOLEAN, domain, C_BOOLEAN_1, domain));
assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
originalPredicate = or(C_BOOLEAN.toSymbolReference(), C_BOOLEAN_1.toSymbolReference());
result = fromPredicate(originalPredicate);
assertEquals(result.getTupleDomain(), TupleDomain.all());
assertEquals(result.getRemainingExpression(), originalPredicate);
originalPredicate = not(and(C_BOOLEAN.toSymbolReference(), C_BOOLEAN_1.toSymbolReference()));
result = fromPredicate(originalPredicate);
assertEquals(result.getTupleDomain(), TupleDomain.all());
assertEquals(result.getRemainingExpression(), originalPredicate);
}
use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class TestDomainCoercer method testCoercedValueSameAsOriginal.
@Test
public void testCoercedValueSameAsOriginal() {
assertEquals(applySaturatedCasts(multipleValues(BIGINT, ImmutableList.of(1L, 10000L, -2000L)), SMALLINT), multipleValues(SMALLINT, ImmutableList.of(1L, 10000L, -2000L)));
Domain original = Domain.create(ValueSet.ofRanges(lessThan(DOUBLE, 0.0), range(DOUBLE, 0.0, false, 1.0, false), range(DOUBLE, 2.0, true, 3.0, true), greaterThan(DOUBLE, 4.0)), true);
assertEquals(applySaturatedCasts(original, REAL), Domain.create(ValueSet.ofRanges(lessThan(REAL, (long) floatToIntBits(0.0f)), range(REAL, (long) floatToIntBits(0.0f), false, (long) floatToIntBits(1.0f), false), range(REAL, (long) floatToIntBits(2.0f), true, (long) floatToIntBits(3.0f), true), greaterThan(REAL, (long) floatToIntBits(4.0f))), true));
}
use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class TupleDomainOrcPredicate method getDomain.
@VisibleForTesting
public static Domain getDomain(Type type, long rowCount, ColumnStatistics columnStatistics) {
if (rowCount == 0) {
return Domain.none(type);
}
if (columnStatistics == null) {
return Domain.all(type);
}
if (columnStatistics.hasNumberOfValues() && columnStatistics.getNumberOfValues() == 0) {
return Domain.onlyNull(type);
}
boolean hasNullValue = columnStatistics.getNumberOfValues() != rowCount;
if (type instanceof TimeType && columnStatistics.getIntegerStatistics() != null) {
// This is the representation of TIME used by Iceberg
return createDomain(type, hasNullValue, columnStatistics.getIntegerStatistics(), value -> ((long) value) * Timestamps.PICOSECONDS_PER_MICROSECOND);
}
if (type.getJavaType() == boolean.class && columnStatistics.getBooleanStatistics() != null) {
BooleanStatistics booleanStatistics = columnStatistics.getBooleanStatistics();
boolean hasTrueValues = (booleanStatistics.getTrueValueCount() != 0);
boolean hasFalseValues = (columnStatistics.getNumberOfValues() != booleanStatistics.getTrueValueCount());
if (hasTrueValues && hasFalseValues) {
return Domain.all(BOOLEAN);
}
if (hasTrueValues) {
return Domain.create(ValueSet.of(BOOLEAN, true), hasNullValue);
}
if (hasFalseValues) {
return Domain.create(ValueSet.of(BOOLEAN, false), hasNullValue);
}
} else if (isShortDecimal(type) && columnStatistics.getDecimalStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> rescale(value, (DecimalType) type).unscaledValue().longValue());
} else if (isLongDecimal(type) && columnStatistics.getDecimalStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> Int128.valueOf(rescale(value, (DecimalType) type).unscaledValue()));
} else if (type instanceof CharType && columnStatistics.getStringStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getStringStatistics(), value -> truncateToLengthAndTrimSpaces(value, type));
} else if (type instanceof VarcharType && columnStatistics.getStringStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getStringStatistics());
} else if (type instanceof DateType && columnStatistics.getDateStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDateStatistics(), value -> (long) value);
} else if ((type.equals(TIMESTAMP_MILLIS) || type.equals(TIMESTAMP_MICROS)) && columnStatistics.getTimestampStatistics() != null) {
// upper bound of the domain we create must be adjusted accordingly, to includes the rounded timestamp.
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), min -> min * MICROSECONDS_PER_MILLISECOND, max -> (max + 1) * MICROSECONDS_PER_MILLISECOND);
} else if (type.equals(TIMESTAMP_NANOS) && columnStatistics.getTimestampStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), min -> new LongTimestamp(min * MICROSECONDS_PER_MILLISECOND, 0), max -> new LongTimestamp((max + 1) * MICROSECONDS_PER_MILLISECOND, 0));
} else if (type.equals(TIMESTAMP_TZ_MILLIS) && columnStatistics.getTimestampStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), value -> packDateTimeWithZone(value, UTC_KEY));
} else if (type.equals(TIMESTAMP_TZ_MICROS) && (columnStatistics.getTimestampStatistics() != null)) {
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), min -> LongTimestampWithTimeZone.fromEpochMillisAndFraction(min, 0, UTC_KEY), max -> LongTimestampWithTimeZone.fromEpochMillisAndFraction(max, 999_000_000, UTC_KEY));
} else if (type.equals(TIMESTAMP_TZ_NANOS) && columnStatistics.getTimestampStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), min -> LongTimestampWithTimeZone.fromEpochMillisAndFraction(min, 0, UTC_KEY), max -> LongTimestampWithTimeZone.fromEpochMillisAndFraction(max, 999_999_000, UTC_KEY));
} else if (type.getJavaType() == long.class && columnStatistics.getIntegerStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getIntegerStatistics());
} else if (type.getJavaType() == double.class && columnStatistics.getDoubleStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDoubleStatistics());
} else if (REAL.equals(type) && columnStatistics.getDoubleStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDoubleStatistics(), value -> (long) floatToRawIntBits(value.floatValue()));
}
return Domain.create(ValueSet.all(type), hasNullValue);
}
use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class TupleDomainParquetPredicate method convertToParquetFilter.
private FilterPredicate convertToParquetFilter(DateTimeZone timeZone) {
FilterPredicate filter = null;
for (RichColumnDescriptor column : columns) {
Domain domain = effectivePredicate.getDomains().get().get(column);
if (domain == null || domain.isNone()) {
continue;
}
if (domain.isAll()) {
continue;
}
FilterPredicate columnFilter = FilterApi.userDefined(new TrinoIntColumn(ColumnPath.get(column.getPath())), new DomainUserDefinedPredicate<>(column, domain, timeZone));
if (filter == null) {
filter = columnFilter;
} else {
filter = FilterApi.and(filter, columnFilter);
}
}
return filter;
}
use of io.trino.spi.predicate.Domain in project trino by trinodb.
the class AtopMetadata method applyFilter.
@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle table, Constraint constraint) {
AtopTableHandle handle = (AtopTableHandle) table;
Optional<Map<ColumnHandle, Domain>> domains = constraint.getSummary().getDomains();
Domain oldEndTimeDomain = handle.getEndTimeConstraint();
Domain oldStartTimeDomain = handle.getStartTimeConstraint();
Domain newEndTimeDomain = oldEndTimeDomain;
Domain newStartTimeDomain = oldStartTimeDomain;
if (domains.isPresent()) {
if (domains.get().containsKey(START_TIME_HANDLE)) {
newStartTimeDomain = domains.get().get(START_TIME_HANDLE).intersect(oldStartTimeDomain);
}
if (domains.get().containsKey(END_TIME_HANDLE)) {
newEndTimeDomain = domains.get().get(END_TIME_HANDLE).intersect(oldEndTimeDomain);
}
}
if (oldEndTimeDomain.equals(newEndTimeDomain) && oldStartTimeDomain.equals(newStartTimeDomain)) {
return Optional.empty();
}
handle = new AtopTableHandle(handle.getSchema(), handle.getTable(), newStartTimeDomain, newEndTimeDomain);
return Optional.of(new ConstraintApplicationResult<>(handle, constraint.getSummary(), false));
}
Aggregations