use of io.trino.spi.type.RowType in project trino by trinodb.
the class CassandraType method createTypeForUserType.
private static Optional<CassandraType> createTypeForUserType(DataType dataType) {
UserType userType = (UserType) dataType;
// Using ImmutableMap is important as we exploit the fact that entries iteration order matches the order of putting values via builder
ImmutableMap.Builder<String, CassandraType> argumentTypes = ImmutableMap.builder();
for (UserType.Field field : userType) {
Optional<CassandraType> cassandraType = CassandraType.toCassandraType(field.getType());
if (cassandraType.isEmpty()) {
return Optional.empty();
}
argumentTypes.put(field.getName(), cassandraType.get());
}
RowType trinoType = RowType.from(argumentTypes.buildOrThrow().entrySet().stream().map(field -> new RowType.Field(Optional.of(field.getKey()), field.getValue().getTrinoType())).collect(toImmutableList()));
return Optional.of(new CassandraType(Kind.UDT, trinoType, argumentTypes.buildOrThrow().values().stream().collect(toImmutableList())));
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class TestParquetPageSourceFactory method testGetNestedMixedRepetitionColumnType.
@Test
public void testGetNestedMixedRepetitionColumnType() {
RowType rowType = rowType(RowType.field("optional_level2", rowType(RowType.field("required_level3", IntegerType.INTEGER))));
HiveColumnHandle columnHandle = new HiveColumnHandle("optional_level1", 0, HiveType.valueOf("struct<optional_level2:struct<required_level3:int>>"), rowType, Optional.of(new HiveColumnProjectionInfo(ImmutableList.of(1, 1), ImmutableList.of("optional_level2", "required_level3"), toHiveType(IntegerType.INTEGER), IntegerType.INTEGER)), REGULAR, Optional.empty());
MessageType fileSchema = new MessageType("hive_schema", new GroupType(OPTIONAL, "optional_level1", new GroupType(OPTIONAL, "optional_level2", new PrimitiveType(REQUIRED, INT32, "required_level3"))));
assertEquals(ParquetPageSourceFactory.getColumnType(columnHandle, fileSchema, true).get(), fileSchema.getType("optional_level1"));
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class TestParquetPredicateUtils method testParquetTupleDomainStruct.
@Test
public void testParquetTupleDomainStruct() {
RowType rowType = rowType(RowType.field("a", INTEGER), RowType.field("b", INTEGER));
HiveColumnHandle columnHandle = createBaseColumn("my_struct", 0, HiveType.valueOf("struct<a:int,b:int>"), rowType, REGULAR, Optional.empty());
TupleDomain<HiveColumnHandle> domain = withColumnDomains(ImmutableMap.of(columnHandle, Domain.notNull(rowType)));
MessageType fileSchema = new MessageType("hive_schema", new GroupType(OPTIONAL, "my_struct", new PrimitiveType(OPTIONAL, INT32, "a"), new PrimitiveType(OPTIONAL, INT32, "b")));
Map<List<String>, RichColumnDescriptor> descriptorsByPath = getDescriptors(fileSchema, fileSchema);
TupleDomain<ColumnDescriptor> tupleDomain = getParquetTupleDomain(descriptorsByPath, domain, fileSchema, true);
assertTrue(tupleDomain.isAll());
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class RcFileTester method assertColumnValueEquals.
private static void assertColumnValueEquals(Type type, Object actual, Object expected) {
if (actual == null) {
assertNull(expected);
return;
}
if (type instanceof ArrayType) {
List<?> actualArray = (List<?>) actual;
List<?> expectedArray = (List<?>) expected;
assertEquals(actualArray.size(), expectedArray.size());
Type elementType = type.getTypeParameters().get(0);
for (int i = 0; i < actualArray.size(); i++) {
Object actualElement = actualArray.get(i);
Object expectedElement = expectedArray.get(i);
assertColumnValueEquals(elementType, actualElement, expectedElement);
}
} else if (type instanceof MapType) {
Map<?, ?> actualMap = (Map<?, ?>) actual;
Map<?, ?> expectedMap = (Map<?, ?>) expected;
assertEquals(actualMap.size(), expectedMap.size());
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
List<Entry<?, ?>> expectedEntries = new ArrayList<>(expectedMap.entrySet());
for (Entry<?, ?> actualEntry : actualMap.entrySet()) {
Iterator<Entry<?, ?>> iterator = expectedEntries.iterator();
while (iterator.hasNext()) {
Entry<?, ?> expectedEntry = iterator.next();
try {
assertColumnValueEquals(keyType, actualEntry.getKey(), expectedEntry.getKey());
assertColumnValueEquals(valueType, actualEntry.getValue(), expectedEntry.getValue());
iterator.remove();
} catch (AssertionError ignored) {
}
}
}
assertTrue(expectedEntries.isEmpty(), "Unmatched entries " + expectedEntries);
} else if (type instanceof RowType) {
List<Type> fieldTypes = type.getTypeParameters();
List<?> actualRow = (List<?>) actual;
List<?> expectedRow = (List<?>) expected;
assertEquals(actualRow.size(), fieldTypes.size());
assertEquals(actualRow.size(), expectedRow.size());
for (int fieldId = 0; fieldId < actualRow.size(); fieldId++) {
Type fieldType = fieldTypes.get(fieldId);
Object actualElement = actualRow.get(fieldId);
Object expectedElement = expectedRow.get(fieldId);
assertColumnValueEquals(fieldType, actualElement, expectedElement);
}
} else if (type.equals(DOUBLE)) {
Double actualDouble = (Double) actual;
Double expectedDouble = (Double) expected;
if (actualDouble.isNaN()) {
assertTrue(expectedDouble.isNaN(), "expected double to be NaN");
} else {
assertEquals(actualDouble, expectedDouble, 0.001);
}
} else if (!Objects.equals(actual, expected)) {
assertEquals(actual, expected);
}
}
use of io.trino.spi.type.RowType in project trino by trinodb.
the class RcFileTester method getJavaObjectInspector.
private static ObjectInspector getJavaObjectInspector(Type type) {
if (type.equals(BOOLEAN)) {
return javaBooleanObjectInspector;
}
if (type.equals(BIGINT)) {
return javaLongObjectInspector;
}
if (type.equals(INTEGER)) {
return javaIntObjectInspector;
}
if (type.equals(SMALLINT)) {
return javaShortObjectInspector;
}
if (type.equals(TINYINT)) {
return javaByteObjectInspector;
}
if (type.equals(REAL)) {
return javaFloatObjectInspector;
}
if (type.equals(DOUBLE)) {
return javaDoubleObjectInspector;
}
if (type instanceof VarcharType) {
return javaStringObjectInspector;
}
if (type.equals(VARBINARY)) {
return javaByteArrayObjectInspector;
}
if (type.equals(DATE)) {
return javaDateObjectInspector;
}
if (type.equals(TIMESTAMP_MILLIS)) {
return javaTimestampObjectInspector;
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
}
if (type instanceof ArrayType) {
return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
}
if (type instanceof MapType) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
}
if (type instanceof RowType) {
return getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(RcFileTester::getJavaObjectInspector).collect(toList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
Aggregations