Skip to content

Commit

Permalink
sql: fix quoting of session settings in stmt bundle env.sql
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mgartner committed Jan 31, 2025
1 parent 6a0a025 commit d75a19d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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 == "" {
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/explain_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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 */
Expand Down

0 comments on commit d75a19d

Please sign in to comment.