Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow INSERT with all defaults #6969

Merged
merged 4 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions go/test/endtoend/vtgate/sequence/seq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ CREATE TABLE lookup_vindex (
keyspace_id BLOB,
UNIQUE KEY (c1, c2)
);
`

CREATE TABLE allDefaults (
id bigint NOT NULL,
foo varchar(255),
primary key (id)
);`

shardedVSchema = `
{
Expand Down Expand Up @@ -141,6 +146,18 @@ CREATE TABLE lookup_vindex (
"column": "id",
"sequence": "id_seq"
}
},
"allDefaults": {
"columnVindexes": [
{
"column": "id",
"name": "hash"
}
],
"autoIncrement": {
"column": "id",
"sequence": "id_seq"
}
},
"lookup_vindex": {
"columnVindexes": [
Expand Down Expand Up @@ -253,7 +270,6 @@ func TestSeq(t *testing.T) {
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("wrong insert: %v, must contain %s", err, want)
}

}

func TestDotTableSeq(t *testing.T) {
Expand All @@ -278,3 +294,25 @@ func TestDotTableSeq(t *testing.T) {
assert.Equal(t, "23000", mysqlErr.State)
assert.Contains(t, mysqlErr.Message, "Duplicate entry")
}

func TestInsertAllDefaults(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
DbName: shardedKeyspaceName,
}
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

// inserting into a table that has default values for all columns works well
exec(t, conn, `insert into allDefaults () values ()`)
result := exec(t, conn, `select * from uks.id_seq`)
assert.Equal(t, 1, len(result.Rows))

// inserting into a table that does not have default values for all columns fails
_, err = conn.ExecuteFetch("insert into lookup_vindex () values ()", 0, false)
require.Error(t, err)
}
37 changes: 34 additions & 3 deletions go/test/endtoend/vtgate/unsharded/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ CREATE TABLE t1 (
UNIQUE KEY (c3),
UNIQUE KEY (c4)
) ENGINE=Innodb;
`

CREATE TABLE allDefaults (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255)
) ENGINE=Innodb;`
VSchema = `
{
"sharded": false,
Expand All @@ -71,6 +75,18 @@ CREATE TABLE t1 (
"type": "VARCHAR"
}
]
},
"allDefaults": {
"columns": [
{
"name": "id",
"type": "INT64"
},
{
"name": "name",
"type": "VARCHAR"
}
]
}
}
}
Expand Down Expand Up @@ -146,7 +162,22 @@ func TestSelectIntoAndLoadFrom(t *testing.T) {
assertMatches(t, conn, `select c1,c2,c3 from t1`, `[[INT64(300) INT64(100) INT64(300)]]`)
}

func exec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result { //nolint:golint,unused
func TestInsertAllDefaults(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

exec(t, conn, `insert into allDefaults () values ()`)
assertMatches(t, conn, `select * from allDefaults`, "[[INT64(1) NULL]]")
}

func exec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result {
t.Helper()
qr, err := conn.ExecuteFetch(query, 1000, true)
require.NoError(t, err)
Expand All @@ -160,7 +191,7 @@ func execAssertError(t *testing.T, conn *mysql.Conn, query string, errorString s
assert.Contains(t, err.Error(), errorString)
}

func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) { //nolint:golint,unused
func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr := exec(t, conn, query)
got := fmt.Sprintf("%v", qr.Rows)
Expand Down
3 changes: 3 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ var (
}, {
input: "insert into user(format, tree, vitess) values ('Chuck', 42, 'Barry')",
output: "insert into user(`format`, `tree`, `vitess`) values ('Chuck', 42, 'Barry')",
}, {
input: "insert into customer () values ()",
output: "insert into customer values ()",
}, {
input: "update /* simple */ a set b = 3",
}, {
Expand Down
Loading