github.com/apache/arrow/go/v16@v16.1.0/arrow/type_traits_interval.go (about)

     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  	"unsafe"
    21  
    22  	"github.com/apache/arrow/go/v16/arrow/endian"
    23  	"github.com/apache/arrow/go/v16/arrow/internal/debug"
    24  )
    25  
    26  var (
    27  	MonthIntervalTraits        monthTraits
    28  	DayTimeIntervalTraits      daytimeTraits
    29  	MonthDayNanoIntervalTraits monthDayNanoTraits
    30  )
    31  
    32  func init() {
    33  	debug.Assert(MonthIntervalSizeBytes == 4, "MonthIntervalSizeBytes should be 4")
    34  	debug.Assert(DayTimeIntervalSizeBytes == 8, "DayTimeIntervalSizeBytes should be 8")
    35  	debug.Assert(MonthDayNanoIntervalSizeBytes == 16, "MonthDayNanoIntervalSizeBytes should be 16")
    36  }
    37  
    38  // MonthInterval traits
    39  
    40  const (
    41  	// MonthIntervalSizeBytes specifies the number of bytes required to store a single MonthInterval in memory
    42  	MonthIntervalSizeBytes = int(unsafe.Sizeof(MonthInterval(0)))
    43  )
    44  
    45  type monthTraits struct{}
    46  
    47  // BytesRequired returns the number of bytes required to store n elements in memory.
    48  func (monthTraits) BytesRequired(n int) int { return MonthIntervalSizeBytes * n }
    49  
    50  // PutValue
    51  func (monthTraits) PutValue(b []byte, v MonthInterval) {
    52  	endian.Native.PutUint32(b, uint32(v))
    53  }
    54  
    55  // CastFromBytes reinterprets the slice b to a slice of type MonthInterval.
    56  //
    57  // NOTE: len(b) must be a multiple of MonthIntervalSizeBytes.
    58  func (monthTraits) CastFromBytes(b []byte) []MonthInterval {
    59  	return GetData[MonthInterval](b)
    60  }
    61  
    62  // CastToBytes reinterprets the slice b to a slice of bytes.
    63  func (monthTraits) CastToBytes(b []MonthInterval) []byte {
    64  	return GetBytes(b)
    65  }
    66  
    67  // Copy copies src to dst.
    68  func (monthTraits) Copy(dst, src []MonthInterval) { copy(dst, src) }
    69  
    70  // DayTimeInterval traits
    71  
    72  const (
    73  	// DayTimeIntervalSizeBytes specifies the number of bytes required to store a single DayTimeInterval in memory
    74  	DayTimeIntervalSizeBytes = int(unsafe.Sizeof(DayTimeInterval{}))
    75  )
    76  
    77  type daytimeTraits struct{}
    78  
    79  // BytesRequired returns the number of bytes required to store n elements in memory.
    80  func (daytimeTraits) BytesRequired(n int) int { return DayTimeIntervalSizeBytes * n }
    81  
    82  // PutValue
    83  func (daytimeTraits) PutValue(b []byte, v DayTimeInterval) {
    84  	endian.Native.PutUint32(b[0:4], uint32(v.Days))
    85  	endian.Native.PutUint32(b[4:8], uint32(v.Milliseconds))
    86  }
    87  
    88  // CastFromBytes reinterprets the slice b to a slice of type DayTimeInterval.
    89  //
    90  // NOTE: len(b) must be a multiple of DayTimeIntervalSizeBytes.
    91  func (daytimeTraits) CastFromBytes(b []byte) []DayTimeInterval {
    92  	return GetData[DayTimeInterval](b)
    93  }
    94  
    95  // CastToBytes reinterprets the slice b to a slice of bytes.
    96  func (daytimeTraits) CastToBytes(b []DayTimeInterval) []byte {
    97  	return GetBytes(b)
    98  }
    99  
   100  // Copy copies src to dst.
   101  func (daytimeTraits) Copy(dst, src []DayTimeInterval) { copy(dst, src) }
   102  
   103  // DayTimeInterval traits
   104  
   105  const (
   106  	// MonthDayNanoIntervalSizeBytes specifies the number of bytes required to store a single DayTimeInterval in memory
   107  	MonthDayNanoIntervalSizeBytes = int(unsafe.Sizeof(MonthDayNanoInterval{}))
   108  )
   109  
   110  type monthDayNanoTraits struct{}
   111  
   112  // BytesRequired returns the number of bytes required to store n elements in memory.
   113  func (monthDayNanoTraits) BytesRequired(n int) int { return MonthDayNanoIntervalSizeBytes * n }
   114  
   115  // PutValue
   116  func (monthDayNanoTraits) PutValue(b []byte, v MonthDayNanoInterval) {
   117  	endian.Native.PutUint32(b[0:4], uint32(v.Months))
   118  	endian.Native.PutUint32(b[4:8], uint32(v.Days))
   119  	endian.Native.PutUint64(b[8:], uint64(v.Nanoseconds))
   120  }
   121  
   122  // CastFromBytes reinterprets the slice b to a slice of type MonthDayNanoInterval.
   123  //
   124  // NOTE: len(b) must be a multiple of MonthDayNanoIntervalSizeBytes.
   125  func (monthDayNanoTraits) CastFromBytes(b []byte) []MonthDayNanoInterval {
   126  	return GetData[MonthDayNanoInterval](b)
   127  }
   128  
   129  // CastToBytes reinterprets the slice b to a slice of bytes.
   130  func (monthDayNanoTraits) CastToBytes(b []MonthDayNanoInterval) []byte {
   131  	return GetBytes(b)
   132  }
   133  
   134  // Copy copies src to dst.
   135  func (monthDayNanoTraits) Copy(dst, src []MonthDayNanoInterval) { copy(dst, src) }