use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class TestSerDeUtils method testStructBlock.
@Test
public void testStructBlock() {
// test simple structs
InnerStruct innerStruct = new InnerStruct(13, 14L);
io.prestosql.spi.type.Type rowType = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT));
Block actual = toBinaryBlock(rowType, innerStruct, getInspector(InnerStruct.class));
Block expected = rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 13, 14L);
assertBlockEquals(actual, expected);
// test complex structs
OuterStruct outerStruct = new OuterStruct();
outerStruct.byteVal = (byte) 1;
outerStruct.shortVal = (short) 2;
outerStruct.intVal = 3;
outerStruct.longVal = 4L;
outerStruct.floatVal = 5.01f;
outerStruct.doubleVal = 6.001d;
outerStruct.stringVal = "seven";
outerStruct.byteArray = new byte[] { '2' };
InnerStruct is1 = new InnerStruct(2, -5L);
InnerStruct is2 = new InnerStruct(-10, 0L);
outerStruct.structArray = new ArrayList<>(2);
outerStruct.structArray.add(is1);
outerStruct.structArray.add(is2);
outerStruct.map = new TreeMap<>();
outerStruct.map.put("twelve", new InnerStruct(0, 5L));
outerStruct.map.put("fifteen", new InnerStruct(-5, -10L));
outerStruct.innerStruct = new InnerStruct(18, 19L);
io.prestosql.spi.type.Type innerRowType = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT));
io.prestosql.spi.type.Type arrayOfInnerRowType = new ArrayType(innerRowType);
io.prestosql.spi.type.Type mapOfInnerRowType = HiveTestUtils.mapType(createUnboundedVarcharType(), innerRowType);
List<io.prestosql.spi.type.Type> outerRowParameterTypes = ImmutableList.of(TINYINT, SMALLINT, INTEGER, BIGINT, REAL, DOUBLE, createUnboundedVarcharType(), createUnboundedVarcharType(), arrayOfInnerRowType, mapOfInnerRowType, innerRowType);
io.prestosql.spi.type.Type outerRowType = RowType.anonymous(outerRowParameterTypes);
actual = toBinaryBlock(outerRowType, outerStruct, getInspector(OuterStruct.class));
ImmutableList.Builder<Object> outerRowValues = ImmutableList.builder();
outerRowValues.add((byte) 1);
outerRowValues.add((short) 2);
outerRowValues.add(3);
outerRowValues.add(4L);
outerRowValues.add(5.01f);
outerRowValues.add(6.001d);
outerRowValues.add("seven");
outerRowValues.add(new byte[] { '2' });
outerRowValues.add(arrayBlockOf(innerRowType, rowBlockOf(innerRowType.getTypeParameters(), 2, -5L), rowBlockOf(ImmutableList.of(INTEGER, BIGINT), -10, 0L)));
outerRowValues.add(mapBlockOf(VARCHAR, innerRowType, new Object[] { utf8Slice("fifteen"), utf8Slice("twelve") }, new Object[] { rowBlockOf(innerRowType.getTypeParameters(), -5, -10L), rowBlockOf(innerRowType.getTypeParameters(), 0, 5L) }));
outerRowValues.add(rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 18, 19L));
assertBlockEquals(actual, rowBlockOf(outerRowParameterTypes, outerRowValues.build().toArray()));
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class TestArrayOfRowsUnnester method testArrayOfRowsUnnester.
/**
* Test operations of ArrayOfRowUnnester incrementally on the input.
* Output final blocks after the whole input has been processed.
*
* Input 3d array {@code elements} stores values from a column with type <array<row<varchar, varchar, ... {@code fieldCount} times> >.
* elements[i] corresponds to a position in this column, represents one array of row(....).
* elements[i][j] represents one row(....) object in the array.
* elements[i][j][k] represents value of kth field in row(...) object.
*/
private static Block[] testArrayOfRowsUnnester(int[] requiredOutputCounts, int[] unnestedLengths, Slice[][][] elements, int fieldCount) {
validateTestInput(requiredOutputCounts, unnestedLengths, elements, fieldCount);
int positionCount = requiredOutputCounts.length;
// True if there is a null Row element inside the array at this position
boolean[] containsNullRowElement = new boolean[positionCount];
// Populate containsNullRowElement
for (int i = 0; i < positionCount; i++) {
containsNullRowElement[i] = false;
if (elements[i] != null) {
for (int j = 0; j < elements[i].length; j++) {
if (elements[i][j] == null) {
containsNullRowElement[i] = true;
}
}
}
}
// Check for null elements in individual input fields
boolean[] nullsPresent = new boolean[fieldCount];
for (int field = 0; field < fieldCount; field++) {
nullsPresent[field] = nullExists(elements[field]);
}
// Create the unnester and input block
RowType rowType = RowType.anonymous(Collections.nCopies(fieldCount, VARCHAR));
Unnester arrayofRowsUnnester = new ArrayOfRowsUnnester(rowType);
Block arrayBlockOfRows = createArrayBlockOfRowBlocks(elements, rowType);
Block[] blocks = null;
// Verify output being produced after processing every position. (quadratic)
for (int inputTestCount = 1; inputTestCount <= elements.length; inputTestCount++) {
// Reset input and prepare for new output
PageBuilderStatus status = new PageBuilderStatus();
arrayofRowsUnnester.resetInput(arrayBlockOfRows);
assertEquals(arrayofRowsUnnester.getInputEntryCount(), elements.length);
arrayofRowsUnnester.startNewOutput(status, 10);
boolean misAligned = false;
// Process inputTestCount positions
for (int i = 0; i < inputTestCount; i++) {
arrayofRowsUnnester.processCurrentAndAdvance(requiredOutputCounts[i]);
int elementsSize = (elements[i] != null ? elements[i].length : 0);
// (2) null Row element
if ((requiredOutputCounts[i] > elementsSize) || containsNullRowElement[i]) {
misAligned = true;
}
}
// Build output block and verify
blocks = arrayofRowsUnnester.buildOutputBlocksAndFlush();
assertEquals(blocks.length, rowType.getFields().size());
// Verify output blocks for individual fields
for (int field = 0; field < blocks.length; field++) {
assertTrue((blocks[field] instanceof DictionaryBlock) || (!nullsPresent[field] && misAligned));
assertFalse((blocks[field] instanceof DictionaryBlock) && (!nullsPresent[field] && misAligned));
Slice[][] fieldElements = getFieldElements(elements, field);
Slice[] expectedOutput = computeExpectedUnnestedOutput(fieldElements, requiredOutputCounts, 0, inputTestCount);
assertBlock(blocks[field], expectedOutput);
}
}
return blocks;
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class BuiltInTypeRegistry method compatibility.
private TypeCompatibility compatibility(Type fromType, Type toType) {
if (fromType.equals(toType)) {
return TypeCompatibility.compatible(toType, true);
}
if (fromType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(toType, true);
}
if (toType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(fromType, false);
}
String fromTypeBaseName = fromType.getTypeSignature().getBase();
String toTypeBaseName = toType.getTypeSignature().getBase();
if (featuresConfig.isLegacyDateTimestampToVarcharCoercion()) {
if ((fromTypeBaseName.equals(StandardTypes.DATE) || fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) && toTypeBaseName.equals(StandardTypes.VARCHAR)) {
return TypeCompatibility.compatible(toType, true);
}
if (fromTypeBaseName.equals(StandardTypes.VARCHAR) && (toTypeBaseName.equals(StandardTypes.DATE) || toTypeBaseName.equals(StandardTypes.TIMESTAMP))) {
return TypeCompatibility.compatible(fromType, true);
}
}
if (fromTypeBaseName.equals(toTypeBaseName)) {
if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) fromType, (DecimalType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) fromType, (VarcharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.CHAR) && !featuresConfig.isLegacyCharToVarcharCoercion()) {
Type commonSuperType = getCommonSuperTypeForChar((CharType) fromType, (CharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.ROW)) {
return typeCompatibilityForRow((RowType) fromType, (RowType) toType);
}
if (isCovariantParametrizedType(fromType)) {
return typeCompatibilityForCovariantParametrizedType(fromType, toType);
}
return TypeCompatibility.incompatible();
}
Optional<Type> coercedType = coerceTypeBase(fromType, toType.getTypeSignature().getBase());
if (coercedType.isPresent()) {
return compatibility(coercedType.get(), toType);
}
coercedType = coerceTypeBase(toType, fromType.getTypeSignature().getBase());
if (coercedType.isPresent()) {
TypeCompatibility typeCompatibility = compatibility(fromType, coercedType.get());
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
return TypeCompatibility.compatible(typeCompatibility.getCommonSuperType(), false);
}
return TypeCompatibility.incompatible();
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class BuiltInTypeRegistry method typeCompatibilityForRow.
private TypeCompatibility typeCompatibilityForRow(RowType firstType, RowType secondType) {
List<Field> firstFields = firstType.getFields();
List<Field> secondFields = secondType.getFields();
if (firstFields.size() != secondFields.size()) {
return TypeCompatibility.incompatible();
}
ImmutableList.Builder<Field> fields = ImmutableList.builder();
boolean coercible = true;
for (int i = 0; i < firstFields.size(); i++) {
Type firstFieldType = firstFields.get(i).getType();
Type secondFieldType = secondFields.get(i).getType();
TypeCompatibility typeCompatibility = compatibility(firstFieldType, secondFieldType);
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
Type commonParameterType = typeCompatibility.getCommonSuperType();
Optional<String> firstParameterName = firstFields.get(i).getName();
Optional<String> secondParameterName = secondFields.get(i).getName();
Optional<String> commonName = firstParameterName.equals(secondParameterName) ? firstParameterName : Optional.empty();
// ignore parameter name for coercible
coercible &= typeCompatibility.isCoercible();
fields.add(new Field(commonName, commonParameterType));
}
return TypeCompatibility.compatible(RowType.from(fields.build()), coercible);
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class TestParquetPredicateUtils method testParquetTupleDomainStructArray.
@Test
public void testParquetTupleDomainStructArray() {
HiveColumnHandle columnHandle = new HiveColumnHandle("my_array_struct", HiveType.valueOf("array<struct<a:int>>"), parseTypeSignature(StandardTypes.ARRAY), 0, REGULAR, Optional.empty());
RowType.Field rowField = new RowType.Field(Optional.of("a"), INTEGER);
RowType rowType = RowType.from(ImmutableList.of(rowField));
TupleDomain<HiveColumnHandle> domain = withColumnDomains(ImmutableMap.of(columnHandle, Domain.notNull(new ArrayType(rowType))));
MessageType fileSchema = new MessageType("hive_schema", new GroupType(OPTIONAL, "my_array_struct", new GroupType(REPEATED, "bag", new GroupType(OPTIONAL, "array_element", new PrimitiveType(OPTIONAL, INT32, "a")))));
Map<List<String>, RichColumnDescriptor> descriptorsByPath = getDescriptors(fileSchema, fileSchema);
TupleDomain<ColumnDescriptor> tupleDomain = ParquetPageSourceFactory.getParquetTupleDomain(descriptorsByPath, domain);
assertTrue(tupleDomain.getDomains().get().isEmpty());
}
Aggregations