github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/anyvalue.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 agg
    16  
    17  import "github.com/matrixorigin/matrixone/pkg/container/types"
    18  
    19  type Anyvalue[T any] struct {
    20  	NotSet []bool
    21  }
    22  
    23  func AnyValueReturnType(typs []types.Type) types.Type {
    24  	return typs[0]
    25  }
    26  
    27  func NewAnyValue[T any]() *Anyvalue[T] {
    28  	return &Anyvalue[T]{}
    29  }
    30  
    31  func (a *Anyvalue[T]) Grows(size int) {
    32  	if len(a.NotSet) == 0 {
    33  		a.NotSet = make([]bool, 0)
    34  	}
    35  
    36  	for i := 0; i < size; i++ {
    37  		a.NotSet = append(a.NotSet, false)
    38  	}
    39  }
    40  
    41  func (a *Anyvalue[T]) Eval(vs []T) []T {
    42  	return vs
    43  }
    44  
    45  func (a *Anyvalue[T]) Fill(i int64, value T, ov T, z int64, isEmpty bool, isNull bool) (T, bool) {
    46  	if !isNull && !a.NotSet[i] {
    47  		a.NotSet[i] = true
    48  		return value, false
    49  	}
    50  	return ov, isEmpty
    51  }
    52  
    53  func (a *Anyvalue[T]) Merge(xIndex int64, yIndex int64, x T, y T, xEmpty bool, yEmpty bool, yAnyValue any) (T, bool) {
    54  	if !yEmpty {
    55  		ya := yAnyValue.(*Anyvalue[T])
    56  		if ya.NotSet[yIndex] && !a.NotSet[xIndex] {
    57  			a.NotSet[xIndex] = true
    58  			return y, false
    59  		}
    60  	}
    61  	return x, xEmpty
    62  }
    63  
    64  func (a *Anyvalue[T]) MarshalBinary() ([]byte, error) {
    65  	return types.EncodeSlice(a.NotSet), nil
    66  }
    67  
    68  func (a *Anyvalue[T]) UnmarshalBinary(data []byte) error {
    69  	// avoid resulting errors caused by morpc overusing memory
    70  	copyData := make([]byte, len(data))
    71  	copy(copyData, data)
    72  	a.NotSet = types.DecodeSlice[bool](copyData)
    73  	return nil
    74  }