Skip to content

Commit 62ed1c8

Browse files
authored
Parse select item alias even without AS keyword (#125)
1 parent 5dc6713 commit 62ed1c8

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

parser/parser_column.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,10 @@ func (p *Parser) parseSelectItem() (*SelectItem, error) {
735735
if err != nil {
736736
return nil, err
737737
}
738+
} else {
739+
alias = p.tryParseIdent()
738740
}
741+
739742
return &SelectItem{
740743
Expr: expr,
741744
Modifiers: modifiers,

parser/parser_common.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ func (p *Parser) tryConsumeKeyword(keyword string) *Token {
7878
return nil
7979
}
8080

81+
func (p *Parser) tryParseIdent() *Ident {
82+
if p.lastTokenKind() != TokenKindIdent {
83+
return nil
84+
}
85+
lastToken := p.last()
86+
_ = p.lexer.consumeToken()
87+
return &Ident{
88+
NamePos: lastToken.Pos,
89+
NameEnd: lastToken.End,
90+
Name: lastToken.String,
91+
QuoteType: lastToken.QuoteType,
92+
}
93+
}
94+
8195
func (p *Parser) parseIdent() (*Ident, error) {
8296
lastToken, err := p.consumeTokenKind(TokenKindIdent)
8397
if err != nil {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Origin SQL:
2+
SELECT field0, field1 as x, field2 y from events;
3+
4+
-- Format SQL:
5+
SELECT field0, field1 AS x, field2 AS y FROM events;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
[
2+
{
3+
"SelectPos": 0,
4+
"StatementEnd": 48,
5+
"With": null,
6+
"Top": null,
7+
"SelectItems": [
8+
{
9+
"Expr": {
10+
"Name": "field0",
11+
"QuoteType": 1,
12+
"NamePos": 7,
13+
"NameEnd": 13
14+
},
15+
"Modifiers": [],
16+
"Alias": null
17+
},
18+
{
19+
"Expr": {
20+
"Name": "field1",
21+
"QuoteType": 1,
22+
"NamePos": 15,
23+
"NameEnd": 21
24+
},
25+
"Modifiers": [],
26+
"Alias": {
27+
"Name": "x",
28+
"QuoteType": 1,
29+
"NamePos": 25,
30+
"NameEnd": 26
31+
}
32+
},
33+
{
34+
"Expr": {
35+
"Name": "field2",
36+
"QuoteType": 1,
37+
"NamePos": 28,
38+
"NameEnd": 34
39+
},
40+
"Modifiers": [],
41+
"Alias": {
42+
"Name": "y",
43+
"QuoteType": 1,
44+
"NamePos": 35,
45+
"NameEnd": 36
46+
}
47+
}
48+
],
49+
"From": {
50+
"FromPos": 37,
51+
"Expr": {
52+
"Table": {
53+
"TablePos": 42,
54+
"TableEnd": 48,
55+
"Alias": null,
56+
"Expr": {
57+
"Database": null,
58+
"Table": {
59+
"Name": "events",
60+
"QuoteType": 1,
61+
"NamePos": 42,
62+
"NameEnd": 48
63+
}
64+
},
65+
"HasFinal": false
66+
},
67+
"StatementEnd": 48,
68+
"SampleRatio": null,
69+
"HasFinal": false
70+
}
71+
},
72+
"ArrayJoin": null,
73+
"Window": null,
74+
"Prewhere": null,
75+
"Where": null,
76+
"GroupBy": null,
77+
"WithTotal": false,
78+
"Having": null,
79+
"OrderBy": null,
80+
"LimitBy": null,
81+
"Limit": null,
82+
"Settings": null,
83+
"Format": null,
84+
"UnionAll": null,
85+
"UnionDistinct": null,
86+
"Except": null
87+
}
88+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT field0, field1 as x, field2 y from events;

0 commit comments

Comments
 (0)