Skip to content

Commit 47921fe

Browse files
authored
Add support for concat operator (#228)
1 parent 39f4fac commit 47921fe

File tree

5 files changed

+191
-6
lines changed

5 files changed

+191
-6
lines changed

parser/lexer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ const (
2525
TokenKindGE TokenKind = ">="
2626
TokenKindQuestionMark TokenKind = "?"
2727

28-
TokenKindPlus TokenKind = "+"
29-
TokenKindMinus TokenKind = "-"
30-
TokenKindMul TokenKind = "*"
31-
TokenKindDiv TokenKind = "/"
32-
TokenKindMod TokenKind = "%"
28+
TokenKindPlus TokenKind = "+"
29+
TokenKindMinus TokenKind = "-"
30+
TokenKindMul TokenKind = "*"
31+
TokenKindDiv TokenKind = "/"
32+
TokenKindMod TokenKind = "%"
33+
TokenKindConcat TokenKind = "||"
3334

3435
TokenKindArrow TokenKind = "->"
3536
TokenKindDash TokenKind = "::"

parser/parser_column.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
PrecedenceCompare
1717
PrecedenceBetweenLike
1818
precedenceIn
19+
PrecedenceConcat
1920
PrecedenceAddSub
2021
PrecedenceMulDivMod
2122
PrecedenceBracket
@@ -49,6 +50,8 @@ func (p *Parser) getNextPrecedence() int {
4950
p.matchTokenKind(TokenKindGE), p.matchTokenKind(TokenKindGT), p.matchTokenKind(TokenKindDoubleEQ),
5051
p.matchTokenKind(TokenKindNE), p.matchTokenKind("<>"):
5152
return PrecedenceCompare
53+
case p.matchTokenKind(TokenKindConcat):
54+
return PrecedenceConcat
5255
case p.matchTokenKind(TokenKindPlus), p.matchTokenKind(TokenKindMinus):
5356
return PrecedenceAddSub
5457
case p.matchTokenKind(TokenKindMul), p.matchTokenKind(TokenKindDiv), p.matchTokenKind(TokenKindMod):
@@ -80,7 +83,7 @@ func (p *Parser) parseInfix(expr Expr, precedence int) (Expr, error) {
8083
p.matchTokenKind(TokenKindGE), p.matchTokenKind(TokenKindGT),
8184
p.matchTokenKind(TokenKindNE), p.matchTokenKind("<>"),
8285
p.matchTokenKind(TokenKindMinus), p.matchTokenKind(TokenKindPlus), p.matchTokenKind(TokenKindMul),
83-
p.matchTokenKind(TokenKindDiv), p.matchTokenKind(TokenKindMod),
86+
p.matchTokenKind(TokenKindDiv), p.matchTokenKind(TokenKindMod), p.matchTokenKind(TokenKindConcat),
8487
p.matchKeyword(KeywordIn), p.matchKeyword(KeywordLike),
8588
p.matchKeyword(KeywordIlike), p.matchKeyword(KeywordAnd), p.matchKeyword(KeywordOr),
8689
p.matchTokenKind(TokenKindArrow), p.matchTokenKind(TokenKindDoubleEQ):
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Origin SQL:
2+
SELECT 'a' || 'b';
3+
SELECT 'a' || 'b' || 'c';
4+
SELECT 'a' || 'b' || 'c' + 5;
5+
6+
7+
-- Format SQL:
8+
SELECT 'a' || 'b';
9+
SELECT 'a' || 'b' || 'c';
10+
SELECT 'a' || 'b' || 'c' + 5;
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
[
2+
{
3+
"SelectPos": 0,
4+
"StatementEnd": 16,
5+
"With": null,
6+
"Top": null,
7+
"HasDistinct": false,
8+
"DistinctOn": null,
9+
"SelectItems": [
10+
{
11+
"Expr": {
12+
"LeftExpr": {
13+
"LiteralPos": 8,
14+
"LiteralEnd": 9,
15+
"Literal": "a"
16+
},
17+
"Operation": "||",
18+
"RightExpr": {
19+
"LiteralPos": 15,
20+
"LiteralEnd": 16,
21+
"Literal": "b"
22+
},
23+
"HasGlobal": false,
24+
"HasNot": false
25+
},
26+
"Modifiers": [],
27+
"Alias": null
28+
}
29+
],
30+
"From": null,
31+
"ArrayJoin": null,
32+
"Window": null,
33+
"Prewhere": null,
34+
"Where": null,
35+
"GroupBy": null,
36+
"WithTotal": false,
37+
"Having": null,
38+
"OrderBy": null,
39+
"LimitBy": null,
40+
"Limit": null,
41+
"Settings": null,
42+
"Format": null,
43+
"UnionAll": null,
44+
"UnionDistinct": null,
45+
"Except": null
46+
},
47+
{
48+
"SelectPos": 19,
49+
"StatementEnd": 42,
50+
"With": null,
51+
"Top": null,
52+
"HasDistinct": false,
53+
"DistinctOn": null,
54+
"SelectItems": [
55+
{
56+
"Expr": {
57+
"LeftExpr": {
58+
"LeftExpr": {
59+
"LiteralPos": 27,
60+
"LiteralEnd": 28,
61+
"Literal": "a"
62+
},
63+
"Operation": "||",
64+
"RightExpr": {
65+
"LiteralPos": 34,
66+
"LiteralEnd": 35,
67+
"Literal": "b"
68+
},
69+
"HasGlobal": false,
70+
"HasNot": false
71+
},
72+
"Operation": "||",
73+
"RightExpr": {
74+
"LiteralPos": 41,
75+
"LiteralEnd": 42,
76+
"Literal": "c"
77+
},
78+
"HasGlobal": false,
79+
"HasNot": false
80+
},
81+
"Modifiers": [],
82+
"Alias": null
83+
}
84+
],
85+
"From": null,
86+
"ArrayJoin": null,
87+
"Window": null,
88+
"Prewhere": null,
89+
"Where": null,
90+
"GroupBy": null,
91+
"WithTotal": false,
92+
"Having": null,
93+
"OrderBy": null,
94+
"LimitBy": null,
95+
"Limit": null,
96+
"Settings": null,
97+
"Format": null,
98+
"UnionAll": null,
99+
"UnionDistinct": null,
100+
"Except": null
101+
},
102+
{
103+
"SelectPos": 45,
104+
"StatementEnd": 73,
105+
"With": null,
106+
"Top": null,
107+
"HasDistinct": false,
108+
"DistinctOn": null,
109+
"SelectItems": [
110+
{
111+
"Expr": {
112+
"LeftExpr": {
113+
"LeftExpr": {
114+
"LiteralPos": 53,
115+
"LiteralEnd": 54,
116+
"Literal": "a"
117+
},
118+
"Operation": "||",
119+
"RightExpr": {
120+
"LiteralPos": 60,
121+
"LiteralEnd": 61,
122+
"Literal": "b"
123+
},
124+
"HasGlobal": false,
125+
"HasNot": false
126+
},
127+
"Operation": "||",
128+
"RightExpr": {
129+
"LeftExpr": {
130+
"LiteralPos": 67,
131+
"LiteralEnd": 68,
132+
"Literal": "c"
133+
},
134+
"Operation": "+",
135+
"RightExpr": {
136+
"NumPos": 72,
137+
"NumEnd": 73,
138+
"Literal": "5",
139+
"Base": 10
140+
},
141+
"HasGlobal": false,
142+
"HasNot": false
143+
},
144+
"HasGlobal": false,
145+
"HasNot": false
146+
},
147+
"Modifiers": [],
148+
"Alias": null
149+
}
150+
],
151+
"From": null,
152+
"ArrayJoin": null,
153+
"Window": null,
154+
"Prewhere": null,
155+
"Where": null,
156+
"GroupBy": null,
157+
"WithTotal": false,
158+
"Having": null,
159+
"OrderBy": null,
160+
"LimitBy": null,
161+
"Limit": null,
162+
"Settings": null,
163+
"Format": null,
164+
"UnionAll": null,
165+
"UnionDistinct": null,
166+
"Except": null
167+
}
168+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT 'a' || 'b';
2+
SELECT 'a' || 'b' || 'c';
3+
SELECT 'a' || 'b' || 'c' + 5;

0 commit comments

Comments
 (0)