github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/show_account_test.go (about)

     1  // Copyright 2021 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 frontend
    16  
    17  import (
    18  	"context"
    19  	"math"
    20  	"testing"
    21  
    22  	"github.com/golang/mock/gomock"
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    27  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    28  	"github.com/matrixorigin/matrixone/pkg/container/types"
    29  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    30  	"github.com/matrixorigin/matrixone/pkg/sql/parsers"
    31  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
    32  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    33  )
    34  
    35  func Test_getSqlForAccountInfo(t *testing.T) {
    36  	type arg struct {
    37  		s    string
    38  		want string
    39  	}
    40  	args := []arg{
    41  		{
    42  			s:    "show accounts;",
    43  			want: "select account_id as `account_id`, account_name as `account_name`, created_time as `created`, status as `status`, suspended_time as `suspended_time`, comments as `comment` from mo_catalog.mo_account ;",
    44  		},
    45  		{
    46  			s:    "show accounts like '%abc';",
    47  			want: "select account_id as `account_id`, account_name as `account_name`, created_time as `created`, status as `status`, suspended_time as `suspended_time`, comments as `comment` from mo_catalog.mo_account where account_name like '%abc';",
    48  		},
    49  	}
    50  
    51  	for _, a := range args {
    52  		one, err := parsers.ParseOne(context.Background(), dialect.MYSQL, a.s, 1, 0)
    53  		assert.NoError(t, err)
    54  		sa1 := one.(*tree.ShowAccounts)
    55  		r1 := getSqlForAllAccountInfo(sa1.Like)
    56  		assert.Equal(t, a.want, r1)
    57  	}
    58  }
    59  
    60  func newAccountInfo(mp *mpool.MPool) (*batch.Batch, error) {
    61  	var err error
    62  	ret := batch.NewWithSize(idxOfComment + 1)
    63  	ret.Vecs[idxOfAccountId] = vector.NewVec(types.New(types.T_int32, 32, -1))
    64  	err = vector.AppendAny(ret.Vecs[idxOfAccountId], int32(0), false, mp)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	ret.Vecs[idxOfAccountName] = vector.NewVec(types.New(types.T_varchar, 300, 0))
    69  	err = vector.AppendAny(ret.Vecs[idxOfAccountName], []byte("acc"), false, mp)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	ret.Vecs[idxOfCreated] = vector.NewVec(types.New(types.T_timestamp, 8, 0))
    74  	err = vector.AppendAny(ret.Vecs[idxOfCreated], types.Timestamp(0), false, mp)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	ret.Vecs[idxOfStatus] = vector.NewVec(types.New(types.T_varchar, 300, 0))
    79  	err = vector.AppendAny(ret.Vecs[idxOfStatus], []byte("status"), false, mp)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	ret.Vecs[idxOfSuspendedTime] = vector.NewVec(types.New(types.T_timestamp, 8, 0))
    84  	err = vector.AppendAny(ret.Vecs[idxOfSuspendedTime], types.Timestamp(0), false, mp)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	ret.Vecs[idxOfComment] = vector.NewVec(types.New(types.T_varchar, 256, 0))
    89  	err = vector.AppendAny(ret.Vecs[idxOfComment], []byte("comment"), false, mp)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	return ret, nil
    94  }
    95  
    96  func newTableStatsResult(mp *mpool.MPool) (*batch.Batch, error) {
    97  	var err error
    98  	ret := batch.NewWithSize(idxOfComment + 1)
    99  	ret.Vecs[idxOfAdminName] = vector.NewVec(types.New(types.T_varchar, 300, 0))
   100  	err = vector.AppendAny(ret.Vecs[idxOfAdminName], []byte("name"), false, mp)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	ret.Vecs[idxOfDBCount] = vector.NewVec(types.New(types.T_int64, 8, 0))
   105  	err = vector.AppendAny(ret.Vecs[idxOfDBCount], int64(0), false, mp)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	ret.Vecs[idxOfTableCount] = vector.NewVec(types.New(types.T_int64, 8, 0))
   110  	err = vector.AppendAny(ret.Vecs[idxOfTableCount], int64(0), false, mp)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	ret.Vecs[idxOfSize] = vector.NewVec(types.New(types.T_decimal128, 29, 3))
   115  	err = vector.AppendAny(ret.Vecs[idxOfSize], types.Decimal128{}, false, mp)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	return ret, nil
   120  }
   121  
   122  func Test_mergeResult(t *testing.T) {
   123  	ctrl := gomock.NewController(t)
   124  	defer ctrl.Finish()
   125  	ses := newTestSession(t, ctrl)
   126  	defer ses.Close()
   127  
   128  	outputBatch := batch.NewWithSize(finalColumnCount)
   129  	accountInfo, err := newAccountInfo(ses.pool)
   130  	assert.Nil(t, err)
   131  	tableStatsResult, err := newTableStatsResult(ses.pool)
   132  	assert.Nil(t, err)
   133  
   134  	err = mergeOutputResult(ses, outputBatch, accountInfo, []*batch.Batch{tableStatsResult})
   135  	assert.Nil(t, err)
   136  }
   137  
   138  func Test_embeddingSizeToBatch(t *testing.T) {
   139  	ctrl := gomock.NewController(t)
   140  	defer ctrl.Finish()
   141  	ses := newTestSession(t, ctrl)
   142  	defer ses.Close()
   143  
   144  	bat := &batch.Batch{}
   145  	for i := 0; i <= finalColumnCount; i++ {
   146  		bat.Vecs = append(bat.Vecs, vector.NewVec(types.T_float64.ToType()))
   147  		vector.AppendFixed(bat.Vecs[i], float64(99), false, ses.pool)
   148  	}
   149  
   150  	size := uint64(1024 * 1024 * 11235)
   151  	embeddingSizeToBatch(bat, size, ses.pool)
   152  
   153  	require.Equal(t, math.Round(float64(size)/1048576.0*1e6)/1e6, vector.GetFixedAt[float64](bat.Vecs[idxOfSize], 0))
   154  }