@Override public Type fieldType(String name) { if ("element".equals(name)) { return elementType(); } return null; }
static void toJson(Types.ListType list, JsonGenerator generator) throws IOException { generator.writeStartObject(); generator.writeStringField(TYPE, LIST); generator.writeNumberField(ELEMENT_ID, list.elementId()); generator.writeFieldName(ELEMENT); toJson(list.elementType(), generator); generator.writeBooleanField(ELEMENT_REQUIRED, !list.isElementOptional()); generator.writeEndObject(); }
@Override public List<String> list(Types.ListType readList, Supplier<List<String>> elementErrors) { if (!currentType.isListType()) { return ImmutableList.of(String.format(": %s cannot be read as a list", currentType)); } Types.ListType list = currentType.asNestedType().asListType(); List<String> errors = Lists.newArrayList(); this.currentType = list.elementType(); try { if (readList.isElementRequired() && list.isElementOptional()) { errors.add(": elements should be required, but are optional"); } errors.addAll(elementErrors.get()); return errors; } finally { this.currentType = list; } }
case LIST: final Types.ListType listType = type.asListType(); return format("array<%s>", convert(listType.elementType())); case MAP: final Types.MapType mapType = type.asMapType();
"Incompatible projected type: %s", current); Types.ListType list = current.asNestedType().asListType(); this.current = list.elementType(); try { Schema elementSchema = element.get();
elementResult = visit(list.elementType(), visitor); } finally { visitor.fieldIds.pop();
return visitor.list(list, new VisitFuture<>(list.elementType(), visitor));
case LIST: final Types.ListType listType = type.asListType(); return String.format("array<%s>", fromIcebergToHiveType(listType.elementType())); case MAP: final Types.MapType mapType = type.asMapType();
ListType listType = type.asListType(); ResourceFieldSchema [] elementFieldSchemas = new ResourceFieldSchema[]{convert(listType.elementType())}; if (listType.elementType().isStructType()) { result.setFields(elementFieldSchemas); } else {
@Override public Type list(Types.ListType list, Supplier<Type> elementResult) { Preconditions.checkArgument(current instanceof ArrayType, "Not an array: %s", current); ArrayType array = (ArrayType) current; Preconditions.checkArgument(array.containsNull() || !list.isElementOptional(), "Cannot project an array of optional elements as required elements: %s", array); this.current = array.elementType(); try { Type elementType = elementResult.get(); if (list.elementType() == elementType) { return list; } // must be a projected element type, create a new list if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementType); } else { return Types.ListType.ofRequired(list.elementId(), elementType); } } finally { this.current = array; } }
@Override public Type list(Types.ListType list, Supplier<Type> elementResult) { Preconditions.checkArgument(current instanceof ArrayType, "Not an array: %s", current); ArrayType array = (ArrayType) current; Preconditions.checkArgument(array.containsNull() || !list.isElementOptional(), "Cannot project an array of optional elements as required elements: %s", array); this.current = array.elementType(); try { Type elementType = elementResult.get(); if (list.elementType() == elementType) { return list; } // must be a projected element type, create a new list if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementType); } else { return Types.ListType.ofRequired(list.elementId(), elementType); } } finally { this.current = array; } }
@Override public Type list(Types.ListType list, Supplier<Type> elementTypeFuture) { Preconditions.checkArgument(sourceType.isListType(), "Not a list: " + sourceType); Types.ListType sourceList = sourceType.asListType(); this.sourceType = sourceList.elementType(); try { Type elementType = elementTypeFuture.get(); if (list.elementType() == elementType) { return list; } if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementType); } else { return Types.ListType.ofRequired(list.elementId(), elementType); } } finally { this.sourceType = sourceList; } }
@Override public Type list(Types.ListType list, Type result) { // use field to apply updates Type elementResult = field(list.fields().get(0), result); if (elementResult == null) { throw new IllegalArgumentException("Cannot delete element type from list: " + list); } if (list.elementType() == elementResult) { return list; } if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementResult); } else { return Types.ListType.ofRequired(list.elementId(), elementResult); } }
@Override public Type list(Types.ListType list, Supplier<Type> elementTypeFuture) { Preconditions.checkArgument(sourceType.isListType(), "Not a list: " + sourceType); Types.ListType sourceList = sourceType.asListType(); int sourceElementId = sourceList.elementId(); this.sourceType = sourceList.elementType(); try { if (list.isElementOptional()) { return Types.ListType.ofOptional(sourceElementId, elementTypeFuture.get()); } else { return Types.ListType.ofRequired(sourceElementId, elementTypeFuture.get()); } } finally { this.sourceType = sourceList; } }
@Override public Type list(Types.ListType list, Type elementResult) { if (selected.contains(list.elementId())) { return list; } else if (elementResult != null) { if (list.elementType() == elementResult) { return list; } else if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementResult); } else { return Types.ListType.ofRequired(list.elementId(), elementResult); } } return null; }
private static void assertEqualsUnsafe(Types.ListType list, Collection<?> expected, ArrayData actual) { Type elementType = list.elementType(); List<?> expectedElements = Lists.newArrayList(expected); for (int i = 0; i < expectedElements.size(); i += 1) { Object expectedValue = expectedElements.get(i); Object actualValue = actual.get(i, convert(elementType)); assertEqualsUnsafe(elementType, expectedValue, actualValue); } }
public static void assertEquals(Types.ListType list, List<?> expected, List<?> actual) { Type elementType = list.elementType(); Assert.assertEquals("List size should match", expected.size(), actual.size()); for (int i = 0; i < expected.size(); i += 1) { Object expectedValue = expected.get(i); Object actualValue = actual.get(i); assertEquals(elementType, expectedValue, actualValue); } }
static void assertEquals(Types.ListType list, List<?> expected, List<?> actual) { Type elementType = list.elementType(); Assert.assertEquals("List size should match", expected.size(), actual.size()); for (int i = 0; i < expected.size(); i += 1) { Object expectedValue = expected.get(i); Object actualValue = actual.get(i); assertEquals(elementType, expectedValue, actualValue); } }
@Test public void testLists() throws Exception { Type[] maps = new Type[] { Types.ListType.ofOptional(2, Types.DoubleType.get()), Types.ListType.ofRequired(5, Types.DoubleType.get()) }; for (Type list : maps) { Type copy = TestHelpers.roundTripSerialize(list); Assert.assertEquals("List serialization should be equal to starting type", list, copy); Assert.assertSame("List serialization should preserve identity type", Types.DoubleType.get(), list.asNestedType().asListType().elementType()); } }
private static void assertEqualsSafe(Types.ListType list, Collection<?> expected, List actual) { Type elementType = list.elementType(); List<?> expectedElements = Lists.newArrayList(expected); for (int i = 0; i < expectedElements.size(); i += 1) { Object expectedValue = expectedElements.get(i); Object actualValue = actual.get(i); assertEqualsSafe(elementType, expectedValue, actualValue); } }