github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/aggexec/aggResult.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  	"bytes"
    19  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    20  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  )
    24  
    25  type basicResult struct {
    26  	mg          AggMemoryManager
    27  	mp          *mpool.MPool
    28  	typ         types.Type
    29  	res         *vector.Vector
    30  	ess         *vector.Vector // empty situation.
    31  	empty       []bool
    32  	groupToSet  int  // row index for aggGet() and aggSet()
    33  	emptyBeNull bool // indicate that if we should set null to the new row.
    34  }
    35  
    36  func (r *basicResult) init(
    37  	mg AggMemoryManager, typ types.Type,
    38  	emptyNull bool) {
    39  	r.typ = typ
    40  	r.emptyBeNull = emptyNull
    41  	r.groupToSet = 0
    42  	if mg == nil {
    43  		return
    44  	}
    45  	r.mg = mg
    46  	r.mp = mg.Mp()
    47  	r.res = mg.GetVector(typ)
    48  	r.ess = mg.GetVector(types.T_bool.ToType())
    49  }
    50  
    51  func (r *basicResult) extend(more int) (oldLen, newLen int, err error) {
    52  	oldLen, newLen = r.res.Length(), r.res.Length()+more
    53  	if err = r.res.PreExtend(more, r.mp); err != nil {
    54  		return oldLen, oldLen, err
    55  	}
    56  	if err = r.ess.PreExtend(more, r.mp); err != nil {
    57  		return oldLen, oldLen, err
    58  	}
    59  	r.res.SetLength(newLen)
    60  	r.ess.SetLength(newLen)
    61  
    62  	r.empty = vector.MustFixedCol[bool](r.ess)
    63  	for i := oldLen; i < newLen; i++ {
    64  		r.empty[i] = true
    65  	}
    66  	return oldLen, newLen, nil
    67  }
    68  
    69  func (r *basicResult) preAllocate(more int) (err error) {
    70  	oldLen := r.res.Length()
    71  	if err = r.res.PreExtend(more, r.mp); err != nil {
    72  		return err
    73  	}
    74  	if err = r.ess.PreExtend(more, r.mp); err != nil {
    75  		return err
    76  	}
    77  	r.res.SetLength(oldLen)
    78  	r.ess.SetLength(oldLen)
    79  	return nil
    80  }
    81  
    82  func (r *basicResult) mergeEmpty(other basicResult, i, j int) {
    83  	r.empty[i] = r.empty[i] && other.empty[j]
    84  }
    85  
    86  func (r *basicResult) groupIsEmpty(i int) bool {
    87  	return r.empty[i]
    88  }
    89  
    90  func (r *basicResult) setGroupNotEmpty(i int) {
    91  	r.empty[i] = false
    92  }
    93  
    94  func (r *basicResult) flush() *vector.Vector {
    95  	if r.emptyBeNull {
    96  		nsp := nulls.NewWithSize(len(r.empty))
    97  		for i, j := uint64(0), uint64(len(r.empty)); i < j; i++ {
    98  			if r.empty[i] {
    99  				nsp.Add(i)
   100  			}
   101  		}
   102  		r.res.SetNulls(nsp)
   103  	}
   104  	result := r.res
   105  	r.res = nil
   106  	return result
   107  }
   108  
   109  func (r *basicResult) free() {
   110  	if r.mg == nil {
   111  		return
   112  	}
   113  	if r.res != nil {
   114  		if r.res.NeedDup() {
   115  			r.res.Free(r.mp)
   116  		} else {
   117  			r.mg.PutVector(r.res)
   118  		}
   119  	}
   120  	if r.ess != nil {
   121  		if r.ess.NeedDup() {
   122  			r.ess.Free(r.mp)
   123  		} else {
   124  			r.mg.PutVector(r.ess)
   125  		}
   126  	}
   127  }
   128  
   129  func (r *basicResult) eq0(other basicResult) bool {
   130  	if !r.typ.Eq(other.typ) {
   131  		return false
   132  	}
   133  	bs1 := vector.MustFixedCol[bool](r.ess)
   134  	bs2 := vector.MustFixedCol[bool](other.ess)
   135  	if len(bs1) != len(bs2) {
   136  		return false
   137  	}
   138  	for i, j := 0, len(bs1); i < j; i++ {
   139  		if bs1[i] != bs2[i] {
   140  			return false
   141  		}
   142  	}
   143  	return true
   144  }
   145  
   146  func (r *basicResult) marshal() ([]byte, error) {
   147  	d1, err := r.res.MarshalBinary()
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	d2, err := r.ess.MarshalBinary()
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  	d := make([]byte, 0, 4+len(d1)+len(d2))
   156  	length := uint32(len(d1))
   157  	d = append(d, types.EncodeUint32(&length)...)
   158  	d = append(d, d1...)
   159  	d = append(d, d2...)
   160  	return d, nil
   161  }
   162  
   163  func (r *basicResult) unmarshal0(data []byte) error {
   164  	if r.mg == nil {
   165  		r.res = vector.NewVec(r.typ)
   166  		r.ess = vector.NewVec(types.T_bool.ToType())
   167  	} else {
   168  		r.res = r.mg.GetVector(r.typ)
   169  		r.ess = r.mg.GetVector(types.T_bool.ToType())
   170  	}
   171  
   172  	length := types.DecodeUint32(data[:4])
   173  	data = data[4:]
   174  
   175  	var mp *mpool.MPool = nil
   176  	if r.mg != nil {
   177  		mp = r.mg.Mp()
   178  	}
   179  
   180  	if err := vectorUnmarshal(r.res, data[:length], mp); err != nil {
   181  		return err
   182  	}
   183  	data = data[length:]
   184  	if err := vectorUnmarshal(r.ess, data, mp); err != nil {
   185  		r.res.Free(mp)
   186  		return err
   187  	}
   188  	r.empty = vector.MustFixedCol[bool](r.ess)
   189  	return nil
   190  }
   191  
   192  type aggFuncResult[T types.FixedSizeTExceptStrType] struct {
   193  	basicResult
   194  	values []T // for quick get/set
   195  }
   196  
   197  type aggFuncBytesResult struct {
   198  	basicResult
   199  }
   200  
   201  func initFixedAggFuncResult[T types.FixedSizeTExceptStrType](
   202  	mg AggMemoryManager, typ types.Type,
   203  	emptyNull bool) aggFuncResult[T] {
   204  	r := aggFuncResult[T]{}
   205  	r.init(mg, typ, emptyNull)
   206  	return r
   207  }
   208  
   209  func (r *aggFuncResult[T]) grows(more int) error {
   210  	oldLen, newLen, err := r.extend(more)
   211  	if err != nil {
   212  		return err
   213  	}
   214  	r.values = vector.MustFixedCol[T](r.res)
   215  	// reset the new row.
   216  	var v T
   217  	for i, j := oldLen, newLen; i < j; i++ {
   218  		r.values[i] = v
   219  	}
   220  	return nil
   221  }
   222  
   223  func (r *aggFuncResult[T]) aggGet() T {
   224  	return r.values[r.groupToSet]
   225  }
   226  
   227  // for agg private structure's Fill.
   228  func (r *aggFuncResult[T]) aggSet(v T) {
   229  	r.values[r.groupToSet] = v
   230  }
   231  
   232  func (r *aggFuncResult[T]) unmarshal(data []byte) error {
   233  	if err := r.unmarshal0(data); err != nil {
   234  		return err
   235  	}
   236  	r.values = vector.MustFixedCol[T](r.res)
   237  	return nil
   238  }
   239  
   240  func (r *aggFuncResult[T]) eq(other aggFuncResult[T]) bool {
   241  	if !r.basicResult.eq0(other.basicResult) {
   242  		return false
   243  	}
   244  	vs1 := vector.MustFixedCol[T](r.res)
   245  	vs2 := vector.MustFixedCol[T](other.res)
   246  	if len(vs1) != len(vs2) {
   247  		return false
   248  	}
   249  	bs1 := vector.MustFixedCol[bool](r.ess)
   250  	for i, j := 0, len(vs1); i < j; i++ {
   251  		if bs1[i] {
   252  			continue
   253  		}
   254  		if vs1[i] != vs2[i] {
   255  			return false
   256  		}
   257  	}
   258  	return true
   259  }
   260  
   261  func initBytesAggFuncResult(
   262  	mg AggMemoryManager, typ types.Type,
   263  	emptyNull bool) aggFuncBytesResult {
   264  	r := aggFuncBytesResult{}
   265  	r.init(mg, typ, emptyNull)
   266  	return r
   267  }
   268  
   269  func (r *aggFuncBytesResult) grows(more int) error {
   270  	oldLen, newLen, err := r.extend(more)
   271  	if err != nil {
   272  		return err
   273  	}
   274  
   275  	var v = []byte("")
   276  	for i, j := oldLen, newLen; i < j; i++ {
   277  		// this will never cause error.
   278  		_ = vector.SetBytesAt(r.res, i, v, r.mp)
   279  	}
   280  	return nil
   281  }
   282  
   283  func (r *aggFuncBytesResult) aggGet() []byte {
   284  	// todo: we cannot do simple optimization to get bytes here because result was not read-only.
   285  	//  the set method may change the max length of the vector.
   286  	//  if we want, we should add a flag to indicate that the vector item's length is <= types.VarlenaInlineSize.
   287  	return r.res.GetBytesAt(r.groupToSet)
   288  }
   289  
   290  func (r *aggFuncBytesResult) aggSet(v []byte) error {
   291  	return vector.SetBytesAt(r.res, r.groupToSet, v, r.mp)
   292  }
   293  
   294  func (r *aggFuncBytesResult) unmarshal(data []byte) error {
   295  	return r.unmarshal0(data)
   296  }
   297  
   298  func (r *aggFuncBytesResult) eq(other aggFuncBytesResult) bool {
   299  	if !r.basicResult.eq0(other.basicResult) {
   300  		return false
   301  	}
   302  	vs1 := vector.MustBytesCol(r.res)
   303  	vs2 := vector.MustBytesCol(other.res)
   304  	if len(vs1) != len(vs2) {
   305  		return false
   306  	}
   307  	bs1 := vector.MustFixedCol[bool](r.ess)
   308  	for i, j := 0, len(vs1); i < j; i++ {
   309  		if bs1[i] {
   310  			continue
   311  		}
   312  		if !bytes.Equal(vs1[i], vs2[i]) {
   313  			return false
   314  		}
   315  	}
   316  	return true
   317  }