@@ -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+
724754func (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 }
0 commit comments