Skip to content

Commit e45d112

Browse files
authored
Support UNION DISTINCT with subquery (#31)
1 parent c49fa96 commit e45d112

18 files changed

+164
-8
lines changed

parser/ast.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,6 +3008,7 @@ type SelectQuery struct {
30083008
Limit *LimitExpr
30093009
Settings *SettingsExprList
30103010
UnionAll *SelectQuery
3011+
UnionDistinct *SelectQuery
30113012
Except *SelectQuery
30123013
}
30133014

@@ -3090,6 +3091,10 @@ func (s *SelectQuery) String(level int) string { // nolint: funlen
30903091
builder.WriteString(NewLine(level))
30913092
builder.WriteString(" UNION ALL ")
30923093
builder.WriteString(s.UnionAll.String(level))
3094+
} else if s.UnionDistinct != nil {
3095+
builder.WriteString(NewLine(level))
3096+
builder.WriteString(" UNION DISTINCT ")
3097+
builder.WriteString(s.UnionDistinct.String(level))
30933098
} else if s.Except != nil {
30943099
builder.WriteString(NewLine(level))
30953100
builder.WriteString(" EXCEPT ")

parser/parser_query.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -671,20 +671,28 @@ func (p *Parser) parseSelectQuery(_ Pos) (*SelectQuery, error) {
671671
}
672672
switch {
673673
case p.tryConsumeKeyword(KeywordUnion) != nil:
674-
if err := p.consumeKeyword(KeywordAll); err != nil {
675-
return nil, err
676-
}
677-
unionAllExpr, err := p.parseSelectStatement(p.Pos())
678-
if err != nil {
679-
return nil, err
674+
switch {
675+
case p.tryConsumeKeyword(KeywordAll) != nil:
676+
unionAllExpr, err := p.parseSelectStatement(p.Pos())
677+
if err != nil {
678+
return nil, err
679+
}
680+
selectExpr.UnionAll = unionAllExpr
681+
case p.tryConsumeKeyword(KeywordDistinct) != nil:
682+
unionDistinctExpr, err := p.parseSelectStatement(p.Pos())
683+
if err != nil {
684+
return nil, err
685+
}
686+
selectExpr.UnionDistinct = unionDistinctExpr
687+
default:
688+
return nil, fmt.Errorf("expected ALL or DISTINCT, got %s", p.lastTokenKind())
680689
}
681-
selectExpr.UnionAll = unionAllExpr
682690
case p.tryConsumeKeyword(KeywordExcept) != nil:
683691
exceptExpr, err := p.parseSelectStatement(p.Pos())
684692
if err != nil {
685693
return nil, err
686694
}
687-
selectExpr.UnionAll = exceptExpr
695+
selectExpr.Except = exceptExpr
688696
}
689697
if hasParen {
690698
if _, err := p.consumeTokenKind(")"); err != nil {

parser/testdata/ddl/output/create_live_view_basic.sql.golden.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
"Limit": null,
119119
"Settings": null,
120120
"UnionAll": null,
121+
"UnionDistinct": null,
121122
"Except": null
122123
}
123124
}

parser/testdata/ddl/output/create_materialized_view_with_empty_table_schema.sql.golden.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@
423423
"Limit": null,
424424
"Settings": null,
425425
"UnionAll": null,
426+
"UnionDistinct": null,
426427
"Except": null
427428
},
428429
"AliasPos": 441,
@@ -466,6 +467,7 @@
466467
"Limit": null,
467468
"Settings": null,
468469
"UnionAll": null,
470+
"UnionDistinct": null,
469471
"Except": null
470472
}
471473
},

parser/testdata/ddl/output/create_view_basic.sql.golden.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
"Limit": null,
128128
"Settings": null,
129129
"UnionAll": null,
130+
"UnionDistinct": null,
130131
"Except": null
131132
}
132133
}

parser/testdata/ddl/output/create_view_on_cluster_with_uuid.sql.golden.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"Limit": null,
8989
"Settings": null,
9090
"UnionAll": null,
91+
"UnionDistinct": null,
9192
"Except": null
9293
}
9394
}

parser/testdata/dml/output/insert_with_select.sql.golden.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"Limit": null,
8989
"Settings": null,
9090
"UnionAll": null,
91+
"UnionDistinct": null,
9192
"Except": null
9293
}
9394
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Origin SQL:
2+
SELECT replica_name FROM system.ha_replicas UNION DISTINCT SELECT replica_name FROM system.ha_unique_replicas format JSON
3+
4+
-- Format SQL:
5+
6+
SELECT
7+
replica_name
8+
FROM
9+
system.ha_replicas
10+
UNION DISTINCT
11+
SELECT
12+
replica_name
13+
FROM
14+
system.ha_unique_replicas;

parser/testdata/query/output/select_simple.sql.golden.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@
375375
"Limit": null,
376376
"Settings": null,
377377
"UnionAll": null,
378+
"UnionDistinct": null,
378379
"Except": null
379380
}
380381
]

parser/testdata/query/output/select_simple_with_bracket.sql.golden.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
"Limit": null,
156156
"Settings": null,
157157
"UnionAll": null,
158+
"UnionDistinct": null,
158159
"Except": null
159160
}
160161
]

0 commit comments

Comments
 (0)