Java Code Examples for org.apache.iceberg.types.Type#asNestedType()

The following examples show how to use org.apache.iceberg.types.Type#asNestedType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaParser.java    From iceberg with Apache License 2.0 6 votes vote down vote up
static void toJson(Type type, JsonGenerator generator) throws IOException {
  if (type.isPrimitiveType()) {
    toJson(type.asPrimitiveType(), generator);
  } else {
    Type.NestedType nested = type.asNestedType();
    switch (type.typeId()) {
      case STRUCT:
        toJson(nested.asStructType(), generator);
        break;
      case LIST:
        toJson(nested.asListType(), generator);
        break;
      case MAP:
        toJson(nested.asMapType(), generator);
        break;
      default:
        throw new IllegalArgumentException("Cannot write unknown type: " + type);
    }
  }
}
 
Example 2
Source File: SchemaConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static CompleteType fromIcebergType(Type type) {
  if (type.isPrimitiveType()) {
    return fromIcebergPrimitiveType(type.asPrimitiveType());
  } else {
    NestedType nestedType = type.asNestedType();
    if (nestedType.isListType()) {
      ListType listType = (ListType)nestedType;
      NestedField elementField = listType.fields().get(0);
      CompleteType elementType = fromIcebergType(elementField.type());
      return (elementType == null) ? null : elementType.asList();
    } else if (nestedType.isStructType()) {
      StructType structType = (StructType)nestedType;
      List<Types.NestedField> structFields = structType.fields();
      List<Field> innerFields = Lists.newArrayList();
      for (Types.NestedField nestedField : structFields) {
        Field field = fromIcebergColumn(nestedField);
        if (field == null) {
          return null;
        }
        innerFields.add(field);
      }
      return CompleteType.struct(innerFields);
    } else {
      // drop map type and all other unknown iceberg column types
      return null;
    }
  }
}
 
Example 3
Source File: SchemaUpdate.java    From iceberg with Apache License 2.0 4 votes vote down vote up
private void internalAddColumn(String parent, String name, boolean isOptional, Type type, String doc) {
  int parentId = TABLE_ROOT_ID;
  String fullName;
  if (parent != null) {
    Types.NestedField parentField = schema.findField(parent);
    Preconditions.checkArgument(parentField != null, "Cannot find parent struct: %s", parent);
    Type parentType = parentField.type();
    if (parentType.isNestedType()) {
      Type.NestedType nested = parentType.asNestedType();
      if (nested.isMapType()) {
        // fields are added to the map value type
        parentField = nested.asMapType().fields().get(1);
      } else if (nested.isListType()) {
        // fields are added to the element type
        parentField = nested.asListType().fields().get(0);
      }
    }
    Preconditions.checkArgument(
        parentField.type().isNestedType() && parentField.type().asNestedType().isStructType(),
        "Cannot add to non-struct column: %s: %s", parent, parentField.type());
    parentId = parentField.fieldId();
    Preconditions.checkArgument(!deletes.contains(parentId),
        "Cannot add to a column that will be deleted: %s", parent);
    Preconditions.checkArgument(schema.findField(parent + "." + name) == null,
        "Cannot add column, name already exists: %s.%s", parent, name);
    fullName = schema.findColumnName(parentId) + "." + name;
  } else {
    Preconditions.checkArgument(schema.findField(name) == null,
        "Cannot add column, name already exists: %s", name);
    fullName = name;
  }

  // assign new IDs in order
  int newId = assignNewColumnId();

  // update tracking for moves
  addedNameToId.put(fullName, newId);
  if (parentId != TABLE_ROOT_ID) {
    idToParent.put(newId, parentId);
  }

  adds.put(parentId, Types.NestedField.of(newId, isOptional, name,
      TypeUtil.assignFreshIds(type, this::assignNewColumnId), doc));
}