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

clisqlshell: implement COPY ... FROM STDIN for CLI #79629

Merged
merged 1 commit into from
Apr 13, 2022

Conversation

otan
Copy link
Contributor

@otan otan commented Apr 8, 2022

Resolves #16392

Steps:

  • Add a lower level API to lib/pq for use.
  • Add some abstraction boundary breakers in clisqlclient that allow a
    lower level handling of the COPY protocol.
  • Altered the state machine in clisqlshell to account for copy.

Release note (cli change): COPY ... FROM STDIN now works from the
cockroach CLI. It is not supported inside transactions.

@otan otan added do-not-merge bors won't merge a PR with this label. X-noremind Bots won't notify about PRs with X-noremind labels Apr 8, 2022
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@otan otan force-pushed the scanner branch 2 times, most recently from a6142ed to b96c8e1 Compare April 8, 2022 03:47
@otan otan changed the title [dnm] clisqlshell: implement a bad version of \COPY [dnm] clisqlshell: implement COPY ... FROM STDIN for CLI Apr 8, 2022
@otan otan force-pushed the scanner branch 6 times, most recently from f04d2c4 to d37b65e Compare April 8, 2022 06:39
@otan otan changed the title [dnm] clisqlshell: implement COPY ... FROM STDIN for CLI clisqlshell: implement COPY ... FROM STDIN for CLI Apr 8, 2022
@otan otan removed do-not-merge bors won't merge a PR with this label. X-noremind Bots won't notify about PRs with X-noremind labels Apr 8, 2022
@otan otan force-pushed the scanner branch 3 times, most recently from b6742de to e71bccc Compare April 8, 2022 10:09
@otan otan marked this pull request as ready for review April 8, 2022 10:09
@otan otan requested a review from a team as a code owner April 8, 2022 10:09
@otan otan requested a review from rafiss April 8, 2022 10:10
@otan otan force-pushed the scanner branch 4 times, most recently from a41ab04 to af53d44 Compare April 8, 2022 14:13
Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

The general design is very nice 💯

I have one remaiining concern on semantics, see below.

Reviewed 15 of 15 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @otan and @rafiss)


pkg/cli/clisqlshell/sql.go, line 1703 at r1 (raw file):

	c.exitErr = c.runWithInterruptableCtx(func(ctx context.Context) error {
		if scanner.FirstLexicalToken(c.concatLines) == lexbase.COPY {
			return c.beginCopyFrom(ctx, c.concatLines)

wowzer, how does this behave if the user enters copy ...; select ... as one line at the prompt?
I want to see tests for this.

I think the code should refuse to treat COPY as a special case unless c.singleStatement is true.

@otan
Copy link
Contributor Author

otan commented Apr 8, 2022

pkg/cli/clisqlshell/sql.go, line 1703 at r1 (raw file):

Previously, knz (kena) wrote…

wowzer, how does this behave if the user enters copy ...; select ... as one line at the prompt?
I want to see tests for this.

I think the code should refuse to treat COPY as a special case unless c.singleStatement is true.

there is a test for this:

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"
eexpect root@
send "COPY t FROM STDIN CSV;SELECT 1;\r"
eexpect "COPY together with other statements in a query string is not supported"
eexpect root@
end_test

@otan
Copy link
Contributor Author

otan commented Apr 8, 2022

pkg/cli/clisqlshell/sql.go, line 1703 at r1 (raw file):

Previously, otan (Oliver Tan) wrote…

there is a test for this:

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"
eexpect root@
send "COPY t FROM STDIN CSV;SELECT 1;\r"
eexpect "COPY together with other statements in a query string is not supported"
eexpect root@
end_test

(it's already handled by the driver)

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz and @otan)


go.mod, line 373 at r2 (raw file):

replace github.com/maruel/panicparse/v2 => github.com/cockroachdb/panicparse/v2 v2.0.0-20211103220158-604c82a44f1e

replace github.com/lib/pq => github.com/cockroachdb/pq v0.0.0-20220408053752-c3ffc8d4376f

i think this fork is missing some of the latest lib/pq changes (they don't look too important, but might as well include them, or merge them into the upstream)

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz and @otan)


pkg/cli/clisqlshell/sql.go, line 1703 at r1 (raw file):

Previously, otan (Oliver Tan) wrote…

(it's already handled by the driver)

to clarify, the "COPY together with other statements in a query string is not supported" message is actually coming from the CRDB server:

// NOTE(andrei): I don't know if Postgres supports receiving a COPY
// together with other statements in the "simple" protocol, but I'd
// rather not worry about it since execution of COPY is special - it
// takes control over the connection.
return c.stmtBuf.Push(
ctx,
sql.SendError{
Err: pgwirebase.NewProtocolViolationErrorf(
"COPY together with other statements in a query string is not supported"),

Copy link
Contributor Author

@otan otan left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz and @rafiss)


go.mod, line 373 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i think this fork is missing some of the latest lib/pq changes (they don't look too important, but might as well include them, or merge them into the upstream)

last time i did some tests failed; also in terms of backportability this is much neater.


pkg/cli/clisqlshell/sql.go, line 1703 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

to clarify, the "COPY together with other statements in a query string is not supported" message is actually coming from the CRDB server:

// NOTE(andrei): I don't know if Postgres supports receiving a COPY
// together with other statements in the "simple" protocol, but I'd
// rather not worry about it since execution of COPY is special - it
// takes control over the connection.
return c.stmtBuf.Push(
ctx,
sql.SendError{
Err: pgwirebase.NewProtocolViolationErrorf(
"COPY together with other statements in a query string is not supported"),

even better!

@knz
Copy link
Contributor

knz commented Apr 12, 2022

huh I hadn't seen you introduced a fork of lib/pq here. can you upstream them directly instead? we have a commit bit there.

Steps:
* Add a lower level API to lib/pq for use.
* Add some abstraction boundary breakers in `clisqlclient` that allow a
  lower level handling of the COPY protocol.
* Altered the state machine in `clisqlshell` to account for copy.

Release note (cli change): COPY ... FROM STDIN now works from the
cockroach CLI. It is not supported inside transactions.
@otan
Copy link
Contributor Author

otan commented Apr 12, 2022

i've tried upgrading lib/pq instead, let's see if all tests pass.

@otan otan requested a review from rafiss April 12, 2022 21:23
@otan
Copy link
Contributor Author

otan commented Apr 13, 2022

ok looks good

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r2, 5 of 6 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz and @rafiss)

@knz
Copy link
Contributor

knz commented Apr 13, 2022

thank you!

@otan
Copy link
Contributor Author

otan commented Apr 13, 2022

bors r+

@craig craig bot merged commit ba9baca into cockroachdb:master Apr 13, 2022
@craig
Copy link
Contributor

craig bot commented Apr 13, 2022

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cli/sql: support COPY FROM STDIN in the sql shell
4 participants