github.com/matrixorigin/matrixone@v1.2.0/pkg/util/executor/mem_executor.go (about) 1 // Copyright 2023 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 executor 16 17 import ( 18 "context" 19 20 "github.com/matrixorigin/matrixone/pkg/common/mpool" 21 "github.com/matrixorigin/matrixone/pkg/container/batch" 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/container/vector" 24 "github.com/matrixorigin/matrixone/pkg/txn/client" 25 ) 26 27 type memExecutor struct { 28 mocker func(sql string) (Result, error) 29 } 30 31 // NewMemExecutor used to testing 32 func NewMemExecutor(mocker func(sql string) (Result, error)) SQLExecutor { 33 return &memExecutor{mocker: mocker} 34 } 35 36 func (e *memExecutor) NewTxnOperator(_ context.Context) client.TxnOperator { 37 return nil 38 } 39 40 func (e *memExecutor) Exec( 41 ctx context.Context, 42 sql string, 43 opts Options) (Result, error) { 44 return e.mocker(sql) 45 } 46 47 func (e *memExecutor) ExecTxn( 48 ctx context.Context, 49 execFunc func(TxnExecutor) error, 50 opts Options) error { 51 te := &memTxnExecutor{mocker: e.mocker} 52 return execFunc(te) 53 } 54 55 type memTxnExecutor struct { 56 mocker func(sql string) (Result, error) 57 } 58 59 func (te *memTxnExecutor) Exec(sql string, _ StatementOption) (Result, error) { 60 return te.mocker(sql) 61 } 62 63 func (te *memTxnExecutor) Use(db string) { 64 65 } 66 67 func (te *memTxnExecutor) LockTable(table string) error { 68 return nil 69 } 70 71 func (te *memTxnExecutor) Txn() client.TxnOperator { 72 return nil 73 } 74 75 // MemResult used to test. Construct a Result from memory. 76 type MemResult struct { 77 cols int 78 types []types.Type 79 res Result 80 } 81 82 func NewMemResult( 83 types []types.Type, 84 mp *mpool.MPool) *MemResult { 85 return &MemResult{res: Result{mp: mp}, types: types, cols: len(types)} 86 } 87 88 func (m *MemResult) NewBatch() { 89 m.res.Batches = append(m.res.Batches, newBatch(m.cols)) 90 } 91 92 func (m *MemResult) GetResult() Result { 93 return m.res 94 } 95 96 func AppendStringRows(m *MemResult, col int, values []string) error { 97 bat := m.res.Batches[len(m.res.Batches)-1] 98 return appendStringCols(bat, col, m.types[col], values, m.res.mp) 99 } 100 101 func AppendBytesRows(m *MemResult, col int, values [][]byte) error { 102 bat := m.res.Batches[len(m.res.Batches)-1] 103 return appendBytesCols(bat, col, m.types[col], values, m.res.mp) 104 } 105 106 func AppendFixedRows[T any](m *MemResult, col int, values []T) error { 107 bat := m.res.Batches[len(m.res.Batches)-1] 108 return appendCols(bat, col, m.types[col], values, m.res.mp) 109 } 110 111 func newBatch(cols int) *batch.Batch { 112 bat := batch.NewWithSize(cols) 113 bat.SetRowCount(cols) 114 return bat 115 } 116 117 func appendCols[T any]( 118 bat *batch.Batch, 119 colIndex int, 120 tp types.Type, 121 values []T, 122 mp *mpool.MPool) error { 123 124 col := vector.NewVec(tp) 125 if err := vector.AppendFixedList(col, values, nil, mp); err != nil { 126 return err 127 } 128 bat.Vecs[colIndex] = col 129 return nil 130 } 131 132 func appendBytesCols( 133 bat *batch.Batch, 134 colIndex int, 135 tp types.Type, 136 values [][]byte, 137 mp *mpool.MPool) error { 138 139 col := vector.NewVec(tp) 140 for _, v := range values { 141 if err := vector.AppendBytes(col, v[:], false, mp); err != nil { 142 return err 143 } 144 } 145 bat.Vecs[colIndex] = col 146 return nil 147 } 148 149 func appendStringCols( 150 bat *batch.Batch, 151 colIndex int, 152 tp types.Type, 153 values []string, 154 mp *mpool.MPool) error { 155 156 col := vector.NewVec(tp) 157 for _, v := range values { 158 if err := vector.AppendBytes(col, []byte(v), false, mp); err != nil { 159 return err 160 } 161 } 162 bat.Vecs[colIndex] = col 163 return nil 164 }