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

fix: mismatch in column count and value count #14417

Merged
merged 12 commits into from
Nov 21, 2023
97 changes: 97 additions & 0 deletions go/test/endtoend/vtgate/queries/rowcolumncount/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package misc

import (
_ "embed"
"flag"
"fmt"
"os"
"testing"

"vitess.io/vitess/go/test/endtoend/utils"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
clusterInstance *cluster.LocalProcessCluster
vtParams mysql.ConnParams
mysqlParams mysql.ConnParams
keyspaceName = "ks_col_row"
cell = "zone"

//go:embed schema.sql
schemaSQL string

//go:embed vschema.json
vschema string
)

func TestMain(m *testing.M) {
defer cluster.PanicHandler(nil)
flag.Parse()

exitCode := func() int {
clusterInstance = cluster.NewCluster(cell, "localhost")
defer clusterInstance.Teardown()

// Start topo server
err := clusterInstance.StartTopo()
if err != nil {
return 1
}

clusterInstance.VtTabletExtraArgs = append(clusterInstance.VtTabletExtraArgs,
"--queryserver-config-max-result-size", "1000000",
"--queryserver-config-query-timeout", "200",
"--queryserver-config-query-pool-timeout", "200")

// Start keyspace
keyspace := &cluster.Keyspace{
Name: keyspaceName,
SchemaSQL: schemaSQL,
VSchema: vschema,
}
err = clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 0, false)
if err != nil {
return 1
}

clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs,
"--query-timeout", "100")
// Start vtgate
err = clusterInstance.StartVtgate()
if err != nil {
return 1
}

vtParams = clusterInstance.GetVTParams(keyspaceName)

// create mysql instance and connection parameters
conn, closer, err := utils.NewMySQL(clusterInstance, keyspaceName, schemaSQL)
if err != nil {
fmt.Println(err)
return 1
}
defer closer()
mysqlParams = conn
return m.Run()
}()
os.Exit(exitCode)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package misc

import (
"context"
"testing"

_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/require"
"vitess.io/vitess/go/vt/vterrors"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
"vitess.io/vitess/go/test/endtoend/utils"
FirePing32 marked this conversation as resolved.
Show resolved Hide resolved
)

func start(t *testing.T) (*mysql.Conn, func()) {
vtConn, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)

deleteAll := func() {
tables := []string{"music", "user"}
for _, table := range tables {
utils.Exec(t, vtConn, "delete from "+table)
}
}

deleteAll()

return vtConn, func() {
deleteAll()
vtConn.Close()
cluster.PanicHandler(t)
}
}

func TestExtraColLength(t *testing.T) {
vtconn, closer := start(t)
defer closer()

_, err := utils.ExecAllowError(t, vtconn, "insert into test (one, two, three, four) values (1, 2, 3)")

require.Equal(t, err, vterrors.VT03006())
}

func TestExtraRowLength(t *testing.T) {
vtconn, closer := start(t)
defer closer()

_, err := utils.ExecAllowError(t, vtconn, "insert into test (one, two, three) values (1, 2, 3, 4)")

require.Equal(t, err, vterrors.VT03006())
}
FirePing32 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions go/test/endtoend/vtgate/queries/rowcolumncount/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create table if not exists a(
one int,
two int,
three int,
four int,
five int,
primary key(one)
) Engine=InnoDB;
18 changes: 18 additions & 0 deletions go/test/endtoend/vtgate/queries/rowcolumncount/vschema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"sharded": true,
"vindexes": {
"hash": {
"type": "hash"
}
},
"tables": {
"a": {
"column_vindexes": [
{
"column": "one",
"name": "hash"
}
]
}
}
}
3 changes: 3 additions & 0 deletions go/vt/vtgate/planbuilder/operators/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ func modifyForAutoinc(ins *sqlparser.Insert, vTable *vindexes.Table) (*Generate,
case sqlparser.Values:
autoIncValues := make(sqlparser.ValTuple, 0, len(rows))
for rowNum, row := range rows {
if len(ins.Columns) != len(row) {
return nil, vterrors.VT03006()
}
// Support the DEFAULT keyword by treating it as null
if _, ok := row[colNum].(*sqlparser.Default); ok {
row[colNum] = &sqlparser.NullVal{}
Expand Down
58 changes: 58 additions & 0 deletions go/vt/vtgate/planbuilder/operators/insert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package operators

import (
"testing"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
)

func TestModifyForAutoinc(t *testing.T) {
var testCases []string = []string{
"insert into test (customer_id, order_date, total_amount, string) values (11, '2023-09-02', 200.00);",
"insert into test (customer_id, order_date, total_amount) values (11, '2023-09-02', 200.00, 'test');",
}
for _, tc := range testCases {
t.Run(tc, func(t *testing.T) {
ast, err := sqlparser.Parse(tc)
require.NoError(t, err)

insert := ast.(*sqlparser.Insert)
_, err = semantics.Analyze(insert, "", &semantics.FakeSI{})
require.NoError(t, err)

ctx := &plancontext.PlanningContext{SemTable: semantics.EmptySemTable()}

FirePing32 marked this conversation as resolved.
Show resolved Hide resolved
tableInfo, qt, err := createQueryTableForDML(ctx, insert.Table, nil)
require.NoError(t, err)

vindexTable, _, err := buildVindexTableForDML(ctx, tableInfo, qt, "insert")
require.NoError(t, err)

modifyInc, err := modifyForAutoinc(insert, vindexTable)

Check failure on line 52 in go/vt/vtgate/planbuilder/operators/insert_test.go

View workflow job for this annotation

GitHub Actions / Unit Test (Race)

not enough arguments in call to modifyForAutoinc

Check failure on line 52 in go/vt/vtgate/planbuilder/operators/insert_test.go

View workflow job for this annotation

GitHub Actions / Unit Test (mysql57)

not enough arguments in call to modifyForAutoinc

Check failure on line 52 in go/vt/vtgate/planbuilder/operators/insert_test.go

View workflow job for this annotation

GitHub Actions / Unit Test (mysql80)

not enough arguments in call to modifyForAutoinc

require.Equal(t, modifyInc, nil)
require.Equal(t, err, vterrors.VT03006())
})
}
}
Loading