github.com/matrixorigin/matrixone@v1.2.0/pkg/container/batch/types.go (about)

     1  // Copyright 2021 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 batch
    16  
    17  import (
    18  	"bytes"
    19  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    20  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/aggexec"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  )
    25  
    26  var (
    27  	EmptyBatch = &Batch{rowCount: 0}
    28  
    29  	EmptyForConstFoldBatch = &Batch{
    30  		Cnt:      1,
    31  		Vecs:     make([]*vector.Vector, 0),
    32  		rowCount: 1,
    33  	}
    34  )
    35  
    36  type EncodeBatch struct {
    37  	rowCount  int64
    38  	Vecs      []*vector.Vector
    39  	Attrs     []string
    40  	AggInfos  [][]byte
    41  	Recursive int32
    42  }
    43  
    44  func (m *EncodeBatch) MarshalBinary() ([]byte, error) {
    45  	// --------------------------------------------------------------------
    46  	// | len | Zs... | len | Vecs... | len | Attrs... | len | AggInfos... |
    47  	// --------------------------------------------------------------------
    48  	var buf bytes.Buffer
    49  
    50  	// row count.
    51  	rl := int64(m.rowCount)
    52  	buf.Write(types.EncodeInt64(&rl))
    53  
    54  	// Vecs
    55  	l := int32(len(m.Vecs))
    56  	buf.Write(types.EncodeInt32(&l))
    57  	for i := 0; i < int(l); i++ {
    58  		data, err := m.Vecs[i].MarshalBinary()
    59  		if err != nil {
    60  			return nil, err
    61  		}
    62  		size := int32(len(data))
    63  		buf.Write(types.EncodeInt32(&size))
    64  		buf.Write(data)
    65  	}
    66  
    67  	// Attrs
    68  	l = int32(len(m.Attrs))
    69  	buf.Write(types.EncodeInt32(&l))
    70  	for i := 0; i < int(l); i++ {
    71  		size := int32(len(m.Attrs[i]))
    72  		buf.Write(types.EncodeInt32(&size))
    73  		n, _ := buf.WriteString(m.Attrs[i])
    74  		if int32(n) != size {
    75  			panic("unexpected length for string")
    76  		}
    77  	}
    78  
    79  	// AggInfos
    80  	l = int32(len(m.AggInfos))
    81  	buf.Write(types.EncodeInt32(&l))
    82  	for i := 0; i < int(l); i++ {
    83  		size := int32(len(m.AggInfos[i]))
    84  		buf.Write(types.EncodeInt32(&size))
    85  		buf.Write(m.AggInfos[i])
    86  	}
    87  
    88  	buf.Write(types.EncodeInt32(&m.Recursive))
    89  
    90  	return buf.Bytes(), nil
    91  }
    92  
    93  func (m *EncodeBatch) UnmarshalBinary(data []byte) error {
    94  	return m.unmarshalBinaryWithAnyMp(data, nil)
    95  }
    96  
    97  func (m *EncodeBatch) UnmarshalBinaryWithCopy(data []byte, mp *mpool.MPool) error {
    98  	return m.unmarshalBinaryWithAnyMp(data, mp)
    99  }
   100  
   101  func (m *EncodeBatch) unmarshalBinaryWithAnyMp(data []byte, mp *mpool.MPool) error {
   102  	// types.DecodeXXX plays with raw pointer, so we make a copy of binary data
   103  	buf := make([]byte, len(data))
   104  	copy(buf, data)
   105  
   106  	// row count
   107  	m.rowCount = types.DecodeInt64(buf[:8])
   108  	buf = buf[8:]
   109  
   110  	// Vecs
   111  	l := types.DecodeInt32(buf[:4])
   112  	buf = buf[4:]
   113  	vecs := make([]*vector.Vector, l)
   114  	for i := 0; i < int(l); i++ {
   115  		size := types.DecodeInt32(buf[:4])
   116  		buf = buf[4:]
   117  
   118  		vecs[i] = new(vector.Vector)
   119  		if mp == nil {
   120  			if err := vecs[i].UnmarshalBinary(buf[:size]); err != nil {
   121  				return err
   122  			}
   123  		} else {
   124  			if err := vecs[i].UnmarshalBinaryWithCopy(buf[:size], mp); err != nil {
   125  				for _, vec := range vecs {
   126  					if vec != nil {
   127  						vec.Free(mp)
   128  					}
   129  				}
   130  				return err
   131  			}
   132  		}
   133  
   134  		buf = buf[size:]
   135  	}
   136  	m.Vecs = vecs
   137  
   138  	// Attrs
   139  	l = types.DecodeInt32(buf[:4])
   140  	buf = buf[4:]
   141  	attrs := make([]string, l)
   142  	for i := 0; i < int(l); i++ {
   143  		size := types.DecodeInt32(buf[:4])
   144  		buf = buf[4:]
   145  		attrs[i] = string(buf[:size])
   146  		buf = buf[size:]
   147  	}
   148  	m.Attrs = attrs
   149  
   150  	// AggInfos
   151  	l = types.DecodeInt32(buf[:4])
   152  	buf = buf[4:]
   153  	aggs := make([][]byte, l)
   154  	for i := 0; i < int(l); i++ {
   155  		size := types.DecodeInt32(buf[:4])
   156  		buf = buf[4:]
   157  		aggs[i] = buf[:size]
   158  		buf = buf[size:]
   159  	}
   160  	m.AggInfos = aggs
   161  
   162  	m.Recursive = types.DecodeInt32(buf[:4])
   163  
   164  	return nil
   165  }
   166  
   167  // Batch represents a part of a relationship
   168  // including an optional list of row numbers, columns and list of attributes
   169  //
   170  //	(SelsData, Sels) - list of row numbers
   171  //	(Attrs) - list of attributes
   172  //	(vecs) 	- columns
   173  type Batch struct {
   174  	// For recursive CTE, 1 is last batch, 2 is end of batch
   175  	Recursive int32
   176  	// Ro if true, Attrs is read only
   177  	Ro         bool
   178  	ShuffleIDX int //used only in shuffle dispatch
   179  	// reference count, default is 1
   180  	Cnt int64
   181  	// Attrs column name list
   182  	Attrs []string
   183  	// Vecs col data
   184  	Vecs []*vector.Vector
   185  
   186  	Aggs []aggexec.AggFuncExec
   187  
   188  	// row count of batch, to instead of old len(Zs).
   189  	rowCount int
   190  
   191  	AuxData any // hash table etc.
   192  }