github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/common/print.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 common
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"time"
    21  
    22  	pkgcatalog "github.com/matrixorigin/matrixone/pkg/catalog"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  )
    25  
    26  type PPLevel int8
    27  
    28  const (
    29  	PPL0 PPLevel = iota
    30  	PPL1
    31  	PPL2
    32  	PPL3
    33  )
    34  
    35  func RepeatStr(str string, times int) string {
    36  	for i := 0; i < times; i++ {
    37  		str = fmt.Sprintf("%s\t", str)
    38  	}
    39  	return str
    40  }
    41  
    42  type opt struct {
    43  	doNotPrintBinary bool
    44  	specialRowid     bool // just a uint64, blockid
    45  }
    46  
    47  type TypePrintOpt interface {
    48  	apply(*opt)
    49  }
    50  
    51  type WithDoNotPrintBin struct{}
    52  
    53  func (w WithDoNotPrintBin) apply(o *opt) { o.doNotPrintBinary = true }
    54  
    55  type WithSpecialRowid struct{}
    56  
    57  func (w WithSpecialRowid) apply(o *opt) { o.specialRowid = true }
    58  
    59  func TypeStringValue(t types.Type, v any, opts ...TypePrintOpt) string {
    60  	if types.IsNull(v) {
    61  		return "null"
    62  	}
    63  	opt := &opt{}
    64  	for _, o := range opts {
    65  		o.apply(opt)
    66  	}
    67  
    68  	switch t.Oid {
    69  	case types.T_bool, types.T_int8, types.T_int16, types.T_int32,
    70  		types.T_int64, types.T_uint8, types.T_uint16, types.T_uint32,
    71  		types.T_uint64, types.T_float32, types.T_float64:
    72  		return fmt.Sprintf("%v", v)
    73  	case types.T_char, types.T_varchar, types.T_text, types.T_blob:
    74  		buf := v.([]byte)
    75  		printable := true
    76  		for _, c := range buf {
    77  			if !strconv.IsPrint(rune(c)) {
    78  				printable = false
    79  				break
    80  			}
    81  		}
    82  		if printable {
    83  			if len(buf) > 500 {
    84  				buf = buf[:500]
    85  			}
    86  			return string(buf)
    87  		} else if opt.doNotPrintBinary {
    88  			return fmt.Sprintf("binary[%d]", len(buf))
    89  		} else {
    90  			return fmt.Sprintf("%x", buf)
    91  		}
    92  	case types.T_date:
    93  		val := v.(types.Date)
    94  		return val.String()
    95  	case types.T_datetime:
    96  		val := v.(types.Datetime)
    97  		return val.String2(6)
    98  	case types.T_time:
    99  		val := v.(types.Time)
   100  		return val.String2(6)
   101  	case types.T_timestamp:
   102  		val := v.(types.Timestamp)
   103  		return val.String2(time.Local, 6)
   104  	case types.T_decimal64:
   105  		val := v.(types.Decimal64)
   106  		return val.String()
   107  	case types.T_decimal128:
   108  		val := v.(types.Decimal128)
   109  		return val.String()
   110  	case types.T_json:
   111  		val := v.([]byte)
   112  		j := types.DecodeJson(val)
   113  		return j.String()
   114  	case types.T_uuid:
   115  		val := v.(types.Uuid)
   116  		return val.ToString()
   117  	case types.T_TS:
   118  		val := v.(types.TS)
   119  		return val.ToString()
   120  	case types.T_Rowid:
   121  		val := v.(types.Rowid)
   122  		if opt.specialRowid {
   123  			return strconv.FormatUint(types.DecodeUint64(val[:]), 10)
   124  		} else {
   125  			val := v.(types.Rowid)
   126  			blkID, offset := pkgcatalog.DecodeRowid(val)
   127  			return fmt.Sprintf("BLK-%d:Off-%d", blkID, offset)
   128  		}
   129  	default:
   130  		return fmt.Sprintf("%v", v)
   131  	}
   132  }