zeroshade commented on code in PR #14114: URL: https://github.com/apache/arrow/pull/14114#discussion_r1073723887
########## go/arrow/array/encoded.go: ########## @@ -156,3 +158,160 @@ func arrayRunEndEncodedApproxEqual(l, r *RunEndEncoded, opt equalOption) bool { } return true } + +type RunEndEncodedBuilder struct { + builder + + dt arrow.DataType + runEnds Builder + values Builder + maxRunEnd uint64 +} + +func NewRunEndEncodedBuilder(mem memory.Allocator, runEnds, encoded arrow.DataType) *RunEndEncodedBuilder { + dt := arrow.RunEndEncodedOf(runEnds, encoded) + if !dt.ValidRunEndsType(runEnds) { + panic("arrow/ree: invalid runEnds type for run length encoded array") + } + + var maxEnd uint64 + switch runEnds.ID() { + case arrow.INT16: + maxEnd = math.MaxInt16 + case arrow.INT32: + maxEnd = math.MaxInt32 + case arrow.INT64: + maxEnd = math.MaxInt64 + } + return &RunEndEncodedBuilder{ + builder: builder{refCount: 1, mem: mem}, + dt: dt, + runEnds: NewBuilder(mem, runEnds), + values: NewBuilder(mem, encoded), + maxRunEnd: maxEnd, + } +} + +func (b *RunEndEncodedBuilder) Type() arrow.DataType { + return b.dt +} + +func (b *RunEndEncodedBuilder) Release() { + debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") + + if atomic.AddInt64(&b.refCount, -1) == 0 { + b.values.Release() + b.runEnds.Release() + } +} + +func (b *RunEndEncodedBuilder) addLength(n uint64) { + if uint64(b.length)+n > b.maxRunEnd { + panic(fmt.Errorf("%w: %s array length must fit be less than %d", arrow.ErrInvalid, b.dt, b.maxRunEnd)) + } + + b.length += int(n) +} + +func (b *RunEndEncodedBuilder) finishRun() { + if b.length == 0 { + return + } + + switch bldr := b.runEnds.(type) { + case *Int16Builder: + bldr.Append(int16(b.length)) + case *Int32Builder: + bldr.Append(int32(b.length)) + case *Int64Builder: + bldr.Append(int64(b.length)) + } +} + +func (b *RunEndEncodedBuilder) ValueBuilder() Builder { return b.values } +func (b *RunEndEncodedBuilder) Append(n uint64) { + b.finishRun() + b.addLength(n) +} +func (b *RunEndEncodedBuilder) ContinueRun(n uint64) { + b.addLength(n) +} +func (b *RunEndEncodedBuilder) AppendNull() { Review Comment: yea, if i'm gonna add `AppendNulls` then I should add it to *all* the builders and make it part of the interface and i don't really wanna do that in this PR haha. -- 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-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org