Search in sources :

Example 1 with RealType

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());
    }
}
Also used : RealType(io.trino.spi.type.RealType) Timestamp(java.sql.Timestamp) BloomFilter(io.trino.orc.metadata.statistics.BloomFilter) TupleDomainOrcPredicate.checkInBloomFilter(io.trino.orc.TupleDomainOrcPredicate.checkInBloomFilter) BigDecimal(java.math.BigDecimal) Type(io.trino.spi.type.Type) RealType(io.trino.spi.type.RealType) Slice(io.airlift.slice.Slice) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 2 with RealType

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);
}
Also used : VarcharType(io.trino.spi.type.VarcharType) TinyintType(io.trino.spi.type.TinyintType) BooleanType(io.trino.spi.type.BooleanType) RowType(io.trino.spi.type.RowType) RealType(io.trino.spi.type.RealType) MapType(io.trino.spi.type.MapType) BigintType(io.trino.spi.type.BigintType) ArrayType(io.trino.spi.type.ArrayType) IntegerType(io.trino.spi.type.IntegerType) DoubleType(io.trino.spi.type.DoubleType) SmallintType(io.trino.spi.type.SmallintType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) DateType(io.trino.spi.type.DateType) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 3 with RealType

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;
}
Also used : ArrayList(java.util.ArrayList) InListExpression(io.trino.sql.tree.InListExpression) Range(io.trino.spi.predicate.Range) InPredicate(io.trino.sql.tree.InPredicate) RealType(io.trino.spi.type.RealType) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) SortedRangeSet(io.trino.spi.predicate.SortedRangeSet) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Expression(io.trino.sql.tree.Expression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) DoubleType(io.trino.spi.type.DoubleType)

Aggregations

RealType (io.trino.spi.type.RealType)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 DoubleType (io.trino.spi.type.DoubleType)2 Map (java.util.Map)2 Slice (io.airlift.slice.Slice)1 TupleDomainOrcPredicate.checkInBloomFilter (io.trino.orc.TupleDomainOrcPredicate.checkInBloomFilter)1 BloomFilter (io.trino.orc.metadata.statistics.BloomFilter)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 Range (io.trino.spi.predicate.Range)1 SortedRangeSet (io.trino.spi.predicate.SortedRangeSet)1 ArrayType (io.trino.spi.type.ArrayType)1 BigintType (io.trino.spi.type.BigintType)1 BooleanType (io.trino.spi.type.BooleanType)1 DateType (io.trino.spi.type.DateType)1 IntegerType (io.trino.spi.type.IntegerType)1 MapType (io.trino.spi.type.MapType)1 RowType (io.trino.spi.type.RowType)1 SmallintType (io.trino.spi.type.SmallintType)1 TinyintType (io.trino.spi.type.TinyintType)1 Type (io.trino.spi.type.Type)1