riverandeye opened a new issue, #2781:
URL: https://github.com/apache/iceberg-go/issues/2781

   ### Summary
   
   `Transaction.Append` and `AppendTable` do not check required fields for 
nulls. A null in a required column is written to Parquet as the zero value of 
its type, the commit succeeds, and the file metrics report no nulls for the 
column. The same happens to a required field inside a struct, and the struct 
itself is then read back as null.
   
   `WriteRecords` rejects a nullable Arrow field for a required column, but 
only at the schema level. A batch whose Arrow field is non-nullable but still 
holds nulls passes and is written the same way.
   
   ### Current behavior
   
   Table schema:
   
   ```
   1: id: required long
   2: name: required string
   3: s: optional struct<4: x: required int>
   ```
   
   Appending two rows through a SQL catalog (SQLite) succeeds:
   
   ```go
   data, _ := array.TableFromJSON(mem, arrowSchema, []string{
        `[{"id": null, "name": null, "s": {"x": null}},
          {"id": 7,    "name": "x",  "s": null}]`})
   
   err := tx.AppendTable(ctx, data, 1024, nil) // nil
   _, err = tx.Commit(ctx)                     // nil
   ```
   
   Reloading the table and scanning it returns:
   
   | Column | Written | Read back | `null_value_counts` |
   |---|---|---|---|
   | `id` | `[null, 7]` | `[0, 7]` | 0 |
   | `name` | `[null, "x"]` | `["", "x"]` | 0 |
   | `s` | `[{x: null}, null]` | `[null, null]` | 2 for `s.x` |
   
   The result is the same whether the Arrow fields are nullable or not. A null 
element in a `list<int required>` column is also accepted, and scanning that 
column afterwards panics with `index out of range`.
   
   `Append` passes the reader straight to the writers, with no check on the 
schema or the data:
   
   
[`table/transaction.go`](https://github.com/apache/iceberg-go/blob/3637784d/table/transaction.go#L1033-L1038)
   
   ```go
   itr := recordsToDataFiles(ctx, meta.loc, meta, recordWritingArgs{
        sc:        rdr.Schema(),
        itr:       array.IterFromReader(rdr),
        fs:        wfs,
        writeUUID: &appendFiles.commitUuid,
   })
   ```
   
   ### Expected behavior
   
   The write fails when a required field holds a null and all of its parents 
are non-null. A required field under a null optional struct, list or map is 
valid and should still be accepted.
   
   Java fails such a write. Its Parquet writer adds a null path only for 
optional fields, so a null in a required field cannot be written.
   
   
[`ParquetValueWriters.java`](https://github.com/apache/iceberg/blob/ce1ce419f90354a86ea7a8d7552f8418e4da7a1a/parquet/src/main/java/org/apache/iceberg/parquet/ParquetValueWriters.java#L57-L64)
   
   ```java
   public static <T> ParquetValueWriter<T> option(
       Type type, int definitionLevel, ParquetValueWriter<T> writer) {
     if (type.isRepetition(Type.Repetition.OPTIONAL)) {
       return new OptionWriter<>(definitionLevel, writer);
     }
   
     return writer;
   }
   ```
   
   ### Proposed change
   
   Check each batch in `RollingDataWriter` right after it is projected to the 
table schema. If a required field holds a null under non-null parents, return 
an error that names the field, for example `cannot write null values to 
required field s.x`.
   
   All data file writes go through this writer, so `Append`, `AppendTable`, 
`Overwrite` and `WriteRecords` get the same check. Nullable Arrow fields that 
hold no nulls are still accepted, as they are today.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to