Skip to content

Commit

Permalink
Add support of the format clause for the select stmt (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
laojianzi authored Jul 16, 2024
1 parent f770c15 commit ee8419d
Show file tree
Hide file tree
Showing 29 changed files with 71 additions and 2 deletions.
10 changes: 10 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4942,6 +4942,7 @@ type SelectQuery struct {
LimitBy *LimitByClause
Limit *LimitClause
Settings *SettingsClause
Format *FormatClause
UnionAll *SelectQuery
UnionDistinct *SelectQuery
Except *SelectQuery
Expand Down Expand Up @@ -5026,6 +5027,10 @@ func (s *SelectQuery) String(level int) string { // nolint: funlen
builder.WriteString(NewLine(level))
builder.WriteString(s.Settings.String(level))
}
if s.Format != nil {
builder.WriteString(NewLine(level))
builder.WriteString(s.Format.String(level))
}
if s.UnionAll != nil {
builder.WriteString(NewLine(level))
builder.WriteString(" UNION ALL ")
Expand Down Expand Up @@ -5115,6 +5120,11 @@ func (s *SelectQuery) Accept(visitor ASTVisitor) error {
return err
}
}
if s.Format != nil {
if err := s.Format.Accept(visitor); err != nil {
return err
}
}
if s.UnionAll != nil {
if err := s.UnionAll.Accept(visitor); err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions parser/parser_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,14 @@ func (p *Parser) parseSelectStmt(pos Pos) (*SelectQuery, error) { // nolint: fun
statementEnd = settings.End()
}

format, err := p.tryParseFormat(p.Pos())
if err != nil {
return nil, err
}
if format != nil {
statementEnd = format.End()
}

return &SelectQuery{
With: withClause,
SelectPos: pos,
Expand All @@ -924,6 +932,7 @@ func (p *Parser) parseSelectStmt(pos Pos) (*SelectQuery, error) { // nolint: fun
LimitBy: limitBy,
Limit: limit,
Settings: settings,
Format: format,
WithTotal: withTotal,
}, nil
}
Expand Down
1 change: 1 addition & 0 deletions parser/testdata/ddl/output/bug_001.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -476,6 +477,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
3 changes: 2 additions & 1 deletion parser/testdata/query/format/select_with_union_distinct.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ FROM
SELECT
replica_name
FROM
system.ha_unique_replicas;
system.ha_unique_replicas
FORMAT JSON;
4 changes: 4 additions & 0 deletions parser/testdata/query/output/select_cast.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -104,6 +105,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -168,6 +170,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -222,6 +225,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
1 change: 1 addition & 0 deletions parser/testdata/query/output/select_simple.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@
},
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -204,6 +205,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -131,6 +132,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -275,6 +277,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -105,6 +106,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -209,6 +211,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"Offset": null
},
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -103,6 +104,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -154,6 +156,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -419,6 +422,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down Expand Up @@ -108,6 +109,7 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": {
"SelectPos": 59,
"StatementEnd": 109,
"StatementEnd": 121,
"With": null,
"Top": null,
"SelectColumns": {
Expand Down Expand Up @@ -114,6 +115,15 @@
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": {
"FormatPos": 110,
"Format": {
"Name": "JSON",
"QuoteType": 1,
"NamePos": 117,
"NameEnd": 121
}
},
"UnionAll": null,
"UnionDistinct": null,
"Except": null
Expand Down
Loading

0 comments on commit ee8419d

Please sign in to comment.