zeroshade commented on code in PR #34534: URL: https://github.com/apache/arrow/pull/34534#discussion_r1132890735
########## go/arrow/compute/internal/kernels/vector_run_end_encode.go: ########## @@ -0,0 +1,953 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build go1.18 + +package kernels + +import ( + "bytes" + "fmt" + "sort" + "unsafe" + + "github.com/apache/arrow/go/v12/arrow" + "github.com/apache/arrow/go/v12/arrow/bitutil" + "github.com/apache/arrow/go/v12/arrow/compute/internal/exec" + "github.com/apache/arrow/go/v12/arrow/decimal128" + "github.com/apache/arrow/go/v12/arrow/decimal256" + "github.com/apache/arrow/go/v12/arrow/float16" + "github.com/apache/arrow/go/v12/arrow/internal/debug" + "github.com/apache/arrow/go/v12/arrow/memory" +) + +type RunEndEncodeState struct { + RunEndType arrow.DataType +} + +func (RunEndEncodeState) TypeName() string { + return "RunEndEncodeOptions" +} + +type RunEndsType interface { + int16 | int32 | int64 +} + +func readFixedWidthVal[V exec.FixedWidthTypes](inputValidity, inputValues []byte, offset int64, out *V) bool { + if bitutil.BitIsNotSet(inputValidity, int(offset)) { + return false + } + + sz := int64(unsafe.Sizeof(*out)) + *out = *(*V)(unsafe.Pointer(&inputValues[offset*sz])) + return true +} + +func writeFixedWidthVal[V exec.FixedWidthTypes](result *exec.ExecResult, offset int64, valid bool, value V) { + if len(result.Buffers[0].Buf) != 0 { + bitutil.SetBitTo(result.Buffers[0].Buf, int(offset), valid) + } + + arr := exec.GetData[V](result.Buffers[1].Buf) + arr[offset] = value +} + +func readBoolVal(inputValidity, inputValues []byte, offset int64, out *bool) bool { + if bitutil.BitIsNotSet(inputValidity, int(offset)) { + return false + } + + *out = bitutil.BitIsSet(inputValues, int(offset)) + return true +} + +func writeBoolVal(result *exec.ExecResult, offset int64, valid bool, value bool) { + if len(result.Buffers[0].Buf) != 0 { + bitutil.SetBitTo(result.Buffers[0].Buf, int(offset), valid) + } + bitutil.SetBitTo(result.Buffers[1].Buf, int(offset), value) +} + +type runEndEncodeLoopFixedWidth[R RunEndsType, V exec.FixedWidthTypes | bool] struct { + inputLen, inputOffset int64 + inputValidity []byte + inputValues []byte + valueType arrow.DataType + + readValue func(inputValidity, inputValues []byte, offset int64, out *V) bool + writeValue func(*exec.ExecResult, int64, bool, V) +} + +func (re *runEndEncodeLoopFixedWidth[R, V]) WriteEncodedRuns(out *exec.ExecResult) int64 { + outputRunEnds := exec.GetData[R](out.Children[0].Buffers[1].Buf) + + readOffset := re.inputOffset + var currentRun V + curRunValid := re.readValue(re.inputValidity, re.inputValues, readOffset, ¤tRun) + readOffset++ + + var writeOffset int64 + for readOffset < re.inputOffset+re.inputLen { + var value V + valid := re.readValue(re.inputValidity, re.inputValues, readOffset, &value) + if valid != curRunValid || value != currentRun { + // close the current run by writing it out + re.writeValue(&out.Children[1], writeOffset, curRunValid, currentRun) + runEnd := R(readOffset - re.inputOffset) + outputRunEnds[writeOffset] = runEnd + writeOffset++ + curRunValid, currentRun = valid, value + } + readOffset++ + } + + re.writeValue(&out.Children[1], writeOffset, curRunValid, currentRun) + outputRunEnds[writeOffset] = R(re.inputLen) + return writeOffset + 1 +} + +func (re *runEndEncodeLoopFixedWidth[R, V]) CountNumberOfRuns() (numValid, numOutput int64) { + offset := re.inputOffset + var currentRun V + curRunValid := re.readValue(re.inputValidity, re.inputValues, offset, ¤tRun) + offset++ + + if curRunValid { + numValid = 1 + } + numOutput = 1 + + for offset < re.inputOffset+re.inputLen { + var value V + valid := re.readValue(re.inputValidity, re.inputValues, offset, &value) + offset++ + // new run + if valid != curRunValid || value != currentRun { + currentRun = value + curRunValid = valid + + numOutput++ + if valid { + numValid++ + } + } + } + return +} + +func (re *runEndEncodeLoopFixedWidth[R, V]) PreallocOutput(ctx *exec.KernelCtx, numOutput int64, out *exec.ExecResult) { + runEndsBuffer := ctx.Allocate(int(numOutput) * int(SizeOf[R]())) + var validityBuffer *memory.Buffer + if len(re.inputValidity) > 0 { + validityBuffer = ctx.AllocateBitmap(numOutput) + } + + var valueBuffer *memory.Buffer + bufSpec := re.valueType.Layout().Buffers[1] + if bufSpec.Kind == arrow.KindBitmap { + valueBuffer = ctx.AllocateBitmap(numOutput) + } else { + valueBuffer = ctx.Allocate(int(numOutput) * bufSpec.ByteWidth) + } + + reeType := arrow.RunEndEncodedOf(exec.GetDataType[R](), re.valueType) + out.Release() Review Comment: it doesn't call the `Release()` method. Go doesn't have destructors. -- 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