lidavidm commented on code in PR #13929: URL: https://github.com/apache/arrow/pull/13929#discussion_r951436060
########## go/arrow/compute/internal/span.go: ########## @@ -0,0 +1,538 @@ +// 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. + +package internal + +import ( + "reflect" + "unsafe" + + "github.com/apache/arrow/go/v10/arrow" + "github.com/apache/arrow/go/v10/arrow/array" + "github.com/apache/arrow/go/v10/arrow/bitutil" + "github.com/apache/arrow/go/v10/arrow/memory" + "github.com/apache/arrow/go/v10/arrow/scalar" +) + +// BufferSpan is a lightweight Buffer holder for ArraySpans that does not +// take ownership of the underlying memory.Buffer at all or could be +// used to reference raw byte slices instead. +type BufferSpan struct { + // Buf should be the byte slice representing this buffer, if this is + // nil then this bufferspan should be considered empty. + Buf []byte + // Owner should point to an underlying parent memory.Buffer if this + // memory is owned by a different, existing, buffer. Retain is not + // called on this buffer, so it must not be released as long as + // this BufferSpan refers to it. + Owner *memory.Buffer + // SelfAlloc tracks whether or not this bufferspan is the only owner + // of the Owning memory.Buffer. This happens when preallocating + // memory or if a kernel allocates it's own buffer for a result. + // In these cases, we have to know so we can properly maintain the + // refcount if this is later turned into an ArrayData object. + SelfAlloc bool +} + +// SetBuffer sets the given buffer into this BufferSpan and marks +// SelfAlloc as false. This should be called when setting a buffer +// that is externally owned/created. +func (b *BufferSpan) SetBuffer(buf *memory.Buffer) { + b.Buf = buf.Bytes() + b.Owner = buf + b.SelfAlloc = false +} + +// WrapBuffer wraps this bufferspan around a buffer and marks +// SelfAlloc as true. This should be called when setting a buffer +// that was allocated as part of an execution rather than just +// re-using an existing buffer from an input array. +func (b *BufferSpan) WrapBuffer(buf *memory.Buffer) { + b.Buf = buf.Bytes() + b.Owner = buf + b.SelfAlloc = true +} + +// ArraySpan is a light-weight, non-owning version of arrow.ArrayData +// for more efficient handling with computation and engines. We use +// explicit go Arrays to define the buffers and some scratch space +// for easily populating and shifting around pointers to memory without +// having to worry about and deal with retain/release during calculations. +type ArraySpan struct { + Type arrow.DataType + Len int64 + Nulls int64 + Offset int64 + Buffers [3]BufferSpan + + Scratch [2]uint64 Review Comment: (IIRC, the scratch space is for dealing with offsets right? Might be good to document what this field is for since it's not immediately obvious) -- 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]
