]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/go/arrow/type_traits_decimal128.go
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / go / arrow / type_traits_decimal128.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 arrow
18
19 import (
20 "reflect"
21 "unsafe"
22
23 "github.com/apache/arrow/go/v6/arrow/decimal128"
24 "github.com/apache/arrow/go/v6/arrow/endian"
25 )
26
27 // Decimal128 traits
28 var Decimal128Traits decimal128Traits
29
30 const (
31 // Decimal128SizeBytes specifies the number of bytes required to store a single decimal128 in memory
32 Decimal128SizeBytes = int(unsafe.Sizeof(decimal128.Num{}))
33 )
34
35 type decimal128Traits struct{}
36
37 // BytesRequired returns the number of bytes required to store n elements in memory.
38 func (decimal128Traits) BytesRequired(n int) int { return Decimal128SizeBytes * n }
39
40 // PutValue
41 func (decimal128Traits) PutValue(b []byte, v decimal128.Num) {
42 endian.Native.PutUint64(b[:8], uint64(v.LowBits()))
43 endian.Native.PutUint64(b[8:], uint64(v.HighBits()))
44 }
45
46 // CastFromBytes reinterprets the slice b to a slice of type uint16.
47 //
48 // NOTE: len(b) must be a multiple of Uint16SizeBytes.
49 func (decimal128Traits) CastFromBytes(b []byte) []decimal128.Num {
50 h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
51
52 var res []decimal128.Num
53 s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
54 s.Data = h.Data
55 s.Len = h.Len / Decimal128SizeBytes
56 s.Cap = h.Cap / Decimal128SizeBytes
57
58 return res
59 }
60
61 // CastToBytes reinterprets the slice b to a slice of bytes.
62 func (decimal128Traits) CastToBytes(b []decimal128.Num) []byte {
63 h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
64
65 var res []byte
66 s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
67 s.Data = h.Data
68 s.Len = h.Len * Decimal128SizeBytes
69 s.Cap = h.Cap * Decimal128SizeBytes
70
71 return res
72 }
73
74 // Copy copies src to dst.
75 func (decimal128Traits) Copy(dst, src []decimal128.Num) { copy(dst, src) }