github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/trace/vector.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 trace 16 17 import ( 18 "encoding/hex" 19 "fmt" 20 "strconv" 21 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/container/vector" 24 ) 25 26 func writeCompletedValue( 27 row []byte, 28 buf *buffer, 29 dst []byte) { 30 tuples, _, _, err := types.DecodeTuple(row) 31 if err != nil { 32 panic(err) 33 } 34 buf.buf.WriteString("[") 35 for i, t := range tuples { 36 switch v := t.(type) { 37 case bool: 38 if v { 39 buf.buf.WriteString("true") 40 } else { 41 buf.buf.WriteString("false") 42 } 43 case int8: 44 buf.buf.MustWrite(intToString(dst, int64(v))) 45 case int16: 46 buf.buf.MustWrite(intToString(dst, int64(v))) 47 case int32: 48 buf.buf.MustWrite(intToString(dst, int64(v))) 49 case int64: 50 buf.buf.MustWrite(intToString(dst, int64(v))) 51 case uint8: 52 buf.buf.MustWrite(uintToString(dst, uint64(v))) 53 case uint16: 54 buf.buf.MustWrite(uintToString(dst, uint64(v))) 55 case uint32: 56 buf.buf.MustWrite(uintToString(dst, uint64(v))) 57 case uint64: 58 buf.buf.MustWrite(uintToString(dst, uint64(v))) 59 case float32: 60 buf.buf.MustWrite(floatToString(dst, float64(v))) 61 case float64: 62 buf.buf.MustWrite(floatToString(dst, float64(v))) 63 case []byte: 64 buf.buf.MustWrite(v) 65 case types.Date: 66 buf.buf.MustWrite(intToString(dst, int64(v))) 67 case types.Time: 68 buf.buf.MustWrite(intToString(dst, int64(v))) 69 case types.Datetime: 70 buf.buf.MustWrite(intToString(dst, int64(v))) 71 case types.Timestamp: 72 buf.buf.MustWrite(intToString(dst, int64(v))) 73 case types.Decimal64: 74 buf.buf.MustWrite(uintToString(dst, uint64(v))) 75 default: 76 buf.buf.WriteString(fmt.Sprintf("%v", t)) 77 } 78 if i < len(tuples)-1 { 79 buf.buf.WriteString(",") 80 } 81 } 82 buf.buf.WriteString("]") 83 } 84 85 func writeValue( 86 vec *vector.Vector, 87 row int, 88 buf *buffer, 89 dst []byte) { 90 t := vec.GetType() 91 switch t.Oid { 92 case types.T_bool: 93 v := vector.MustFixedCol[bool](vec)[row] 94 if v { 95 buf.buf.WriteString("true") 96 } else { 97 buf.buf.WriteString("false") 98 } 99 case types.T_bit: 100 v := vector.MustFixedCol[uint64](vec)[row] 101 buf.buf.MustWrite(uintToString(dst, uint64(v))) 102 case types.T_int8: 103 v := vector.MustFixedCol[int8](vec)[row] 104 buf.buf.MustWrite(intToString(dst, int64(v))) 105 case types.T_int16: 106 v := vector.MustFixedCol[int16](vec)[row] 107 buf.buf.MustWrite(intToString(dst, int64(v))) 108 case types.T_int32: 109 v := vector.MustFixedCol[int32](vec)[row] 110 buf.buf.MustWrite(intToString(dst, int64(v))) 111 case types.T_int64: 112 v := vector.MustFixedCol[int64](vec)[row] 113 buf.buf.MustWrite(intToString(dst, int64(v))) 114 case types.T_uint8: 115 v := vector.MustFixedCol[uint8](vec)[row] 116 buf.buf.MustWrite(uintToString(dst, uint64(v))) 117 case types.T_uint16: 118 v := vector.MustFixedCol[uint16](vec)[row] 119 buf.buf.MustWrite(uintToString(dst, uint64(v))) 120 case types.T_uint32: 121 v := vector.MustFixedCol[uint32](vec)[row] 122 buf.buf.MustWrite(uintToString(dst, uint64(v))) 123 case types.T_uint64: 124 v := vector.MustFixedCol[uint64](vec)[row] 125 buf.buf.MustWrite(uintToString(dst, uint64(v))) 126 case types.T_float32: 127 v := vector.MustFixedCol[float32](vec)[row] 128 buf.buf.MustWrite(floatToString(dst, float64(v))) 129 case types.T_float64: 130 v := vector.MustFixedCol[float64](vec)[row] 131 buf.buf.MustWrite(floatToString(dst, float64(v))) 132 case types.T_date: 133 v := vector.MustFixedCol[types.Date](vec)[row] 134 buf.buf.MustWrite(intToString(dst, int64(v))) 135 case types.T_time: 136 v := vector.MustFixedCol[types.Time](vec)[row] 137 buf.buf.MustWrite(intToString(dst, int64(v))) 138 case types.T_datetime: 139 v := vector.MustFixedCol[types.Datetime](vec)[row] 140 buf.buf.MustWrite(intToString(dst, int64(v))) 141 case types.T_timestamp: 142 v := vector.MustFixedCol[types.Timestamp](vec)[row] 143 buf.buf.MustWrite(intToString(dst, int64(v))) 144 case types.T_decimal64: 145 v := vector.MustFixedCol[types.Decimal64](vec)[row] 146 buf.buf.MustWrite(uintToString(dst, uint64(v))) 147 case types.T_uuid: 148 v := vector.MustFixedCol[types.Uuid](vec)[row] 149 buf.buf.MustWrite(v[:]) 150 case types.T_char, types.T_varchar, types.T_binary: 151 data := vec.GetBytesAt(row) 152 buf.buf.MustWrite(data) 153 case types.T_enum: 154 v := vector.MustFixedCol[types.Enum](vec)[row] 155 buf.buf.MustWrite(uintToString(dst, uint64(v))) 156 case types.T_Rowid: 157 v := vector.MustFixedCol[types.Rowid](vec)[row] 158 buf.buf.WriteString(v.String()) 159 case types.T_TS: 160 v := vector.MustFixedCol[types.TS](vec)[row] 161 buf.buf.MustWrite(intToString(dst, int64(v.Physical()))) 162 buf.buf.WriteString("-") 163 buf.buf.MustWrite(intToString(dst, int64(v.Logical()))) 164 case types.T_Blockid: 165 v := vector.MustFixedCol[types.Blockid](vec)[row] 166 n := hex.EncodedLen(len(v[:])) 167 hex.Encode(dst[:n], v[:]) 168 buf.buf.MustWrite(dst[:n]) 169 default: 170 buf.buf.WriteString("not support") 171 } 172 } 173 174 func uintToString(dst []byte, v uint64) []byte { 175 return AppendUint(dst[:0], v, 10) 176 } 177 178 func intToString(dst []byte, v int64) []byte { 179 return AppendInt(dst[:0], v, 10) 180 } 181 182 func floatToString(dst []byte, v float64) []byte { 183 return strconv.AppendFloat(dst[:0], v, 'f', -1, 64) 184 }