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);
}
}
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;
}
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;
}
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<>());
}
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()));
}
}
Aggregations