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

executor: add placeholder count check in prepare stage #7162

Merged
merged 6 commits into from
Jul 29, 2018
Merged

executor: add placeholder count check in prepare stage #7162

merged 6 commits into from
Jul 29, 2018

Conversation

lysu
Copy link
Contributor

@lysu lysu commented Jul 26, 2018

What have you changed? (mandatory)

Now, client will meet error when input parameter placeholder count > 65535 in exec stage
this PR add placeholder num check in prepare stage to return error more quick and more evident and make it compatible with mysql.

What is the type of the changes? (mandatory)

  • Improvement (non-breaking change which is an improvement to an existing feature)

How has this PR been tested? (mandatory)

  • uint tests
  • manual tests

Does this PR affect documentation (docs/docs-cn) update? (mandatory)

no

Does this PR affect tidb-ansible update? (mandatory)

no

Does this PR need to be added to the release notes? (mandatory)

no

Refer to a related PR or issue link (optional)

Benchmark result if necessary (optional)

Add a few positive/negative examples (optional)

run

package main

import "database/sql"

import (
	"bytes"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/test")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	_, err = db.Exec("drop table if exists t")
	if err != nil {
		panic(err)
	}

	_, err = db.Exec("create table t (v int)")
	if err != nil {
		panic(err)
	}

	const pc = 65535 + 2

	var param [pc]interface{}
	param[0] = 0
	var sqlBuilder bytes.Buffer
	sqlBuilder.WriteString("insert into t values (?)")
	for i := 0; i < pc-1; i++ {
		sqlBuilder.WriteString(",(?)")
		param[i+1] = i
	}

	stmt, err := db.Prepare(sqlBuilder.String())
	if err != nil {
		panic(err)
	}

	result, err := stmt.Exec(param[:]...)
	if err != nil {
		panic(err)
	}

	fmt.Println(result)

}

expect:

panic: Error 1390: Prepared statement contains too many placeholders

goroutine 1 [running]:
main.main()
	test/main.go:41 +0x3d2

but got:

panic: sql: expected 1 arguments, got 65537

goroutine 1 [running]:
main.main()
	test/main.go:46 +0x3c2

This change is Reviewable

ddl/table.go Outdated
@@ -203,7 +203,8 @@ func onTruncateTable(t *meta.Meta, job *model.Job) (ver int64, _ error) {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
tblInfo, err := getTableInfo(t, job, schemaID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before rebase ...go tool vet will say this shadow err...but after rebase complain is disappeared, orz....

reverted..

@lysu
Copy link
Contributor Author

lysu commented Jul 26, 2018

/run-all-tests

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test case to cover this?

@@ -21,6 +21,8 @@ import (
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/testkit"
"golang.org/x/net/context"
"math"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move these 2 lines to line 17

@lysu
Copy link
Contributor Author

lysu commented Jul 27, 2018

PTAL @XuHuaiyu

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@winoros winoros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@lysu lysu added the status/LGT2 Indicates that a PR has LGTM 2. label Jul 27, 2018
@coocood coocood merged commit 4a203fd into pingcap:master Jul 29, 2018
@lysu lysu deleted the fix-prepared-stmt-too-many-placeholder-check branch September 27, 2018 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/LGT2 Indicates that a PR has LGTM 2. type/compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants