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

[GitHub] [arrow] lidavidm commented on a diff in pull request #35769: GH-35627: [Go][Format][Integration] Add StringView/BinaryView to Go implementation

lidavidm commented on code in PR #35769:
URL: https://github.com/apache/arrow/pull/35769#discussion_r1206920219


##########
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")

Review Comment:
   should we assert `!IsInline` for `BufferIndex` and `BufferOffset`?



##########
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:
   What are these for?



-- 
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