Skip to content

Commit

Permalink
Merge pull request #940 from youtube/suguwork
Browse files Browse the repository at this point in the history
sqlparser: fix binary unary operator bug
  • Loading branch information
sougou committed Jul 30, 2015
2 parents 3d6f8eb + 92c3650 commit 4cae0ba
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
24 changes: 14 additions & 10 deletions data/test/sqlparser_test/parse_pass.sql
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ select /* select as a value expression */ 1 from t where a = (select a from t)
select /* parenthesised value */ 1 from t where a = (b)
select /* over-parenthesize */ ((1)) from t where ((a)) in (((1))) and ((a, b)) in ((((1, 1))), ((2, 2)))
select /* dot-parenthesize */ (a.b) from t where (b.c) = 2
select /* & */ 1 from t where a = b&c
select /* | */ 1 from t where a = b|c
select /* ^ */ 1 from t where a = b^c
select /* + */ 1 from t where a = b+c
select /* - */ 1 from t where a = b-c
select /* * */ 1 from t where a = b*c
select /* / */ 1 from t where a = b/c
select /* % */ 1 from t where a = b%c
select /* & */ 1 from t where a = b & c
select /* & */ 1 from t where a = b & c
select /* | */ 1 from t where a = b | c
select /* ^ */ 1 from t where a = b ^ c
select /* + */ 1 from t where a = b + c
select /* - */ 1 from t where a = b - c
select /* * */ 1 from t where a = b * c
select /* / */ 1 from t where a = b / c
select /* % */ 1 from t where a = b % c
select /* % no space */ 1 from t where a = b%c#select /* % no space */ 1 from t where a = b % c
select /* u+ */ 1 from t where a = +b
select /* u- */ 1 from t where a = -b
select /* u~ */ 1 from t where a = ~b
Expand Down Expand Up @@ -130,12 +132,14 @@ select /* order by asc */ 1 from t order by a asc
select /* order by desc */ 1 from t order by a desc
select /* limit a */ 1 from t limit a
select /* limit a,b */ 1 from t limit a, b
select /* binary unary */ a- -b from t#select /* binary unary */ a - -b from t
select /* - - */ - -b from t
insert /* simple */ into a values (1)
insert /* a.b */ into a.b values (1)
insert /* multi-value */ into a values (1, 2)
insert /* multi-value list */ into a values (1, 2), (3, 4)
insert /* set */ into a set a = 1, a.b = 2#insert /* set */ into a(a, a.b) values (1, 2)
insert /* value expression list */ into a values (a+1, 2*3)
insert /* value expression list */ into a values (a + 1, 2 * 3)
insert /* column list */ into a(a, b) values (1, 2)
insert /* qualified column list */ into a(a, a.b) values (1, 2)
insert /* select */ into a select b, c from d
Expand All @@ -144,7 +148,7 @@ update /* simple */ a set b = 3
update /* a.b */ a.b set b = 3
update /* b.c */ a set b.c = 3
update /* list */ a set b = 3, c = 4
update /* expression */ a set b = 3+4
update /* expression */ a set b = 3 + 4
update /* where */ a set b = 3 where a = b
update /* order */ a set b = 3 order by c desc
update /* limit */ a set b = 3 limit c
Expand Down
16 changes: 8 additions & 8 deletions data/test/tabletserver/exec_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@
"PlanId": "PASS_SELECT",
"Reason": "SELECT_LIST",
"TableName": "a",
"FieldQuery": "select eid+1 from a where 1 != 1",
"FullQuery": "select eid+1 from a limit :#maxLimit",
"FieldQuery": "select eid + 1 from a where 1 != 1",
"FullQuery": "select eid + 1 from a limit :#maxLimit",
"OuterQuery": null,
"Subquery": null,
"IndexUsed": "",
Expand Down Expand Up @@ -590,7 +590,7 @@
"Reason": "WHERE",
"TableName": "a",
"FieldQuery": "select * from a where 1 != 1",
"FullQuery": "select * from a where eid+1 = 1 limit :#maxLimit",
"FullQuery": "select * from a where eid + 1 = 1 limit :#maxLimit",
"OuterQuery": null,
"Subquery": null,
"IndexUsed": "",
Expand Down Expand Up @@ -1617,7 +1617,7 @@
"Reason": "DEFAULT",
"TableName": "a",
"FieldQuery": null,
"FullQuery": "insert into a(eid, id) values (1+1, 2)",
"FullQuery": "insert into a(eid, id) values (1 + 1, 2)",
"OuterQuery": null,
"Subquery": null,
"IndexUsed": "",
Expand Down Expand Up @@ -1931,9 +1931,9 @@
"Reason": "DEFAULT",
"TableName": "a",
"FieldQuery": null,
"FullQuery": "update a set name = 'foo' where eid+1 = 1",
"FullQuery": "update a set name = 'foo' where eid + 1 = 1",
"OuterQuery": "update a set name = 'foo' where :#pk",
"Subquery": "select eid, id from a where eid+1 = 1 limit :#maxLimit for update",
"Subquery": "select eid, id from a where eid + 1 = 1 limit :#maxLimit for update",
"IndexUsed": "",
"ColumnNumbers": null,
"PKValues": null,
Expand Down Expand Up @@ -2117,9 +2117,9 @@
"Reason": "DEFAULT",
"TableName": "a",
"FieldQuery": null,
"FullQuery": "delete from a where eid+1 = 1",
"FullQuery": "delete from a where eid + 1 = 1",
"OuterQuery": "delete from a where :#pk",
"Subquery": "select eid, id from a where eid+1 = 1 limit :#maxLimit for update",
"Subquery": "select eid, id from a where eid + 1 = 1 limit :#maxLimit for update",
"IndexUsed": "",
"ColumnNumbers": null,
"PKValues": null,
Expand Down
2 changes: 1 addition & 1 deletion data/test/vtgate/select_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@
"Reason":"",
"Table": "user",
"Original":"select * from user where id in (1+1, 2)",
"Rewritten":"select * from user where id in (1+1, 2)",
"Rewritten":"select * from user where id in (1 + 1, 2)",
"Subquery": "",
"Vindex": "",
"Col": "",
Expand Down
6 changes: 5 additions & 1 deletion go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ const (
)

func (node *BinaryExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v%c%v", node.Left, node.Operator, node.Right)
buf.Myprintf("%v %c %v", node.Left, node.Operator, node.Right)
}

// UnaryExpr represents a unary value expression.
Expand All @@ -747,6 +747,10 @@ const (
)

func (node *UnaryExpr) Format(buf *TrackedBuffer) {
if _, unary := node.Expr.(*UnaryExpr); unary {
buf.Myprintf("%c %v", node.Operator, node.Expr)
return
}
buf.Myprintf("%c%v", node.Operator, node.Expr)
}

Expand Down
8 changes: 4 additions & 4 deletions test/queryservice_tests/nocache_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
result=[(2L, 1L), (2L, 2L)],
rowcount=2,
rewritten=[
'select eid+1, id from vtocc_a where 1 != 1',
'select /* complex select list */ eid+1, id from vtocc_a limit 10001']),
'select eid + 1, id from vtocc_a where 1 != 1',
'select /* complex select list */ eid + 1, id from vtocc_a limit 10001']),
Case(doc="*",
sql='select /* * */ * from vtocc_a',
result=[(1L, 1L, 'abcd', 'efgh'), (1L, 2L, 'bcde', 'fghi')],
Expand Down Expand Up @@ -164,7 +164,7 @@
rowcount=1,
rewritten=[
'select id from vtocc_a where 1 != 1',
'select /* complex where */ id from vtocc_a where id+1 = 2 limit 10001']),
'select /* complex where */ id from vtocc_a where id + 1 = 2 limit 10001']),

Case(doc='complex where (non-value operand)',
sql='select /* complex where (non-value operand) */ eid, id from vtocc_a where eid = id',
Expand Down Expand Up @@ -680,7 +680,7 @@
"insert into vtocc_a(eid, id, name, foo) values (2, 1, '', '')",
Case(sql="delete from vtocc_a where eid = 1+1 and id = 1",
rewritten=[
'select eid, id from vtocc_a where eid = 1+1 and id = 1 limit 10001 for update',
'select eid, id from vtocc_a where eid = 1 + 1 and id = 1 limit 10001 for update',
"delete from vtocc_a where (eid = 2 and id = 1) /* _stream vtocc_a (eid id ) (2 1 )"],
rowcount=1),
'commit',
Expand Down

0 comments on commit 4cae0ba

Please sign in to comment.