Search in sources :

Example 1 with DoubleType

use of io.trino.spi.type.DoubleType 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 2 with DoubleType

use of io.trino.spi.type.DoubleType 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

DoubleType (io.trino.spi.type.DoubleType)2 RealType (io.trino.spi.type.RealType)2 ImmutableMap (com.google.common.collect.ImmutableMap)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 VarcharType (io.trino.spi.type.VarcharType)1 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)1 Expression (io.trino.sql.tree.Expression)1 InListExpression (io.trino.sql.tree.InListExpression)1 InPredicate (io.trino.sql.tree.InPredicate)1