github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logtail/tools.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 logtail
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"sort"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    23  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/logutil"
    26  	"github.com/matrixorigin/matrixone/pkg/objectio"
    27  	"github.com/matrixorigin/matrixone/pkg/pb/api"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    30  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    31  	"go.uber.org/zap/zapcore"
    32  )
    33  
    34  func ToStringTemplate(vec containers.Vector, printN int, opts ...common.TypePrintOpt) string {
    35  	var w bytes.Buffer
    36  	_, _ = w.WriteString(fmt.Sprintf("[%d]: ", vec.Length()))
    37  	if printN < 0 || printN > vec.Length() {
    38  		printN = vec.Length()
    39  	}
    40  	first := true
    41  	typ := vec.GetType()
    42  	for i := 0; i < printN; i++ {
    43  		if !first {
    44  			_ = w.WriteByte(',')
    45  		}
    46  		v := vec.Get(i)
    47  		vIsNull := vec.IsNull(i)
    48  		_, _ = w.WriteString(common.TypeStringValue(*typ, v, vIsNull, opts...))
    49  		first = false
    50  	}
    51  
    52  	return w.String()
    53  }
    54  
    55  func DebugBatchToString(name string, bat *containers.Batch, isSpecialRowID bool, lvl zapcore.Level) string {
    56  	if logutil.GetSkip1Logger().Core().Enabled(lvl) {
    57  		return BatchToString(name, bat, isSpecialRowID)
    58  	}
    59  	return "not required level"
    60  }
    61  
    62  func BatchToString(name string, bat *containers.Batch, isSpecialRowID bool) string {
    63  	var w bytes.Buffer
    64  	_, _ = w.WriteString(fmt.Sprintf("[BatchName=%s]\n", name))
    65  	for i, vec := range bat.Vecs {
    66  		_, _ = w.WriteString(fmt.Sprintf("(attr=%s)", bat.Attrs[i]))
    67  		if bat.Attrs[i] == catalog.AttrRowID {
    68  			if isSpecialRowID {
    69  				_, _ = w.WriteString(ToStringTemplate(vec, common.DefaultMaxRowsToPrint, common.WithSpecialRowid{}))
    70  			} else {
    71  				_, _ = w.WriteString(ToStringTemplate(vec, common.DefaultMaxRowsToPrint))
    72  			}
    73  		} else {
    74  			_, _ = w.WriteString(ToStringTemplate(vec, common.DefaultMaxRowsToPrint, common.WithDoNotPrintBin{}))
    75  		}
    76  		_ = w.WriteByte('\n')
    77  	}
    78  	return w.String()
    79  }
    80  
    81  // make batch, append necessary field like commit ts
    82  func makeRespBatchFromSchema(schema *catalog.Schema, mp *mpool.MPool) *containers.Batch {
    83  	bat := containers.NewBatch()
    84  
    85  	bat.AddVector(
    86  		catalog.AttrRowID,
    87  		containers.MakeVector(types.T_Rowid.ToType(), mp),
    88  	)
    89  	bat.AddVector(
    90  		catalog.AttrCommitTs,
    91  		containers.MakeVector(types.T_TS.ToType(), mp),
    92  	)
    93  	// Types() is not used, then empty schema can also be handled here
    94  	typs := schema.AllTypes()
    95  	attrs := schema.AllNames()
    96  	for i, attr := range attrs {
    97  		if attr == catalog.PhyAddrColumnName {
    98  			continue
    99  		}
   100  		bat.AddVector(
   101  			attr,
   102  			containers.MakeVector(typs[i], mp),
   103  		)
   104  	}
   105  	return bat
   106  }
   107  
   108  // GetDataWindowForLogtail returns the batch according to the writeSchema.
   109  // columns are sorted by seqnum and vacancy is filled with zero value
   110  func DataChangeToLogtailBatch(src *containers.BatchWithVersion) *containers.Batch {
   111  	seqnums := src.Seqnums
   112  	if len(seqnums) != len(src.Vecs) {
   113  		panic("unmatched seqnums length")
   114  	}
   115  
   116  	// sort by seqnum
   117  	sort.Sort(src)
   118  
   119  	bat := containers.NewBatchWithCapacity(int(src.NextSeqnum) + 2)
   120  	if src.Deletes != nil {
   121  		bat.Deletes = src.Deletes
   122  	}
   123  
   124  	i := len(src.Seqnums) - 1
   125  	// move special column first, no abort column in logtail
   126  	if src.Seqnums[i] != objectio.SEQNUM_ROWID || src.Seqnums[i-1] != objectio.SEQNUM_COMMITTS {
   127  		panic(fmt.Sprintf("bad last seqnums %v", src.Seqnums))
   128  	}
   129  	bat.AddVector(src.Attrs[i], src.Vecs[i])
   130  	bat.AddVector(src.Attrs[i-1], src.Vecs[i-1])
   131  
   132  	for i, seqnum := range seqnums {
   133  		if seqnum >= objectio.SEQNUM_UPPER {
   134  			// two special column has been moved
   135  			continue
   136  		}
   137  		for len(bat.Vecs) < 2+int(seqnum) {
   138  			bat.AppendPlaceholder()
   139  		}
   140  		bat.AddVector(src.Attrs[i], src.Vecs[i].TryConvertConst())
   141  	}
   142  	return bat
   143  }
   144  
   145  func containersBatchToProtoBatch(bat *containers.Batch) (*api.Batch, error) {
   146  	mobat := containers.ToCNBatch(bat)
   147  	return batch.BatchToProtoBatch(mobat)
   148  }