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


##########
go/arrow/array/binary.go:
##########
@@ -24,6 +24,7 @@ import (
        "unsafe"
 
        "github.com/apache/arrow/go/v15/arrow"
+  "github.com/apache/arrow/go/v15/arrow/memory"

Review Comment:
   formatting issue?



##########
go/arrow/array/bufferbuilder.go:
##########
@@ -18,7 +18,9 @@ package array
 
 import (
        "sync/atomic"
-
+       "unsafe"
+  
+  "github.com/apache/arrow/go/v15/arrow"

Review Comment:
   format?



##########
go/arrow/array/binary.go:
##########
@@ -318,6 +319,126 @@ func arrayEqualLargeBinary(left, right *LargeBinary) bool 
{
        return true
 }
 
+type ViewLike interface {
+       arrow.Array
+       ValueHeader(int) *arrow.ViewHeader
+}
+
+type BinaryView struct {
+       array
+       values      []arrow.ViewHeader
+       dataBuffers []*memory.Buffer
+}
+
+func NewBinaryViewData(data arrow.ArrayData) *BinaryView {
+       a := &BinaryView{}
+       a.refCount = 1
+       a.setData(data.(*Data))
+       return a
+}
+
+func (a *BinaryView) setData(data *Data) {
+       if len(data.buffers) < 2 {
+               panic("len(data.buffers) < 2")
+       }
+       a.array.setData(data)
+
+       if valueData := data.buffers[1]; valueData != nil {
+               a.values = 
arrow.ViewHeaderTraits.CastFromBytes(valueData.Bytes())
+       }
+
+       a.dataBuffers = data.buffers[2:]
+}
+
+func (a *BinaryView) ValueHeader(i int) *arrow.ViewHeader {
+       if i < 0 || i >= a.array.data.length {
+               panic("arrow/array: index out of range")
+       }
+       return &a.values[a.array.data.offset+i]
+}
+
+func (a *BinaryView) Value(i int) []byte {
+       s := a.ValueHeader(i)
+       if s.IsInline() {
+               return s.InlineBytes()
+       }
+       start := s.BufferOffset()
+       buf := a.dataBuffers[s.BufferIndex()]
+       return buf.Bytes()[start : start+int32(s.Len())]
+}
+
+// ValueString returns the value at index i as a string instead of
+// a byte slice, without copying the underlying data.
+func (a *BinaryView) ValueString(i int) string {
+       b := a.Value(i)
+       return *(*string)(unsafe.Pointer(&b))
+}
+
+func (a *BinaryView) String() string {
+       var o strings.Builder
+       o.WriteString("[")
+       for i := 0; i < a.Len(); i++ {
+               if i > 0 {
+                       o.WriteString(" ")
+               }
+               switch {
+               case a.IsNull(i):
+                       o.WriteString(NullValueStr)
+               default:
+                       fmt.Fprintf(&o, "%q", a.ValueString(i))
+               }
+       }
+       o.WriteString("]")
+       return o.String()
+}
+
+// ValueStr is paired with AppendValueFromString in that it returns
+// the value at index i as a string: Semantically this means that for
+// a null value it will return the string "(null)", otherwise it will
+// return the value as a base64 encoded string suitable for CSV/JSON.
+//
+// This is always going to be less performant than just using ValueString
+// and exists to fulfill the Array interface to provide a method which
+// can produce a human readable string for a given index.
+func (a *BinaryView) ValueStr(i int) string {
+       if a.IsNull(i) {
+               return NullValueStr
+       }
+       return base64.StdEncoding.EncodeToString(a.Value(i))
+}
+
+func (a *BinaryView) GetOneForMarshal(i int) interface{} {
+       if a.IsNull(i) {
+               return nil
+       }
+       return a.Value(i)
+}
+
+func (a *BinaryView) MarshalJSON() ([]byte, error) {

Review Comment:
   please add tests for `MarshalJSON` <-> `Unmarshal`



##########
go/arrow/datatype_viewheader.go:
##########
@@ -0,0 +1,141 @@
+// 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 arrow
+
+import (
+       "bytes"
+       "unsafe"
+
+       "github.com/apache/arrow/go/v15/arrow/endian"
+       "github.com/apache/arrow/go/v15/arrow/internal/debug"
+       "github.com/apache/arrow/go/v15/arrow/memory"
+)
+
+const (
+       StringViewPrefixLen  = 4
+       stringViewInlineSize = 12

Review Comment:
   ```suggestion
        ViewPrefixLen  = 4
        viewInlineSize = 12
   ```



##########
go/arrow/array/binary.go:
##########
@@ -318,6 +319,126 @@ func arrayEqualLargeBinary(left, right *LargeBinary) bool 
{
        return true
 }
 
+type ViewLike interface {
+       arrow.Array
+       ValueHeader(int) *arrow.ViewHeader
+}
+
+type BinaryView struct {
+       array
+       values      []arrow.ViewHeader
+       dataBuffers []*memory.Buffer
+}
+
+func NewBinaryViewData(data arrow.ArrayData) *BinaryView {
+       a := &BinaryView{}
+       a.refCount = 1
+       a.setData(data.(*Data))
+       return a
+}
+
+func (a *BinaryView) setData(data *Data) {
+       if len(data.buffers) < 2 {
+               panic("len(data.buffers) < 2")
+       }
+       a.array.setData(data)
+
+       if valueData := data.buffers[1]; valueData != nil {
+               a.values = 
arrow.ViewHeaderTraits.CastFromBytes(valueData.Bytes())
+       }
+
+       a.dataBuffers = data.buffers[2:]
+}
+
+func (a *BinaryView) ValueHeader(i int) *arrow.ViewHeader {
+       if i < 0 || i >= a.array.data.length {
+               panic("arrow/array: index out of range")
+       }
+       return &a.values[a.array.data.offset+i]
+}
+
+func (a *BinaryView) Value(i int) []byte {
+       s := a.ValueHeader(i)
+       if s.IsInline() {
+               return s.InlineBytes()
+       }
+       start := s.BufferOffset()
+       buf := a.dataBuffers[s.BufferIndex()]
+       return buf.Bytes()[start : start+int32(s.Len())]
+}
+
+// ValueString returns the value at index i as a string instead of
+// a byte slice, without copying the underlying data.
+func (a *BinaryView) ValueString(i int) string {
+       b := a.Value(i)
+       return *(*string)(unsafe.Pointer(&b))
+}
+
+func (a *BinaryView) String() string {
+       var o strings.Builder
+       o.WriteString("[")
+       for i := 0; i < a.Len(); i++ {
+               if i > 0 {
+                       o.WriteString(" ")
+               }
+               switch {
+               case a.IsNull(i):
+                       o.WriteString(NullValueStr)
+               default:
+                       fmt.Fprintf(&o, "%q", a.ValueString(i))
+               }
+       }
+       o.WriteString("]")
+       return o.String()
+}
+
+// ValueStr is paired with AppendValueFromString in that it returns
+// the value at index i as a string: Semantically this means that for
+// a null value it will return the string "(null)", otherwise it will
+// return the value as a base64 encoded string suitable for CSV/JSON.
+//
+// This is always going to be less performant than just using ValueString
+// and exists to fulfill the Array interface to provide a method which
+// can produce a human readable string for a given index.
+func (a *BinaryView) ValueStr(i int) string {

Review Comment:
   please add tests for `ValueStr` <-> `AppendValueFromString`



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

Reply via email to