Skip to content

Commit 8ca8a97

Browse files
authored
Add support of parsing SETTING boolean values (#213)
1 parent 42fd0bb commit 8ca8a97

13 files changed

+428
-7
lines changed

parser/ast.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4914,6 +4914,30 @@ func (s *StringLiteral) Accept(visitor ASTVisitor) error {
49144914
return visitor.VisitStringLiteral(s)
49154915
}
49164916

4917+
type BoolLiteral struct {
4918+
LiteralPos Pos
4919+
LiteralEnd Pos
4920+
Literal string
4921+
}
4922+
4923+
func (b *BoolLiteral) Pos() Pos {
4924+
return b.LiteralPos
4925+
}
4926+
4927+
func (b *BoolLiteral) End() Pos {
4928+
return b.LiteralEnd
4929+
}
4930+
4931+
func (b *BoolLiteral) String() string {
4932+
return b.Literal
4933+
}
4934+
4935+
func (b *BoolLiteral) Accept(visitor ASTVisitor) error {
4936+
visitor.Enter(b)
4937+
defer visitor.Leave(b)
4938+
return visitor.VisitBoolLiteral(b)
4939+
}
4940+
49174941
type PlaceHolder struct {
49184942
PlaceholderPos Pos
49194943
PlaceHolderEnd Pos

parser/ast_visitor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ type ASTVisitor interface {
192192
VisitSelectItem(expr *SelectItem) error
193193
VisitTargetPairExpr(expr *TargetPair) error
194194
VisitDistinctOn(expr *DistinctOn) error
195+
VisitBoolLiteral(expr *BoolLiteral) error
195196

196197
Enter(expr Expr)
197198
Leave(expr Expr)
@@ -1540,6 +1541,13 @@ func (v *DefaultASTVisitor) VisitDistinctOn(expr *DistinctOn) error {
15401541
return nil
15411542
}
15421543

1544+
func (v *DefaultASTVisitor) VisitBoolLiteral(expr *BoolLiteral) error {
1545+
if v.Visit != nil {
1546+
return v.Visit(expr)
1547+
}
1548+
return nil
1549+
}
1550+
15431551
func (v *DefaultASTVisitor) Enter(expr Expr) {}
15441552

15451553
func (v *DefaultASTVisitor) Leave(expr Expr) {}

parser/parse_system.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ func (p *Parser) parseSetStmt(pos Pos) (*SetStmt, error) {
1919
}, nil
2020
}
2121

22+
func (p *Parser) parseSettingsStmt(pos Pos) (*SetStmt, error) {
23+
if err := p.expectKeyword(KeywordSettings); err != nil {
24+
return nil, err
25+
}
26+
settings, err := p.parseSettingsClause(p.Pos())
27+
if err != nil {
28+
return nil, err
29+
}
30+
return &SetStmt{
31+
SetPos: pos,
32+
Settings: settings,
33+
}, nil
34+
}
35+
2236
func (p *Parser) parseSystemFlushExpr(pos Pos) (*SystemFlushExpr, error) {
2337
if err := p.expectKeyword(KeywordFlush); err != nil {
2438
return nil, err

parser/parser_table.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,17 @@ func (p *Parser) parseSettingsExpr(pos Pos) (*SettingExpr, error) {
10961096
return nil, err
10971097
}
10981098
expr = m
1099+
case p.matchKeyword(KeywordTrue), p.matchKeyword(KeywordFalse):
1100+
// Handle TRUE/FALSE keywords as boolean literals
1101+
lastToken := p.last()
1102+
_ = p.lexer.consumeToken()
1103+
expr = &BoolLiteral{
1104+
LiteralPos: lastToken.Pos,
1105+
LiteralEnd: lastToken.End,
1106+
Literal: lastToken.String,
1107+
}
10991108
default:
1100-
return nil, fmt.Errorf("unexpected token: %q, expected <number> or <string>", p.last().String)
1109+
return nil, fmt.Errorf("unexpected token: %q, expected <number>, <bool> or <string>", p.last().String)
11011110
}
11021111

11031112
return &SettingExpr{
@@ -1232,6 +1241,8 @@ func (p *Parser) parseStmt(pos Pos) (Expr, error) {
12321241
expr, err = p.parseUseStmt(pos)
12331242
case p.matchKeyword(KeywordSet):
12341243
expr, err = p.parseSetStmt(pos)
1244+
case p.matchKeyword(KeywordSettings):
1245+
expr, err = p.parseSettingsStmt(pos)
12351246
case p.matchKeyword(KeywordSystem):
12361247
expr, err = p.parseSystemStmt(pos)
12371248
case p.matchKeyword(KeywordOptimize):
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Origin SQL:
2+
SET allow_suspicious_low_cardinality_types = true;
3+
4+
SET max_block_size = 65536;
5+
6+
SET output_format_json_quote_64bit_integers = 'true';
7+
8+
SET max_threads = 8, max_memory_usage = 10000000000, enable_optimize_predicate_expression = false;
9+
10+
SET allow_experimental_analyzer = true;
11+
12+
13+
-- Format SQL:
14+
SET allow_suspicious_low_cardinality_types=true;
15+
SET max_block_size=65536;
16+
SET output_format_json_quote_64bit_integers='true';
17+
SET max_threads=8, max_memory_usage=10000000000, enable_optimize_predicate_expression=false;
18+
SET allow_experimental_analyzer=true;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Origin SQL:
2+
SETTINGS allow_suspicious_low_cardinality_types = true;
3+
4+
SETTINGS max_block_size = 65536;
5+
6+
SETTINGS output_format_json_quote_64bit_integers = 'true';
7+
8+
SETTINGS max_threads = 8, max_memory_usage = 10000000000, enable_optimize_predicate_expression = false;
9+
10+
SETTINGS allow_experimental_analyzer = true;
11+
12+
13+
-- Format SQL:
14+
SET allow_suspicious_low_cardinality_types=true;
15+
SET max_block_size=65536;
16+
SET output_format_json_quote_64bit_integers='true';
17+
SET max_threads=8, max_memory_usage=10000000000, enable_optimize_predicate_expression=false;
18+
SET allow_experimental_analyzer=true;
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
[
2+
{
3+
"SetPos": 0,
4+
"Settings": {
5+
"SettingsPos": 4,
6+
"ListEnd": 49,
7+
"Items": [
8+
{
9+
"SettingsPos": 4,
10+
"Name": {
11+
"Name": "allow_suspicious_low_cardinality_types",
12+
"QuoteType": 1,
13+
"NamePos": 4,
14+
"NameEnd": 42
15+
},
16+
"Expr": {
17+
"LiteralPos": 45,
18+
"LiteralEnd": 49,
19+
"Literal": "true"
20+
}
21+
}
22+
]
23+
}
24+
},
25+
{
26+
"SetPos": 52,
27+
"Settings": {
28+
"SettingsPos": 56,
29+
"ListEnd": 78,
30+
"Items": [
31+
{
32+
"SettingsPos": 56,
33+
"Name": {
34+
"Name": "max_block_size",
35+
"QuoteType": 1,
36+
"NamePos": 56,
37+
"NameEnd": 70
38+
},
39+
"Expr": {
40+
"NumPos": 73,
41+
"NumEnd": 78,
42+
"Literal": "65536",
43+
"Base": 10
44+
}
45+
}
46+
]
47+
}
48+
},
49+
{
50+
"SetPos": 81,
51+
"Settings": {
52+
"SettingsPos": 85,
53+
"ListEnd": 132,
54+
"Items": [
55+
{
56+
"SettingsPos": 85,
57+
"Name": {
58+
"Name": "output_format_json_quote_64bit_integers",
59+
"QuoteType": 1,
60+
"NamePos": 85,
61+
"NameEnd": 124
62+
},
63+
"Expr": {
64+
"LiteralPos": 128,
65+
"LiteralEnd": 132,
66+
"Literal": "true"
67+
}
68+
}
69+
]
70+
}
71+
},
72+
{
73+
"SetPos": 136,
74+
"Settings": {
75+
"SettingsPos": 140,
76+
"ListEnd": 233,
77+
"Items": [
78+
{
79+
"SettingsPos": 140,
80+
"Name": {
81+
"Name": "max_threads",
82+
"QuoteType": 1,
83+
"NamePos": 140,
84+
"NameEnd": 151
85+
},
86+
"Expr": {
87+
"NumPos": 154,
88+
"NumEnd": 155,
89+
"Literal": "8",
90+
"Base": 10
91+
}
92+
},
93+
{
94+
"SettingsPos": 157,
95+
"Name": {
96+
"Name": "max_memory_usage",
97+
"QuoteType": 1,
98+
"NamePos": 157,
99+
"NameEnd": 173
100+
},
101+
"Expr": {
102+
"NumPos": 176,
103+
"NumEnd": 187,
104+
"Literal": "10000000000",
105+
"Base": 10
106+
}
107+
},
108+
{
109+
"SettingsPos": 189,
110+
"Name": {
111+
"Name": "enable_optimize_predicate_expression",
112+
"QuoteType": 1,
113+
"NamePos": 189,
114+
"NameEnd": 225
115+
},
116+
"Expr": {
117+
"LiteralPos": 228,
118+
"LiteralEnd": 233,
119+
"Literal": "false"
120+
}
121+
}
122+
]
123+
}
124+
},
125+
{
126+
"SetPos": 236,
127+
"Settings": {
128+
"SettingsPos": 240,
129+
"ListEnd": 274,
130+
"Items": [
131+
{
132+
"SettingsPos": 240,
133+
"Name": {
134+
"Name": "allow_experimental_analyzer",
135+
"QuoteType": 1,
136+
"NamePos": 240,
137+
"NameEnd": 267
138+
},
139+
"Expr": {
140+
"LiteralPos": 270,
141+
"LiteralEnd": 274,
142+
"Literal": "true"
143+
}
144+
}
145+
]
146+
}
147+
}
148+
]

0 commit comments

Comments
 (0)