Skip to content

Commit e48304e

Browse files
committed
Merge branch 'main' into fulghum/dolt-5735
2 parents 77d2616 + 5f335ee commit e48304e

File tree

9 files changed

+90
-197
lines changed

9 files changed

+90
-197
lines changed

enginetest/enginetests.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,10 @@ func TestRecursiveViewDefinition(t *testing.T, harness Harness) {
18971897
db, err := e.Analyzer.Catalog.Database(ctx, "mydb")
18981898
require.NoError(t, err)
18991899

1900+
if pdb, ok := db.(mysql_db.PrivilegedDatabase); ok {
1901+
db = pdb.Unwrap()
1902+
}
1903+
19001904
vdb, ok := db.(sql.ViewDatabase)
19011905
require.True(t, ok, "expected sql.ViewDatabase")
19021906

enginetest/queries/script_queries.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,35 +2902,6 @@ var ScriptTests = []ScriptTest{
29022902
},
29032903
},
29042904
},
2905-
{
2906-
Name: "rename views with RENAME TABLE ... TO .. statement",
2907-
SetUpScript: []string{
2908-
"create table t1 (id int primary key, v1 int);",
2909-
"create view v1 as select * from t1;",
2910-
},
2911-
Assertions: []ScriptTestAssertion{
2912-
{
2913-
Query: "show tables;",
2914-
Expected: []sql.Row{{"myview"}, {"t1"}, {"v1"}},
2915-
},
2916-
{
2917-
Query: "rename table v1 to view1",
2918-
Expected: []sql.Row{{types.OkResult{RowsAffected: 0}}},
2919-
},
2920-
{
2921-
Query: "show tables;",
2922-
Expected: []sql.Row{{"myview"}, {"t1"}, {"view1"}},
2923-
},
2924-
{
2925-
Query: "rename table view1 to newViewName, t1 to newTableName",
2926-
Expected: []sql.Row{{types.OkResult{RowsAffected: 0}}},
2927-
},
2928-
{
2929-
Query: "show tables;",
2930-
Expected: []sql.Row{{"myview"}, {"newTableName"}, {"newViewName"}},
2931-
},
2932-
},
2933-
},
29342905
}
29352906

29362907
var SpatialScriptTests = []ScriptTest{

sql/analyzer/resolve_views.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919

2020
"github.com/dolthub/go-mysql-server/sql"
21+
"github.com/dolthub/go-mysql-server/sql/mysql_db"
2122
"github.com/dolthub/go-mysql-server/sql/parse"
2223
"github.com/dolthub/go-mysql-server/sql/plan"
2324
"github.com/dolthub/go-mysql-server/sql/transform"
@@ -50,7 +51,11 @@ func resolveViews(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel R
5051
return nil, transform.SameTree, err
5152
}
5253

53-
if vdb, vok := db.(sql.ViewDatabase); vok {
54+
maybeVdb := db
55+
if privilegedDatabase, ok := maybeVdb.(mysql_db.PrivilegedDatabase); ok {
56+
maybeVdb = privilegedDatabase.Unwrap()
57+
}
58+
if vdb, vok := maybeVdb.(sql.ViewDatabase); vok {
5459
viewDef, vdok, verr := vdb.GetViewDefinition(ctx, viewName)
5560
if verr != nil {
5661
return nil, transform.SameTree, verr

sql/errors.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,6 @@ var (
555555
// are automatically rolled back. Clients receiving this error must retry the transaction.
556556
ErrLockDeadlock = errors.NewKind("serialization failure: %s, try restarting transaction.")
557557

558-
// ErrViewsNotSupported is returned when attempting to access a view on a database that doesn't support them.
559-
ErrViewsNotSupported = errors.NewKind("database '%s' doesn't support views")
560-
561558
// ErrExistingView is returned when a CREATE VIEW statement uses a name that already exists
562559
ErrExistingView = errors.NewKind("the view %s.%s already exists")
563560

sql/information_schema/information_schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,6 +2793,9 @@ func viewsInDatabase(ctx *Context, db Database) ([]ViewDefinition, error) {
27932793
var views []ViewDefinition
27942794
dbName := db.Name()
27952795

2796+
if privilegedDatabase, ok := db.(mysql_db.PrivilegedDatabase); ok {
2797+
db = privilegedDatabase.Unwrap()
2798+
}
27962799
if vdb, ok := db.(ViewDatabase); ok {
27972800
dbViews, err := vdb.AllViews(ctx)
27982801
if err != nil {

sql/mysql_db/privileged_database_provider.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ var _ sql.TableCopierDatabase = PrivilegedDatabase{}
116116
var _ sql.ReadOnlyDatabase = PrivilegedDatabase{}
117117
var _ sql.TemporaryTableDatabase = PrivilegedDatabase{}
118118
var _ sql.CollatedDatabase = PrivilegedDatabase{}
119-
var _ sql.ViewDatabase = PrivilegedDatabase{}
120119

121120
// NewPrivilegedDatabase returns a new PrivilegedDatabase.
122121
func NewPrivilegedDatabase(grantTables *MySQLDb, db sql.Database) sql.Database {
@@ -385,38 +384,6 @@ func (pdb PrivilegedDatabase) UpdateEvent(ctx *sql.Context, ed sql.EventDefiniti
385384
return sql.ErrEventsNotSupported.New(pdb.db.Name())
386385
}
387386

388-
// CreateView implements sql.ViewDatabase
389-
func (pdb PrivilegedDatabase) CreateView(ctx *sql.Context, name string, selectStatement, createViewStmt string) error {
390-
if db, ok := pdb.db.(sql.ViewDatabase); ok {
391-
return db.CreateView(ctx, name, selectStatement, createViewStmt)
392-
}
393-
return sql.ErrViewsNotSupported.New(pdb.db.Name())
394-
}
395-
396-
// DropView implements sql.ViewDatabase
397-
func (pdb PrivilegedDatabase) DropView(ctx *sql.Context, name string) error {
398-
if db, ok := pdb.db.(sql.ViewDatabase); ok {
399-
return db.DropView(ctx, name)
400-
}
401-
return sql.ErrViewsNotSupported.New(pdb.db.Name())
402-
}
403-
404-
// GetViewDefinition implements sql.ViewDatabase
405-
func (pdb PrivilegedDatabase) GetViewDefinition(ctx *sql.Context, viewName string) (sql.ViewDefinition, bool, error) {
406-
if db, ok := pdb.db.(sql.ViewDatabase); ok {
407-
return db.GetViewDefinition(ctx, viewName)
408-
}
409-
return sql.ViewDefinition{}, false, sql.ErrViewsNotSupported.New(pdb.db.Name())
410-
}
411-
412-
// AllViews implements sql.ViewDatabase
413-
func (pdb PrivilegedDatabase) AllViews(ctx *sql.Context) ([]sql.ViewDefinition, error) {
414-
if db, ok := pdb.db.(sql.ViewDatabase); ok {
415-
return db.AllViews(ctx)
416-
}
417-
return nil, sql.ErrViewsNotSupported.New(pdb.db.Name())
418-
}
419-
420387
// CopyTableData implements the interface sql.TableCopierDatabase.
421388
func (pdb PrivilegedDatabase) CopyTableData(ctx *sql.Context, sourceTable string, destinationTable string) (uint64, error) {
422389
if db, ok := pdb.db.(sql.TableCopierDatabase); ok {

sql/plan/alter_table.go

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,6 @@ func (r *RenameTable) String() string {
5353
return fmt.Sprintf("Rename table %s to %s", r.OldNames, r.NewNames)
5454
}
5555

56-
func (r *RenameTable) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
57-
// TODO: 'ALTER TABLE newViewName RENAME view1' should fail on renaming views - should be fixed in vitess
58-
renamer, _ := r.Db.(sql.TableRenamer)
59-
viewDb, _ := r.Db.(sql.ViewDatabase)
60-
viewRegistry := ctx.GetViewRegistry()
61-
62-
for i, oldName := range r.OldNames {
63-
if tbl, exists := r.tableExists(ctx, oldName); exists {
64-
err := r.renameTable(ctx, renamer, tbl, oldName, r.NewNames[i])
65-
if err != nil {
66-
return nil, err
67-
}
68-
} else {
69-
success, err := r.renameView(ctx, viewDb, viewRegistry, oldName, r.NewNames[i])
70-
if err != nil {
71-
return nil, err
72-
} else if !success {
73-
return nil, sql.ErrTableNotFound.New(oldName)
74-
}
75-
}
76-
}
77-
78-
return sql.RowsToRowIter(sql.NewRow(types.NewOkResult(0))), nil
79-
}
80-
8156
func (r *RenameTable) WithChildren(children ...sql.Node) (sql.Node, error) {
8257
return NillaryWithChildren(r, children...)
8358
}
@@ -99,106 +74,6 @@ func (*RenameTable) CollationCoercibility(ctx *sql.Context) (collation sql.Colla
9974
return sql.Collation_binary, 7
10075
}
10176

102-
func (r *RenameTable) tableExists(ctx *sql.Context, name string) (sql.Table, bool) {
103-
tbl, ok, err := r.Db.GetTableInsensitive(ctx, name)
104-
if err != nil || !ok {
105-
return nil, false
106-
}
107-
return tbl, true
108-
}
109-
110-
func (r *RenameTable) renameTable(ctx *sql.Context, renamer sql.TableRenamer, tbl sql.Table, oldName, newName string) error {
111-
if renamer == nil {
112-
return sql.ErrRenameTableNotSupported.New(r.Db.Name())
113-
}
114-
115-
if fkTable, ok := tbl.(sql.ForeignKeyTable); ok {
116-
parentFks, err := fkTable.GetReferencedForeignKeys(ctx)
117-
if err != nil {
118-
return err
119-
}
120-
for _, parentFk := range parentFks {
121-
//TODO: support renaming tables across databases for foreign keys
122-
if strings.ToLower(parentFk.Database) != strings.ToLower(parentFk.ParentDatabase) {
123-
return fmt.Errorf("updating foreign key table names across databases is not yet supported")
124-
}
125-
parentFk.ParentTable = newName
126-
childTbl, ok, err := r.Db.GetTableInsensitive(ctx, parentFk.Table)
127-
if err != nil {
128-
return err
129-
}
130-
if !ok {
131-
return sql.ErrTableNotFound.New(parentFk.Table)
132-
}
133-
childFkTbl, ok := childTbl.(sql.ForeignKeyTable)
134-
if !ok {
135-
return fmt.Errorf("referenced table `%s` supports foreign keys but declaring table `%s` does not", parentFk.ParentTable, parentFk.Table)
136-
}
137-
err = childFkTbl.UpdateForeignKey(ctx, parentFk.Name, parentFk)
138-
if err != nil {
139-
return err
140-
}
141-
}
142-
143-
fks, err := fkTable.GetDeclaredForeignKeys(ctx)
144-
if err != nil {
145-
return err
146-
}
147-
for _, fk := range fks {
148-
fk.Table = newName
149-
err = fkTable.UpdateForeignKey(ctx, fk.Name, fk)
150-
if err != nil {
151-
return err
152-
}
153-
}
154-
}
155-
156-
err := renamer.RenameTable(ctx, oldName, newName)
157-
if err != nil {
158-
return err
159-
}
160-
161-
return nil
162-
}
163-
164-
func (r *RenameTable) renameView(ctx *sql.Context, viewDb sql.ViewDatabase, vr *sql.ViewRegistry, oldName, newName string) (bool, error) {
165-
if viewDb != nil {
166-
oldView, exists, err := viewDb.GetViewDefinition(ctx, oldName)
167-
if err != nil {
168-
return false, err
169-
} else if !exists {
170-
return false, nil
171-
}
172-
173-
err = viewDb.DropView(ctx, oldName)
174-
if err != nil {
175-
return false, err
176-
}
177-
178-
err = viewDb.CreateView(ctx, newName, oldView.TextDefinition, oldView.CreateViewStatement)
179-
if err != nil {
180-
return false, err
181-
}
182-
183-
return true, nil
184-
} else {
185-
view, exists := vr.View(r.Db.Name(), oldName)
186-
if !exists {
187-
return false, nil
188-
}
189-
190-
err := vr.Delete(r.Db.Name(), oldName)
191-
if err != nil {
192-
return false, nil
193-
}
194-
err = vr.Register(r.Db.Name(), sql.NewView(newName, view.Definition(), view.TextDefinition(), view.CreateStatement()))
195-
if err != nil {
196-
return false, nil
197-
}
198-
return true, nil
199-
}
200-
}
201-
20277
type AddColumn struct {
20378
ddlNode
20479
Table sql.Node

sql/rowexec/ddl.go

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,72 @@ func (b *BaseBuilder) buildDropCheck(ctx *sql.Context, n *plan.DropCheck, row sq
233233
}
234234

235235
func (b *BaseBuilder) buildRenameTable(ctx *sql.Context, n *plan.RenameTable, row sql.Row) (sql.RowIter, error) {
236-
return n.RowIter(ctx, row)
236+
renamer, ok := n.Db.(sql.TableRenamer)
237+
if !ok {
238+
return nil, sql.ErrRenameTableNotSupported.New(n.Db.Name())
239+
}
240+
241+
var err error
242+
for i, oldName := range n.OldNames {
243+
var tbl sql.Table
244+
var ok bool
245+
tbl, ok, err = n.Db.GetTableInsensitive(ctx, oldName)
246+
if err != nil {
247+
return nil, err
248+
}
249+
250+
if !ok {
251+
return nil, sql.ErrTableNotFound.New(oldName)
252+
}
253+
254+
if fkTable, ok := tbl.(sql.ForeignKeyTable); ok {
255+
parentFks, err := fkTable.GetReferencedForeignKeys(ctx)
256+
if err != nil {
257+
return nil, err
258+
}
259+
for _, parentFk := range parentFks {
260+
//TODO: support renaming tables across databases for foreign keys
261+
if strings.ToLower(parentFk.Database) != strings.ToLower(parentFk.ParentDatabase) {
262+
return nil, fmt.Errorf("updating foreign key table names across databases is not yet supported")
263+
}
264+
parentFk.ParentTable = n.NewNames[i]
265+
childTbl, ok, err := n.Db.GetTableInsensitive(ctx, parentFk.Table)
266+
if err != nil {
267+
return nil, err
268+
}
269+
if !ok {
270+
return nil, sql.ErrTableNotFound.New(parentFk.Table)
271+
}
272+
childFkTbl, ok := childTbl.(sql.ForeignKeyTable)
273+
if !ok {
274+
return nil, fmt.Errorf("referenced table `%s` supports foreign keys but declaring table `%s` does not", parentFk.ParentTable, parentFk.Table)
275+
}
276+
err = childFkTbl.UpdateForeignKey(ctx, parentFk.Name, parentFk)
277+
if err != nil {
278+
return nil, err
279+
}
280+
}
281+
282+
fks, err := fkTable.GetDeclaredForeignKeys(ctx)
283+
if err != nil {
284+
return nil, err
285+
}
286+
for _, fk := range fks {
287+
fk.Table = n.NewNames[i]
288+
err = fkTable.UpdateForeignKey(ctx, fk.Name, fk)
289+
if err != nil {
290+
return nil, err
291+
}
292+
}
293+
}
294+
295+
err = renamer.RenameTable(ctx, tbl.Name(), n.NewNames[i])
296+
if err != nil {
297+
return nil, err
298+
}
299+
}
300+
301+
return sql.RowsToRowIter(sql.NewRow(types.NewOkResult(0))), nil
237302
}
238303

239304
func (b *BaseBuilder) buildModifyColumn(ctx *sql.Context, n *plan.ModifyColumn, row sql.Row) (sql.RowIter, error) {
@@ -791,6 +856,7 @@ func (b *BaseBuilder) buildAlterDB(ctx *sql.Context, n *plan.AlterDB, row sql.Ro
791856

792857
func (b *BaseBuilder) buildCreateTable(ctx *sql.Context, n *plan.CreateTable, row sql.Row) (sql.RowIter, error) {
793858
var err error
859+
var vd sql.ViewDatabase
794860

795861
// If it's set to Invalid, then no collation has been explicitly defined
796862
if n.Collation == sql.Collation_Unspecified {
@@ -862,11 +928,13 @@ func (b *BaseBuilder) buildCreateTable(ctx *sql.Context, n *plan.CreateTable, ro
862928
return sql.RowsToRowIter(), err
863929
}
864930

865-
if vdb, vok := n.Db.(sql.ViewDatabase); vok {
866-
_, ok, err := vdb.GetViewDefinition(ctx, n.Name())
931+
vd, _ = maybePrivDb.(sql.ViewDatabase)
932+
if vd != nil {
933+
_, ok, err := vd.GetViewDefinition(ctx, n.Name())
867934
if err != nil {
868935
return nil, err
869936
}
937+
870938
if ok {
871939
return nil, sql.ErrTableAlreadyExists.New(n.Name())
872940
}

sql/rowexec/show.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,11 @@ func (b *BaseBuilder) buildShowTables(ctx *sql.Context, n *plan.ShowTables, row
193193
}
194194

195195
// TODO: currently there is no way to see views AS OF a particular time
196-
db := n.Database()
197-
if vdb, ok := db.(sql.ViewDatabase); ok {
196+
maybeVdb := n.Database()
197+
if privilegedDatabase, ok := maybeVdb.(mysql_db.PrivilegedDatabase); ok {
198+
maybeVdb = privilegedDatabase.Unwrap()
199+
}
200+
if vdb, ok := maybeVdb.(sql.ViewDatabase); ok {
198201
views, err := vdb.AllViews(ctx)
199202
if err != nil {
200203
return nil, err
@@ -208,7 +211,7 @@ func (b *BaseBuilder) buildShowTables(ctx *sql.Context, n *plan.ShowTables, row
208211
}
209212
}
210213

211-
for _, view := range ctx.GetViewRegistry().ViewsInDatabase(db.Name()) {
214+
for _, view := range ctx.GetViewRegistry().ViewsInDatabase(maybeVdb.Name()) {
212215
row := sql.Row{view.Name()}
213216
if n.Full {
214217
row = append(row, "VIEW")

0 commit comments

Comments
 (0)