Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion parser/parser_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,14 @@ func (p *Parser) parseSelectItem() (*SelectItem, error) {

modifiers := make([]*FunctionExpr, 0)
for {
if p.matchKeyword(KeywordExcept) || p.matchKeyword(KeywordApply) || p.matchKeyword(KeywordReplace) {
if p.matchKeyword(KeywordExcept) && !p.peekTokenKind(TokenKindLParen) {
// ClickHouse also accepts a single column without brackets: `* EXCEPT col`.
modifier, err := p.parseExceptColumn()
if err != nil {
return nil, err
}
modifiers = append(modifiers, modifier)
} else if p.matchKeyword(KeywordExcept) || p.matchKeyword(KeywordApply) || p.matchKeyword(KeywordReplace) {
modifier, err := p.parseFunctionExpr(p.Pos())
if err != nil {
return nil, err
Expand Down Expand Up @@ -1226,6 +1233,30 @@ func (p *Parser) parseSelectItem() (*SelectItem, error) {
}, nil
}

// parseExceptColumn parses `EXCEPT col` into the same modifier as `EXCEPT (col)`.
func (p *Parser) parseExceptColumn() (*FunctionExpr, error) {
name, err := p.parseAnyKeyword()
if err != nil {
return nil, err
}
column, err := p.parseIdent()
if err != nil {
return nil, err
}
return &FunctionExpr{
Name: name,
Params: &ParamExprList{
LeftParenPos: column.Pos(),
RightParenPos: column.End(),
Items: &ColumnExprList{
ListPos: column.Pos(),
ListEnd: column.End(),
Items: []Expr{column},
},
},
}, nil
}

func (p *Parser) parseColumnCaseExpr(pos Pos) (*CaseExpr, error) {
// CASE expr
caseExpr := &CaseExpr{CasePos: pos}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Origin SQL:
SELECT * EXCEPT b FROM t;
SELECT t.* EXCEPT b, c FROM t;
SELECT * EXCEPT b EXCEPT (c, d) FROM t;


-- Beautify SQL:
SELECT
* EXCEPT(b)
FROM
t;
SELECT
t.* EXCEPT(b),
c
FROM
t;
SELECT
* EXCEPT(b) EXCEPT(c, d)
FROM
t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Origin SQL:
SELECT * EXCEPT b FROM t;
SELECT t.* EXCEPT b, c FROM t;
SELECT * EXCEPT b EXCEPT (c, d) FROM t;


-- Format SQL:
SELECT * EXCEPT(b) FROM t;
SELECT t.* EXCEPT(b), c FROM t;
SELECT * EXCEPT(b) EXCEPT(c, d) FROM t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
[
{
"SelectPos": 0,
"StatementEnd": 24,
"With": null,
"Top": null,
"HasDistinct": false,
"DistinctOn": null,
"SelectItems": [
{
"Expr": {
"Name": "*",
"QuoteType": 0,
"NamePos": 7,
"NameEnd": 8
},
"Modifiers": [
{
"Name": {
"Name": "EXCEPT",
"QuoteType": 1,
"NamePos": 9,
"NameEnd": 15
},
"Params": {
"LeftParenPos": 16,
"RightParenPos": 17,
"Items": {
"ListPos": 16,
"ListEnd": 17,
"HasDistinct": false,
"Items": [
{
"Name": "b",
"QuoteType": 1,
"NamePos": 16,
"NameEnd": 17
}
]
},
"ColumnArgList": null
}
}
],
"Alias": null
}
],
"From": {
"FromPos": 18,
"Expr": {
"Table": {
"TablePos": 23,
"TableEnd": 24,
"Alias": null,
"Expr": {
"Database": null,
"Table": {
"Name": "t",
"QuoteType": 1,
"NamePos": 23,
"NameEnd": 24
}
},
"HasFinal": false
},
"StatementEnd": 24,
"SampleRatio": null,
"HasFinal": false
}
},
"Window": null,
"Prewhere": null,
"Where": null,
"GroupBy": null,
"WithTotal": false,
"Having": null,
"Qualify": null,
"OrderBy": null,
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null,
"Intersect": null
},
{
"SelectPos": 26,
"StatementEnd": 55,
"With": null,
"Top": null,
"HasDistinct": false,
"DistinctOn": null,
"SelectItems": [
{
"Expr": {
"Ident": {
"Name": "t",
"QuoteType": 1,
"NamePos": 33,
"NameEnd": 34
},
"DotIdent": {
"Name": "*",
"QuoteType": 0,
"NamePos": 35,
"NameEnd": 36
}
},
"Modifiers": [
{
"Name": {
"Name": "EXCEPT",
"QuoteType": 1,
"NamePos": 37,
"NameEnd": 43
},
"Params": {
"LeftParenPos": 44,
"RightParenPos": 45,
"Items": {
"ListPos": 44,
"ListEnd": 45,
"HasDistinct": false,
"Items": [
{
"Name": "b",
"QuoteType": 1,
"NamePos": 44,
"NameEnd": 45
}
]
},
"ColumnArgList": null
}
}
],
"Alias": null
},
{
"Expr": {
"Name": "c",
"QuoteType": 1,
"NamePos": 47,
"NameEnd": 48
},
"Modifiers": [],
"Alias": null
}
],
"From": {
"FromPos": 49,
"Expr": {
"Table": {
"TablePos": 54,
"TableEnd": 55,
"Alias": null,
"Expr": {
"Database": null,
"Table": {
"Name": "t",
"QuoteType": 1,
"NamePos": 54,
"NameEnd": 55
}
},
"HasFinal": false
},
"StatementEnd": 55,
"SampleRatio": null,
"HasFinal": false
}
},
"Window": null,
"Prewhere": null,
"Where": null,
"GroupBy": null,
"WithTotal": false,
"Having": null,
"Qualify": null,
"OrderBy": null,
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null,
"Intersect": null
},
{
"SelectPos": 57,
"StatementEnd": 95,
"With": null,
"Top": null,
"HasDistinct": false,
"DistinctOn": null,
"SelectItems": [
{
"Expr": {
"Name": "*",
"QuoteType": 0,
"NamePos": 64,
"NameEnd": 65
},
"Modifiers": [
{
"Name": {
"Name": "EXCEPT",
"QuoteType": 1,
"NamePos": 66,
"NameEnd": 72
},
"Params": {
"LeftParenPos": 73,
"RightParenPos": 74,
"Items": {
"ListPos": 73,
"ListEnd": 74,
"HasDistinct": false,
"Items": [
{
"Name": "b",
"QuoteType": 1,
"NamePos": 73,
"NameEnd": 74
}
]
},
"ColumnArgList": null
}
},
{
"Name": {
"Name": "EXCEPT",
"QuoteType": 1,
"NamePos": 75,
"NameEnd": 81
},
"Params": {
"LeftParenPos": 82,
"RightParenPos": 87,
"Items": {
"ListPos": 83,
"ListEnd": 87,
"HasDistinct": false,
"Items": [
{
"Expr": {
"Name": "c",
"QuoteType": 1,
"NamePos": 83,
"NameEnd": 84
},
"Alias": null
},
{
"Expr": {
"Name": "d",
"QuoteType": 1,
"NamePos": 86,
"NameEnd": 87
},
"Alias": null
}
]
},
"ColumnArgList": null
}
}
],
"Alias": null
}
],
"From": {
"FromPos": 89,
"Expr": {
"Table": {
"TablePos": 94,
"TableEnd": 95,
"Alias": null,
"Expr": {
"Database": null,
"Table": {
"Name": "t",
"QuoteType": 1,
"NamePos": 94,
"NameEnd": 95
}
},
"HasFinal": false
},
"StatementEnd": 95,
"SampleRatio": null,
"HasFinal": false
}
},
"Window": null,
"Prewhere": null,
"Where": null,
"GroupBy": null,
"WithTotal": false,
"Having": null,
"Qualify": null,
"OrderBy": null,
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null,
"Intersect": null
}
]
Loading
Loading