Search in sources :

Example 21 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class OrcTester method assertColumnValueEquals.

private static void assertColumnValueEquals(Type type, Object actual, Object expected) {
    if (actual == null) {
        assertNull(expected);
        return;
    }
    String baseType = type.getTypeSignature().getBase();
    if (StandardTypes.ARRAY.equals(baseType)) {
        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 (StandardTypes.MAP.equals(baseType)) {
        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) {
                    log.debug(ignored.getMessage());
                }
            }
        }
        assertTrue(expectedEntries.isEmpty(), "Unmatched entries " + expectedEntries);
    } else if (StandardTypes.ROW.equals(baseType)) {
        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);
    }
}
Also used : VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Entry(java.util.Map.Entry) AbstractIterator(com.google.common.collect.AbstractIterator) Iterator(java.util.Iterator) Arrays.asList(java.util.Arrays.asList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 22 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class OrcTester method testRow.

// later we can extend for multiple columns
private static boolean testRow(List<Type> types, List<?> values, int row, Map<Integer, TupleDomainFilter> columnFilters) {
    for (int column = 0; column < types.size(); column++) {
        TupleDomainFilter filter = columnFilters.get(column);
        if (filter == null) {
            continue;
        }
        Object value = values.get(row);
        if (value == null) {
            if (!filter.testNull()) {
                return false;
            }
        } else {
            Type type = types.get(column);
            if (type == BOOLEAN) {
                if (!filter.testBoolean((Boolean) value)) {
                    return false;
                }
            } else if (type == BIGINT || type == INTEGER || type == SMALLINT) {
                if (!filter.testLong(((Number) value).longValue())) {
                    return false;
                }
            } else if (type == DATE) {
                if (!filter.testLong(((SqlDate) value).getDays())) {
                    return false;
                }
            } else if (type == TIMESTAMP) {
                return filter.testLong(((SqlTimestamp) value).getMillis());
            } else if (type == VARCHAR) {
                return filter.testBytes(((String) value).getBytes(), 0, ((String) value).length());
            } else if (type instanceof CharType) {
                String charString = String.valueOf(value);
                return filter.testBytes(charString.getBytes(StandardCharsets.UTF_8), 0, charString.length());
            } else if (type == VARBINARY) {
                byte[] binary = ((SqlVarbinary) value).getBytes();
                return filter.testBytes(binary, 0, binary.length);
            } else if (type instanceof DecimalType) {
                DecimalType decimalType = (DecimalType) type;
                BigDecimal bigDecimal = ((SqlDecimal) value).toBigDecimal();
                if (decimalType.isShort()) {
                    return filter.testLong(bigDecimal.unscaledValue().longValue());
                }
            } else if (type == DOUBLE) {
                if (!filter.testDouble((double) value)) {
                    return false;
                }
            } else {
                fail("Unsupported type: " + type);
            }
        }
    }
    return true;
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) BigDecimal(java.math.BigDecimal) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) SqlDate(io.prestosql.spi.type.SqlDate) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType)

Example 23 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class OrcTester method decodeRecordReaderMap.

private static Object decodeRecordReaderMap(Type type, Map<?, ?> map) {
    Type keyType = type.getTypeParameters().get(0);
    Type valueType = type.getTypeParameters().get(1);
    Map<Object, Object> newMap = new HashMap<>();
    for (Entry<?, ?> entry : map.entrySet()) {
        newMap.put(decodeRecordReaderValue(keyType, entry.getKey()), decodeRecordReaderValue(valueType, entry.getValue()));
    }
    return newMap;
}
Also used : VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) HashMap(java.util.HashMap)

Example 24 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class OrcTester method createCustomOrcSelectiveRecordReader.

static OrcSelectiveRecordReader createCustomOrcSelectiveRecordReader(TempFile tempFile, OrcPredicate predicate, List<Type> types, int initialBatchSize, Map<Integer, TupleDomainFilter> filters) throws IOException {
    OrcDataSource orcDataSource = new FileOrcDataSource(tempFile.getFile(), new DataSize(1, MEGABYTE), new DataSize(1, MEGABYTE), new DataSize(1, MEGABYTE), true, tempFile.getFile().lastModified());
    OrcReader orcReader = new OrcReader(orcDataSource, OrcFileTail.readFrom(orcDataSource, Optional.empty()), new DataSize(1, MEGABYTE), new DataSize(1, MEGABYTE), new DataSize(1, MEGABYTE));
    assertEquals(orcReader.getColumnNames(), makeColumnNames(types.size()));
    assertEquals(orcReader.getFooter().getRowsInRowGroup(), 10_000);
    Map<Integer, Type> columnTypes = IntStream.range(0, types.size()).boxed().collect(toImmutableMap(Function.identity(), types::get));
    return orcReader.createSelectiveRecordReader(orcReader.getRootColumn().getNestedColumns(), orcReader.getRootColumn().getNestedColumns(), types, ImmutableList.of(0), columnTypes, filters, ImmutableMap.of(), predicate, 0, orcDataSource.getSize(), HIVE_STORAGE_TIME_ZONE, newSimpleAggregatedMemoryContext(), initialBatchSize, RuntimeException::new, Optional.empty(), null, OrcCacheStore.CACHE_NOTHING, new OrcCacheProperties(), Optional.empty(), new HashMap<>(), null, false, ImmutableMap.of(), ImmutableMap.of(), new HashSet<>());
}
Also used : BigInteger(java.math.BigInteger) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) DataSize(io.airlift.units.DataSize)

Example 25 with Type

use of io.prestosql.spi.type.Type in project hetu-core by openlookeng.

the class OrcTester method testListRoundTrip.

private void testListRoundTrip(Type type, List<?> readValues) throws Exception {
    Type arrayType = arrayType(type);
    // values in simple list
    testRoundTripType(arrayType, readValues.stream().map(OrcTester::toHiveList).collect(toList()));
    if (structuralNullTestsEnabled) {
        // values and nulls in simple list
        testRoundTripType(arrayType, insertNullEvery(5, readValues).stream().map(OrcTester::toHiveList).collect(toList()));
        // all null values in simple list
        testRoundTripType(arrayType, readValues.stream().map(value -> toHiveList(null)).collect(toList()));
    }
}
Also used : VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type)

Aggregations

Type (io.prestosql.spi.type.Type)764 Test (org.testng.annotations.Test)260 ImmutableList (com.google.common.collect.ImmutableList)230 List (java.util.List)222 ArrayList (java.util.ArrayList)212 ArrayType (io.prestosql.spi.type.ArrayType)200 RowType (io.prestosql.spi.type.RowType)194 VarcharType (io.prestosql.spi.type.VarcharType)159 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)150 Map (java.util.Map)150 Block (io.prestosql.spi.block.Block)133 ImmutableMap (com.google.common.collect.ImmutableMap)131 Page (io.prestosql.spi.Page)129 PrestoException (io.prestosql.spi.PrestoException)123 DecimalType (io.prestosql.spi.type.DecimalType)118 HashMap (java.util.HashMap)115 Optional (java.util.Optional)113 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)112 Slice (io.airlift.slice.Slice)88 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)87