Search in sources :

Example 26 with DecimalType

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

the class OrcType method toOrcType.

private static List<OrcType> toOrcType(int nextFieldTypeIndex, Type type) {
    if (BOOLEAN.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BOOLEAN));
    }
    if (TINYINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BYTE));
    }
    if (SMALLINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.SHORT));
    }
    if (INTEGER.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.INT));
    }
    if (BIGINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.LONG));
    }
    if (DOUBLE.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.DOUBLE));
    }
    if (REAL.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.FLOAT));
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return ImmutableList.of(new OrcType(OrcTypeKind.STRING));
        }
        return ImmutableList.of(new OrcType(OrcTypeKind.VARCHAR, varcharType.getBoundedLength()));
    }
    if (type instanceof CharType) {
        return ImmutableList.of(new OrcType(OrcTypeKind.CHAR, ((CharType) type).getLength()));
    }
    if (VARBINARY.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BINARY));
    }
    if (DATE.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.DATE));
    }
    if (TIMESTAMP_MILLIS.equals(type) || TIMESTAMP_MICROS.equals(type) || TIMESTAMP_NANOS.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP));
    }
    if (TIMESTAMP_TZ_MILLIS.equals(type) || TIMESTAMP_TZ_MICROS.equals(type) || TIMESTAMP_TZ_NANOS.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP_INSTANT));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return ImmutableList.of(new OrcType(OrcTypeKind.DECIMAL, decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType) {
        return createOrcArrayType(nextFieldTypeIndex, type.getTypeParameters().get(0));
    }
    if (type instanceof MapType) {
        return createOrcMapType(nextFieldTypeIndex, type.getTypeParameters().get(0), type.getTypeParameters().get(1));
    }
    if (type instanceof RowType) {
        List<String> fieldNames = new ArrayList<>();
        for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
            TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
            fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
        }
        List<Type> fieldTypes = type.getTypeParameters();
        return createOrcRowType(nextFieldTypeIndex, fieldNames, fieldTypes);
    }
    throw new TrinoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(io.trino.spi.type.VarcharType) ArrayList(java.util.ArrayList) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) CharType(io.trino.spi.type.CharType) DecimalType(io.trino.spi.type.DecimalType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 27 with DecimalType

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

the class LongDecimalStatisticsBuilder method addBlock.

@Override
public void addBlock(Type type, Block block) {
    int scale = ((DecimalType) type).getScale();
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (!block.isNull(position)) {
            Int128 value = (Int128) type.getObject(block, position);
            addValue(new BigDecimal(value.toBigInteger(), scale));
        }
    }
}
Also used : DecimalType(io.trino.spi.type.DecimalType) Int128(io.trino.spi.type.Int128) BigDecimal(java.math.BigDecimal)

Example 28 with DecimalType

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

the class ShortDecimalColumnReader method readValue.

@Override
protected void readValue(BlockBuilder blockBuilder, Type trinoType) {
    if (!((trinoType instanceof DecimalType) || isIntegerType(trinoType))) {
        throw new ParquetDecodingException(format("Unsupported Trino column type (%s) for Parquet column (%s)", trinoType, columnDescriptor));
    }
    long value;
    // When decimals are encoded with primitive types Parquet stores unscaled values
    if (columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == INT32) {
        value = valuesReader.readInteger();
    } else if (columnDescriptor.getPrimitiveType().getPrimitiveTypeName() == INT64) {
        value = valuesReader.readLong();
    } else {
        byte[] bytes = valuesReader.readBytes().getBytes();
        if (typeLength <= Long.BYTES) {
            value = getShortDecimalValue(bytes);
        } else {
            int startOffset = bytes.length - Long.BYTES;
            checkBytesFitInShortDecimal(bytes, startOffset, trinoType);
            value = getShortDecimalValue(bytes, startOffset, Long.BYTES);
        }
    }
    if (trinoType instanceof DecimalType) {
        DecimalType trinoDecimalType = (DecimalType) trinoType;
        if (isShortDecimal(trinoDecimalType)) {
            long rescale = longTenToNth(Math.abs(trinoDecimalType.getScale() - parquetDecimalType.getScale()));
            long convertedValue = shortToShortCast(value, parquetDecimalType.getPrecision(), parquetDecimalType.getScale(), trinoDecimalType.getPrecision(), trinoDecimalType.getScale(), rescale, rescale / 2);
            trinoType.writeLong(blockBuilder, convertedValue);
        } else if (isLongDecimal(trinoDecimalType)) {
            trinoType.writeObject(blockBuilder, shortToLongCast(value, parquetDecimalType.getPrecision(), parquetDecimalType.getScale(), trinoDecimalType.getPrecision(), trinoDecimalType.getScale()));
        }
    } else {
        if (parquetDecimalType.getScale() != 0) {
            throw new TrinoException(NOT_SUPPORTED, format("Unsupported Trino column type (%s) for Parquet column (%s)", trinoType, columnDescriptor));
        }
        if (!isInValidNumberRange(trinoType, value)) {
            throw new TrinoException(NOT_SUPPORTED, format("Could not coerce from %s to %s: %s", parquetDecimalType, trinoType, value));
        }
        trinoType.writeLong(blockBuilder, value);
    }
}
Also used : ParquetDecodingException(org.apache.parquet.io.ParquetDecodingException) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException)

Example 29 with DecimalType

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

the class StandardColumnMappings method shortDecimalWriteFunction.

public static LongWriteFunction shortDecimalWriteFunction(DecimalType decimalType) {
    requireNonNull(decimalType, "decimalType is null");
    checkArgument(decimalType.isShort());
    return (statement, index, value) -> {
        BigInteger unscaledValue = BigInteger.valueOf(value);
        BigDecimal bigDecimal = new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
        statement.setBigDecimal(index, bigDecimal);
    };
}
Also used : UNNECESSARY(java.math.RoundingMode.UNNECESSARY) DateTimeZone(org.joda.time.DateTimeZone) Time(java.sql.Time) NANOSECONDS_PER_MICROSECOND(io.trino.spi.type.Timestamps.NANOSECONDS_PER_MICROSECOND) Timestamps.roundDiv(io.trino.spi.type.Timestamps.roundDiv) CASE_INSENSITIVE_CHARACTER_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.CASE_INSENSITIVE_CHARACTER_PUSHDOWN) CharType.createCharType(io.trino.spi.type.CharType.createCharType) BigDecimal(java.math.BigDecimal) Slices.wrappedBuffer(io.airlift.slice.Slices.wrappedBuffer) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ResultSet(java.sql.ResultSet) LocalTime(java.time.LocalTime) BigInteger(java.math.BigInteger) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) INTEGER(io.trino.spi.type.IntegerType.INTEGER) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) Timestamp(java.sql.Timestamp) MICROSECONDS_PER_SECOND(io.trino.spi.type.Timestamps.MICROSECONDS_PER_SECOND) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) PreparedStatement(java.sql.PreparedStatement) Instant(java.time.Instant) String.format(java.lang.String.format) BIGINT(io.trino.spi.type.BigintType.BIGINT) LocalDate(java.time.LocalDate) Decimals(io.trino.spi.type.Decimals) ISOChronology(org.joda.time.chrono.ISOChronology) UTC(java.time.ZoneOffset.UTC) DecimalType(io.trino.spi.type.DecimalType) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) BaseEncoding.base16(com.google.common.io.BaseEncoding.base16) Slice(io.airlift.slice.Slice) TimeType(io.trino.spi.type.TimeType) Math.floorMod(java.lang.Math.floorMod) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) PICOSECONDS_PER_NANOSECOND(io.trino.spi.type.Timestamps.PICOSECONDS_PER_NANOSECOND) Type(io.trino.spi.type.Type) LocalDateTime(java.time.LocalDateTime) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) Shorts(com.google.common.primitives.Shorts) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) TimestampType(io.trino.spi.type.TimestampType) NANOSECONDS_PER_DAY(io.trino.spi.type.Timestamps.NANOSECONDS_PER_DAY) VarcharType(io.trino.spi.type.VarcharType) Float.floatToRawIntBits(java.lang.Float.floatToRawIntBits) SQLException(java.sql.SQLException) Decimals.encodeShortScaledValue(io.trino.spi.type.Decimals.encodeShortScaledValue) Verify.verify(com.google.common.base.Verify.verify) FULL_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.FULL_PUSHDOWN) Objects.requireNonNull(java.util.Objects.requireNonNull) DAYS(java.util.concurrent.TimeUnit.DAYS) Timestamps.round(io.trino.spi.type.Timestamps.round) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) TIME(io.trino.spi.type.TimeType.TIME) Math.floorDiv(java.lang.Math.floorDiv) Math.toIntExact(java.lang.Math.toIntExact) Int128(io.trino.spi.type.Int128) PICOSECONDS_PER_DAY(io.trino.spi.type.Timestamps.PICOSECONDS_PER_DAY) LongTimestamp(io.trino.spi.type.LongTimestamp) CharMatcher(com.google.common.base.CharMatcher) SignedBytes(com.google.common.primitives.SignedBytes) Date(java.sql.Date) DISABLE_PUSHDOWN(io.trino.plugin.jdbc.PredicatePushdownController.DISABLE_PUSHDOWN) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) CharType(io.trino.spi.type.CharType) SliceUtf8.countCodePoints(io.airlift.slice.SliceUtf8.countCodePoints) TINYINT(io.trino.spi.type.TinyintType.TINYINT) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 30 with DecimalType

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

the class ImplementAvgDecimal method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(ARGUMENT);
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
    DecimalType type = (DecimalType) columnHandle.getColumnType();
    verify(aggregateFunction.getOutputType().equals(type));
    return Optional.of(new JdbcExpression(format("CAST(avg(%s) AS decimal(%s, %s))", context.rewriteExpression(argument).orElseThrow(), type.getPrecision(), type.getScale()), columnHandle.getJdbcTypeHandle()));
}
Also used : Variable(io.trino.spi.expression.Variable) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) DecimalType(io.trino.spi.type.DecimalType) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Aggregations

DecimalType (io.trino.spi.type.DecimalType)79 VarcharType (io.trino.spi.type.VarcharType)50 CharType (io.trino.spi.type.CharType)39 TrinoException (io.trino.spi.TrinoException)31 Type (io.trino.spi.type.Type)29 TimestampType (io.trino.spi.type.TimestampType)23 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)22 ArrayType (io.trino.spi.type.ArrayType)21 BigDecimal (java.math.BigDecimal)19 Int128 (io.trino.spi.type.Int128)16 BigInteger (java.math.BigInteger)15 Block (io.trino.spi.block.Block)14 Slice (io.airlift.slice.Slice)13 TimeType (io.trino.spi.type.TimeType)13 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)13 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)13 MapType (io.trino.spi.type.MapType)12 ArrayList (java.util.ArrayList)12 ImmutableList (com.google.common.collect.ImmutableList)11 RowType (io.trino.spi.type.RowType)11