use of io.trino.spi.type.RowType in project trino by trinodb.
the class CheckpointEntryIterator method readMinMax.
private Map<String, Object> readMinMax(Block block, int blockPosition, List<ColumnMetadata> eligibleColumns) {
if (block.isNull(blockPosition)) {
// Statistics were not collected
return ImmutableMap.of();
}
Block valuesBlock = block.getObject(blockPosition, Block.class);
ImmutableMap.Builder<String, Object> values = ImmutableMap.builder();
for (int i = 0; i < eligibleColumns.size(); i++) {
ColumnMetadata metadata = eligibleColumns.get(i);
String name = metadata.getName();
Type type = metadata.getType();
if (valuesBlock.isNull(i)) {
continue;
}
if (type instanceof RowType) {
if (checkpointRowStatisticsWritingEnabled) {
// RowType column statistics are not used for query planning, but need to be copied when writing out new Checkpoint files.
values.put(name, valuesBlock.getSingleValueBlock(i));
}
continue;
}
if (type instanceof TimestampWithTimeZoneType) {
Instant instant = Instant.ofEpochMilli(LongMath.divide((Long) readNativeValue(TIMESTAMP_MILLIS, valuesBlock, i), MICROSECONDS_PER_MILLISECOND, UNNECESSARY));
if (!instant.atZone(UTC).toLocalDate().isBefore(START_OF_MODERN_ERA)) {
values.put(name, packDateTimeWithZone(instant.toEpochMilli(), UTC_KEY));
}
continue;
}
values.put(name, readNativeValue(type, valuesBlock, i));
}
return values.buildOrThrow();
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class BlockAssertions method createRandomBlockForNestedType.
private static Block createRandomBlockForNestedType(Type type, int positionCount, float nullRate) {
// Builds isNull and offsets of size positionCount
boolean[] isNull = null;
Set<Integer> nullPositions = null;
if (nullRate > 0) {
isNull = new boolean[positionCount];
nullPositions = chooseNullPositions(positionCount, nullRate);
}
int[] offsets = new int[positionCount + 1];
for (int position = 0; position < positionCount; position++) {
if (nullRate > 0 && nullPositions.contains(position)) {
isNull[position] = true;
offsets[position + 1] = offsets[position];
} else {
// RowType doesn't need offsets, so we just use 1,
// for ArrayType and MapType we choose randomly either array length or map size at the current position
offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : RANDOM.nextInt(ENTRY_SIZE) + 1);
}
}
// Builds the nested block of size offsets[positionCount].
if (type instanceof ArrayType) {
Block valuesBlock = createRandomBlockForType(((ArrayType) type).getElementType(), offsets[positionCount], nullRate);
return fromElementBlock(positionCount, Optional.ofNullable(isNull), offsets, valuesBlock);
}
if (type instanceof MapType) {
MapType mapType = (MapType) type;
Block keyBlock = createRandomBlockForType(mapType.getKeyType(), offsets[positionCount], 0.0f);
Block valueBlock = createRandomBlockForType(mapType.getValueType(), offsets[positionCount], nullRate);
return mapType.createBlockFromKeyValue(Optional.ofNullable(isNull), offsets, keyBlock, valueBlock);
}
if (type instanceof RowType) {
List<Type> fieldTypes = type.getTypeParameters();
Block[] fieldBlocks = new Block[fieldTypes.size()];
for (int i = 0; i < fieldBlocks.length; i++) {
fieldBlocks[i] = createRandomBlockForType(fieldTypes.get(i), positionCount, nullRate);
}
return RowBlock.fromFieldBlocks(positionCount, Optional.ofNullable(isNull), fieldBlocks);
}
throw new IllegalArgumentException(format("type %s is not supported.", type));
}
use of io.trino.spi.type.RowType 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.RowType in project trino by trinodb.
the class BigQueryResultPageSource method writeBlock.
private void writeBlock(BlockBuilder output, Type type, Object value) {
if (type instanceof ArrayType && value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
for (Object element : (List<?>) value) {
appendTo(type.getTypeParameters().get(0), element, builder);
}
output.closeEntry();
return;
}
if (type instanceof RowType && value instanceof GenericRecord) {
GenericRecord record = (GenericRecord) value;
BlockBuilder builder = output.beginBlockEntry();
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));
}
checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
for (int index = 0; index < type.getTypeParameters().size(); index++) {
appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
}
output.closeEntry();
return;
}
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Block: " + type.getTypeSignature());
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class TestTypeConversions method testConvertOneLevelRecordColumn.
@Test
public void testConvertOneLevelRecordColumn() {
BigQueryColumnHandle column = new BigQueryColumnHandle("rec", BigQueryType.RECORD, Field.Mode.NULLABLE, null, null, ImmutableList.of(new BigQueryColumnHandle("sub_s", BigQueryType.STRING, Field.Mode.NULLABLE, null, null, ImmutableList.of(), null), new BigQueryColumnHandle("sub_i", BigQueryType.INTEGER, Field.Mode.NULLABLE, null, null, ImmutableList.of(), null)), null);
ColumnMetadata metadata = column.getColumnMetadata();
RowType targetType = RowType.rowType(RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT));
assertThat(metadata.getType()).isEqualTo(targetType);
}
Aggregations