Skip to content

Commit

Permalink
sql: don't include quotes in parsed strings
Browse files Browse the repository at this point in the history
The planner.TypeAsString* helpers, currently only really used in backup/restore/import, were using
parser.AsStringWithFlags to get the string value of the expr they retured. However even in 'bareStrings'
mode, this still prints non-trivial strings *in quotes*. If calling code is planning to use the value of
the string, for instance as a URI or parameter value, mangling it with added quotes is almost certainly
not desired. These helpers are used to read params from a query for processing, not print parsable SQL.
  • Loading branch information
dt committed Aug 22, 2017
1 parent 214f4b6 commit a783537
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/ccl/acceptanceccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func BenchmarkRestoreBig(b *testing.B) {
b.Fatal(err)
}

dbName := fmt.Sprintf("bank%d", b.N)
dbName := fmt.Sprintf("bank %d", b.N)
r.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))

b.ResetTimer()
Expand Down
10 changes: 5 additions & 5 deletions pkg/ccl/sqlccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ func TestBackupRestoreIncremental(t *testing.T) {
// the greatest key in the diff is less than the previous backups.
sqlDB.Exec(`INSERT INTO data.bank VALUES (0, -1, 'final')`)
checksums = append(checksums, checksumBankPayload(t, sqlDB))
finalBackupDir := filepath.Join(dir, "final")
finalBackupDir := filepath.Join(dir, "final layer")
sqlDB.Exec(fmt.Sprintf(`BACKUP TABLE data.bank TO '%s' %s`,
finalBackupDir, fmt.Sprintf(` INCREMENTAL FROM %s`, strings.Join(backupDirs, `,`)),
))
Expand Down Expand Up @@ -1837,18 +1837,18 @@ func TestRestoreInto(t *testing.T) {

sqlDB.Exec(`BACKUP DATABASE data TO $1`, dir)

restoreStmt := fmt.Sprintf(`RESTORE data.bank FROM '%s' WITH OPTIONS ('into_db'='data2')`, dir)
restoreStmt := fmt.Sprintf(`RESTORE data.bank FROM '%s' WITH into_db = 'data 2'`, dir)

_, err := sqlDB.DB.Exec(restoreStmt)
if !testutils.IsError(err, "a database named \"data2\" needs to exist") {
if !testutils.IsError(err, "a database named \"data 2\" needs to exist") {
t.Fatal(err)
}

sqlDB.Exec(`CREATE DATABASE data2`)
sqlDB.Exec(`CREATE DATABASE "data 2"`)
sqlDB.Exec(restoreStmt)

expected := sqlDB.QueryStr(`SELECT * FROM data.bank`)
sqlDB.CheckQueryResults(`SELECT * FROM data2.bank`, expected)
sqlDB.CheckQueryResults(`SELECT * FROM "data 2".bank`, expected)
}

func TestBackupRestorePermissions(t *testing.T) {
Expand Down
19 changes: 16 additions & 3 deletions pkg/sql/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/pkg/errors"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -342,7 +343,11 @@ func (p *planner) TypeAsString(e parser.Expr, op string) (func() (string, error)
if err != nil {
return "", err
}
return parser.AsStringWithFlags(d, parser.FmtBareStrings), nil
str, ok := d.(*parser.DString)
if !ok {
return "", errors.Errorf("failed to cast %T to string", d)
}
return string(*str), nil
}
return fn, nil
}
Expand Down Expand Up @@ -377,7 +382,11 @@ func (p *planner) TypeAsStringOpts(
if err != nil {
return nil, err
}
res[name] = parser.AsStringWithFlags(d, parser.FmtBareStrings)
str, ok := d.(*parser.DString)
if !ok {
return res, errors.Errorf("failed to cast %T to string", d)
}
res[name] = string(*str)
}
return res, nil
}
Expand Down Expand Up @@ -405,7 +414,11 @@ func (p *planner) TypeAsStringArray(
if err != nil {
return nil, err
}
strs[i] = parser.AsStringWithFlags(d, parser.FmtBareStrings)
str, ok := d.(*parser.DString)
if !ok {
return strs, errors.Errorf("failed to cast %T to string", d)
}
strs[i] = string(*str)
}
return strs, nil
}
Expand Down

0 comments on commit a783537

Please sign in to comment.