]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/go/arrow/scalar/binary.go
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / go / arrow / scalar / binary.go
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package scalar
18
19 import (
20 "bytes"
21 "unicode/utf8"
22
23 "github.com/apache/arrow/go/v6/arrow"
24 "github.com/apache/arrow/go/v6/arrow/memory"
25 "golang.org/x/xerrors"
26 )
27
28 type BinaryScalar interface {
29 Scalar
30
31 Retain()
32 Release()
33 Data() []byte
34 }
35
36 type Binary struct {
37 scalar
38
39 Value *memory.Buffer
40 }
41
42 func (b *Binary) Retain() { b.Value.Retain() }
43 func (b *Binary) Release() { b.Value.Release() }
44 func (b *Binary) value() interface{} { return b.Value }
45 func (b *Binary) Data() []byte { return b.Value.Bytes() }
46 func (b *Binary) equals(rhs Scalar) bool {
47 return bytes.Equal(b.Value.Bytes(), rhs.(BinaryScalar).Data())
48 }
49 func (b *Binary) String() string {
50 if !b.Valid {
51 return "null"
52 }
53
54 return string(b.Value.Bytes())
55 }
56
57 func (b *Binary) CastTo(to arrow.DataType) (Scalar, error) {
58 if !b.Valid {
59 return MakeNullScalar(to), nil
60 }
61
62 switch to.ID() {
63 case arrow.BINARY:
64 return b, nil
65 case arrow.STRING:
66 return NewStringScalarFromBuffer(b.Value), nil
67 case arrow.FIXED_SIZE_BINARY:
68 if b.Value.Len() == to.(*arrow.FixedSizeBinaryType).ByteWidth {
69 return NewFixedSizeBinaryScalar(b.Value, to), nil
70 }
71 }
72
73 return nil, xerrors.Errorf("cannot cast non-null binary scalar to type %s", to)
74 }
75
76 func (b *Binary) Validate() (err error) {
77 err = b.scalar.Validate()
78 if err == nil {
79 err = validateOptional(&b.scalar, b.Value, "value")
80 }
81 return
82 }
83
84 func (b *Binary) ValidateFull() error {
85 return b.Validate()
86 }
87
88 func NewBinaryScalar(val *memory.Buffer, typ arrow.DataType) *Binary {
89 return &Binary{scalar{typ, true}, val}
90 }
91
92 type String struct {
93 *Binary
94 }
95
96 func (s *String) Validate() error {
97 return s.Binary.Validate()
98 }
99
100 func (s *String) ValidateFull() (err error) {
101 if err = s.Validate(); err != nil {
102 return
103 }
104 if s.Valid && !utf8.ValidString(string(s.Value.Bytes())) {
105 err = xerrors.Errorf("%s scalar contains invalid utf8 data", s.Type)
106 }
107 return
108 }
109
110 func (s *String) CastTo(to arrow.DataType) (Scalar, error) {
111 if !s.Valid {
112 return MakeNullScalar(to), nil
113 }
114
115 if to.ID() == arrow.FIXED_SIZE_BINARY {
116 if s.Value.Len() == to.(*arrow.FixedSizeBinaryType).ByteWidth {
117 return NewFixedSizeBinaryScalar(s.Value, to), nil
118 }
119 return nil, xerrors.Errorf("cannot convert string scalar of %s to type %s", string(s.Value.Bytes()), to)
120 }
121
122 return ParseScalar(to, string(s.Value.Bytes()))
123 }
124
125 func NewStringScalar(val string) *String {
126 buf := memory.NewBufferBytes([]byte(val))
127 defer buf.Release()
128 return NewStringScalarFromBuffer(buf)
129 }
130
131 func NewStringScalarFromBuffer(val *memory.Buffer) *String {
132 val.Retain()
133 return &String{NewBinaryScalar(val, arrow.BinaryTypes.String)}
134 }
135
136 type FixedSizeBinary struct {
137 *Binary
138 }
139
140 func (b *FixedSizeBinary) Validate() (err error) {
141 if err = b.Binary.Validate(); err != nil {
142 return
143 }
144
145 if b.Valid {
146 width := b.Type.(*arrow.FixedSizeBinaryType).ByteWidth
147 if b.Value.Len() != width {
148 err = xerrors.Errorf("%s scalar should have a value of size %d, got %d", b.Type, width, b.Value.Len())
149 }
150 }
151 return
152 }
153
154 func (b *FixedSizeBinary) ValidateFull() error { return b.Validate() }
155
156 func NewFixedSizeBinaryScalar(val *memory.Buffer, typ arrow.DataType) *FixedSizeBinary {
157 val.Retain()
158 return &FixedSizeBinary{NewBinaryScalar(val, typ)}
159 }