Skip to content

Commit

Permalink
Fix should allow the table alias without the AS keyword (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk authored Jun 27, 2024
1 parent 64841d1 commit 61b3867
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 44 deletions.
3 changes: 1 addition & 2 deletions parser/parser_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ func (p *Parser) parseExpr(pos Pos) (Expr, error) {
}
switch {
case p.matchKeyword(KeywordAs): // syntax: columnExpr (alias | AS identifier)
aliasPos := p.Pos()
_ = p.lexer.consumeToken()
alias, err := p.parseIdent()
if err != nil {
return nil, err
}
return &AliasExpr{
AliasPos: aliasPos,
AliasPos: alias.Pos(),
Expr: orExpr,
Alias: alias,
}, nil
Expand Down
13 changes: 12 additions & 1 deletion parser/parser_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,18 @@ func (p *Parser) parseTableExpr(pos Pos) (*TableExpr, error) {
}
expr = &AliasExpr{
Expr: expr,
AliasPos: asToken.Pos,
AliasPos: alias.Pos(),
Alias: alias,
}
tableEnd = expr.End()
} else if p.matchTokenKind(TokenIdent) && p.lastTokenKind() != TokenKeyword {
alias, err := p.parseIdent()
if err != nil {
return nil, err
}
expr = &AliasExpr{
Expr: expr,
AliasPos: alias.Pos(),
Alias: alias,
}
tableEnd = expr.End()
Expand Down
18 changes: 9 additions & 9 deletions parser/testdata/ddl/output/bug_001.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"ColumnArgList": null
}
},
"AliasPos": 185,
"AliasPos": 188,
"Alias": {
"Name": "x",
"QuoteType": 1,
Expand Down Expand Up @@ -140,7 +140,7 @@
"ColumnArgList": null
}
},
"AliasPos": 236,
"AliasPos": 239,
"Alias": {
"Name": "y",
"QuoteType": 1,
Expand Down Expand Up @@ -180,7 +180,7 @@
"ColumnArgList": null
}
},
"AliasPos": 287,
"AliasPos": 290,
"Alias": {
"Name": "z",
"QuoteType": 1,
Expand Down Expand Up @@ -220,7 +220,7 @@
"ColumnArgList": null
}
},
"AliasPos": 338,
"AliasPos": 341,
"Alias": {
"Name": "a",
"QuoteType": 1,
Expand Down Expand Up @@ -260,7 +260,7 @@
"ColumnArgList": null
}
},
"AliasPos": 389,
"AliasPos": 392,
"Alias": {
"Name": "b",
"QuoteType": 1,
Expand Down Expand Up @@ -300,7 +300,7 @@
"ColumnArgList": null
}
},
"AliasPos": 440,
"AliasPos": 443,
"Alias": {
"Name": "c",
"QuoteType": 1,
Expand Down Expand Up @@ -340,7 +340,7 @@
"ColumnArgList": null
}
},
"AliasPos": 491,
"AliasPos": 494,
"Alias": {
"Name": "d",
"QuoteType": 1,
Expand Down Expand Up @@ -380,7 +380,7 @@
"ColumnArgList": null
}
},
"AliasPos": 539,
"AliasPos": 542,
"Alias": {
"Name": "e",
"QuoteType": 1,
Expand Down Expand Up @@ -420,7 +420,7 @@
"ColumnArgList": null
}
},
"AliasPos": 587,
"AliasPos": 590,
"Alias": {
"Name": "f",
"QuoteType": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
"ColumnArgList": null
}
},
"AliasPos": 281,
"AliasPos": 284,
"Alias": {
"Name": "f3",
"QuoteType": 1,
Expand Down Expand Up @@ -325,7 +325,7 @@
"ColumnArgList": null
}
},
"AliasPos": 342,
"AliasPos": 345,
"Alias": {
"Name": "f4",
"QuoteType": 1,
Expand Down Expand Up @@ -365,7 +365,7 @@
"ColumnArgList": null
}
},
"AliasPos": 401,
"AliasPos": 404,
"Alias": {
"Name": "f5",
"QuoteType": 1,
Expand Down Expand Up @@ -405,7 +405,7 @@
"ColumnArgList": null
}
},
"AliasPos": 451,
"AliasPos": 454,
"Alias": {
"Name": "f6",
"QuoteType": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"ColumnArgList": null
}
},
"AliasPos": 236,
"AliasPos": 239,
"Alias": {
"Name": "f333",
"QuoteType": 1,
Expand Down Expand Up @@ -317,7 +317,7 @@
"Frame": null
}
},
"AliasPos": 349,
"AliasPos": 352,
"Alias": {
"Name": "rn",
"QuoteType": 1,
Expand Down Expand Up @@ -431,7 +431,7 @@
"UnionDistinct": null,
"Except": null
},
"AliasPos": 441,
"AliasPos": 444,
"Alias": {
"Name": "tmp",
"QuoteType": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Origin SQL:
SELECT t1.Timestamp FROM my_table t1 INNER JOIN my_other_table t2 ON t1.a=t2.b

-- Format SQL:

SELECT
t1.Timestamp
FROM
my_table AS t1
INNER JOIN my_other_table AS t2 ON t1.a = t2.b;
10 changes: 5 additions & 5 deletions parser/testdata/query/output/select_cast.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
}
},
"AliasPos": 26,
"AliasPos": 29,
"Alias": {
"Name": "value",
"QuoteType": 1,
Expand Down Expand Up @@ -82,7 +82,7 @@
"Literal": "Float64"
}
},
"AliasPos": 62,
"AliasPos": 65,
"Alias": {
"Name": "value",
"QuoteType": 1,
Expand Down Expand Up @@ -134,7 +134,7 @@
"Literal": "1",
"Base": 10
},
"AliasPos": 82,
"AliasPos": 85,
"Alias": {
"Name": "Float64",
"QuoteType": 1,
Expand All @@ -146,7 +146,7 @@
},
"ColumnArgList": null
},
"AliasPos": 94,
"AliasPos": 97,
"Alias": {
"Name": "value",
"QuoteType": 1,
Expand Down Expand Up @@ -200,7 +200,7 @@
"HasGlobal": false,
"HasNot": false
},
"AliasPos": 122,
"AliasPos": 125,
"Alias": {
"Name": "value",
"QuoteType": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"LiteralEnd": 11,
"Literal": "abc"
},
"AliasPos": 13,
"AliasPos": 17,
"Alias": {
"Name": "value2",
"QuoteType": 2,
Expand Down
4 changes: 2 additions & 2 deletions parser/testdata/query/output/select_simple.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"ColumnArgList": null
}
},
"AliasPos": 32,
"AliasPos": 35,
"Alias": {
"Name": "f3",
"QuoteType": 1,
Expand Down Expand Up @@ -116,7 +116,7 @@
"Frame": null
}
},
"AliasPos": 91,
"AliasPos": 94,
"Alias": {
"Name": "rn",
"QuoteType": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"ColumnArgList": null
}
},
"AliasPos": 43,
"AliasPos": 46,
"Alias": {
"Name": "res",
"QuoteType": 1,
Expand Down Expand Up @@ -133,7 +133,7 @@
}
}
},
"AliasPos": 61,
"AliasPos": 64,
"Alias": {
"Name": "f2",
"QuoteType": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"NamePos": 71,
"NameEnd": 73
},
"AliasPos": 74,
"AliasPos": 77,
"Alias": {
"Name": "new_f1",
"QuoteType": 1,
Expand All @@ -145,7 +145,7 @@
"NamePos": 89,
"NameEnd": 91
},
"AliasPos": 92,
"AliasPos": 95,
"Alias": {
"Name": "new_f2",
"QuoteType": 1,
Expand All @@ -160,7 +160,7 @@
"NamePos": 107,
"NameEnd": 109
},
"AliasPos": 110,
"AliasPos": 113,
"Alias": {
"Name": "new_f3",
"QuoteType": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"NamePos": 16,
"NameEnd": 18
},
"AliasPos": 19,
"AliasPos": 22,
"Alias": {
"Name": "a0",
"QuoteType": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"NamePos": 16,
"NameEnd": 18
},
"AliasPos": 19,
"AliasPos": 22,
"Alias": {
"Name": "a0",
"QuoteType": 1,
Expand Down
Loading

0 comments on commit 61b3867

Please sign in to comment.