github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/memorystorage/memorytable/nullable.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package memorytable
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  )
    24  
    25  // Nullable represents a nullable value
    26  type Nullable struct {
    27  	IsNull bool
    28  	Value  any
    29  }
    30  
    31  // Equal reports whether tow Nullable values are equal
    32  func (n Nullable) Equal(n2 Nullable) bool {
    33  	if n.IsNull || n2.IsNull {
    34  		return false
    35  	}
    36  	bsA, ok := n.Value.([]byte)
    37  	if ok {
    38  		bsB, ok := n2.Value.([]byte)
    39  		if ok {
    40  			return bytes.Equal(bsA, bsB)
    41  		}
    42  		panic(fmt.Sprintf("type not the same: %T %T", n.Value, n2.Value))
    43  	}
    44  	return n.Value == n2.Value
    45  }
    46  
    47  // AppendVector append the value to a vector
    48  func (n Nullable) AppendVector(
    49  	vec *vector.Vector, mp *mpool.MPool) error {
    50  	value := n.Value
    51  	str, ok := value.(string)
    52  	if ok {
    53  		value = []byte(str)
    54  	}
    55  	return vector.AppendAny(vec, value, n.IsNull, mp)
    56  }