Skip to content

Commit ee8419d

Browse files
authored
Add support of the format clause for the select stmt (#79)
1 parent f770c15 commit ee8419d

29 files changed

+71
-2
lines changed

parser/ast.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,6 +4942,7 @@ type SelectQuery struct {
49424942
LimitBy *LimitByClause
49434943
Limit *LimitClause
49444944
Settings *SettingsClause
4945+
Format *FormatClause
49454946
UnionAll *SelectQuery
49464947
UnionDistinct *SelectQuery
49474948
Except *SelectQuery
@@ -5026,6 +5027,10 @@ func (s *SelectQuery) String(level int) string { // nolint: funlen
50265027
builder.WriteString(NewLine(level))
50275028
builder.WriteString(s.Settings.String(level))
50285029
}
5030+
if s.Format != nil {
5031+
builder.WriteString(NewLine(level))
5032+
builder.WriteString(s.Format.String(level))
5033+
}
50295034
if s.UnionAll != nil {
50305035
builder.WriteString(NewLine(level))
50315036
builder.WriteString(" UNION ALL ")
@@ -5115,6 +5120,11 @@ func (s *SelectQuery) Accept(visitor ASTVisitor) error {
51155120
return err
51165121
}
51175122
}
5123+
if s.Format != nil {
5124+
if err := s.Format.Accept(visitor); err != nil {
5125+
return err
5126+
}
5127+
}
51185128
if s.UnionAll != nil {
51195129
if err := s.UnionAll.Accept(visitor); err != nil {
51205130
return err

parser/parser_query.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,14 @@ func (p *Parser) parseSelectStmt(pos Pos) (*SelectQuery, error) { // nolint: fun
907907
statementEnd = settings.End()
908908
}
909909

910+
format, err := p.tryParseFormat(p.Pos())
911+
if err != nil {
912+
return nil, err
913+
}
914+
if format != nil {
915+
statementEnd = format.End()
916+
}
917+
910918
return &SelectQuery{
911919
With: withClause,
912920
SelectPos: pos,
@@ -924,6 +932,7 @@ func (p *Parser) parseSelectStmt(pos Pos) (*SelectQuery, error) { // nolint: fun
924932
LimitBy: limitBy,
925933
Limit: limit,
926934
Settings: settings,
935+
Format: format,
927936
WithTotal: withTotal,
928937
}, nil
929938
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@
501501
"LimitBy": null,
502502
"Limit": null,
503503
"Settings": null,
504+
"Format": null,
504505
"UnionAll": null,
505506
"UnionDistinct": null,
506507
"Except": null

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
@@ -127,6 +127,7 @@
127127
"LimitBy": null,
128128
"Limit": null,
129129
"Settings": null,
130+
"Format": null,
130131
"UnionAll": null,
131132
"UnionDistinct": null,
132133
"Except": null

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@
486486
"LimitBy": null,
487487
"Limit": null,
488488
"Settings": null,
489+
"Format": null,
489490
"UnionAll": null,
490491
"UnionDistinct": null,
491492
"Except": null

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
@@ -427,6 +427,7 @@
427427
"LimitBy": null,
428428
"Limit": null,
429429
"Settings": null,
430+
"Format": null,
430431
"UnionAll": null,
431432
"UnionDistinct": null,
432433
"Except": null
@@ -476,6 +477,7 @@
476477
"LimitBy": null,
477478
"Limit": null,
478479
"Settings": null,
480+
"Format": null,
479481
"UnionAll": null,
480482
"UnionDistinct": null,
481483
"Except": null

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
"LimitBy": null,
139139
"Limit": null,
140140
"Settings": null,
141+
"Format": null,
141142
"UnionAll": null,
142143
"UnionDistinct": null,
143144
"Except": null

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
@@ -93,6 +93,7 @@
9393
"LimitBy": null,
9494
"Limit": null,
9595
"Settings": null,
96+
"Format": null,
9697
"UnionAll": null,
9798
"UnionDistinct": null,
9899
"Except": null

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"LimitBy": null,
9494
"Limit": null,
9595
"Settings": null,
96+
"Format": null,
9697
"UnionAll": null,
9798
"UnionDistinct": null,
9899
"Except": null

parser/testdata/query/format/select_with_union_distinct.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ FROM
1111
SELECT
1212
replica_name
1313
FROM
14-
system.ha_unique_replicas;
14+
system.ha_unique_replicas
15+
FORMAT JSON;

0 commit comments

Comments
 (0)