use of io.trino.spi.type.RealType in project trino by trinodb.
the class TestOrcBloomFilters method testBloomFilterPredicateValuesExisting.
@Test
public void testBloomFilterPredicateValuesExisting() {
BloomFilter bloomFilter = new BloomFilter(TEST_VALUES.size() * 10, 0.01);
for (Map.Entry<Object, Type> testValue : TEST_VALUES.entrySet()) {
Object o = testValue.getKey();
if (o instanceof Long) {
if (testValue.getValue() instanceof RealType) {
bloomFilter.addDouble(intBitsToFloat(((Number) o).intValue()));
} else {
bloomFilter.addLong((Long) o);
}
} else if (o instanceof Integer) {
bloomFilter.addLong((Integer) o);
} else if (o instanceof String) {
bloomFilter.add(((String) o).getBytes(UTF_8));
} else if (o instanceof BigDecimal) {
bloomFilter.add(o.toString().getBytes(UTF_8));
} else if (o instanceof Slice) {
bloomFilter.add(((Slice) o).getBytes());
} else if (o instanceof Timestamp) {
bloomFilter.addLong(((Timestamp) o).getTime());
} else if (o instanceof Double) {
bloomFilter.addDouble((Double) o);
} else {
fail("Unsupported type " + o.getClass());
}
}
for (Map.Entry<Object, Type> testValue : TEST_VALUES.entrySet()) {
boolean matched = checkInBloomFilter(bloomFilter, testValue.getKey(), testValue.getValue());
assertTrue(matched, "type " + testValue.getClass());
}
}
use of io.trino.spi.type.RealType in project trino by trinodb.
the class TestHiveBucketing method toNativeContainerValue.
private static Object toNativeContainerValue(Type type, Object hiveValue) {
if (hiveValue == null) {
return null;
}
if (type instanceof ArrayType) {
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Object subElement : (Iterable<?>) hiveValue) {
appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
if (type instanceof RowType) {
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
int field = 0;
for (Object subElement : (Iterable<?>) hiveValue) {
appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
field++;
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
if (type instanceof MapType) {
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Entry<?, ?> entry : ((Map<?, ?>) hiveValue).entrySet()) {
appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
if (type instanceof BooleanType) {
return hiveValue;
}
if (type instanceof TinyintType) {
return (long) (byte) hiveValue;
}
if (type instanceof SmallintType) {
return (long) (short) hiveValue;
}
if (type instanceof IntegerType) {
return (long) (int) hiveValue;
}
if (type instanceof BigintType) {
return hiveValue;
}
if (type instanceof RealType) {
return (long) Float.floatToRawIntBits((float) hiveValue);
}
if (type instanceof DoubleType) {
return hiveValue;
}
if (type instanceof VarcharType) {
return Slices.utf8Slice(hiveValue.toString());
}
if (type instanceof DateType) {
return (long) ((Date) hiveValue).toEpochDay();
}
throw new IllegalArgumentException("Unsupported bucketing type: " + type);
}
use of io.trino.spi.type.RealType in project trino by trinodb.
the class DomainTranslator method extractDisjuncts.
private List<Expression> extractDisjuncts(Session session, Type type, Ranges ranges, SymbolReference reference) {
List<Expression> disjuncts = new ArrayList<>();
List<Expression> singleValues = new ArrayList<>();
List<Range> orderedRanges = ranges.getOrderedRanges();
SortedRangeSet sortedRangeSet = SortedRangeSet.copyOf(type, orderedRanges);
SortedRangeSet complement = sortedRangeSet.complement();
List<Range> singleValueExclusionsList = complement.getOrderedRanges().stream().filter(Range::isSingleValue).collect(toList());
List<Range> originalUnionSingleValues = SortedRangeSet.copyOf(type, singleValueExclusionsList).union(sortedRangeSet).getOrderedRanges();
PeekingIterator<Range> singleValueExclusions = peekingIterator(singleValueExclusionsList.iterator());
/*
For types including NaN, it is incorrect to introduce range "all" while processing a set of ranges,
even if the component ranges cover the entire value set.
This is because partial ranges don't include NaN, while range "all" does.
Example: ranges (unbounded , 1.0) and (1.0, unbounded) should not be coalesced to (unbounded, unbounded) with excluded point 1.0.
That result would be further translated to expression "xxx <> 1.0", which is satisfied by NaN.
To avoid error, in such case the ranges are not optimised.
*/
if (type instanceof RealType || type instanceof DoubleType) {
boolean originalRangeIsAll = orderedRanges.stream().anyMatch(Range::isAll);
boolean coalescedRangeIsAll = originalUnionSingleValues.stream().anyMatch(Range::isAll);
if (!originalRangeIsAll && coalescedRangeIsAll) {
for (Range range : orderedRanges) {
disjuncts.add(processRange(session, type, range, reference));
}
return disjuncts;
}
}
for (Range range : originalUnionSingleValues) {
if (range.isSingleValue()) {
singleValues.add(literalEncoder.toExpression(session, range.getSingleValue(), type));
continue;
}
// attempt to optimize ranges that can be coalesced as long as single value points are excluded
List<Expression> singleValuesInRange = new ArrayList<>();
while (singleValueExclusions.hasNext() && range.contains(singleValueExclusions.peek())) {
singleValuesInRange.add(literalEncoder.toExpression(session, singleValueExclusions.next().getSingleValue(), type));
}
if (!singleValuesInRange.isEmpty()) {
disjuncts.add(combineRangeWithExcludedPoints(session, type, reference, range, singleValuesInRange));
continue;
}
disjuncts.add(processRange(session, type, range, reference));
}
// Add back all of the possible single values either as an equality or an IN predicate
if (singleValues.size() == 1) {
disjuncts.add(new ComparisonExpression(EQUAL, reference, getOnlyElement(singleValues)));
} else if (singleValues.size() > 1) {
disjuncts.add(new InPredicate(reference, new InListExpression(singleValues)));
}
return disjuncts;
}
Aggregations