use of io.trino.spi.type.RowType in project trino by trinodb.
the class HiveWriteUtils method getField.
public static Object getField(DateTimeZone localZone, Type type, Block block, int position) {
if (block.isNull(position)) {
return null;
}
if (BOOLEAN.equals(type)) {
return type.getBoolean(block, position);
}
if (BIGINT.equals(type)) {
return type.getLong(block, position);
}
if (INTEGER.equals(type)) {
return toIntExact(type.getLong(block, position));
}
if (SMALLINT.equals(type)) {
return Shorts.checkedCast(type.getLong(block, position));
}
if (TINYINT.equals(type)) {
return SignedBytes.checkedCast(type.getLong(block, position));
}
if (REAL.equals(type)) {
return intBitsToFloat((int) type.getLong(block, position));
}
if (DOUBLE.equals(type)) {
return type.getDouble(block, position);
}
if (type instanceof VarcharType) {
return new Text(type.getSlice(block, position).getBytes());
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
return new Text(padSpaces(type.getSlice(block, position), charType).toStringUtf8());
}
if (VARBINARY.equals(type)) {
return type.getSlice(block, position).getBytes();
}
if (DATE.equals(type)) {
return Date.ofEpochDay(toIntExact(type.getLong(block, position)));
}
if (type instanceof TimestampType) {
return getHiveTimestamp(localZone, (TimestampType) type, block, position);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getHiveDecimal(decimalType, block, position);
}
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
Block arrayBlock = block.getObject(position, Block.class);
List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
list.add(getField(localZone, elementType, arrayBlock, i));
}
return unmodifiableList(list);
}
if (type instanceof MapType) {
Type keyType = ((MapType) type).getKeyType();
Type valueType = ((MapType) type).getValueType();
Block mapBlock = block.getObject(position, Block.class);
Map<Object, Object> map = new HashMap<>();
for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
map.put(getField(localZone, keyType, mapBlock, i), getField(localZone, valueType, mapBlock, i + 1));
}
return unmodifiableMap(map);
}
if (type instanceof RowType) {
List<Type> fieldTypes = type.getTypeParameters();
Block rowBlock = block.getObject(position, Block.class);
checkCondition(fieldTypes.size() == rowBlock.getPositionCount(), StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
List<Object> row = new ArrayList<>(rowBlock.getPositionCount());
for (int i = 0; i < rowBlock.getPositionCount(); i++) {
row.add(getField(localZone, fieldTypes.get(i), rowBlock, i));
}
return unmodifiableList(row);
}
throw new TrinoException(NOT_SUPPORTED, "unsupported type: " + type);
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class TestReaderProjectionsAdapter method createProjectedColumnBlock.
private static Block createProjectedColumnBlock(Block data, Type finalType, Type blockType, List<Integer> dereferences) {
if (dereferences.size() == 0) {
return data;
}
BlockBuilder builder = finalType.createBlockBuilder(null, data.getPositionCount());
for (int i = 0; i < data.getPositionCount(); i++) {
Type sourceType = blockType;
Block currentData = null;
boolean isNull = data.isNull(i);
if (!isNull) {
// Get SingleRowBlock corresponding to element at position i
currentData = data.getObject(i, Block.class);
}
// Apply all dereferences except for the last one, because the type can be different
for (int j = 0; j < dereferences.size() - 1; j++) {
if (isNull) {
// If null element is discovered at any dereferencing step, break
break;
}
checkArgument(sourceType instanceof RowType);
if (currentData.isNull(dereferences.get(j))) {
currentData = null;
} else {
sourceType = ((RowType) sourceType).getFields().get(dereferences.get(j)).getType();
currentData = currentData.getObject(dereferences.get(j), Block.class);
}
isNull = isNull || (currentData == null);
}
if (isNull) {
// Append null if any of the elements in the dereference chain were null
builder.appendNull();
} else {
int lastDereference = dereferences.get(dereferences.size() - 1);
finalType.appendTo(currentData, lastDereference, builder);
}
}
return builder.build();
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class TestPushDownDereferencesRules method testPushdownDereferencesThroughUnnest.
@Test
public void testPushdownDereferencesThroughUnnest() {
ArrayType arrayType = new ArrayType(BIGINT);
tester().assertThat(new PushDownDereferenceThroughUnnest(tester().getTypeAnalyzer())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("msg[1]")), p.unnest(ImmutableList.of(p.symbol("msg", ROW_TYPE)), ImmutableList.of(new UnnestNode.Mapping(p.symbol("arr", arrayType), ImmutableList.of(p.symbol("field")))), Optional.empty(), INNER, Optional.empty(), p.values(p.symbol("msg", ROW_TYPE), p.symbol("arr", arrayType))))).matches(strictProject(ImmutableMap.of("x", PlanMatchPattern.expression("msg_x")), unnest(strictProject(ImmutableMap.of("msg_x", PlanMatchPattern.expression("msg[1]"), "msg", PlanMatchPattern.expression("msg"), "arr", PlanMatchPattern.expression("arr")), values("msg", "arr")))));
// Test with dereferences on unnested column
RowType rowType = rowType(field("f1", BIGINT), field("f2", BIGINT));
ArrayType nestedColumnType = new ArrayType(rowType(field("f1", BIGINT), field("f2", rowType)));
tester().assertThat(new PushDownDereferenceThroughUnnest(tester().getTypeAnalyzer())).on(p -> p.project(Assignments.of(p.symbol("deref_replicate", BIGINT), expression("replicate[2]"), p.symbol("deref_unnest", BIGINT), expression("unnested_row[2]")), p.unnest(ImmutableList.of(p.symbol("replicate", rowType)), ImmutableList.of(new UnnestNode.Mapping(p.symbol("nested", nestedColumnType), ImmutableList.of(p.symbol("unnested_bigint", BIGINT), p.symbol("unnested_row", rowType)))), p.values(p.symbol("replicate", rowType), p.symbol("nested", nestedColumnType))))).matches(strictProject(ImmutableMap.of("deref_replicate", PlanMatchPattern.expression("symbol"), "deref_unnest", // not pushed down
PlanMatchPattern.expression("unnested_row[2]")), unnest(ImmutableList.of("replicate", "symbol"), ImmutableList.of(unnestMapping("nested", ImmutableList.of("unnested_bigint", "unnested_row"))), strictProject(ImmutableMap.of("symbol", PlanMatchPattern.expression("replicate[2]"), "replicate", PlanMatchPattern.expression("replicate"), "nested", PlanMatchPattern.expression("nested")), values("replicate", "nested")))));
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class TestPushLimitThroughProject method testPushDownLimitThroughOverlappingDereferences.
@Test
public void testPushDownLimitThroughOverlappingDereferences() {
RowType rowType = RowType.from(ImmutableList.of(new RowType.Field(Optional.of("x"), BIGINT), new RowType.Field(Optional.of("y"), BIGINT)));
tester().assertThat(new PushLimitThroughProject(tester().getTypeAnalyzer())).on(p -> {
Symbol a = p.symbol("a", rowType);
return p.limit(1, p.project(Assignments.of(p.symbol("b"), new SubscriptExpression(a.toSymbolReference(), new LongLiteral("1")), p.symbol("c", rowType), a.toSymbolReference()), p.values(a)));
}).matches(project(ImmutableMap.of("b", expression("a[1]"), "c", expression("a")), limit(1, values("a"))));
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class AvroDecoderTestUtil method checkArrayValues.
public static void checkArrayValues(Block block, Type type, Object value) {
assertNotNull(type, "Type is null");
assertTrue(type instanceof ArrayType, "Unexpected type");
assertNotNull(block, "Block is null");
assertNotNull(value, "Value is null");
List<?> list = (List<?>) value;
assertEquals(block.getPositionCount(), list.size());
Type elementType = ((ArrayType) type).getElementType();
if (elementType instanceof ArrayType) {
for (int index = 0; index < block.getPositionCount(); index++) {
if (block.isNull(index)) {
assertNull(list.get(index));
continue;
}
Block arrayBlock = block.getObject(index, Block.class);
checkArrayValues(arrayBlock, elementType, list.get(index));
}
} else if (elementType instanceof MapType) {
for (int index = 0; index < block.getPositionCount(); index++) {
if (block.isNull(index)) {
assertNull(list.get(index));
continue;
}
Block mapBlock = block.getObject(index, Block.class);
checkMapValues(mapBlock, elementType, list.get(index));
}
} else if (elementType instanceof RowType) {
for (int index = 0; index < block.getPositionCount(); index++) {
if (block.isNull(index)) {
assertNull(list.get(index));
continue;
}
Block rowBlock = block.getObject(index, Block.class);
checkRowValues(rowBlock, elementType, list.get(index));
}
} else {
for (int index = 0; index < block.getPositionCount(); index++) {
checkPrimitiveValue(getObjectValue(elementType, block, index), list.get(index));
}
}
}
Aggregations