Skip to content

Commit

Permalink
sqlparser: Remove unneeded escaping
Browse files Browse the repository at this point in the history
It's not necessary to escape a double quote since we quote everything
with single quotes. This removes a lot of escaping noise, especially
around JSON usage.

Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink committed Jun 24, 2024
1 parent 465ffcf commit 4ce25bb
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 111 deletions.
23 changes: 23 additions & 0 deletions go/sqltypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,25 @@ var SQLEncodeMap [256]byte
// SQLDecodeMap is the reverse of SQLEncodeMap
var SQLDecodeMap [256]byte

// encodeRef is a map of characters we use for escaping.
// This doesn't include double quotes since we don't need
// to escape that, as we always generate single quoted strings.
var encodeRef = map[byte]byte{
'\x00': '0',
'\'': '\'',
'\b': 'b',
'\n': 'n',
'\r': 'r',
'\t': 't',
26: 'Z', // ctl-Z
'\\': '\\',
}

// decodeRef is a map of characters we use for unescaping.
// We do need all characters here, since we do accept
// escaped double quotes in single quote strings and
// double quoted strings.
var decodeRef = map[byte]byte{
'\x00': '0',
'\'': '\'',
'"': '"',
Expand Down Expand Up @@ -931,6 +949,11 @@ func init() {
for i := range SQLEncodeMap {
if to, ok := encodeRef[byte(i)]; ok {
SQLEncodeMap[byte(i)] = to
}
}

for i := range SQLDecodeMap {
if to, ok := decodeRef[byte(i)]; ok {
SQLDecodeMap[to] = byte(i)
}
}
Expand Down
8 changes: 4 additions & 4 deletions go/sqltypes/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func TestEncode(t *testing.T) {
outASCII: "'Zm9v'",
}, {
in: TestValue(VarChar, "\x00'\"\b\n\r\t\x1A\\"),
outSQL: "'\\0\\'\\\"\\b\\n\\r\\t\\Z\\\\'",
outSQL: "'\\0\\'\"\\b\\n\\r\\t\\Z\\\\'",
outASCII: "'ACciCAoNCRpc'",
}, {
in: TestValue(Bit, "a"),
Expand Down Expand Up @@ -442,7 +442,7 @@ func TestEncodeStringSQL(t *testing.T) {
},
{
in: "\x00'\"\b\n\r\t\x1A\\",
out: "'\\0\\'\\\"\\b\\n\\r\\t\\Z\\\\'",
out: "'\\0\\'\"\\b\\n\\r\\t\\Z\\\\'",
},
}
for _, tcase := range testcases {
Expand Down Expand Up @@ -632,7 +632,7 @@ func TestEncodeSQLStringBuilder(t *testing.T) {
outSQL: "'foo'",
}, {
in: TestValue(VarChar, "\x00'\"\b\n\r\t\x1A\\"),
outSQL: "'\\0\\'\\\"\\b\\n\\r\\t\\Z\\\\'",
outSQL: "'\\0\\'\"\\b\\n\\r\\t\\Z\\\\'",
}, {
in: TestValue(Bit, "a"),
outSQL: "b'01100001'",
Expand Down Expand Up @@ -663,7 +663,7 @@ func TestEncodeSQLBytes2(t *testing.T) {
outSQL: "'foo'",
}, {
in: TestValue(VarChar, "\x00'\"\b\n\r\t\x1A\\"),
outSQL: "'\\0\\'\\\"\\b\\n\\r\\t\\Z\\\\'",
outSQL: "'\\0\\'\"\\b\\n\\r\\t\\Z\\\\'",
}, {
in: TestValue(Bit, "a"),
outSQL: "b'01100001'",
Expand Down
4 changes: 3 additions & 1 deletion go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,9 @@ func (idx *IndexDefinition) Format(buf *TrackedBuffer) {
buf.astPrintf(idx, ")")

for _, opt := range idx.Options {
buf.astPrintf(idx, " %s", opt.Name)
if opt.Name != "" {
buf.astPrintf(idx, " %s", opt.Name)
}
if opt.String != "" {
buf.astPrintf(idx, " %#s", opt.String)
} else if opt.Value != nil {
Expand Down
Loading

0 comments on commit 4ce25bb

Please sign in to comment.