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

cmd/explaintest: fix wrong result comparison for explain test #30717

Merged
merged 7 commits into from
Dec 15, 2021
Merged
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
43 changes: 40 additions & 3 deletions cmd/explaintest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ type tester struct {
resultFD *os.File
// ctx is used for Compile sql statement
ctx sessionctx.Context

// outputLen, lastQuery, lastLine and lastResult are used for checking the correctness of last query.
// See https://github.com/pingcap/tidb/issues/29475 for details.
outputLen int
lastText string
lastQuery query
lastResult []byte
}

func newTester(name string) *tester {
Expand Down Expand Up @@ -172,6 +179,10 @@ LOOP:
}
}

if err = t.checkLastResult(); err != nil {
return errors.Annotate(err, fmt.Sprintf("sql:%v", t.lastQuery.Query))
}

return t.flushResult()
}

Expand Down Expand Up @@ -365,7 +376,7 @@ func (t *tester) execute(query query) error {
t.singleQuery = false

if err != nil {
return errors.Trace(errors.Errorf("run \"%v\" at line %d err %v", st.Text(), query.Line, err))
return errors.Trace(errors.Errorf("run \"%v\" at line %d err %v", qText, query.Line, err))
}

if !record && !create {
Expand All @@ -374,11 +385,15 @@ func (t *tester) execute(query query) error {

buf := make([]byte, t.buf.Len()-offset)
if _, err = t.resultFD.ReadAt(buf, int64(offset)); !(err == nil || err == io.EOF) {
return errors.Trace(errors.Errorf("run \"%v\" at line %d err, we got \n%s\nbut read result err %s", st.Text(), query.Line, gotBuf, err))
return errors.Trace(errors.Errorf("run \"%v\" at line %d err, we got \n%s\nbut read result err %s", qText, query.Line, gotBuf, err))
}
if !bytes.Equal(gotBuf, buf) {
return errors.Trace(errors.Errorf("run \"%v\" at line %d err, we need:\n%s\nbut got:\n%s\n", query.Query, query.Line, buf, gotBuf))
return errors.Trace(errors.Errorf("run \"%v\" at line %d err, we need:\n%s\nbut got:\n%s\n", qText, query.Line, buf, gotBuf))
}
t.outputLen = t.buf.Len()
t.lastText = qText
t.lastQuery = query
t.lastResult = gotBuf
}
}
return errors.Trace(err)
Expand Down Expand Up @@ -566,6 +581,28 @@ func (t *tester) resultFileName() string {
return fmt.Sprintf("./r/%s.result", t.name)
}

func (t *tester) checkLastResult() error {
if record || create || t.outputLen == 0 {
return nil
}
fi, err := t.resultFD.Stat()
if err != nil {
return err
}
size := fi.Size()
if size == int64(t.outputLen) {
return nil
}
buf := make([]byte, int(size)-t.outputLen+len(t.lastResult))
if _, err = t.resultFD.ReadAt(buf, int64(t.outputLen-len(t.lastResult))); !(err == nil || err == io.EOF) {
return errors.Trace(errors.Errorf("run \"%v\" at line %d err, we got \n%s\nbut read result err %s", t.lastText, t.lastQuery.Line, t.lastResult, err))
}
if !bytes.Equal(t.lastResult, buf) {
return errors.Trace(errors.Errorf("run \"%v\" at line %d err, we need:\n%s\nbut got:\n%s\n", t.lastText, t.lastQuery.Line, buf, t.lastResult))
}
return nil
}

func loadAllTests() ([]string, error) {
// tests must be in t folder
files, err := os.ReadDir("./t")
Expand Down