zeroshade commented on code in PR #35769:
URL: https://github.com/apache/arrow/pull/35769#discussion_r1207041723
##########
go/arrow/datatype_binary.go:
##########
@@ -83,16 +93,168 @@ func (t *LargeStringType) Layout() DataTypeLayout {
func (t *LargeStringType) OffsetTypeTraits() OffsetTraits { return Int64Traits
}
func (LargeStringType) IsUtf8() bool { return true }
+const (
+ StringHeaderPrefixLen = 4
+ stringHeaderInlineSize = 12
+)
+
+func IsStringHeaderInline(length int) bool {
+ return length < stringHeaderInlineSize
+}
+
+// StringHeader is a variable length string (utf8) or byte slice with
+// a 4 byte prefix and inline optimization for small values (12 bytes
+// or fewer). This is similar to Go's standard string but limited by
+// a length of Uint32Max and up to the first four bytes of the string
+// are copied into the struct. This prefix allows failing comparisons
+// early and can reduce CPU cache working set when dealing with short
+// strings.
+//
+// There are two situations:
+//
+// Short string |----|----|--------|
+// ^ ^ ^
+// | | |
+// size prefix remaining in-line portion, zero padded
+//
+// IO Long String |----|----|----|----|
+// ^ ^ ^ ^
+// | | | |
+// size prefix buffer index and offset to out-of-line portion
+//
+// Adapted from TU Munich's UmbraDB [1], Velox, DuckDB.
+//
+// [1]: https://db.in.tum.de/~freitag/papers/p29-neumann-cidr20.pdf
+type StringHeader struct {
+ size uint32
+ // the first 4 bytes of this are the prefix for the string
+ // if size <= StringHeaderInlineSize, then the entire string
+ // is in the data array and is zero padded.
+ // if size > StringHeaderInlineSize, the next 8 bytes are 2 uint32
+ // values which are the buffer index and offset in that buffer
+ // containing the full string.
+ data [stringHeaderInlineSize]byte
+}
+
+func (sh *StringHeader) IsInline() bool {
+ return sh.size <= uint32(stringHeaderInlineSize)
+}
+
+func (sh *StringHeader) Len() int { return int(sh.size) }
+func (sh *StringHeader) Prefix() [StringHeaderPrefixLen]byte {
+ return *(*[4]byte)(unsafe.Pointer(&sh.data))
+}
+
+func (sh *StringHeader) BufferIndex() uint32 {
+ return endian.Native.Uint32(sh.data[StringHeaderPrefixLen:])
+}
+
+func (sh *StringHeader) BufferOffset() uint32 {
+ return endian.Native.Uint32(sh.data[StringHeaderPrefixLen+4:])
+}
+
+func (sh *StringHeader) InlineBytes() (data []byte) {
+ debug.Assert(sh.IsInline(), "calling InlineBytes on non-inline
StringHeader")
+ return sh.data[:sh.size]
+}
+
+func (sh *StringHeader) InlineData() (data string) {
+ debug.Assert(sh.IsInline(), "calling InlineData on non-inline
StringHeader")
+ h := (*reflect.StringHeader)(unsafe.Pointer(&data))
+ h.Data = uintptr(unsafe.Pointer(&sh.data))
+ h.Len = int(sh.size)
+ return
+}
+
+func (sh *StringHeader) SetBytes(data []byte) int {
+ sh.size = uint32(len(data))
+ if sh.IsInline() {
+ return copy(sh.data[:], data)
+ }
+ return copy(sh.data[:4], data)
+}
+
+func (sh *StringHeader) SetString(data string) int {
+ sh.size = uint32(len(data))
+ if sh.IsInline() {
+ return copy(sh.data[:], data)
+ }
+ return copy(sh.data[:4], data)
+}
+
+func (sh *StringHeader) SetIndexOffset(bufferIndex, offset uint32) {
+ endian.Native.PutUint32(sh.data[StringHeaderPrefixLen:], bufferIndex)
+ endian.Native.PutUint32(sh.data[StringHeaderPrefixLen+4:], offset)
+}
+
+func (sh *StringHeader) Equals(buffers []*memory.Buffer, other *StringHeader,
otherBuffers []*memory.Buffer) bool {
+ if sh.sizeAndPrefixAsInt() != other.sizeAndPrefixAsInt() {
+ return false
+ }
+
+ if sh.IsInline() {
+ return sh.inlinedAsInt64() == other.inlinedAsInt64()
+ }
+
+ data := buffers[sh.BufferIndex()].Bytes()[sh.BufferOffset() :
sh.BufferOffset()+sh.size]
+ otherData :=
otherBuffers[other.BufferIndex()].Bytes()[other.BufferOffset() :
other.BufferOffset()+other.size]
+ return bytes.Equal(data, otherData)
+}
+
+func (sh *StringHeader) inlinedAsInt64() int64 {
+ s := unsafe.Slice((*int64)(unsafe.Pointer(sh)), 2)
+ return s[1]
+}
+
+func (sh *StringHeader) sizeAndPrefixAsInt() int64 {
+ s := unsafe.Slice((*int64)(unsafe.Pointer(sh)), 2)
+ return s[0]
+}
+
+type BinaryViewType struct{}
+
+func (*BinaryViewType) ID() Type { return BINARY_VIEW }
+func (*BinaryViewType) Name() string { return "binary_view" }
+func (*BinaryViewType) String() string { return "binary_view" }
+func (*BinaryViewType) IsUtf8() bool { return false }
+func (*BinaryViewType) binary() {}
+func (*BinaryViewType) view() {}
Review Comment:
convenience interfaces. in `datatype.go` there's `BinaryDataType` which
defines an interface adding `IsUtf8() bool` which requires a `binary()` method
so that you can easily write code that works for *all* of these binary
interfaces via type switches or interfaces.
Then I added this `view()` method for a `BinaryViewDataType` so you can
write code targeting both BinaryView and StringView without needing to
duplicate code by just using the interface in the type switch.
--
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]