use of io.trino.spi.type.RowType in project trino by trinodb.
the class TestTypeConversions method testConvertTwoLevelsRecordField.
@Test
public void testConvertTwoLevelsRecordField() {
Field field = Field.of("rec", LegacySQLTypeName.RECORD, Field.of("sub_rec", LegacySQLTypeName.RECORD, Field.of("sub_sub_s", LegacySQLTypeName.STRING), Field.of("sub_sub_i", LegacySQLTypeName.INTEGER)), Field.of("sub_s", LegacySQLTypeName.STRING), Field.of("sub_i", LegacySQLTypeName.INTEGER));
ColumnMetadata metadata = Conversions.toColumnMetadata(field);
RowType targetType = RowType.rowType(RowType.field("sub_rec", RowType.rowType(RowType.field("sub_sub_s", VarcharType.VARCHAR), RowType.field("sub_sub_i", BigintType.BIGINT))), RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT));
assertThat(metadata.getType()).isEqualTo(targetType);
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class TestTypeConversions method testConvertOneLevelRecordField.
@Test
public void testConvertOneLevelRecordField() {
Field field = Field.of("rec", LegacySQLTypeName.RECORD, Field.of("sub_s", LegacySQLTypeName.STRING), Field.of("sub_i", LegacySQLTypeName.INTEGER));
ColumnMetadata metadata = Conversions.toColumnMetadata(field);
RowType targetType = RowType.rowType(RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT));
assertThat(metadata.getType()).isEqualTo(targetType);
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class GeoFunctions method geometryNearestPoints.
@SqlNullable
@Description("Return the closest points on the two geometries")
@ScalarFunction("geometry_nearest_points")
@SqlType("row(" + GEOMETRY_TYPE_NAME + "," + GEOMETRY_TYPE_NAME + ")")
public static Block geometryNearestPoints(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) {
Geometry leftGeometry = JtsGeometrySerde.deserialize(left);
Geometry rightGeometry = JtsGeometrySerde.deserialize(right);
if (leftGeometry.isEmpty() || rightGeometry.isEmpty()) {
return null;
}
RowType rowType = RowType.anonymous(ImmutableList.of(GEOMETRY, GEOMETRY));
PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(rowType));
GeometryFactory geometryFactory = leftGeometry.getFactory();
Coordinate[] nearestCoordinates = DistanceOp.nearestPoints(leftGeometry, rightGeometry);
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder entryBlockBuilder = blockBuilder.beginBlockEntry();
GEOMETRY.writeSlice(entryBlockBuilder, JtsGeometrySerde.serialize(geometryFactory.createPoint(nearestCoordinates[0])));
GEOMETRY.writeSlice(entryBlockBuilder, JtsGeometrySerde.serialize(geometryFactory.createPoint(nearestCoordinates[1])));
blockBuilder.closeEntry();
pageBuilder.declarePosition();
return rowType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class ZipFunction method zip.
@UsedByGeneratedCode
public static Block zip(List<Type> types, Block... arrays) {
int biggestCardinality = 0;
for (Block array : arrays) {
biggestCardinality = Math.max(biggestCardinality, array.getPositionCount());
}
RowType rowType = RowType.anonymous(types);
BlockBuilder outputBuilder = rowType.createBlockBuilder(null, biggestCardinality);
for (int outputPosition = 0; outputPosition < biggestCardinality; outputPosition++) {
BlockBuilder rowBuilder = outputBuilder.beginBlockEntry();
for (int fieldIndex = 0; fieldIndex < arrays.length; fieldIndex++) {
if (arrays[fieldIndex].getPositionCount() <= outputPosition) {
rowBuilder.appendNull();
} else {
types.get(fieldIndex).appendTo(arrays[fieldIndex], outputPosition, rowBuilder);
}
}
outputBuilder.closeEntry();
}
return outputBuilder.build();
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class OrcTester method preprocessWriteValueHive.
private static Object preprocessWriteValueHive(Type type, Object value) {
if (value == null) {
return null;
}
if (type.equals(BOOLEAN)) {
return value;
}
if (type.equals(TINYINT)) {
return ((Number) value).byteValue();
}
if (type.equals(SMALLINT)) {
return ((Number) value).shortValue();
}
if (type.equals(INTEGER)) {
return ((Number) value).intValue();
}
if (type.equals(BIGINT)) {
return ((Number) value).longValue();
}
if (type.equals(REAL)) {
return ((Number) value).floatValue();
}
if (type.equals(DOUBLE)) {
return ((Number) value).doubleValue();
}
if (type instanceof VarcharType) {
return value;
}
if (type instanceof CharType) {
return new HiveChar((String) value, ((CharType) type).getLength());
}
if (type.equals(VARBINARY)) {
return ((SqlVarbinary) value).getBytes();
}
if (type.equals(DATE)) {
return Date.ofEpochDay(((SqlDate) value).getDays());
}
if (type.equals(TIMESTAMP_MILLIS) || type.equals(TIMESTAMP_MICROS) || type.equals(TIMESTAMP_NANOS)) {
LocalDateTime dateTime = ((SqlTimestamp) value).toLocalDateTime();
return Timestamp.ofEpochSecond(dateTime.toEpochSecond(ZoneOffset.UTC), dateTime.getNano());
}
if (type.equals(TIMESTAMP_TZ_MILLIS) || type.equals(TIMESTAMP_TZ_MICROS) || type.equals(TIMESTAMP_TZ_NANOS)) {
SqlTimestampWithTimeZone timestamp = (SqlTimestampWithTimeZone) value;
int nanosOfMilli = roundDiv(timestamp.getPicosOfMilli(), PICOSECONDS_PER_NANOSECOND);
return Timestamp.ofEpochMilli(timestamp.getEpochMillis(), nanosOfMilli);
}
if (type instanceof DecimalType) {
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
}
if (type instanceof ArrayType) {
Type elementType = type.getTypeParameters().get(0);
return ((List<?>) value).stream().map(element -> preprocessWriteValueHive(elementType, element)).collect(toList());
}
if (type instanceof MapType) {
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Map<Object, Object> newMap = new HashMap<>();
for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
newMap.put(preprocessWriteValueHive(keyType, entry.getKey()), preprocessWriteValueHive(valueType, entry.getValue()));
}
return newMap;
}
if (type instanceof RowType) {
List<?> fieldValues = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
List<Object> newStruct = new ArrayList<>();
for (int fieldId = 0; fieldId < fieldValues.size(); fieldId++) {
newStruct.add(preprocessWriteValueHive(fieldTypes.get(fieldId), fieldValues.get(fieldId)));
}
return newStruct;
}
throw new IllegalArgumentException("unsupported type: " + type);
}
Aggregations