github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/ctl/table_stat_test.go (about) 1 // Copyright 2021 - 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 ctl 16 17 import ( 18 "context" 19 "github.com/golang/mock/gomock" 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/defines" 23 mock_frontend "github.com/matrixorigin/matrixone/pkg/frontend/test" 24 "github.com/matrixorigin/matrixone/pkg/testutil" 25 "github.com/matrixorigin/matrixone/pkg/vm/engine" 26 "github.com/stretchr/testify/require" 27 "testing" 28 "time" 29 ) 30 31 func TestMoTableRowsAndTableSize(t *testing.T) { 32 33 v1 := testutil.MakeVarcharVector([]string{""}, []uint64{}) 34 v2 := testutil.MakeVarcharVector([]string{""}, []uint64{}) 35 36 inputVectors := []*vector.Vector{v1, v2} 37 ctrl := gomock.NewController(t) 38 defer ctrl.Finish() 39 40 proc := testutil.NewProc() 41 txnOperator := mock_frontend.NewMockTxnOperator(ctrl) 42 txnOperator.EXPECT().Commit(gomock.Any()).Return(nil).AnyTimes() 43 txnOperator.EXPECT().Rollback(gomock.Any()).Return(nil).AnyTimes() 44 45 txnClient := mock_frontend.NewMockTxnClient(ctrl) 46 txnClient.EXPECT().New().Return(txnOperator, nil).AnyTimes() 47 48 db := mock_frontend.NewMockDatabase(ctrl) 49 db.EXPECT().Relations(gomock.Any()).Return(nil, nil).AnyTimes() 50 51 table := mock_frontend.NewMockRelation(ctrl) 52 table.EXPECT().Ranges(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() 53 table.EXPECT().TableDefs(gomock.Any()).Return(nil, nil).AnyTimes() 54 table.EXPECT().GetPrimaryKeys(gomock.Any()).Return(nil, nil).AnyTimes() 55 table.EXPECT().GetHideKeys(gomock.Any()).Return(nil, nil).AnyTimes() 56 table.EXPECT().Stats(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() 57 58 attrs := []*engine.Attribute{ 59 {Name: "a", Type: types.T_int32.ToType()}, 60 {Name: "b", Type: types.T_int64.ToType()}, 61 } 62 63 table.EXPECT().TableColumns(gomock.Any()).Return(attrs, nil).AnyTimes() 64 65 table.EXPECT().GetTableID(gomock.Any()).Return(uint64(10)).AnyTimes() 66 table.EXPECT().Rows(gomock.Any()).Return(int64(10), nil).AnyTimes() 67 table.EXPECT().Size(gomock.Any(), gomock.Any()).Return(int64(0), nil).AnyTimes() 68 db.EXPECT().Relation(gomock.Any(), gomock.Any()).Return(table, nil).AnyTimes() 69 70 eng := mock_frontend.NewMockEngine(ctrl) 71 eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes() 72 eng.EXPECT().Commit(gomock.Any(), gomock.Any()).Return(nil).AnyTimes() 73 eng.EXPECT().Rollback(gomock.Any(), gomock.Any()).Return(nil).AnyTimes() 74 eng.EXPECT().Hints().Return(engine.Hints{ 75 CommitOrRollbackTimeout: time.Second, 76 }).AnyTimes() 77 eng.EXPECT().Database(gomock.Any(), gomock.Any(), gomock.Any()).Return(db, nil).AnyTimes() 78 proc.Ctx = context.WithValue(proc.Ctx, defines.EngineKey{}, eng) 79 proc.TxnClient = txnClient 80 81 r, err := MoTableRows(inputVectors, proc) 82 require.Nil(t, err) 83 require.Equal(t, "10", r.String()) 84 85 size, err := MoTableSize(inputVectors, proc) 86 require.Nil(t, err) 87 require.Equal(t, "120", size.String()) 88 89 }