Skip to content

Commit 466374b

Browse files
committed
Merge remote-tracking branch 'upstream/master' into parameterized-view-call-arguments
# Conflicts: # parser/parser_test.go
2 parents 2315cd0 + 530cefa commit 466374b

17 files changed

Lines changed: 1088 additions & 12 deletions

‎parser/ast.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4955,13 +4955,20 @@ type LimitClause struct {
49554955
LimitPos Pos
49564956
Limit Expr
49574957
Offset Expr
4958+
// WithTies is set for LIMIT n WITH TIES and LIMIT m, n WITH TIES.
4959+
WithTies bool
4960+
// WithTiesEnd is the end of the TIES keyword when WithTies is set.
4961+
WithTiesEnd Pos
49584962
}
49594963

49604964
func (l *LimitClause) Pos() Pos {
49614965
return l.LimitPos
49624966
}
49634967

49644968
func (l *LimitClause) End() Pos {
4969+
if l.WithTies {
4970+
return l.WithTiesEnd
4971+
}
49654972
if l.Offset != nil {
49664973
return l.Offset.End()
49674974
}

‎parser/format.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,17 @@ func (l *LimitByClause) FormatSQL(formatter *Formatter) {
19211921
}
19221922

19231923
func (l *LimitClause) FormatSQL(formatter *Formatter) {
1924+
if l.WithTies {
1925+
// WITH TIES is not valid after OFFSET, so keep the comma form.
1926+
formatter.WriteString("LIMIT ")
1927+
if l.Offset != nil {
1928+
formatter.WriteExpr(l.Offset)
1929+
formatter.WriteString(", ")
1930+
}
1931+
formatter.WriteExpr(l.Limit)
1932+
formatter.WriteString(" WITH TIES")
1933+
return
1934+
}
19241935
if l.Limit != nil {
19251936
formatter.WriteString("LIMIT ")
19261937
formatter.WriteExpr(l.Limit)

‎parser/parser_query.go‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ func (p *Parser) parseJoinType(_ Pos) []string {
196196
modifiers = append(modifiers, p.current().String)
197197
_ = p.lexer.consumeToken()
198198
}
199-
case p.matchKeyword(KeywordSemi), p.matchKeyword(KeywordAsof):
199+
case p.matchKeyword(KeywordSemi), p.matchKeyword(KeywordAnti), p.matchKeyword(KeywordAsof):
200+
// Strictness may come first, as in ANTI LEFT JOIN. ClickHouse prints
201+
// joins in this order, so stored view definitions use it.
200202
modifiers = append(modifiers, p.current().String)
201203
_ = p.lexer.consumeToken()
202204
if p.matchKeyword(KeywordLeft) || p.matchKeyword(KeywordRight) {
@@ -692,19 +694,28 @@ func (p *Parser) parseLimitClause(pos Pos) (*LimitClause, error) {
692694
var limit Expr
693695
var offset Expr
694696
var err error
697+
var withTies bool
698+
var withTiesEnd Pos
695699
if p.tryConsumeKeywords(KeywordLimit) {
696700
limit, err = p.parseExpr(p.Pos())
697701
if err != nil {
698702
return nil, err
699703
}
700704

705+
// ClickHouse accepts WITH TIES after LIMIT n and LIMIT m, n, but not
706+
// after LIMIT n OFFSET m.
701707
if p.tryConsumeKeywords(KeywordOffset) {
702708
offset, err = p.parseExpr(p.Pos())
703709
} else if token, consumeErr := p.tryConsumeTokenKind(TokenKindComma); consumeErr != nil {
704710
return nil, consumeErr
705711
} else if token != nil {
706712
offset = limit
707713
limit, err = p.parseExpr(p.Pos())
714+
if err == nil {
715+
withTiesEnd, withTies = p.tryConsumeWithTies()
716+
}
717+
} else {
718+
withTiesEnd, withTies = p.tryConsumeWithTies()
708719
}
709720
} else if p.tryConsumeKeywords(KeywordOffset) {
710721
offset, err = p.parseExpr(p.Pos())
@@ -715,12 +726,31 @@ func (p *Parser) parseLimitClause(pos Pos) (*LimitClause, error) {
715726
}
716727

717728
return &LimitClause{
718-
LimitPos: pos,
719-
Limit: limit,
720-
Offset: offset,
729+
LimitPos: pos,
730+
Limit: limit,
731+
Offset: offset,
732+
WithTies: withTies,
733+
WithTiesEnd: withTiesEnd,
721734
}, nil
722735
}
723736

737+
// tryConsumeWithTies consumes WITH TIES and returns the end of TIES. WITH also
738+
// starts other clauses, so the lexer is rewound unless TIES follows.
739+
func (p *Parser) tryConsumeWithTies() (Pos, bool) {
740+
if !p.matchKeyword(KeywordWith) {
741+
return 0, false
742+
}
743+
savedState := p.lexer.saveState()
744+
_ = p.lexer.consumeToken()
745+
if !p.matchKeyword(KeywordTies) {
746+
p.lexer.restoreState(savedState)
747+
return 0, false
748+
}
749+
withTiesEnd := p.End()
750+
_ = p.lexer.consumeToken()
751+
return withTiesEnd, true
752+
}
753+
724754
func (p *Parser) tryParseLimitByClause(pos Pos) (Expr, error) {
725755
if !p.matchKeyword(KeywordLimit) {
726756
return nil, nil
@@ -764,9 +794,13 @@ func (p *Parser) parseLimitByClause(pos Pos) (Expr, error) {
764794
}
765795

766796
var by *ColumnExprList
767-
if !p.tryConsumeKeywords(KeywordBy) {
797+
if !p.matchKeyword(KeywordBy) {
768798
return limit, nil
769799
}
800+
if limit.WithTies {
801+
return nil, fmt.Errorf("WITH TIES cannot be combined with LIMIT BY")
802+
}
803+
_ = p.lexer.consumeToken()
770804
if by, err = p.parseColumnExprListWithLParen(p.Pos()); err != nil {
771805
return nil, err
772806
}

‎parser/parser_test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ func TestParser_InvalidSyntax(t *testing.T) {
290290
"(SELECT 1) SETTINGS max_threads=1 UNION ALL SELECT 2",
291291
// AS at the very end of an ORDER BY item leaves no token to check for SELECT
292292
"SELECT * FROM t ORDER BY a AS",
293+
// WITH TIES follows LIMIT n or LIMIT m, n only
294+
"SELECT a FROM t ORDER BY a LIMIT 2 OFFSET 1 WITH TIES",
295+
"SELECT a FROM t ORDER BY a LIMIT 2 WITH TIES OFFSET 1",
296+
"SELECT a FROM t ORDER BY a OFFSET 1 WITH TIES",
297+
"SELECT a FROM t ORDER BY a LIMIT 2 WITH",
298+
"SELECT a FROM t ORDER BY a LIMIT 1 WITH TIES BY a",
299+
// a leading SEMI or ANTI takes LEFT or RIGHT only
300+
"SELECT * FROM t1 ANTI INNER JOIN t2 ON t1.a = t2.a",
301+
"SELECT * FROM t1 ANTI ANY LEFT JOIN t2 ON t1.a = t2.a",
293302
// arguments of a parameterized view call
294303
"SELECT * FROM db.v(a = {x})", // query parameter without a type
295304
"SELECT * FROM db.v(a = {'k': 1})", // not a query parameter
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Origin SQL:
2+
SELECT id FROM t ORDER BY ts LIMIT 10 WITH TIES;
3+
SELECT id FROM t ORDER BY ts LIMIT 5, 10 WITH TIES;
4+
SELECT id FROM t ORDER BY ts LIMIT 1 BY id LIMIT 10 WITH TIES;
5+
SELECT id FROM (SELECT id, ts FROM t ORDER BY ts LIMIT 10 WITH TIES) GROUP BY id
6+
7+
8+
-- Beautify SQL:
9+
SELECT
10+
id
11+
FROM
12+
t
13+
ORDER BY
14+
ts
15+
LIMIT 10 WITH TIES;
16+
SELECT
17+
id
18+
FROM
19+
t
20+
ORDER BY
21+
ts
22+
LIMIT 5, 10 WITH TIES;
23+
SELECT
24+
id
25+
FROM
26+
t
27+
ORDER BY
28+
ts
29+
LIMIT 1 BY id
30+
LIMIT 10 WITH TIES;
31+
SELECT
32+
id
33+
FROM
34+
(SELECT
35+
id,
36+
ts
37+
FROM
38+
t
39+
ORDER BY
40+
ts
41+
LIMIT 10 WITH TIES)
42+
GROUP BY
43+
id;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Origin SQL:
2+
SELECT a.id
3+
FROM t1 AS a
4+
ANTI LEFT JOIN t2 AS b ON a.id = b.id
5+
ANTI RIGHT JOIN t3 AS c ON a.id = c.id
6+
GLOBAL ANTI LEFT OUTER JOIN t4 AS d ON a.id = d.id
7+
ANTI JOIN t5 AS e ON a.id = e.id
8+
9+
10+
-- Beautify SQL:
11+
SELECT
12+
a.id
13+
FROM
14+
t1 AS a
15+
ANTI LEFT JOIN
16+
t2 AS b ON a.id = b.id
17+
ANTI RIGHT JOIN
18+
t3 AS c ON a.id = c.id
19+
GLOBAL ANTI LEFT OUTER JOIN
20+
t4 AS d ON a.id = d.id
21+
ANTI JOIN
22+
t5 AS e ON a.id = e.id;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Origin SQL:
2+
SELECT id FROM t ORDER BY ts LIMIT 10 WITH TIES;
3+
SELECT id FROM t ORDER BY ts LIMIT 5, 10 WITH TIES;
4+
SELECT id FROM t ORDER BY ts LIMIT 1 BY id LIMIT 10 WITH TIES;
5+
SELECT id FROM (SELECT id, ts FROM t ORDER BY ts LIMIT 10 WITH TIES) GROUP BY id
6+
7+
8+
-- Format SQL:
9+
SELECT id FROM t ORDER BY ts LIMIT 10 WITH TIES;
10+
SELECT id FROM t ORDER BY ts LIMIT 5, 10 WITH TIES;
11+
SELECT id FROM t ORDER BY ts LIMIT 1 BY id LIMIT 10 WITH TIES;
12+
SELECT id FROM (SELECT id, ts FROM t ORDER BY ts LIMIT 10 WITH TIES) GROUP BY id;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Origin SQL:
2+
SELECT a.id
3+
FROM t1 AS a
4+
ANTI LEFT JOIN t2 AS b ON a.id = b.id
5+
ANTI RIGHT JOIN t3 AS c ON a.id = c.id
6+
GLOBAL ANTI LEFT OUTER JOIN t4 AS d ON a.id = d.id
7+
ANTI JOIN t5 AS e ON a.id = e.id
8+
9+
10+
-- Format SQL:
11+
SELECT a.id FROM t1 AS a ANTI LEFT JOIN t2 AS b ON a.id = b.id ANTI RIGHT JOIN t3 AS c ON a.id = c.id GLOBAL ANTI LEFT OUTER JOIN t4 AS d ON a.id = d.id ANTI JOIN t5 AS e ON a.id = e.id;

‎parser/testdata/query/output/select_extract_with_regex.sql.golden.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@
442442
"Literal": "10000",
443443
"Base": 10
444444
},
445-
"Offset": null
445+
"Offset": null,
446+
"WithTies": false,
447+
"WithTiesEnd": 0
446448
},
447449
"Settings": null,
448450
"Format": null,

0 commit comments

Comments
 (0)