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