github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/query_builder_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 plan
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"testing"
    21  
    22  	"github.com/golang/mock/gomock"
    23  	"github.com/matrixorigin/matrixone/pkg/catalog"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    26  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestBuildTable_AlterView(t *testing.T) {
    31  	ctrl := gomock.NewController(t)
    32  	defer ctrl.Finish()
    33  	type arg struct {
    34  		obj   *ObjectRef
    35  		table *TableDef
    36  	}
    37  	store := make(map[string]arg)
    38  
    39  	store["db.a"] = arg{
    40  		&plan.ObjectRef{},
    41  		&plan.TableDef{
    42  			TableType: catalog.SystemOrdinaryRel,
    43  			Cols: []*ColDef{
    44  				{
    45  					Name: "a",
    46  					Typ: plan.Type{
    47  						Id:    int32(types.T_varchar),
    48  						Width: types.MaxVarcharLen,
    49  						Table: "a",
    50  					},
    51  				},
    52  			},
    53  		}}
    54  
    55  	vData, err := json.Marshal(ViewData{
    56  		"create view v as select a from a",
    57  		"db",
    58  	})
    59  	assert.NoError(t, err)
    60  
    61  	store["db.v"] = arg{nil,
    62  		&plan.TableDef{
    63  			TableType: catalog.SystemViewRel,
    64  			ViewSql: &plan.ViewDef{
    65  				View: string(vData),
    66  			}},
    67  	}
    68  	ctx := NewMockCompilerContext2(ctrl)
    69  	ctx.EXPECT().ResolveVariable(gomock.Any(), gomock.Any(), gomock.Any()).Return("", nil).AnyTimes()
    70  	ctx.EXPECT().Resolve(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(
    71  		func(schemaName string, tableName string, snapshot Snapshot) (*ObjectRef, *TableDef) {
    72  			if schemaName == "" {
    73  				schemaName = "db"
    74  			}
    75  			x := store[schemaName+"."+tableName]
    76  			return x.obj, x.table
    77  		}).AnyTimes()
    78  	ctx.EXPECT().GetContext().Return(context.Background()).AnyTimes()
    79  	ctx.EXPECT().GetProcess().Return(nil).AnyTimes()
    80  	ctx.EXPECT().Stats(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
    81  	ctx.EXPECT().GetBuildingAlterView().Return(true, "db", "v").AnyTimes()
    82  	ctx.EXPECT().DatabaseExists(gomock.Any(), gomock.Any()).Return(true).AnyTimes()
    83  
    84  	qb := NewQueryBuilder(plan.Query_SELECT, ctx, false)
    85  	tb := &tree.TableName{}
    86  	tb.SchemaName = "db"
    87  	tb.ObjectName = "v"
    88  	bc := NewBindContext(qb, nil)
    89  	_, err = qb.buildTable(tb, bc, -1, nil)
    90  	assert.Error(t, err)
    91  }