github.com/apache/arrow/go/v16@v16.1.0/arrow/array/boolean.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 array
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/apache/arrow/go/v16/arrow"
    25  	"github.com/apache/arrow/go/v16/arrow/bitutil"
    26  	"github.com/apache/arrow/go/v16/arrow/memory"
    27  	"github.com/apache/arrow/go/v16/internal/json"
    28  )
    29  
    30  // A type which represents an immutable sequence of boolean values.
    31  type Boolean struct {
    32  	array
    33  	values []byte
    34  }
    35  
    36  // NewBoolean creates a boolean array from the data memory.Buffer and contains length elements.
    37  // The nullBitmap buffer can be nil of there are no null values.
    38  // If nulls is not known, use UnknownNullCount to calculate the value of NullN at runtime from the nullBitmap buffer.
    39  func NewBoolean(length int, data *memory.Buffer, nullBitmap *memory.Buffer, nulls int) *Boolean {
    40  	arrdata := NewData(arrow.FixedWidthTypes.Boolean, length, []*memory.Buffer{nullBitmap, data}, nil, nulls, 0)
    41  	defer arrdata.Release()
    42  	return NewBooleanData(arrdata)
    43  }
    44  
    45  func NewBooleanData(data arrow.ArrayData) *Boolean {
    46  	a := &Boolean{}
    47  	a.refCount = 1
    48  	a.setData(data.(*Data))
    49  	return a
    50  }
    51  
    52  func (a *Boolean) Value(i int) bool {
    53  	if i < 0 || i >= a.array.data.length {
    54  		panic("arrow/array: index out of range")
    55  	}
    56  	return bitutil.BitIsSet(a.values, a.array.data.offset+i)
    57  }
    58  
    59  func (a *Boolean) ValueStr(i int) string {
    60  	if a.IsNull(i) {
    61  		return NullValueStr
    62  	} else {
    63  		return strconv.FormatBool(a.Value(i))
    64  	}
    65  }
    66  
    67  func (a *Boolean) String() string {
    68  	o := new(strings.Builder)
    69  	o.WriteString("[")
    70  	for i := 0; i < a.Len(); i++ {
    71  		if i > 0 {
    72  			fmt.Fprintf(o, " ")
    73  		}
    74  		switch {
    75  		case a.IsNull(i):
    76  			o.WriteString(NullValueStr)
    77  		default:
    78  			fmt.Fprintf(o, "%v", a.Value(i))
    79  		}
    80  	}
    81  	o.WriteString("]")
    82  	return o.String()
    83  }
    84  
    85  func (a *Boolean) setData(data *Data) {
    86  	a.array.setData(data)
    87  	vals := data.buffers[1]
    88  	if vals != nil {
    89  		a.values = vals.Bytes()
    90  	}
    91  }
    92  
    93  func (a *Boolean) GetOneForMarshal(i int) interface{} {
    94  	if a.IsValid(i) {
    95  		return a.Value(i)
    96  	}
    97  	return nil
    98  }
    99  
   100  func (a *Boolean) MarshalJSON() ([]byte, error) {
   101  	vals := make([]interface{}, a.Len())
   102  	for i := 0; i < a.Len(); i++ {
   103  		if a.IsValid(i) {
   104  			vals[i] = a.Value(i)
   105  		} else {
   106  			vals[i] = nil
   107  		}
   108  	}
   109  	return json.Marshal(vals)
   110  }
   111  
   112  func arrayEqualBoolean(left, right *Boolean) bool {
   113  	for i := 0; i < left.Len(); i++ {
   114  		if left.IsNull(i) {
   115  			continue
   116  		}
   117  		if left.Value(i) != right.Value(i) {
   118  			return false
   119  		}
   120  	}
   121  	return true
   122  }
   123  
   124  var (
   125  	_ arrow.Array = (*Boolean)(nil)
   126  )