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));
}
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));
}
}
}
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);
}
}
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);
};
}
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()));
}
Aggregations