github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/aggexec/tool.go (about)

     1  // Copyright 2024 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 aggexec
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  )
    22  
    23  // vectorAppendWildly is a more efficient version of vector.AppendFixed.
    24  // It ignores the const and null flags check, and uses a wilder way to append (avoiding the overhead of appending one by one).
    25  func vectorAppendWildly[T numeric | types.Decimal64 | types.Decimal128](v *vector.Vector, mp *mpool.MPool, value T) error {
    26  	oldLen := v.Length()
    27  	if oldLen == v.Capacity() {
    28  		if err := v.PreExtend(10, mp); err != nil {
    29  			return err
    30  		}
    31  	}
    32  	v.SetLength(oldLen + 1)
    33  
    34  	var vs []T
    35  	vector.ToSlice(v, &vs)
    36  	vs[oldLen] = value
    37  	return nil
    38  }
    39  
    40  func vectorAppendBytesWildly(v *vector.Vector, mp *mpool.MPool, value []byte) error {
    41  	var va types.Varlena
    42  	if err := vector.BuildVarlenaFromByteSlice(v, &va, &value, mp); err != nil {
    43  		return err
    44  	}
    45  
    46  	oldLen := v.Length()
    47  	if oldLen == v.Capacity() {
    48  		if err := v.PreExtend(10, mp); err != nil {
    49  			return err
    50  		}
    51  	}
    52  	v.SetLength(oldLen + 1)
    53  
    54  	var vs []types.Varlena
    55  	vector.ToSlice(v, &vs)
    56  	vs[oldLen] = va
    57  	return nil
    58  }
    59  
    60  // vectorUnmarshal is instead of vector.UnmarshalBinary.
    61  // it will check if mp is nil first.
    62  func vectorUnmarshal(v *vector.Vector, data []byte, mp *mpool.MPool) error {
    63  	if mp == nil {
    64  		return v.UnmarshalBinary(data)
    65  	}
    66  	return v.UnmarshalBinaryWithCopy(data, mp)
    67  }
    68  
    69  func FromD64ToD128(v types.Decimal64) types.Decimal128 {
    70  	k := types.Decimal128{
    71  		B0_63:   uint64(v),
    72  		B64_127: 0,
    73  	}
    74  	if v.Sign() {
    75  		k.B64_127 = ^k.B64_127
    76  	}
    77  	return k
    78  }