Search in sources :

Example 16 with Int128

use of io.trino.spi.type.Int128 in project trino by trinodb.

the class TestLongDecimalStream method test.

@Test
public void test() throws IOException {
    Random random = new Random(0);
    List<List<Int128>> groups = new ArrayList<>();
    for (int groupIndex = 0; groupIndex < 3; groupIndex++) {
        List<Int128> group = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            BigInteger value = new BigInteger(120, random);
            group.add(Int128.valueOf(value));
        }
        groups.add(group);
    }
    testWriteValue(groups);
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List) Int128(io.trino.spi.type.Int128) DecimalStreamCheckpoint(io.trino.orc.checkpoint.DecimalStreamCheckpoint) Test(org.testng.annotations.Test)

Example 17 with Int128

use of io.trino.spi.type.Int128 in project trino by trinodb.

the class FunctionAssertions method interpret.

private Object interpret(Expression expression, Type expectedType, Session session) {
    Map<NodeRef<Expression>, Type> expressionTypes = getTypes(session, getPlannerContext(), INPUT_TYPES, expression);
    ExpressionInterpreter evaluator = new ExpressionInterpreter(expression, runner.getPlannerContext(), session, expressionTypes);
    Object result = evaluator.evaluate(symbol -> {
        int position = 0;
        int channel = INPUT_MAPPING.get(symbol);
        Type type = INPUT_TYPES.get(symbol);
        Block block = SOURCE_PAGE.getBlock(channel);
        if (block.isNull(position)) {
            return null;
        }
        Class<?> javaType = type.getJavaType();
        if (javaType == boolean.class) {
            return type.getBoolean(block, position);
        } else if (javaType == long.class) {
            return type.getLong(block, position);
        } else if (javaType == double.class) {
            return type.getDouble(block, position);
        } else if (javaType == Slice.class) {
            return type.getSlice(block, position);
        } else if (javaType == Block.class || javaType == Int128.class) {
            return type.getObject(block, position);
        } else {
            throw new UnsupportedOperationException("not yet implemented");
        }
    });
    // convert result from stack type to Type ObjectValue
    Block block = Utils.nativeValueToBlock(expectedType, result);
    return expectedType.getObjectValue(session.toConnectorSession(), block, 0);
}
Also used : NodeRef(io.trino.sql.tree.NodeRef) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) RowType(io.trino.spi.type.RowType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) Slice(io.airlift.slice.Slice) ExpressionInterpreter(io.trino.sql.planner.ExpressionInterpreter) BlockAssertions.createShortDecimalsBlock(io.trino.block.BlockAssertions.createShortDecimalsBlock) BlockAssertions.createDoublesBlock(io.trino.block.BlockAssertions.createDoublesBlock) BlockAssertions.createIntsBlock(io.trino.block.BlockAssertions.createIntsBlock) BlockAssertions.createRowBlock(io.trino.block.BlockAssertions.createRowBlock) BlockAssertions.createStringsBlock(io.trino.block.BlockAssertions.createStringsBlock) BlockAssertions.createSlicesBlock(io.trino.block.BlockAssertions.createSlicesBlock) BlockAssertions.createTimestampsWithTimeZoneMillisBlock(io.trino.block.BlockAssertions.createTimestampsWithTimeZoneMillisBlock) Block(io.trino.spi.block.Block) BlockAssertions.createLongDecimalsBlock(io.trino.block.BlockAssertions.createLongDecimalsBlock) BlockAssertions.createLongsBlock(io.trino.block.BlockAssertions.createLongsBlock) BlockAssertions.createBooleansBlock(io.trino.block.BlockAssertions.createBooleansBlock) Int128(io.trino.spi.type.Int128)

Example 18 with Int128

use of io.trino.spi.type.Int128 in project trino by trinodb.

the class TestTupleDomainParquetPredicate method testLongDecimalWithNoScale.

@Test
public void testLongDecimalWithNoScale() throws Exception {
    ColumnDescriptor columnDescriptor = createColumnDescriptor(FIXED_LEN_BYTE_ARRAY, "LongDecimalColumnWithNoScale");
    DecimalType type = createDecimalType(20, 0);
    Int128 zero = Int128.ZERO;
    Int128 hundred = Int128.valueOf(100L);
    assertEquals(getDomain(columnDescriptor, type, 0, null, ID, UTC), all(type));
    assertEquals(getDomain(columnDescriptor, type, 10, binaryColumnStats(100L, 100L), ID, UTC), singleValue(type, hundred));
    assertEquals(getDomain(columnDescriptor, type, 10, binaryColumnStats(0L, 100L), ID, UTC), create(ValueSet.ofRanges(range(type, zero, true, hundred, true)), false));
    // fail on corrupted statistics
    assertThatExceptionOfType(ParquetCorruptionException.class).isThrownBy(() -> getDomain(columnDescriptor, type, 10, binaryColumnStats(100L, 10L), ID, UTC)).withMessage("Corrupted statistics for column \"[] required fixed_len_byte_array(0) LongDecimalColumnWithNoScale\" in Parquet file \"testFile\": [min: 0x64, max: 0x0A, num_nulls: 0]");
}
Also used : ColumnDescriptor(org.apache.parquet.column.ColumnDescriptor) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) DecimalType(io.trino.spi.type.DecimalType) Int128(io.trino.spi.type.Int128) Test(org.testng.annotations.Test)

Example 19 with Int128

use of io.trino.spi.type.Int128 in project trino by trinodb.

the class DecimalAverageAggregation method average.

@VisibleForTesting
public static Int128 average(LongDecimalWithOverflowAndLongState state, DecimalType type) {
    long[] decimal = state.getDecimalArray();
    int offset = state.getDecimalArrayOffset();
    long overflow = state.getOverflow();
    if (overflow != 0) {
        BigDecimal sum = new BigDecimal(Int128.valueOf(decimal[offset], decimal[offset + 1]).toBigInteger(), type.getScale());
        sum = sum.add(new BigDecimal(OVERFLOW_MULTIPLIER.multiply(BigInteger.valueOf(overflow))));
        BigDecimal count = BigDecimal.valueOf(state.getLong());
        return Decimals.encodeScaledValue(sum.divide(count, type.getScale(), ROUND_HALF_UP), type.getScale());
    }
    Int128 result = divideRoundUp(decimal[offset], decimal[offset + 1], 0, 0, state.getLong(), 0);
    if (overflows(result)) {
        throw new ArithmeticException("Decimal overflow");
    }
    return result;
}
Also used : Int128(io.trino.spi.type.Int128) BigDecimal(java.math.BigDecimal) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with Int128

use of io.trino.spi.type.Int128 in project trino by trinodb.

the class TypeUtils method trinoNativeToJdbcObject.

private static Object trinoNativeToJdbcObject(ConnectorSession session, Type type, Object object) {
    if (object == null) {
        return null;
    }
    if (DOUBLE.equals(type) || BOOLEAN.equals(type) || BIGINT.equals(type)) {
        return object;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            BigInteger unscaledValue = BigInteger.valueOf((long) object);
            return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
        }
        BigInteger unscaledValue = ((Int128) object).toBigInteger();
        return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
    }
    if (REAL.equals(type)) {
        return intBitsToFloat(toIntExact((long) object));
    }
    if (TINYINT.equals(type)) {
        return SignedBytes.checkedCast((long) object);
    }
    if (SMALLINT.equals(type)) {
        return Shorts.checkedCast((long) object);
    }
    if (INTEGER.equals(type)) {
        return toIntExact((long) object);
    }
    if (DATE.equals(type)) {
        // convert to midnight in default time zone
        long millis = DAYS.toMillis((long) object);
        return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return ((Slice) object).toStringUtf8();
    }
    if (type instanceof ArrayType) {
        // process subarray of multi-dimensional array
        return getJdbcObjectArray(session, ((ArrayType) type).getElementType(), (Block) object);
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported type: " + type);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) VarcharType(io.trino.spi.type.VarcharType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(io.trino.spi.type.DecimalType) BigInteger(java.math.BigInteger) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) Int128(io.trino.spi.type.Int128) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Date(java.sql.Date)

Aggregations

Int128 (io.trino.spi.type.Int128)21 DecimalType (io.trino.spi.type.DecimalType)14 Slice (io.airlift.slice.Slice)9 VarcharType (io.trino.spi.type.VarcharType)7 BigDecimal (java.math.BigDecimal)7 CharType (io.trino.spi.type.CharType)6 BigInteger (java.math.BigInteger)6 TrinoException (io.trino.spi.TrinoException)5 TimestampType (io.trino.spi.type.TimestampType)5 Type (io.trino.spi.type.Type)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 Test (org.testng.annotations.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)3 Block (io.trino.spi.block.Block)3 BIGINT (io.trino.spi.type.BigintType.BIGINT)3 BOOLEAN (io.trino.spi.type.BooleanType.BOOLEAN)3 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)3 Float.intBitsToFloat (java.lang.Float.intBitsToFloat)3