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

dumpling: fix panic when query result is empty #45201

Merged
merged 1 commit into from
Jul 10, 2023
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
24 changes: 20 additions & 4 deletions dumpling/export/ir_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func (td *tableData) Start(tctx *tcontext.Context, conn *sql.Conn) error {
if err = rows.Err(); err != nil {
return errors.Annotatef(err, "sql: %s", td.query)
}
td.SQLRowIter = nil
td.rows = rows
if td.needColTypes {
ns, err := rows.Columns()
Expand All @@ -226,17 +227,27 @@ func (td *tableData) Start(tctx *tcontext.Context, conn *sql.Conn) error {
td.colTypes = append(td.colTypes, c.DatabaseTypeName())
}
}
td.SQLRowIter = newRowIter(rows, td.colLen)

return nil
}

func (td *tableData) Rows() SQLRowIter {
// should be initialized lazily since it calls rows.Next() which might close the rows when
// there's nothing to read, causes code which relies on rows not closed to fail.
if td.SQLRowIter == nil {
td.SQLRowIter = newRowIter(td.rows, td.colLen)
}
return td.SQLRowIter
}

func (td *tableData) Close() error {
return td.SQLRowIter.Close()
if td.SQLRowIter != nil {
// will close td.rows internally
return td.SQLRowIter.Close()
} else if td.rows != nil {
return td.rows.Close()
}
return nil
}

func (td *tableData) RawRows() *sql.Rows {
Expand Down Expand Up @@ -351,16 +362,21 @@ func newMultiQueriesChunk(queries []string, colLength int) *multiQueriesChunk {
func (td *multiQueriesChunk) Start(tctx *tcontext.Context, conn *sql.Conn) error {
td.tctx = tctx
td.conn = conn
td.SQLRowIter = newMultiQueryChunkIter(td.tctx, td.conn, td.queries, td.colLen)
return nil
}

func (td *multiQueriesChunk) Rows() SQLRowIter {
if td.SQLRowIter == nil {
td.SQLRowIter = newMultiQueryChunkIter(td.tctx, td.conn, td.queries, td.colLen)
}
return td.SQLRowIter
}

func (td *multiQueriesChunk) Close() error {
return td.SQLRowIter.Close()
if td.SQLRowIter != nil {
return td.SQLRowIter.Close()
}
return nil
}

func (*multiQueriesChunk) RawRows() *sql.Rows {
Expand Down
8 changes: 8 additions & 0 deletions dumpling/tests/basic/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@ export GO_FAILPOINTS="github.com/pingcap/tidb/dumpling/export/SetIOTotalBytes=re
run_dumpling -B "test_db" -L ${DUMPLING_OUTPUT_DIR}/dumpling.log
cnt=$(grep "IOTotalBytes=" ${DUMPLING_OUTPUT_DIR}/dumpling.log | grep -v "IOTotalBytes=0" | wc -l)
[ "$cnt" -ge 1 ]

export GO_FAILPOINTS=""
export DUMPLING_TEST_PORT=4000
echo "Test for empty query result, should success."
run_sql "drop database if exists test_db;"
run_sql "create database test_db;"
run_sql "create table test_db.test_table (a int primary key);"
run_dumpling --sql "select * from test_db.test_table" --filetype csv > ${DUMPLING_OUTPUT_DIR}/dumpling.log