Skip to content

Commit 7fdf5b3

Browse files
authored
have plan.EmptyTable implement sql.Updatable interface (#1885)
1 parent 37d77b9 commit 7fdf5b3

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed

enginetest/queries/script_queries.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,14 @@ var ScriptTests = []ScriptTest{
16961696
{12, 3},
16971697
},
16981698
},
1699+
{
1700+
Query: `update test2 inner join (select * from test3 order by val) as t3 on false SET test2.val=t3.val`,
1701+
Expected: []sql.Row{{types.OkResult{RowsAffected: 0, Info: plan.UpdateInfo{
1702+
Matched: 0,
1703+
Updated: 0,
1704+
Warnings: 0,
1705+
}}}},
1706+
},
16991707
},
17001708
},
17011709
{
@@ -3362,6 +3370,27 @@ var ScriptTests = []ScriptTest{
33623370
},
33633371
},
33643372
},
3373+
{
3374+
Name: "empty table update",
3375+
SetUpScript: []string{
3376+
"create table t (i int primary key)",
3377+
"insert into t values (1), (2), (3)",
3378+
},
3379+
Assertions: []ScriptTestAssertion{
3380+
{
3381+
Query: "update t set i = 0 where false",
3382+
Expected: []sql.Row{{types.OkResult{RowsAffected: 0, InsertID: 0, Info: plan.UpdateInfo{Matched: 0}}}},
3383+
},
3384+
{
3385+
Query: "select * from t",
3386+
Expected: []sql.Row{
3387+
{1},
3388+
{2},
3389+
{3},
3390+
},
3391+
},
3392+
},
3393+
},
33653394
}
33663395

33673396
var SpatialScriptTests = []ScriptTest{

sql/plan/empty_table.go

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
package plan
1616

17-
import "github.com/dolthub/go-mysql-server/sql"
17+
import (
18+
"io"
19+
20+
"github.com/dolthub/go-mysql-server/sql"
21+
)
1822

1923
func IsEmptyTable(n sql.Node) bool {
2024
_, ok := n.(*EmptyTable)
@@ -26,6 +30,7 @@ func NewEmptyTableWithSchema(schema sql.Schema) sql.Node {
2630

2731
var _ sql.Node = (*EmptyTable)(nil)
2832
var _ sql.CollationCoercible = (*EmptyTable)(nil)
33+
var _ sql.UpdatableTable = (*EmptyTable)(nil)
2934

3035
type EmptyTable struct {
3136
schema sql.Schema
@@ -37,11 +42,12 @@ func (*EmptyTable) Children() []sql.Node { return nil }
3742
func (*EmptyTable) Resolved() bool { return true }
3843
func (e *EmptyTable) String() string { return "EmptyTable" }
3944

45+
// RowIter implements the sql.Node interface.
4046
func (*EmptyTable) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
4147
return sql.RowsToRowIter(), nil
4248
}
4349

44-
// WithChildren implements the Node interface.
50+
// WithChildren implements the sql.Node interface.
4551
func (e *EmptyTable) WithChildren(children ...sql.Node) (sql.Node, error) {
4652
if len(children) != 0 {
4753
return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 0)
@@ -59,3 +65,78 @@ func (e *EmptyTable) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedO
5965
func (*EmptyTable) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
6066
return sql.Collation_binary, 7
6167
}
68+
69+
// Updater implements the sql.UpdatableTable interface.
70+
func (e *EmptyTable) Updater(ctx *sql.Context) sql.RowUpdater {
71+
return &emptyTableUpdater{}
72+
}
73+
74+
// Collation implements the sql.UpdatableTable interface.
75+
func (e *EmptyTable) Collation() sql.CollationID {
76+
return sql.Collation_Default
77+
}
78+
79+
// Partitions implements the sql.UpdatableTable interface.
80+
func (e *EmptyTable) Partitions(_ *sql.Context) (sql.PartitionIter, error) {
81+
return &emptyTablePartitionIter{}, nil
82+
}
83+
84+
// PartitionRows implements the sql.UpdatableTable interface.
85+
func (e *EmptyTable) PartitionRows(_ *sql.Context, _ sql.Partition) (sql.RowIter, error) {
86+
return &emptyTableIter{}, nil
87+
}
88+
89+
type emptyTableUpdater struct{}
90+
91+
var _ sql.RowUpdater = (*emptyTableUpdater)(nil)
92+
93+
// StatementBegin implements the sql.EditOpenerCloser interface
94+
func (e *emptyTableUpdater) StatementBegin(_ *sql.Context) {}
95+
96+
// DiscardChanges implements the sql.EditOpenerCloser interface
97+
func (e *emptyTableUpdater) DiscardChanges(_ *sql.Context, _ error) error {
98+
return nil
99+
}
100+
101+
// StatementComplete implements the sql.EditOpenerCloser interface
102+
func (e *emptyTableUpdater) StatementComplete(_ *sql.Context) error {
103+
return nil
104+
}
105+
106+
// Update implements the sql.RowUpdater interface
107+
func (e *emptyTableUpdater) Update(_ *sql.Context, _ sql.Row, _ sql.Row) error {
108+
return nil
109+
}
110+
111+
// Close implements the sql.Closer interface
112+
func (e *emptyTableUpdater) Close(_ *sql.Context) error {
113+
return nil
114+
}
115+
116+
type emptyTableIter struct{}
117+
118+
var _ sql.RowIter = (*emptyTableIter)(nil)
119+
120+
// Next implements the sql.RowIter interface.
121+
func (e *emptyTableIter) Next(_ *sql.Context) (sql.Row, error) {
122+
return nil, io.EOF
123+
}
124+
125+
// Close implements the sql.RowIter interface.
126+
func (e *emptyTableIter) Close(_ *sql.Context) error {
127+
return nil
128+
}
129+
130+
type emptyTablePartitionIter struct{}
131+
132+
var _ sql.PartitionIter = (*emptyTablePartitionIter)(nil)
133+
134+
// Close implements the sql.PartitionIter interface.
135+
func (e *emptyTablePartitionIter) Close(_ *sql.Context) error {
136+
return nil
137+
}
138+
139+
// Next implements the sql.PartitionIter interface.
140+
func (e *emptyTablePartitionIter) Next(_ *sql.Context) (sql.Partition, error) {
141+
return nil, io.EOF
142+
}

0 commit comments

Comments
 (0)