Skip to content

Commit a919b4d

Browse files
dbussinkmeiji163
authored andcommitted
sqlparser: Remove unneeded escaping (vitessio#16255)
Signed-off-by: Dirkjan Bussink <[email protected]> Signed-off-by: meiji163 <[email protected]>
1 parent 3e8aa8f commit a919b4d

File tree

19 files changed

+258
-228
lines changed

19 files changed

+258
-228
lines changed

go/sqltypes/value.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,25 @@ var SQLEncodeMap [256]byte
849849
// SQLDecodeMap is the reverse of SQLEncodeMap
850850
var SQLDecodeMap [256]byte
851851

852+
// encodeRef is a map of characters we use for escaping.
853+
// This doesn't include double quotes since we don't need
854+
// to escape that, as we always generate single quoted strings.
852855
var encodeRef = map[byte]byte{
856+
'\x00': '0',
857+
'\'': '\'',
858+
'\b': 'b',
859+
'\n': 'n',
860+
'\r': 'r',
861+
'\t': 't',
862+
26: 'Z', // ctl-Z
863+
'\\': '\\',
864+
}
865+
866+
// decodeRef is a map of characters we use for unescaping.
867+
// We do need all characters here, since we do accept
868+
// escaped double quotes in single quote strings and
869+
// double quoted strings.
870+
var decodeRef = map[byte]byte{
853871
'\x00': '0',
854872
'\'': '\'',
855873
'"': '"',
@@ -869,6 +887,11 @@ func init() {
869887
for i := range SQLEncodeMap {
870888
if to, ok := encodeRef[byte(i)]; ok {
871889
SQLEncodeMap[byte(i)] = to
890+
}
891+
}
892+
893+
for i := range SQLDecodeMap {
894+
if to, ok := decodeRef[byte(i)]; ok {
872895
SQLDecodeMap[to] = byte(i)
873896
}
874897
}

go/sqltypes/value_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ func TestEncode(t *testing.T) {
455455
outASCII: "'Zm9v'",
456456
}, {
457457
in: TestValue(VarChar, "\x00'\"\b\n\r\t\x1A\\"),
458-
outSQL: "'\\0\\'\\\"\\b\\n\\r\\t\\Z\\\\'",
458+
outSQL: "'\\0\\'\"\\b\\n\\r\\t\\Z\\\\'",
459459
outASCII: "'ACciCAoNCRpc'",
460460
}, {
461461
in: TestValue(Bit, "a"),

go/vt/binlog/binlogplayer/binlog_player_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func applyEvents(blp *BinlogPlayer) func() error {
382382
func TestCreateVReplicationKeyRange(t *testing.T) {
383383
want := "insert into _vt.vreplication " +
384384
"(workflow, source, pos, max_tps, max_replication_lag, time_updated, transaction_timestamp, state, db_name, workflow_type, workflow_sub_type, defer_secondary_keys) " +
385-
`values ('Resharding', 'keyspace:\"ks\" shard:\"0\" key_range:{end:\"\\x80\"}', 'MariaDB/0-1-1083', 9223372036854775807, 9223372036854775807, 481823, 0, 'Running', 'db', 0, 0, false)`
385+
`values ('Resharding', 'keyspace:"ks" shard:"0" key_range:{end:"\\x80"}', 'MariaDB/0-1-1083', 9223372036854775807, 9223372036854775807, 481823, 0, 'Running', 'db', 0, 0, false)`
386386

387387
bls := binlogdatapb.BinlogSource{
388388
Keyspace: "ks",
@@ -401,7 +401,7 @@ func TestCreateVReplicationKeyRange(t *testing.T) {
401401
func TestCreateVReplicationTables(t *testing.T) {
402402
want := "insert into _vt.vreplication " +
403403
"(workflow, source, pos, max_tps, max_replication_lag, time_updated, transaction_timestamp, state, db_name, workflow_type, workflow_sub_type, defer_secondary_keys) " +
404-
`values ('Resharding', 'keyspace:\"ks\" shard:\"0\" tables:\"a\" tables:\"b\"', 'MariaDB/0-1-1083', 9223372036854775807, 9223372036854775807, 481823, 0, 'Running', 'db', 0, 0, false)`
404+
`values ('Resharding', 'keyspace:"ks" shard:"0" tables:"a" tables:"b"', 'MariaDB/0-1-1083', 9223372036854775807, 9223372036854775807, 481823, 0, 'Running', 'db', 0, 0, false)`
405405

406406
bls := binlogdatapb.BinlogSource{
407407
Keyspace: "ks",

go/vt/sqlparser/ast_format.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,9 @@ func (idx *IndexDefinition) Format(buf *TrackedBuffer) {
838838
buf.astPrintf(idx, ")")
839839

840840
for _, opt := range idx.Options {
841-
buf.astPrintf(idx, " %s", opt.Name)
841+
if opt.Name != "" {
842+
buf.astPrintf(idx, " %s", opt.Name)
843+
}
842844
if opt.String != "" {
843845
buf.astPrintf(idx, " %#s", opt.String)
844846
} else if opt.Value != nil {

go/vt/sqlparser/parse_test.go

Lines changed: 88 additions & 82 deletions
Large diffs are not rendered by default.

go/vt/sqlparser/parsed_query_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestGenerateQuery(t *testing.T) {
8989
"v1": sqltypes.ValueBindVariable(sqltypes.MakeTrusted(querypb.Type_JSON, []byte(`{"key": "value"}`))),
9090
"v2": sqltypes.ValueBindVariable(sqltypes.MakeTrusted(querypb.Type_RAW, []byte(`json_object("k", "v")`))),
9191
},
92-
output: `insert into t values ('{\"key\": \"value\"}', json_object("k", "v"))`,
92+
output: `insert into t values ('{"key": "value"}', json_object("k", "v"))`,
9393
}, {
9494
desc: "list bind vars 0 arguments",
9595
query: "select * from a where id in ::vals",

go/vt/sqlparser/testdata/select_cases.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4286,7 +4286,7 @@ INPUT
42864286
select * from t1 where MATCH a,b AGAINST ('"xt indexes"' IN BOOLEAN MODE);
42874287
END
42884288
OUTPUT
4289-
select * from t1 where match(a, b) against ('\"xt indexes\"' in boolean mode)
4289+
select * from t1 where match(a, b) against ('"xt indexes"' in boolean mode)
42904290
END
42914291
INPUT
42924292
select max(value) from t1 AS m LEFT JOIN t2 AS c1 ON m.c1id = c1.id AND c1.active = 'Yes' LEFT JOIN t3 AS c2 ON m.c2id = c2.id AND c2.active = 'Yes' WHERE m.pid=1 AND (c1.id IS NOT NULL OR c2.id IS NOT NULL);
@@ -5660,7 +5660,7 @@ INPUT
56605660
select * from t1 where MATCH a,b AGAINST ('"text search" "now support"' IN BOOLEAN MODE);
56615661
END
56625662
OUTPUT
5663-
select * from t1 where match(a, b) against ('\"text search\" \"now support\"' in boolean mode)
5663+
select * from t1 where match(a, b) against ('"text search" "now support"' in boolean mode)
56645664
END
56655665
INPUT
56665666
select insert('hello', 1, -18446744073709551616, 'hi');
@@ -8048,7 +8048,7 @@ INPUT
80488048
select * from t1 where MATCH a,b AGAINST ('"text i"' IN BOOLEAN MODE);
80498049
END
80508050
OUTPUT
8051-
select * from t1 where match(a, b) against ('\"text i\"' in boolean mode)
8051+
select * from t1 where match(a, b) against ('"text i"' in boolean mode)
80528052
END
80538053
INPUT
80548054
select column_name,data_type,CHARACTER_OCTET_LENGTH, CHARACTER_MAXIMUM_LENGTH from information_schema.columns where table_name='t1' order by column_name;
@@ -8144,7 +8144,7 @@ INPUT
81448144
select * from t1 where MATCH a,b AGAINST ('"support now"' IN BOOLEAN MODE);
81458145
END
81468146
OUTPUT
8147-
select * from t1 where match(a, b) against ('\"support now\"' in boolean mode)
8147+
select * from t1 where match(a, b) against ('"support now"' in boolean mode)
81488148
END
81498149
INPUT
81508150
select hex(inet_aton('127.1.1'));
@@ -12212,7 +12212,7 @@ INPUT
1221212212
select * from t1 where MATCH a,b AGAINST ('"text search" +"now support"' IN BOOLEAN MODE);
1221312213
END
1221412214
OUTPUT
12215-
select * from t1 where match(a, b) against ('\"text search\" +\"now support\"' in boolean mode)
12215+
select * from t1 where match(a, b) against ('"text search" +"now support"' in boolean mode)
1221612216
END
1221712217
INPUT
1221812218
select trigger_name from information_schema.triggers where event_object_table='t1';
@@ -14750,7 +14750,7 @@ INPUT
1475014750
select * from t1 where MATCH a,b AGAINST('"space model' IN BOOLEAN MODE);
1475114751
END
1475214752
OUTPUT
14753-
select * from t1 where match(a, b) against ('\"space model' in boolean mode)
14753+
select * from t1 where match(a, b) against ('"space model' in boolean mode)
1475414754
END
1475514755
INPUT
1475614756
select @ujis4 = CONVERT(@utf84 USING ujis);
@@ -14906,7 +14906,7 @@ INPUT
1490614906
select * from t1 where MATCH a,b AGAINST ('"text search" -"now support"' IN BOOLEAN MODE);
1490714907
END
1490814908
OUTPUT
14909-
select * from t1 where match(a, b) against ('\"text search\" -\"now support\"' in boolean mode)
14909+
select * from t1 where match(a, b) against ('"text search" -"now support"' in boolean mode)
1491014910
END
1491114911
INPUT
1491214912
select _rowid,t1._rowid,skey,sval from t1;
@@ -17612,7 +17612,7 @@ INPUT
1761217612
select 'aaa','aa''a',"aa""a";
1761317613
END
1761417614
OUTPUT
17615-
select 'aaa', 'aa\'a', 'aa\"a' from dual
17615+
select 'aaa', 'aa\'a', 'aa"a' from dual
1761617616
END
1761717617
INPUT
1761817618
select cast('18446744073709551615' as signed);
@@ -18542,7 +18542,7 @@ INPUT
1854218542
select * from t1 where MATCH a,b AGAINST ('"now support"' IN BOOLEAN MODE);
1854318543
END
1854418544
OUTPUT
18545-
select * from t1 where match(a, b) against ('\"now support\"' in boolean mode)
18545+
select * from t1 where match(a, b) against ('"now support"' in boolean mode)
1854618546
END
1854718547
INPUT
1854818548
select date_add("1997-12-31 23:59:59",INTERVAL "10000:99:99" HOUR_SECOND);
@@ -22148,7 +22148,7 @@ INPUT
2214822148
select * from t1 where MATCH a,b AGAINST ('"Now sUPPort"' IN BOOLEAN MODE);
2214922149
END
2215022150
OUTPUT
22151-
select * from t1 where match(a, b) against ('\"Now sUPPort\"' in boolean mode)
22151+
select * from t1 where match(a, b) against ('"Now sUPPort"' in boolean mode)
2215222152
END
2215322153
INPUT
2215422154
select sum(all a),count(all a),avg(all a),std(all a),variance(all a),bit_or(all a),bit_and(all a),min(all a),max(all a),min(all c),max(all c) from t1;

go/vt/vtctl/workflow/materializer_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,11 +2453,11 @@ func TestMaterializerOneToOne(t *testing.T) {
24532453
insertPrefix+
24542454
`\(`+
24552455
`'workflow', `+
2456-
(`'keyspace:\\"sourceks\\" shard:\\"0\\" `+
2456+
(`'keyspace:"sourceks" shard:"0" `+
24572457
`filter:{`+
2458-
`rules:{match:\\"t1\\" filter:\\"select.*t1\\"} `+
2459-
`rules:{match:\\"t2\\" filter:\\"select.*t3\\"} `+
2460-
`rules:{match:\\"t4\\"}`+
2458+
`rules:{match:"t1" filter:"select.*t1"} `+
2459+
`rules:{match:"t2" filter:"select.*t3"} `+
2460+
`rules:{match:"t4"}`+
24612461
`}', `)+
24622462
`'', [0-9]*, [0-9]*, 'zone1', 'primary,rdonly', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false`+
24632463
`\)`+eol,
@@ -2495,9 +2495,9 @@ func TestMaterializerManyToOne(t *testing.T) {
24952495
env.tmc.expectVRQuery(
24962496
200,
24972497
insertPrefix+
2498-
`\('workflow', 'keyspace:\\"sourceks\\" shard:\\"-80\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1\\"} rules:{match:\\"t2\\" filter:\\"select.*t3\\"}}', '', [0-9]*, [0-9]*, '', '', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false\)`+
2498+
`\('workflow', 'keyspace:"sourceks" shard:"-80" filter:{rules:{match:"t1" filter:"select.*t1"} rules:{match:"t2" filter:"select.*t3"}}', '', [0-9]*, [0-9]*, '', '', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false\)`+
24992499
`, `+
2500-
`\('workflow', 'keyspace:\\"sourceks\\" shard:\\"80-\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1\\"} rules:{match:\\"t2\\" filter:\\"select.*t3\\"}}', '', [0-9]*, [0-9]*, '', '', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false\)`+
2500+
`\('workflow', 'keyspace:"sourceks" shard:"80-" filter:{rules:{match:"t1" filter:"select.*t1"} rules:{match:"t2" filter:"select.*t3"}}', '', [0-9]*, [0-9]*, '', '', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false\)`+
25012501
eol,
25022502
&sqltypes.Result{},
25032503
)
@@ -2551,13 +2551,13 @@ func TestMaterializerOneToMany(t *testing.T) {
25512551
env.tmc.expectVRQuery(
25522552
200,
25532553
insertPrefix+
2554-
`.*shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*-80.*`,
2554+
`.*shard:"0" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*-80.*`,
25552555
&sqltypes.Result{},
25562556
)
25572557
env.tmc.expectVRQuery(
25582558
210,
25592559
insertPrefix+
2560-
`.*shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*80-.*`,
2560+
`.*shard:"0" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*80-.*`,
25612561
&sqltypes.Result{},
25622562
)
25632563
env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{})
@@ -2611,15 +2611,15 @@ func TestMaterializerManyToMany(t *testing.T) {
26112611
env.tmc.expectVRQuery(
26122612
200,
26132613
insertPrefix+
2614-
`.*shard:\\"-40\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*-80.*`+
2615-
`.*shard:\\"40-\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*-80.*`,
2614+
`.*shard:"-40" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*-80.*`+
2615+
`.*shard:"40-" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*-80.*`,
26162616
&sqltypes.Result{},
26172617
)
26182618
env.tmc.expectVRQuery(
26192619
210,
26202620
insertPrefix+
2621-
`.*shard:\\"-40\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*80-.*`+
2622-
`.*shard:\\"40-\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*80-.*`,
2621+
`.*shard:"-40" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*80-.*`+
2622+
`.*shard:"40-" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1.*targetks\.xxhash.*80-.*`,
26232623
&sqltypes.Result{},
26242624
)
26252625
env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{})
@@ -2675,13 +2675,13 @@ func TestMaterializerMulticolumnVindex(t *testing.T) {
26752675
env.tmc.expectVRQuery(
26762676
200,
26772677
insertPrefix+
2678-
`.*shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1, c2.*targetks\.region.*-80.*`,
2678+
`.*shard:"0" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1, c2.*targetks\.region.*-80.*`,
26792679
&sqltypes.Result{},
26802680
)
26812681
env.tmc.expectVRQuery(
26822682
210,
26832683
insertPrefix+
2684-
`.*shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1, c2.*targetks\.region.*80-.*`,
2684+
`.*shard:"0" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1, c2.*targetks\.region.*80-.*`,
26852685
&sqltypes.Result{},
26862686
)
26872687
env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{})
@@ -2720,7 +2720,7 @@ func TestMaterializerDeploySchema(t *testing.T) {
27202720
env.tmc.expectVRQuery(
27212721
200,
27222722
insertPrefix+
2723-
`\('workflow', 'keyspace:\\"sourceks\\" shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1\\"} rules:{match:\\"t2\\" filter:\\"select.*t3\\"}}', '', [0-9]*, [0-9]*, '', '', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false\)`+
2723+
`\('workflow', 'keyspace:"sourceks" shard:"0" filter:{rules:{match:"t1" filter:"select.*t1"} rules:{match:"t2" filter:"select.*t3"}}', '', [0-9]*, [0-9]*, '', '', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false\)`+
27242724
eol,
27252725
&sqltypes.Result{},
27262726
)
@@ -2761,7 +2761,7 @@ func TestMaterializerCopySchema(t *testing.T) {
27612761
env.tmc.expectVRQuery(
27622762
200,
27632763
insertPrefix+
2764-
`\('workflow', 'keyspace:\\"sourceks\\" shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1\\"} rules:{match:\\"t2\\" filter:\\"select.*t3\\"}}', '', [0-9]*, [0-9]*, '', '', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false\)`+
2764+
`\('workflow', 'keyspace:"sourceks" shard:"0" filter:{rules:{match:"t1" filter:"select.*t1"} rules:{match:"t2" filter:"select.*t3"}}', '', [0-9]*, [0-9]*, '', '', [0-9]*, 0, 'Stopped', 'vt_targetks', 0, 0, false\)`+
27652765
eol,
27662766
&sqltypes.Result{},
27672767
)
@@ -2821,13 +2821,13 @@ func TestMaterializerExplicitColumns(t *testing.T) {
28212821
env.tmc.expectVRQuery(
28222822
200,
28232823
insertPrefix+
2824-
`.*shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1, c2.*targetks\.region.*-80.*`,
2824+
`.*shard:"0" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1, c2.*targetks\.region.*-80.*`,
28252825
&sqltypes.Result{},
28262826
)
28272827
env.tmc.expectVRQuery(
28282828
210,
28292829
insertPrefix+
2830-
`.*shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c1, c2.*targetks\.region.*80-.*`,
2830+
`.*shard:"0" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c1, c2.*targetks\.region.*80-.*`,
28312831
&sqltypes.Result{},
28322832
)
28332833
env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{})
@@ -2884,13 +2884,13 @@ func TestMaterializerRenamedColumns(t *testing.T) {
28842884
env.tmc.expectVRQuery(
28852885
200,
28862886
insertPrefix+
2887-
`.*shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c3, c4.*targetks\.region.*-80.*`,
2887+
`.*shard:"0" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c3, c4.*targetks\.region.*-80.*`,
28882888
&sqltypes.Result{},
28892889
)
28902890
env.tmc.expectVRQuery(
28912891
210,
28922892
insertPrefix+
2893-
`.*shard:\\"0\\" filter:{rules:{match:\\"t1\\" filter:\\"select.*t1 where in_keyrange\(c3, c4.*targetks\.region.*80-.*`,
2893+
`.*shard:"0" filter:{rules:{match:"t1" filter:"select.*t1 where in_keyrange\(c3, c4.*targetks\.region.*80-.*`,
28942894
&sqltypes.Result{},
28952895
)
28962896
env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{})

go/vt/vtctl/workflow/resharder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func TestReshardCreate(t *testing.T) {
186186
env.tmc.expectVRQuery(
187187
tabletUID,
188188
insertPrefix+
189-
`\('`+workflowName+`', 'keyspace:\\"`+targetKeyspaceName+`\\" shard:\\"0\\" filter:{rules:{match:\\"/.*\\" filter:\\"`+target+`\\"}}', '', [0-9]*, [0-9]*, '`+
189+
`\('`+workflowName+`', 'keyspace:"`+targetKeyspaceName+`" shard:"0" filter:{rules:{match:"/.*" filter:"`+target+`"}}', '', [0-9]*, [0-9]*, '`+
190190
env.cell+`', '`+tabletTypesStr+`', [0-9]*, 0, 'Stopped', 'vt_`+targetKeyspaceName+`', 4, 0, false\)`+eol,
191191
&sqltypes.Result{},
192192
)

0 commit comments

Comments
 (0)