You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "serramatutu (via GitHub)" <gi...@apache.org> on 2026/07/22 14:12:26 UTC

[PR] Add `Builder.Truncate()` method [arrow-go]

serramatutu opened a new pull request, #981:
URL: https://github.com/apache/arrow-go/pull/981

   - **Add `Truncate` to `Builder` interface with blanket impl**
   - **Implement `Truncate` for DictionaryBuilder**
   - **Implement `Truncate` for REE Builder**
   - **Implement `Truncate` for FixedSizeListBuilder**
   - **Implement `Truncate` for ListBuilder**
   - **Implement `Truncate` for MapBuilder**
   - **Implement `Truncate` for StructBuilder**
   - **Implement `Truncate` for Union Builders**
   - **Add consistency test for `Truncate` for all relevant types**
   
   ### Rationale for this change
   
   While working on [this change](https://github.com/apache/arrow-go/pull/833), I ran into an issue: when a struct builder runs into an error while decoding one of the columns from JSON, there is no way to "un-append" rows from its sibling rows.
   
   The two options are to either (1) `AppendNull()` to all siblings and return the error, which would leave a bad row behind, or (2) eagerly `return err` and leave the builder in a broken state.
   
   ```
   for i := 0; i < b.schema.NumFields(); i++ {
   	val, ok := keylist[b.schema.Field(i).Name]
   	if !ok {
   		b.fields[i].AppendNull()
   		continue
   	}
   
   	valDec := json.NewDecoder(bytes.NewReader(val))
   	valDec.UseNumber()
   	if err := b.fields[i].UnmarshalOne(valDec); err != nil {
   		// TODO: what to do here? We might already have appended to previous siblings (i-1, i-2, ...)
   	}
   }
   ```
   
   What I want to do instead is:
   
   ```
   if err := b.fields[i].UnmarshalOne(valDec); err != nil {
       for j := 0; j < i; j++ {
           b.fields[j].Truncate(b.fields[j].Len() - 1)
       }
       return err
   }
   ```
   
   ### What changes are included in this PR?
   
   Add `Truncate(n int)` to the `Builder` interface. This new method will keep the underlying buffers but truncate the arrays to at most length `n`. It keeps `Len()` and `NullN()` sound. Calling `b.Truncate(b.Len() - 1)` is the equivalente of "un-appending" one element.
   
   
   ### Are these changes tested?
   
   Yes, I added a `TestBuilderTruncate` that checks that `Truncate` works for all data types.
   
   It's worth noting I wanted to call `ValidateFull()` after truncate, but it seems like REE arrays have an issue with `ValidateFull()` that precedes this PR.
   
   
   ### Are there any user-facing changes?
   
   Yes, this adds a new method to the public API of `Builder`
   
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(arrow/array): Add `Builder.Truncate()` method [arrow-go]

Posted by "serramatutu (via GitHub)" <gi...@apache.org>.
serramatutu commented on PR #981:
URL: https://github.com/apache/arrow-go/pull/981#issuecomment-5059782216

   After talking with Matt I decided to close this and do something else for that PR instead.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(arrow/array): Add `Builder.Truncate()` method [arrow-go]

Posted by "serramatutu (via GitHub)" <gi...@apache.org>.
serramatutu closed pull request #981: feat(arrow/array): Add `Builder.Truncate()` method
URL: https://github.com/apache/arrow-go/pull/981


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org