Skip to content

Commit 2154d0b

Browse files
authored
Fix CASE statement WHEN spacing (#231)
1 parent c35e37e commit 2154d0b

File tree

4 files changed

+180
-1
lines changed

4 files changed

+180
-1
lines changed

parser/ast.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5444,7 +5444,10 @@ func (c *CaseExpr) String() string {
54445444
if c.Expr != nil {
54455445
builder.WriteString(c.Expr.String())
54465446
}
5447-
for _, when := range c.Whens {
5447+
for i, when := range c.Whens {
5448+
if i > 0 {
5449+
builder.WriteByte(' ')
5450+
}
54485451
builder.WriteString(when.String())
54495452
}
54505453
if c.Else != nil {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Origin SQL:
2+
SELECT
3+
*,
4+
CASE
5+
WHEN col2 = 'value1' THEN 'when1'
6+
WHEN col3 = 'value2' THEN 'when2'
7+
ELSE 'else'
8+
END as check_result
9+
FROM table_name
10+
WHERE col1 = '123456789'
11+
12+
13+
-- Format SQL:
14+
SELECT *, CASE WHEN col2 = 'value1' THEN 'when1' WHEN col3 = 'value2' THEN 'when2' ELSE 'else' END AS check_result FROM table_name WHERE col1 = '123456789';
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
[
2+
{
3+
"SelectPos": 0,
4+
"StatementEnd": 190,
5+
"With": null,
6+
"Top": null,
7+
"HasDistinct": false,
8+
"DistinctOn": null,
9+
"SelectItems": [
10+
{
11+
"Expr": {
12+
"Name": "*",
13+
"QuoteType": 0,
14+
"NamePos": 11,
15+
"NameEnd": 11
16+
},
17+
"Modifiers": [],
18+
"Alias": null
19+
},
20+
{
21+
"Expr": {
22+
"CasePos": 18,
23+
"EndPos": 0,
24+
"Expr": null,
25+
"Whens": [
26+
{
27+
"WhenPos": 31,
28+
"ThenPos": 52,
29+
"When": {
30+
"LeftExpr": {
31+
"Name": "col2",
32+
"QuoteType": 1,
33+
"NamePos": 36,
34+
"NameEnd": 40
35+
},
36+
"Operation": "=",
37+
"RightExpr": {
38+
"LiteralPos": 44,
39+
"LiteralEnd": 50,
40+
"Literal": "value1"
41+
},
42+
"HasGlobal": false,
43+
"HasNot": false
44+
},
45+
"Then": {
46+
"LiteralPos": 58,
47+
"LiteralEnd": 63,
48+
"Literal": "when1"
49+
},
50+
"ElsePos": 0,
51+
"Else": null
52+
},
53+
{
54+
"WhenPos": 73,
55+
"ThenPos": 94,
56+
"When": {
57+
"LeftExpr": {
58+
"Name": "col3",
59+
"QuoteType": 1,
60+
"NamePos": 78,
61+
"NameEnd": 82
62+
},
63+
"Operation": "=",
64+
"RightExpr": {
65+
"LiteralPos": 86,
66+
"LiteralEnd": 92,
67+
"Literal": "value2"
68+
},
69+
"HasGlobal": false,
70+
"HasNot": false
71+
},
72+
"Then": {
73+
"LiteralPos": 100,
74+
"LiteralEnd": 105,
75+
"Literal": "when2"
76+
},
77+
"ElsePos": 0,
78+
"Else": null
79+
}
80+
],
81+
"ElsePos": 115,
82+
"Else": {
83+
"LiteralPos": 121,
84+
"LiteralEnd": 125,
85+
"Literal": "else"
86+
}
87+
},
88+
"Modifiers": [],
89+
"Alias": {
90+
"Name": "check_result",
91+
"QuoteType": 1,
92+
"NamePos": 138,
93+
"NameEnd": 150
94+
}
95+
}
96+
],
97+
"From": {
98+
"FromPos": 151,
99+
"Expr": {
100+
"Table": {
101+
"TablePos": 156,
102+
"TableEnd": 166,
103+
"Alias": null,
104+
"Expr": {
105+
"Database": null,
106+
"Table": {
107+
"Name": "table_name",
108+
"QuoteType": 1,
109+
"NamePos": 156,
110+
"NameEnd": 166
111+
}
112+
},
113+
"HasFinal": false
114+
},
115+
"StatementEnd": 166,
116+
"SampleRatio": null,
117+
"HasFinal": false
118+
}
119+
},
120+
"Window": null,
121+
"Prewhere": null,
122+
"Where": {
123+
"WherePos": 167,
124+
"Expr": {
125+
"LeftExpr": {
126+
"Name": "col1",
127+
"QuoteType": 1,
128+
"NamePos": 173,
129+
"NameEnd": 177
130+
},
131+
"Operation": "=",
132+
"RightExpr": {
133+
"LiteralPos": 181,
134+
"LiteralEnd": 190,
135+
"Literal": "123456789"
136+
},
137+
"HasGlobal": false,
138+
"HasNot": false
139+
}
140+
},
141+
"GroupBy": null,
142+
"WithTotal": false,
143+
"Having": null,
144+
"OrderBy": null,
145+
"LimitBy": null,
146+
"Limit": null,
147+
"Settings": null,
148+
"Format": null,
149+
"UnionAll": null,
150+
"UnionDistinct": null,
151+
"Except": null
152+
}
153+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SELECT
2+
*,
3+
CASE
4+
WHEN col2 = 'value1' THEN 'when1'
5+
WHEN col3 = 'value2' THEN 'when2'
6+
ELSE 'else'
7+
END as check_result
8+
FROM table_name
9+
WHERE col1 = '123456789'

0 commit comments

Comments
 (0)