Skip to content

Commit

Permalink
Merge pull request #93260 from cockroachdb/blathers/backport-release-…
Browse files Browse the repository at this point in the history
…22.1-93100

release-22.1: clisqlshell: correctly handle sending zero input for COPY
  • Loading branch information
otan authored Dec 12, 2022
2 parents 58afeb6 + f5b5d2c commit c849cfb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
11 changes: 7 additions & 4 deletions pkg/cli/clisqlclient/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ func (c *CopyFromState) Commit(ctx context.Context, cleanupFunc func(), lines st
return func(ctx context.Context, conn Conn) (Rows, bool, error) {
defer cleanupFunc()
rows, isMulti, err := func() (Rows, bool, error) {
for _, l := range strings.Split(lines, "\n") {
_, err := c.copyFromer.CopyData(ctx, l)
if err != nil {
return nil, false, err
// Do not send anything if it is just an empty string.
if lines != "" {
for _, l := range strings.Split(lines, "\n") {
_, err := c.copyFromer.CopyData(ctx, l)
if err != nil {
return nil, false, err
}
}
}
r, err := c.copyFromer.Exec(nil)
Expand Down
6 changes: 4 additions & 2 deletions pkg/cli/clisqlshell/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ func (c *cliState) doStartLine(nextState cliStateEnum) cliStateEnum {
c.atEOF = false
c.partialLines = c.partialLines[:0]
c.partialStmtsLen = 0
c.concatLines = ""

c.useContinuePrompt = false

Expand Down Expand Up @@ -1243,7 +1244,8 @@ func (c *cliState) doHandleCliCmd(loopState, nextState cliStateEnum) cliStateEnu

case `\.`:
if c.inCopy() {
c.concatLines += "\n" + `\.`
c.partialLines = append(c.partialLines, `\.`)
c.partialStmtsLen++
return cliRunStatement
}
return c.invalidSyntax(errState)
Expand Down Expand Up @@ -1602,7 +1604,7 @@ func (c *cliState) doPrepareStatementLine(
(c.inCopy() && (strings.HasSuffix(c.concatLines, "\n"+`\.`) || c.atEOF)) ||
// We're always at the end of a statement if EOF is reached in the
// single statement mode.
c.singleStatement && c.atEOF
(c.singleStatement && c.atEOF)
if c.atEOF {
// Definitely no more input expected.
if !endOfStmt {
Expand Down
16 changes: 16 additions & 0 deletions pkg/cli/interactive_tests/test_copy.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ eexpect "could not parse"

end_test

start_test "check EMPTY copy"

send "COPY t FROM STDIN;\r"
eexpect ">>"
send_eof
eexpect "COPY 0"
eexpect root@

send "COPY t FROM STDIN;\r"
eexpect ">>"
send "\\.\r"
eexpect "COPY 0"
eexpect root@

end_test

start_test "multi statement with COPY"
send "SELECT 1; COPY t FROM STDIN CSV;\r"
eexpect "COPY together with other statements in a query string is not supported"
Expand Down

0 comments on commit c849cfb

Please sign in to comment.