github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/chunk/row.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package chunk 15 16 import ( 17 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 18 "github.com/whtcorpsinc/milevadb/types" 19 "github.com/whtcorpsinc/milevadb/types/json" 20 ) 21 22 // Row represents a event of data, can be used to access values. 23 type Row struct { 24 c *Chunk 25 idx int 26 } 27 28 // Chunk returns the Chunk which the event belongs to. 29 func (r Row) Chunk() *Chunk { 30 return r.c 31 } 32 33 // IsEmpty returns true if the Row is empty. 34 func (r Row) IsEmpty() bool { 35 return r == Row{} 36 } 37 38 // Idx returns the event index of Chunk. 39 func (r Row) Idx() int { 40 return r.idx 41 } 42 43 // Len returns the number of values in the event. 44 func (r Row) Len() int { 45 return r.c.NumDefCauss() 46 } 47 48 // GetInt64 returns the int64 value with the defCausIdx. 49 func (r Row) GetInt64(defCausIdx int) int64 { 50 return r.c.defCausumns[defCausIdx].GetInt64(r.idx) 51 } 52 53 // GetUint64 returns the uint64 value with the defCausIdx. 54 func (r Row) GetUint64(defCausIdx int) uint64 { 55 return r.c.defCausumns[defCausIdx].GetUint64(r.idx) 56 } 57 58 // GetFloat32 returns the float32 value with the defCausIdx. 59 func (r Row) GetFloat32(defCausIdx int) float32 { 60 return r.c.defCausumns[defCausIdx].GetFloat32(r.idx) 61 } 62 63 // GetFloat64 returns the float64 value with the defCausIdx. 64 func (r Row) GetFloat64(defCausIdx int) float64 { 65 return r.c.defCausumns[defCausIdx].GetFloat64(r.idx) 66 } 67 68 // GetString returns the string value with the defCausIdx. 69 func (r Row) GetString(defCausIdx int) string { 70 return r.c.defCausumns[defCausIdx].GetString(r.idx) 71 } 72 73 // GetBytes returns the bytes value with the defCausIdx. 74 func (r Row) GetBytes(defCausIdx int) []byte { 75 return r.c.defCausumns[defCausIdx].GetBytes(r.idx) 76 } 77 78 // GetTime returns the Time value with the defCausIdx. 79 func (r Row) GetTime(defCausIdx int) types.Time { 80 return r.c.defCausumns[defCausIdx].GetTime(r.idx) 81 } 82 83 // GetDuration returns the Duration value with the defCausIdx. 84 func (r Row) GetDuration(defCausIdx int, fillFsp int) types.Duration { 85 return r.c.defCausumns[defCausIdx].GetDuration(r.idx, fillFsp) 86 } 87 88 func (r Row) getNameValue(defCausIdx int) (string, uint64) { 89 return r.c.defCausumns[defCausIdx].getNameValue(r.idx) 90 } 91 92 // GetEnum returns the Enum value with the defCausIdx. 93 func (r Row) GetEnum(defCausIdx int) types.Enum { 94 return r.c.defCausumns[defCausIdx].GetEnum(r.idx) 95 } 96 97 // GetSet returns the Set value with the defCausIdx. 98 func (r Row) GetSet(defCausIdx int) types.Set { 99 return r.c.defCausumns[defCausIdx].GetSet(r.idx) 100 } 101 102 // GetMyDecimal returns the MyDecimal value with the defCausIdx. 103 func (r Row) GetMyDecimal(defCausIdx int) *types.MyDecimal { 104 return r.c.defCausumns[defCausIdx].GetDecimal(r.idx) 105 } 106 107 // GetJSON returns the JSON value with the defCausIdx. 108 func (r Row) GetJSON(defCausIdx int) json.BinaryJSON { 109 return r.c.defCausumns[defCausIdx].GetJSON(r.idx) 110 } 111 112 // GetCausetRow converts chunk.Row to types.CausetRow. 113 // Keep in mind that GetCausetRow has a reference to r.c, which is a chunk, 114 // this function works only if the underlying chunk is valid or unchanged. 115 func (r Row) GetCausetRow(fields []*types.FieldType) []types.Causet { 116 datumRow := make([]types.Causet, 0, r.c.NumDefCauss()) 117 for defCausIdx := 0; defCausIdx < r.c.NumDefCauss(); defCausIdx++ { 118 causet := r.GetCauset(defCausIdx, fields[defCausIdx]) 119 datumRow = append(datumRow, causet) 120 } 121 return datumRow 122 } 123 124 // GetCauset implements the chunk.Row interface. 125 func (r Row) GetCauset(defCausIdx int, tp *types.FieldType) types.Causet { 126 var d types.Causet 127 switch tp.Tp { 128 case allegrosql.TypeTiny, allegrosql.TypeShort, allegrosql.TypeInt24, allegrosql.TypeLong, allegrosql.TypeLonglong: 129 if !r.IsNull(defCausIdx) { 130 if allegrosql.HasUnsignedFlag(tp.Flag) { 131 d.SetUint64(r.GetUint64(defCausIdx)) 132 } else { 133 d.SetInt64(r.GetInt64(defCausIdx)) 134 } 135 } 136 case allegrosql.TypeYear: 137 // FIXBUG: because insert type of TypeYear is definite int64, so we regardless of the unsigned flag. 138 if !r.IsNull(defCausIdx) { 139 d.SetInt64(r.GetInt64(defCausIdx)) 140 } 141 case allegrosql.TypeFloat: 142 if !r.IsNull(defCausIdx) { 143 d.SetFloat32(r.GetFloat32(defCausIdx)) 144 } 145 case allegrosql.TypeDouble: 146 if !r.IsNull(defCausIdx) { 147 d.SetFloat64(r.GetFloat64(defCausIdx)) 148 } 149 case allegrosql.TypeVarchar, allegrosql.TypeVarString, allegrosql.TypeString: 150 if !r.IsNull(defCausIdx) { 151 d.SetString(r.GetString(defCausIdx), tp.DefCauslate) 152 } 153 case allegrosql.TypeBlob, allegrosql.TypeTinyBlob, allegrosql.TypeMediumBlob, allegrosql.TypeLongBlob: 154 if !r.IsNull(defCausIdx) { 155 d.SetBytes(r.GetBytes(defCausIdx)) 156 } 157 case allegrosql.TypeDate, allegrosql.TypeDatetime, allegrosql.TypeTimestamp: 158 if !r.IsNull(defCausIdx) { 159 d.SetMysqlTime(r.GetTime(defCausIdx)) 160 } 161 case allegrosql.TypeDuration: 162 if !r.IsNull(defCausIdx) { 163 duration := r.GetDuration(defCausIdx, tp.Decimal) 164 d.SetMysqlDuration(duration) 165 } 166 case allegrosql.TypeNewDecimal: 167 if !r.IsNull(defCausIdx) { 168 d.SetMysqlDecimal(r.GetMyDecimal(defCausIdx)) 169 d.SetLength(tp.Flen) 170 // If tp.Decimal is unspecified(-1), we should set it to the real 171 // fraction length of the decimal value, if not, the d.Frac will 172 // be set to MAX_UINT16 which will cause unexpected BadNumber error 173 // when encoding. 174 if tp.Decimal == types.UnspecifiedLength { 175 d.SetFrac(d.Frac()) 176 } else { 177 d.SetFrac(tp.Decimal) 178 } 179 } 180 case allegrosql.TypeEnum: 181 if !r.IsNull(defCausIdx) { 182 d.SetMysqlEnum(r.GetEnum(defCausIdx), tp.DefCauslate) 183 } 184 case allegrosql.TypeSet: 185 if !r.IsNull(defCausIdx) { 186 d.SetMysqlSet(r.GetSet(defCausIdx), tp.DefCauslate) 187 } 188 case allegrosql.TypeBit: 189 if !r.IsNull(defCausIdx) { 190 d.SetMysqlBit(r.GetBytes(defCausIdx)) 191 } 192 case allegrosql.TypeJSON: 193 if !r.IsNull(defCausIdx) { 194 d.SetMysqlJSON(r.GetJSON(defCausIdx)) 195 } 196 } 197 return d 198 } 199 200 // GetRaw returns the underlying raw bytes with the defCausIdx. 201 func (r Row) GetRaw(defCausIdx int) []byte { 202 return r.c.defCausumns[defCausIdx].GetRaw(r.idx) 203 } 204 205 // IsNull returns if the causet in the chunk.Row is null. 206 func (r Row) IsNull(defCausIdx int) bool { 207 return r.c.defCausumns[defCausIdx].IsNull(r.idx) 208 } 209 210 // CopyConstruct creates a new event and copies this event's data into it. 211 func (r Row) CopyConstruct() Row { 212 newChk := renewWithCapacity(r.c, 1, 1) 213 newChk.AppendRow(r) 214 return newChk.GetRow(0) 215 }