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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
23 "github.com/apache/arrow/go/v6/arrow"
24 "github.com/apache/arrow/go/v6/arrow/memory"
25 "golang.org/x/xerrors"
28 type BinaryScalar interface {
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())
49 func (b *Binary) String() string {
54 return string(b.Value.Bytes())
57 func (b *Binary) CastTo(to arrow.DataType) (Scalar, error) {
59 return MakeNullScalar(to), nil
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
73 return nil, xerrors.Errorf("cannot cast non-null binary scalar to type %s", to)
76 func (b *Binary) Validate() (err error) {
77 err = b.scalar.Validate()
79 err = validateOptional(&b.scalar, b.Value, "value")
84 func (b *Binary) ValidateFull() error {
88 func NewBinaryScalar(val *memory.Buffer, typ arrow.DataType) *Binary {
89 return &Binary{scalar{typ, true}, val}
96 func (s *String) Validate() error {
97 return s.Binary.Validate()
100 func (s *String) ValidateFull() (err error) {
101 if err = s.Validate(); err != nil {
104 if s.Valid && !utf8.ValidString(string(s.Value.Bytes())) {
105 err = xerrors.Errorf("%s scalar contains invalid utf8 data", s.Type)
110 func (s *String) CastTo(to arrow.DataType) (Scalar, error) {
112 return MakeNullScalar(to), nil
115 if to.ID() == arrow.FIXED_SIZE_BINARY {
116 if s.Value.Len() == to.(*arrow.FixedSizeBinaryType).ByteWidth {
117 return NewFixedSizeBinaryScalar(s.Value, to), nil
119 return nil, xerrors.Errorf("cannot convert string scalar of %s to type %s", string(s.Value.Bytes()), to)
122 return ParseScalar(to, string(s.Value.Bytes()))
125 func NewStringScalar(val string) *String {
126 buf := memory.NewBufferBytes([]byte(val))
128 return NewStringScalarFromBuffer(buf)
131 func NewStringScalarFromBuffer(val *memory.Buffer) *String {
133 return &String{NewBinaryScalar(val, arrow.BinaryTypes.String)}
136 type FixedSizeBinary struct {
140 func (b *FixedSizeBinary) Validate() (err error) {
141 if err = b.Binary.Validate(); err != nil {
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())
154 func (b *FixedSizeBinary) ValidateFull() error { return b.Validate() }
156 func NewFixedSizeBinaryScalar(val *memory.Buffer, typ arrow.DataType) *FixedSizeBinary {
158 return &FixedSizeBinary{NewBinaryScalar(val, typ)}