github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/disttae/reader.go (about) 1 // Copyright 2022 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 disttae 16 17 import ( 18 "context" 19 "sort" 20 21 "github.com/matrixorigin/matrixone/pkg/catalog" 22 "github.com/matrixorigin/matrixone/pkg/common/moerr" 23 "github.com/matrixorigin/matrixone/pkg/common/mpool" 24 "github.com/matrixorigin/matrixone/pkg/container/batch" 25 "github.com/matrixorigin/matrixone/pkg/container/types" 26 "github.com/matrixorigin/matrixone/pkg/pb/plan" 27 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/dataio/blockio" 28 ) 29 30 func (r *emptyReader) Close() error { 31 return nil 32 } 33 34 func (r *emptyReader) Read(ctx context.Context, cols []string, expr *plan.Expr, m *mpool.MPool) (*batch.Batch, error) { 35 return nil, nil 36 } 37 38 func (r *blockReader) Close() error { 39 return nil 40 } 41 42 func (r *blockReader) Read(ctx context.Context, cols []string, _ *plan.Expr, m *mpool.MPool) (*batch.Batch, error) { 43 if len(r.blks) == 0 { 44 return nil, nil 45 } 46 defer func() { r.blks = r.blks[1:] }() 47 48 info := &r.blks[0].Info 49 50 if len(cols) != len(r.colIdxs) { 51 if len(r.colIdxs) == 0 { 52 r.colIdxs = make([]uint16, len(cols)) 53 r.colTypes = make([]types.Type, len(cols)) 54 r.colNulls = make([]bool, len(cols)) 55 r.pkidxInColIdxs = -1 56 for i, column := range cols { 57 // sometimes Name2ColIndex have no row_id, sometimes have one 58 if column == catalog.Row_ID { 59 if colIdx, ok := r.tableDef.Name2ColIndex[column]; ok { 60 r.colIdxs[i] = uint16(colIdx) 61 } else { 62 r.colIdxs[i] = uint16(len(r.tableDef.Name2ColIndex)) 63 } 64 r.colTypes[i] = types.T_Rowid.ToType() 65 } else { 66 r.colIdxs[i] = uint16(r.tableDef.Name2ColIndex[column]) 67 if r.colIdxs[i] == uint16(r.primaryIdx) { 68 r.pkidxInColIdxs = i 69 r.pkName = column 70 } 71 colDef := r.tableDef.Cols[r.colIdxs[i]] 72 r.colTypes[i] = types.T(colDef.Typ.Id).ToType() 73 if colDef.Default != nil { 74 r.colNulls[i] = colDef.Default.NullAbility 75 } 76 } 77 } 78 } else { 79 panic(moerr.NewInternalError(ctx, "blockReader reads different number of columns")) 80 } 81 } 82 83 bat, err := blockio.BlockRead(r.ctx, info, cols, r.colIdxs, r.colTypes, r.colNulls, r.tableDef, r.ts, r.fs, m) 84 if err != nil { 85 return nil, err 86 } 87 88 // if it's not sorted, just return 89 if !r.blks[0].Info.Sorted || r.pkidxInColIdxs == -1 || r.expr == nil { 90 return bat, nil 91 } 92 93 // if expr like : pkCol = xx, we will try to find(binary search) the row in batch 94 vec := bat.GetVector(int32(r.pkidxInColIdxs)) 95 canCompute, v := getPkValueByExpr(r.expr, r.pkName, vec.Typ.Oid) 96 if canCompute { 97 row := findRowByPkValue(vec, v) 98 if row >= vec.Length() { 99 // can not find row. 100 bat.Shrink([]int64{}) 101 } else if row > -1 { 102 // maybe find row. 103 bat.Shrink([]int64{int64(row)}) 104 } 105 } 106 return bat, nil 107 } 108 109 func (r *blockMergeReader) Close() error { 110 return nil 111 } 112 113 func (r *blockMergeReader) Read(ctx context.Context, cols []string, expr *plan.Expr, m *mpool.MPool) (*batch.Batch, error) { 114 if len(r.blks) == 0 { 115 r.sels = nil 116 return nil, nil 117 } 118 defer func() { r.blks = r.blks[1:] }() 119 info := &r.blks[0].meta.Info 120 121 if len(cols) != len(r.colIdxs) { 122 if len(r.colIdxs) == 0 { 123 r.colIdxs = make([]uint16, len(cols)) 124 r.colTypes = make([]types.Type, len(cols)) 125 r.colNulls = make([]bool, len(cols)) 126 for i, column := range cols { 127 // sometimes Name2ColIndex have no row_id, sometimes have one 128 if column == catalog.Row_ID { 129 if colIdx, ok := r.tableDef.Name2ColIndex[column]; ok { 130 r.colIdxs[i] = uint16(colIdx) 131 } else { 132 r.colIdxs[i] = uint16(len(r.tableDef.Name2ColIndex)) 133 } 134 r.colTypes[i] = types.T_Rowid.ToType() 135 } else { 136 r.colIdxs[i] = uint16(r.tableDef.Name2ColIndex[column]) 137 colDef := r.tableDef.Cols[r.colIdxs[i]] 138 r.colTypes[i] = types.T(colDef.Typ.Id).ToType() 139 if colDef.Default != nil { 140 r.colNulls[i] = colDef.Default.NullAbility 141 } 142 } 143 } 144 } else { 145 panic(moerr.NewInternalError(ctx, "blockReader reads different number of columns")) 146 } 147 } 148 149 bat, err := blockio.BlockRead(r.ctx, info, cols, r.colIdxs, r.colTypes, r.colNulls, r.tableDef, r.ts, r.fs, m) 150 if err != nil { 151 return nil, err 152 } 153 r.sels = r.sels[:0] 154 deletes := make([]int, len(r.blks[0].deletes)) 155 copy(deletes, r.blks[0].deletes) 156 sort.Ints(deletes) 157 for i := 0; i < bat.Length(); i++ { 158 if len(deletes) > 0 && i == deletes[0] { 159 deletes = deletes[1:] 160 continue 161 } 162 r.sels = append(r.sels, int64(i)) 163 } 164 bat.Shrink(r.sels) 165 return bat, nil 166 } 167 168 func (r *mergeReader) Close() error { 169 return nil 170 } 171 172 func (r *mergeReader) Read(ctx context.Context, cols []string, expr *plan.Expr, m *mpool.MPool) (*batch.Batch, error) { 173 if len(r.rds) == 0 { 174 return nil, nil 175 } 176 for len(r.rds) > 0 { 177 bat, err := r.rds[0].Read(ctx, cols, expr, m) 178 if err != nil { 179 for _, rd := range r.rds { 180 rd.Close() 181 } 182 return nil, err 183 } 184 if bat == nil { 185 r.rds = r.rds[1:] 186 } 187 if bat != nil { 188 return bat, nil 189 } 190 } 191 return nil, nil 192 }