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

cli: fix double-quote escaping for JSON values with format=sql #131881

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Oct 4, 2024

A previous commit (98f9c8a) made an attempt to fix how JSON values are escaped when they contain invalid UTF8 codes and are displayed in the CLI using the --format=sql flag (see #107518).

That commit ended up breaking how JSON values are escaped when they contain double quotes.

Luckily it turns out that both problems were actually caused by a long-lived mistake in the clisqlexec.FormatVal function. It shouldn't use fmt.Sprintf("%+q", s) to escape a string that has invalid characters, as that conflicts with how SQL strings are normally escaped. The proper way is to use lexbase.EscapeSQLString(s).

fixes #131257
Release note (bug fix): Fixed a bug where the CLI would not correctly escape JSON values that had double-quotes inside of a string when using the --format=sql flag.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

A previous commit (98f9c8a) made an
attempt to fix how JSON values are escaped when they contain invalid
UTF8 codes and are displayed in the CLI using the --format=sql flag
(see cockroachdb#107518).

That commit ended up breaking how JSON values are escaped when they
contain double quotes.

Luckily it turns out that both problems were actually caused by a
long-lived mistake in the `clisqlexec.FormatVal` function. It shouldn't
use `fmt.Sprintf("%+q", s)` to escape a string that has invalid
characters, as that conflicts with how SQL strings are normally escaped.
The proper way is to use `lexbase.EscapeSQLString(s)`.

Release note (bug fix): Fixed a bug where the CLI would not correctly
escape JSON values that had double-quotes inside of a string when using
the --format=sql flag.
@rafiss rafiss added backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.2.x Flags PRs that need to be backported to 24.2 labels Oct 4, 2024
@rafiss rafiss marked this pull request as ready for review October 4, 2024 14:11
@rafiss rafiss requested review from a team as code owners October 4, 2024 14:11
@rafiss rafiss requested review from rharding6373 and removed request for a team October 4, 2024 14:11
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

:lgtm:

Looks good one suggestion

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


pkg/cli/clisqlexec/format_sql_test.go line 14 at r1 (raw file):

	defer c.Cleanup()

	testData := []string{

Nit: I'm guessing we can't have an automated comaparison in this family of tests?

Copy link
Collaborator Author

@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! 1 of 0 LGTMs obtained (waiting on @fqazi and @rharding6373)


pkg/cli/clisqlexec/format_sql_test.go line 14 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Nit: I'm guessing we can't have an automated comaparison in this family of tests?

what did you mean by automated comparison?

this test does assert that it has the correct output. for example: https://github.com/cockroachdb/cockroach/actions/runs/11174532794/job/31064361460?pr=131881

Copy link
Collaborator Author

@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! 1 of 0 LGTMs obtained (waiting on @fqazi and @rharding6373)


pkg/cli/clisqlexec/format_sql_test.go line 14 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

what did you mean by automated comparison?

this test does assert that it has the correct output. for example: https://github.com/cockroachdb/cockroach/actions/runs/11174532794/job/31064361460?pr=131881

further reading: https://go.dev/blog/examples

Copy link
Collaborator

@fqazi fqazi 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! 1 of 0 LGTMs obtained (waiting on @rafiss and @rharding6373)


pkg/cli/clisqlexec/format_sql_test.go line 14 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

further reading: https://go.dev/blog/examples

TIL, thank you.

@rafiss
Copy link
Collaborator Author

rafiss commented Oct 4, 2024

tftr!

bors r+

@craig craig bot merged commit 74af8a5 into cockroachdb:master Oct 4, 2024
23 checks passed
Copy link

blathers-crl bot commented Oct 4, 2024

Based on the specified backports for this PR, I applied new labels to the following linked issue(s). Please adjust the labels as needed to match the branches actually affected by the issue(s), including adding any known older branches.


Issue #131257: branch-release-24.2.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.2.x Flags PRs that need to be backported to 24.2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect json value string escaping in cockroach sql --format sql
3 participants