Class FileSchema.Builder

java.lang.Object
dev.hardwood.schema.FileSchema.Builder
Enclosing class:
FileSchema

public static final class FileSchema.Builder extends Object

Builder for constructing a FileSchema programmatically.

Fields are added as top-level primitive leaves (primitive(String, PhysicalType, RepetitionType)), nested struct groups (struct(String, RepetitionType, Consumer)), LIST groups (list(String, RepetitionType, Consumer)), or MAP groups (map(String, RepetitionType, PhysicalType, Consumer)). The LIST and MAP groups emit the canonical 3-level and 2-level physical layouts.

Every method of this builder and of the builders its lambdas receive throws IllegalArgumentException for a null argument, and for a field name that is empty or already declared among the same group's fields. Error messages name a nested field by its dotted path, a list element as <list>.list.element and a map value as <map>.key_value.value.

The name, physical type and repetition of a primitive leaf are positional. Its optional attributes (logical type, type length, field id) are set in a FileSchema.PrimitiveBuilder lambda:

FileSchema schema = FileSchema.builder("schema")
        .primitive("id", PhysicalType.FIXED_LEN_BYTE_ARRAY, RepetitionType.REQUIRED,
                col -> col.logicalType(LogicalType.uuid()).fieldId(1))
        .primitive("name", PhysicalType.BYTE_ARRAY, RepetitionType.OPTIONAL,
                col -> col.logicalType(LogicalType.string()).fieldId(2))
        .build();
  • Method Details

    • primitive

      public FileSchema.Builder primitive(String columnName, PhysicalType type, RepetitionType repetition)
      Append a primitive column without optional attributes.
      Parameters:
      columnName - the column name
      type - the physical type
      repetition - REQUIRED or OPTIONAL
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, repetition is REPEATED, or type is FIXED_LEN_BYTE_ARRAY (which needs a type length or an annotation that implies one)
    • primitive

      public FileSchema.Builder primitive(String columnName, PhysicalType type, RepetitionType repetition, Consumer<FileSchema.PrimitiveBuilder> column)

      Append a primitive column whose optional attributes are set by column.

      A FIXED_LEN_BYTE_ARRAY column without a FileSchema.PrimitiveBuilder.typeLength takes the length its annotation implies: 16 bytes for UUID, 12 for INTERVAL, 2 for FLOAT16, and for DECIMAL the fewest bytes that hold its precision.

      Parameters:
      columnName - the column name
      type - the physical type
      repetition - REQUIRED or OPTIONAL
      column - sets the column's logical type, type length and field id
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, repetition is REPEATED, the type length does not match the type, the annotation does not apply to the type, or type is FIXED_LEN_BYTE_ARRAY with no type length and no annotation that implies one
    • struct

      public FileSchema.Builder struct(String structName, RepetitionType repetition, Consumer<FileSchema.StructBuilder> filler)
      Append a struct group whose fields are declared by filler.
      Parameters:
      structName - the group name
      repetition - REQUIRED or OPTIONAL
      filler - declares the group's fields, and optionally its field id
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, repetition is REPEATED or the group has no fields
    • file

      public FileSchema.Builder file(String fileName, RepetitionType repetition)
      Append a FILE group, a reference to a range of bytes stored inline or in an external file. The group declares all six children the format defines, each OPTIONAL: uri and content_type and checksum (BYTE_ARRAY annotated STRING), offset and size (INT64), and inline (BYTE_ARRAY). A row writes it through StructBuilder.setFile(String, dev.hardwood.row.PqFile).
      Parameters:
      fileName - the group name
      repetition - REQUIRED or OPTIONAL
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, or repetition is REPEATED
    • file

      public FileSchema.Builder file(String fileName, RepetitionType repetition, Consumer<FileSchema.GroupBuilder> group)
      Append a FILE group whose own attributes are set by group, as file(String, RepetitionType) describes. The children of the group have no field id.
      Parameters:
      fileName - the group name
      repetition - REQUIRED or OPTIONAL
      group - sets the group's field id
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, or repetition is REPEATED
    • list

      public FileSchema.Builder list(String listName, RepetitionType repetition, Consumer<FileSchema.ElementBuilder> element)
      Append a LIST group whose element is declared by element.
      Parameters:
      listName - the list group name
      repetition - REQUIRED or OPTIONAL (whether the list itself may be null)
      element - declares the list's element
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, repetition is REPEATED or no element is declared
    • list

      Append a LIST group whose own attributes are set by group and whose element is declared by element.
      Parameters:
      listName - the list group name
      repetition - REQUIRED or OPTIONAL (whether the list itself may be null)
      group - sets the list group's field id
      element - declares the list's element
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, repetition is REPEATED or no element is declared
    • map

      public FileSchema.Builder map(String mapName, RepetitionType repetition, PhysicalType keyType, Consumer<FileSchema.ElementBuilder> value)
      Append a MAP group whose value is declared by value. The key is a required primitive of keyType per the canonical layout.
      Parameters:
      mapName - the map group name
      repetition - REQUIRED or OPTIONAL (whether the map itself may be null)
      keyType - the physical type of the required key
      value - declares the map's value like a list element
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, repetition is REPEATED, no value is declared, or keyType is FIXED_LEN_BYTE_ARRAY
    • map

      Append a MAP group whose key has optional attributes, set by key. A STRING key is the usual case. A FIXED_LEN_BYTE_ARRAY key takes a type length as primitive(String, PhysicalType, RepetitionType, Consumer) describes.
      Parameters:
      mapName - the map group name
      repetition - REQUIRED or OPTIONAL (whether the map itself may be null)
      keyType - the physical type of the required key
      key - sets the key's logical type, type length and field id
      value - declares the map's value like a list element
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, repetition is REPEATED, no value is declared, or the key's attributes do not match keyType
    • map

      Append a MAP group whose own attributes are set by group and whose key has optional attributes, set by key, as map(String, RepetitionType, PhysicalType, Consumer, Consumer) describes.
      Parameters:
      mapName - the map group name
      repetition - REQUIRED or OPTIONAL (whether the map itself may be null)
      group - sets the map group's field id
      keyType - the physical type of the required key
      key - sets the key's logical type, type length and field id
      value - declares the map's value like a list element
      Throws:
      IllegalArgumentException - if the name is null, empty or already declared, an argument is null, repetition is REPEATED, no value is declared, or the key's attributes do not match keyType
    • build

      public FileSchema build()
      Build the schema.
      Throws:
      UnsupportedOperationException - if no fields were added; the writer does not produce a schema with no columns