From d75a19daa947b9648c9d67f037261e68d4fe6d79 Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Fri, 31 Jan 2025 11:04:32 -0500 Subject: [PATCH] sql: fix quoting of session settings in stmt bundle env.sql The values of session setting in the `env.sql` file of a statement bundle are now wrapped in single quotes if they contain whitespace. The statement bundle session setting tests have been updated to ensure that `env.sql` is parsable. Release note: None --- pkg/sql/explain_bundle.go | 4 +++- pkg/sql/explain_bundle_test.go | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/sql/explain_bundle.go b/pkg/sql/explain_bundle.go index 227e984b7883..0f182b683b88 100644 --- a/pkg/sql/explain_bundle.go +++ b/pkg/sql/explain_bundle.go @@ -822,6 +822,8 @@ func init() { binarySVForBundle = &st.SV } +var anyWhitespace = regexp.MustCompile(`\s+`) + // PrintSessionSettings appends information about all session variables that // differ from their defaults. // @@ -900,7 +902,7 @@ func (c *stmtEnvCollector) PrintSessionSettings(w io.Writer, sv *settings.Values if skip && !all { continue } - if _, ok := sessionVarNeedsEscaping[varName]; ok { + if _, ok := sessionVarNeedsEscaping[varName]; ok || anyWhitespace.MatchString(value) { value = lexbase.EscapeSQLString(value) } if value == "" { diff --git a/pkg/sql/explain_bundle_test.go b/pkg/sql/explain_bundle_test.go index 91355eea57d2..a0db22a2bc16 100644 --- a/pkg/sql/explain_bundle_test.go +++ b/pkg/sql/explain_bundle_test.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/kv/kvserver" "github.com/cockroachdb/cockroach/pkg/security/username" + "github.com/cockroachdb/cockroach/pkg/sql/parser" "github.com/cockroachdb/cockroach/pkg/testutils" "github.com/cockroachdb/cockroach/pkg/testutils/pgtest" "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" @@ -222,6 +223,7 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R {"testing_optimizer_random_seed", "123"}, {"timezone", "+8"}, {"unconstrained_non_covering_index_scan_enabled", "on"}, + {"default_transaction_isolation", "'read committed'"}, } for _, tc := range testcases { t.Run(tc.sessionVar, func(t *testing.T) { @@ -235,6 +237,9 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R if reg.FindString(contents) == "" { return errors.Errorf("could not find 'SET %s' in env.sql", tc.sessionVar) } + if _, err := parser.Parse(contents); err != nil { + return errors.Wrap(err, "could not parse env.sql") + } } return nil }, false, /* expectErrors */