Interface StructBuilder


@Experimental public interface StructBuilder

Sets the fields of one struct — a record being written, or a struct nested inside one.

A record is a struct, so RowWriter.writeRow(Consumer) hands the same builder a nested setStruct(String, Consumer) does. Fields are addressed by the name they carry in the schema, or by their position in it; the synthetic list / element and key_value groups a LIST or MAP introduces are never spelled by the caller.

rows.writeRow(row -> row
        .setLong("id", 1)
        .setString("name", "hardwood")
        .setStruct("address", address -> address.setString("city", "Berlin")));

Every setter has an index-taking mirror, and getFieldCount() and getFieldName(int) report the positions those indices address — the same pair FieldAccessor exposes on the read side.

A loop that walks a row's fields by index can therefore read back and write forward through the same positions, provided the write schema mirrors what was read. The reader's index is the position among the projected fields, in the order the projection requests them; this one is the position in the write schema's declaration order. They are the same position when the whole file is read into its own schema, and they diverge under a projection: reading three of ten columns and writing into the ten-column schema shifts every position, and where the columns that land on each other happen to share a type the values are written to the wrong fields rather than rejected. Comparing getFieldName(int) against the reader's for each index costs one string comparison per field and turns that into a failure.

A field that is never set is written as null if it is OPTIONAL, and fails the record if it is REQUIRED; setNull(String) states the same thing explicitly, and a null value handed to any of the object-typed setters does too. Setting the same field twice within one scope, naming a field the schema does not have, or using a setter that does not fit the field's declared type all throw at the call.

The builder is valid only inside the lambda it was passed to. Retaining one and using it afterwards throws IllegalStateException rather than writing into a later record.

This API is Experimental: the shape may change in future releases.

  • Method Details

    • setInt

      StructBuilder setInt(String name, int value)

      Sets an INT32 field.

      The value is stored as declared: a DATE, TIME(MILLIS) or DECIMAL annotation on the column does not change what this setter writes, so it is the raw-value escape hatch alongside setDate(String, LocalDate) and friends — mirroring the reader, whose getInt returns the stored int whatever the annotation. The value is still range-checked against what the annotation can hold: an INT(8) or INT(16) bounds it to that width, signed or unsigned, a TIME to the times of day its unit can spell, and a DECIMAL to the digits its precision declares. UINT_32 bounds nothing, because every bit pattern is one of its values and a negative int is the only way to spell one above Integer.MAX_VALUE.

      Parameters:
      name - the field name
      value - the value
      Returns:
      this builder, for chaining
      Throws:
      IllegalArgumentException - if the schema has no such field, the field is not INT32, the field is already set in this scope, or the value is out of range for the field's annotation
      IllegalStateException - if this builder's scope has ended
    • setLong

      StructBuilder setLong(String name, long value)
      Sets an INT64 field.
      See Also:
    • setFloat

      StructBuilder setFloat(String name, float value)
      Sets a FLOAT field.
      See Also:
    • setDouble

      StructBuilder setDouble(String name, double value)
      Sets a DOUBLE field.
      See Also:
    • setBoolean

      StructBuilder setBoolean(String name, boolean value)
      Sets a BOOLEAN field.
      See Also:
    • setString

      StructBuilder setString(String name, String value)
      Sets a BYTE_ARRAY field annotated STRING, ENUM or JSON from the value's UTF-8 bytes. A null value sets the field null. An unannotated BYTE_ARRAY field takes [#setBinary(String, byte[])].
      See Also:
    • setBinary

      StructBuilder setBinary(String name, byte[] value)
      Sets a BYTE_ARRAY or FIXED_LEN_BYTE_ARRAY field from its bytes, which are referenced rather than copied and must not be mutated until the record is written. A FIXED_LEN_BYTE_ARRAY field requires exactly its declared length, and a DECIMAL field an unscaled value the declared precision holds. A null value sets the field null.
      See Also:
    • setDate

      StructBuilder setDate(String name, LocalDate value)
      Sets an INT32 field annotated DATE, as days since the Unix epoch. A null value sets the field null.
      See Also:
    • setTime

      StructBuilder setTime(String name, LocalTime value)
      Sets a field annotated TIME, in the unit the annotation declares. A value carrying finer precision than the unit can hold is rejected, or narrowed, according to the configured PrecisionLossPolicy. A null value sets the field null.
      See Also:
    • setTimestamp

      StructBuilder setTimestamp(String name, Instant value)

      Sets a field annotated TIMESTAMP with isAdjustedToUTC = true, in the unit the annotation declares, over an INT64 or a FIXED_LEN_BYTE_ARRAY(12). A value carrying finer precision than the unit can hold is rejected, or narrowed, according to the configured PrecisionLossPolicy; one outside the range an INT64 of that unit spans is rejected under either. A FIXED_LEN_BYTE_ARRAY(12) holds every Instant. A null value sets the field null.

      A local-wall-clock column — isAdjustedToUTC = false — is written through setLocalTimestamp(String, LocalDateTime) instead, mirroring the reader's split between getTimestamp and getLocalTimestamp; using the wrong one throws rather than silently reinterpreting the instant.

      See Also:
    • setLocalTimestamp

      StructBuilder setLocalTimestamp(String name, LocalDateTime value)
      Sets a field annotated TIMESTAMP with isAdjustedToUTC = false, over an INT64 or a FIXED_LEN_BYTE_ARRAY(12).
      See Also:
    • setDecimal

      StructBuilder setDecimal(String name, BigDecimal value)
      Sets a field annotated DECIMAL, in whichever of INT32, INT64, BYTE_ARRAY or FIXED_LEN_BYTE_ARRAY the column declares. The value is rescaled to the declared scale when that is lossless, and otherwise rejected or truncated according to the configured PrecisionLossPolicy; its unscaled value must fit the declared precision under either. A null value sets the field null.
      See Also:
    • setUuid

      StructBuilder setUuid(String name, UUID value)
      Sets a FIXED_LEN_BYTE_ARRAY(16) field annotated UUID. A null value sets the field null.
      See Also:
    • setInterval

      StructBuilder setInterval(String name, PqInterval value)
      Sets a FIXED_LEN_BYTE_ARRAY(12) field annotated INTERVAL. A null value sets the field null.
      See Also:
    • setNull

      StructBuilder setNull(String name)
      Sets a field null. Equivalent to leaving it unset, and available so code that walks its fields uniformly can say so explicitly.
      Parameters:
      name - the field name
      Returns:
      this builder, for chaining
      Throws:
      IllegalArgumentException - if the schema has no such field, the field is REQUIRED, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setStruct

      StructBuilder setStruct(String name, Consumer<StructBuilder> filler)
      Sets a nested struct field, populating it through filler. Leaving the field unset writes a null struct; entering it with a filler that sets nothing writes a present struct whose own fields are null.
      Parameters:
      name - the field name
      filler - populates the nested struct
      Returns:
      this builder, for chaining
      Throws:
      IllegalArgumentException - if the schema has no such field, the field is not a struct group (a FILE group is set through setFile(String, PqFile)), or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setFile

      StructBuilder setFile(String name, PqFile value)
      Sets a FILE field to the reference value. A null value sets the field null.
      Parameters:
      name - the field name
      value - the reference
      Returns:
      this builder, for chaining
      Throws:
      IllegalArgumentException - if the schema has no such field, the field is not a group annotated FILE, the group has no child for a field value sets, value is not a valid reference or its checksum is not a well-formed token (see PqFile.withChecksum(String)), or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setList

      StructBuilder setList(String name, Consumer<ListBuilder> filler)
      Sets a LIST field, appending its entries through filler. An absent list — the field left unset, or setNull(String) — and an empty list, whose filler appends nothing, are distinct and both writable.
      Parameters:
      name - the field name
      filler - appends the list's entries
      Returns:
      this builder, for chaining
      Throws:
      IllegalArgumentException - if the schema has no such field, the field is not a LIST group, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setMap

      StructBuilder setMap(String name, Consumer<MapBuilder> filler)
      Sets a MAP field, appending its entries through filler. An absent map and an empty map are distinct and both writable.
      Parameters:
      name - the field name
      filler - appends the map's entries
      Returns:
      this builder, for chaining
      Throws:
      IllegalArgumentException - if the schema has no such field, the field is not a MAP group, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setValue

      StructBuilder setValue(String name, Object value)

      Sets a field from a value whose type is known only at runtime, in the form FieldAccessor.getValue(String) returns for the field's declared type, so a value read from one file can be written to another as it is:

      for (int i = 0; i < row.getFieldCount(); i++) {
          out.setValue(row.getFieldName(i), row.getValue(i));
      }
      
      Field Value
      BOOLEAN, INT32, INT64, FLOAT, DOUBLE Boolean, Integer, Long, Float, Double
      INT32 annotated signed INT(8) / INT(16) Byte / Short
      STRING, ENUM, JSON String
      DATE, TIME, DECIMAL, UUID, INTERVAL LocalDate, LocalTime, BigDecimal, UUID, PqInterval
      TIMESTAMP, UTC-adjusted / local Instant / LocalDateTime
      FLOAT16 Float
      Other BYTE_ARRAY / FIXED_LEN_BYTE_ARRAY byte[]
      FILE group PqFile
      Struct group PqStruct
      LIST group PqList
      MAP group PqMap

      The value is written through the typed setter for that form, with the same checks, and a null value sets the field null. A value of any other type is rejected rather than converted; so is a FLOAT16 value a half-precision float cannot hold exactly.

      A struct is copied field by field, by name: each field of this struct's group takes the value of the field of the same name in the PqStruct, recursively. A field the source does not have is left unset, which writes it null and fails the record if it is REQUIRED; a field of the source this group does not declare is not written. A list's elements and a map's keys and values are written the same way, in order.

      Parameters:
      name - the field name
      value - the value, or null
      Returns:
      this builder, for chaining
      Throws:
      IllegalArgumentException - if the schema has no such field, the value is not the form the field's type takes (a PqVariant among them, VARIANT values not being writable), a nested value is rejected as its typed setter rejects it, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • getFieldCount

      int getFieldCount()
      The number of fields this struct declares — the record's top-level fields for the builder RowWriter.writeRow(Consumer) hands over, and a nested struct's own fields for the one setStruct(String, Consumer) does. A MAP entry declares two, key and value.
      Returns:
      the field count
      Throws:
      IllegalStateException - if this builder's scope has ended
    • getFieldName

      String getFieldName(int fieldIndex)
      The name of the field at fieldIndex, the inverse of the resolution the by-name setters perform.
      Parameters:
      fieldIndex - the field index, 0-based
      Returns:
      the field name
      Throws:
      IndexOutOfBoundsException - if fieldIndex is not in [0, getFieldCount())
      IllegalStateException - if this builder's scope has ended
    • setInt

      StructBuilder setInt(int fieldIndex, int value)
      Sets an INT32 field by index. See setInt(String, int).
      Parameters:
      fieldIndex - the field index, 0-based
      value - the value
      Returns:
      this builder, for chaining
      Throws:
      IndexOutOfBoundsException - if fieldIndex is not in [0, getFieldCount())
      IllegalArgumentException - if the field is not INT32, the field is already set in this scope, or the value is out of range for the field's annotation
      IllegalStateException - if this builder's scope has ended
    • setLong

      StructBuilder setLong(int fieldIndex, long value)
      Sets an INT64 field by index. See setLong(String, long).
      See Also:
    • setFloat

      StructBuilder setFloat(int fieldIndex, float value)
      Sets a FLOAT field by index. See setFloat(String, float).
      See Also:
    • setDouble

      StructBuilder setDouble(int fieldIndex, double value)
      Sets a DOUBLE field by index. See setDouble(String, double).
      See Also:
    • setBoolean

      StructBuilder setBoolean(int fieldIndex, boolean value)
      Sets a BOOLEAN field by index. See setBoolean(String, boolean).
      See Also:
    • setString

      StructBuilder setString(int fieldIndex, String value)
      Sets a STRING-shaped field by index. See setString(String, String).
      See Also:
    • setBinary

      StructBuilder setBinary(int fieldIndex, byte[] value)
      Sets a BYTE_ARRAY or FIXED_LEN_BYTE_ARRAY field by index. See [#setBinary(String, byte[])].
      See Also:
    • setDate

      StructBuilder setDate(int fieldIndex, LocalDate value)
      Sets a DATE field by index. See setDate(String, LocalDate).
      See Also:
    • setTime

      StructBuilder setTime(int fieldIndex, LocalTime value)
      Sets a TIME field by index. See setTime(String, LocalTime).
      See Also:
    • setTimestamp

      StructBuilder setTimestamp(int fieldIndex, Instant value)
      Sets a UTC-adjusted TIMESTAMP field by index. See setTimestamp(String, Instant).
      See Also:
    • setLocalTimestamp

      StructBuilder setLocalTimestamp(int fieldIndex, LocalDateTime value)
      Sets a local-wall-clock TIMESTAMP field by index. See setLocalTimestamp(String, LocalDateTime).
      See Also:
    • setDecimal

      StructBuilder setDecimal(int fieldIndex, BigDecimal value)
      Sets a DECIMAL field by index. See setDecimal(String, BigDecimal).
      See Also:
    • setUuid

      StructBuilder setUuid(int fieldIndex, UUID value)
      Sets a UUID field by index. See setUuid(String, UUID).
      See Also:
    • setInterval

      StructBuilder setInterval(int fieldIndex, PqInterval value)
      Sets an INTERVAL field by index. See setInterval(String, PqInterval).
      See Also:
    • setNull

      StructBuilder setNull(int fieldIndex)
      Sets a field null by index. See setNull(String).
      Parameters:
      fieldIndex - the field index, 0-based
      Returns:
      this builder, for chaining
      Throws:
      IndexOutOfBoundsException - if fieldIndex is not in [0, getFieldCount())
      IllegalArgumentException - if the field is REQUIRED, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setStruct

      StructBuilder setStruct(int fieldIndex, Consumer<StructBuilder> filler)
      Sets a nested struct field by index. See setStruct(String, Consumer).
      Parameters:
      fieldIndex - the field index, 0-based
      filler - populates the nested struct
      Returns:
      this builder, for chaining
      Throws:
      IndexOutOfBoundsException - if fieldIndex is not in [0, getFieldCount())
      IllegalArgumentException - if the field is not a struct group, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setFile

      StructBuilder setFile(int fieldIndex, PqFile value)
      Sets a FILE field to the reference value, by position.
      See Also:
    • setList

      StructBuilder setList(int fieldIndex, Consumer<ListBuilder> filler)
      Sets a LIST field by index. See setList(String, Consumer).
      Parameters:
      fieldIndex - the field index, 0-based
      filler - appends the list's entries
      Returns:
      this builder, for chaining
      Throws:
      IndexOutOfBoundsException - if fieldIndex is not in [0, getFieldCount())
      IllegalArgumentException - if the field is not a LIST group, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setMap

      StructBuilder setMap(int fieldIndex, Consumer<MapBuilder> filler)
      Sets a MAP field by index. See setMap(String, Consumer).
      Parameters:
      fieldIndex - the field index, 0-based
      filler - appends the map's entries
      Returns:
      this builder, for chaining
      Throws:
      IndexOutOfBoundsException - if fieldIndex is not in [0, getFieldCount())
      IllegalArgumentException - if the field is not a MAP group, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended
    • setValue

      StructBuilder setValue(int fieldIndex, Object value)
      Sets a field by index from a value whose type is known only at runtime. See setValue(String, Object).
      Parameters:
      fieldIndex - the field index, 0-based
      value - the value, or null
      Returns:
      this builder, for chaining
      Throws:
      IndexOutOfBoundsException - if fieldIndex is not in [0, getFieldCount())
      IllegalArgumentException - if the value is not the form the field's type takes, a nested value is rejected as its typed setter rejects it, or the field is already set in this scope
      IllegalStateException - if this builder's scope has ended