Search in sources :

Example 11 with RowType

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()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ArrayType(io.prestosql.spi.type.ArrayType) RowType(io.prestosql.spi.type.RowType) ArrayType(io.prestosql.spi.type.ArrayType) Type(java.lang.reflect.Type) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) Block(io.prestosql.spi.block.Block) Test(org.testng.annotations.Test)

Example 12 with RowType

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;
}
Also used : DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) RowType(io.prestosql.spi.type.RowType) PageBuilderStatus(io.prestosql.spi.block.PageBuilderStatus) Slice(io.airlift.slice.Slice) ColumnarTestUtils.assertBlock(io.prestosql.block.ColumnarTestUtils.assertBlock) DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) Block(io.prestosql.spi.block.Block)

Example 13 with RowType

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();
}
Also used : Re2JRegexpType(io.prestosql.type.Re2JRegexpType) DecimalType(io.prestosql.spi.type.DecimalType) ColorType(io.prestosql.type.ColorType) ParametricType(io.prestosql.spi.type.ParametricType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) LikePatternType(io.prestosql.spi.type.LikePatternType) ArrayType(io.prestosql.spi.type.ArrayType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) CharParametricType(io.prestosql.spi.type.CharParametricType) VarcharParametricType(io.prestosql.spi.type.VarcharParametricType) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) JoniRegexpType(io.prestosql.type.JoniRegexpType) CodePointsType(io.prestosql.type.CodePointsType) MapType(io.prestosql.spi.type.MapType) JsonPathType(io.prestosql.type.JsonPathType) SetDigestType(io.prestosql.type.setdigest.SetDigestType) DecimalParametricType(io.prestosql.spi.type.DecimalParametricType) VarcharType(io.prestosql.spi.type.VarcharType)

Example 14 with RowType

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);
}
Also used : Field(io.prestosql.spi.type.RowType.Field) Re2JRegexpType(io.prestosql.type.Re2JRegexpType) DecimalType(io.prestosql.spi.type.DecimalType) ColorType(io.prestosql.type.ColorType) ParametricType(io.prestosql.spi.type.ParametricType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) LikePatternType(io.prestosql.spi.type.LikePatternType) ArrayType(io.prestosql.spi.type.ArrayType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) CharParametricType(io.prestosql.spi.type.CharParametricType) VarcharParametricType(io.prestosql.spi.type.VarcharParametricType) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) JoniRegexpType(io.prestosql.type.JoniRegexpType) CodePointsType(io.prestosql.type.CodePointsType) MapType(io.prestosql.spi.type.MapType) JsonPathType(io.prestosql.type.JsonPathType) SetDigestType(io.prestosql.type.setdigest.SetDigestType) DecimalParametricType(io.prestosql.spi.type.DecimalParametricType) VarcharType(io.prestosql.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList)

Example 15 with RowType

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());
}
Also used : RichColumnDescriptor(io.prestosql.parquet.RichColumnDescriptor) RichColumnDescriptor(io.prestosql.parquet.RichColumnDescriptor) ColumnDescriptor(org.apache.parquet.column.ColumnDescriptor) RowType(io.prestosql.spi.type.RowType) ArrayType(io.prestosql.spi.type.ArrayType) GroupType(org.apache.parquet.schema.GroupType) PrimitiveType(org.apache.parquet.schema.PrimitiveType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HiveColumnHandle(io.prestosql.plugin.hive.HiveColumnHandle) MessageType(org.apache.parquet.schema.MessageType) Test(org.testng.annotations.Test)

Aggregations

RowType (io.prestosql.spi.type.RowType)76 ArrayType (io.prestosql.spi.type.ArrayType)38 Type (io.prestosql.spi.type.Type)35 MapType (io.prestosql.spi.type.MapType)29 Test (org.testng.annotations.Test)25 List (java.util.List)19 ImmutableList (com.google.common.collect.ImmutableList)18 VarcharType (io.prestosql.spi.type.VarcharType)17 Block (io.prestosql.spi.block.Block)16 BlockBuilder (io.prestosql.spi.block.BlockBuilder)15 Map (java.util.Map)12 BigintType (io.prestosql.spi.type.BigintType)11 DecimalType (io.prestosql.spi.type.DecimalType)11 DoubleType (io.prestosql.spi.type.DoubleType)11 BooleanType (io.prestosql.spi.type.BooleanType)10 IntegerType (io.prestosql.spi.type.IntegerType)10 RealType (io.prestosql.spi.type.RealType)10 SmallintType (io.prestosql.spi.type.SmallintType)10 TimestampType (io.prestosql.spi.type.TimestampType)10 VarbinaryType (io.prestosql.spi.type.VarbinaryType)10