Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4914,6 +4914,30 @@ func (s *StringLiteral) Accept(visitor ASTVisitor) error {
return visitor.VisitStringLiteral(s)
}

type BoolLiteral struct {
LiteralPos Pos
LiteralEnd Pos
Literal string
}

func (b *BoolLiteral) Pos() Pos {
return b.LiteralPos
}

func (b *BoolLiteral) End() Pos {
return b.LiteralEnd
}

func (b *BoolLiteral) String() string {
return b.Literal
}

func (b *BoolLiteral) Accept(visitor ASTVisitor) error {
visitor.Enter(b)
defer visitor.Leave(b)
return visitor.VisitBoolLiteral(b)
}

type PlaceHolder struct {
PlaceholderPos Pos
PlaceHolderEnd Pos
Expand Down
8 changes: 8 additions & 0 deletions parser/ast_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ type ASTVisitor interface {
VisitSelectItem(expr *SelectItem) error
VisitTargetPairExpr(expr *TargetPair) error
VisitDistinctOn(expr *DistinctOn) error
VisitBoolLiteral(expr *BoolLiteral) error

Enter(expr Expr)
Leave(expr Expr)
Expand Down Expand Up @@ -1540,6 +1541,13 @@ func (v *DefaultASTVisitor) VisitDistinctOn(expr *DistinctOn) error {
return nil
}

func (v *DefaultASTVisitor) VisitBoolLiteral(expr *BoolLiteral) error {
if v.Visit != nil {
return v.Visit(expr)
}
return nil
}

func (v *DefaultASTVisitor) Enter(expr Expr) {}

func (v *DefaultASTVisitor) Leave(expr Expr) {}
14 changes: 14 additions & 0 deletions parser/parse_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ func (p *Parser) parseSetStmt(pos Pos) (*SetStmt, error) {
}, nil
}

func (p *Parser) parseSettingsStmt(pos Pos) (*SetStmt, error) {
if err := p.expectKeyword(KeywordSettings); err != nil {
return nil, err
}
settings, err := p.parseSettingsClause(p.Pos())
if err != nil {
return nil, err
}
return &SetStmt{
SetPos: pos,
Settings: settings,
}, nil
}

func (p *Parser) parseSystemFlushExpr(pos Pos) (*SystemFlushExpr, error) {
if err := p.expectKeyword(KeywordFlush); err != nil {
return nil, err
Expand Down
13 changes: 12 additions & 1 deletion parser/parser_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,17 @@ func (p *Parser) parseSettingsExpr(pos Pos) (*SettingExpr, error) {
return nil, err
}
expr = m
case p.matchKeyword(KeywordTrue), p.matchKeyword(KeywordFalse):
// Handle TRUE/FALSE keywords as boolean literals
lastToken := p.last()
_ = p.lexer.consumeToken()
expr = &BoolLiteral{
LiteralPos: lastToken.Pos,
LiteralEnd: lastToken.End,
Literal: lastToken.String,
}
default:
return nil, fmt.Errorf("unexpected token: %q, expected <number> or <string>", p.last().String)
return nil, fmt.Errorf("unexpected token: %q, expected <number>, <bool> or <string>", p.last().String)
}

return &SettingExpr{
Expand Down Expand Up @@ -1232,6 +1241,8 @@ func (p *Parser) parseStmt(pos Pos) (Expr, error) {
expr, err = p.parseUseStmt(pos)
case p.matchKeyword(KeywordSet):
expr, err = p.parseSetStmt(pos)
case p.matchKeyword(KeywordSettings):
expr, err = p.parseSettingsStmt(pos)
case p.matchKeyword(KeywordSystem):
expr, err = p.parseSystemStmt(pos)
case p.matchKeyword(KeywordOptimize):
Expand Down
18 changes: 18 additions & 0 deletions parser/testdata/basic/format/set_statement.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Origin SQL:
SET allow_suspicious_low_cardinality_types = true;

SET max_block_size = 65536;

SET output_format_json_quote_64bit_integers = 'true';

SET max_threads = 8, max_memory_usage = 10000000000, enable_optimize_predicate_expression = false;

SET allow_experimental_analyzer = true;


-- Format SQL:
SET allow_suspicious_low_cardinality_types=true;
SET max_block_size=65536;
SET output_format_json_quote_64bit_integers='true';
SET max_threads=8, max_memory_usage=10000000000, enable_optimize_predicate_expression=false;
SET allow_experimental_analyzer=true;
18 changes: 18 additions & 0 deletions parser/testdata/basic/format/settings_statement.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Origin SQL:
SETTINGS allow_suspicious_low_cardinality_types = true;

SETTINGS max_block_size = 65536;

SETTINGS output_format_json_quote_64bit_integers = 'true';

SETTINGS max_threads = 8, max_memory_usage = 10000000000, enable_optimize_predicate_expression = false;

SETTINGS allow_experimental_analyzer = true;


-- Format SQL:
SET allow_suspicious_low_cardinality_types=true;
SET max_block_size=65536;
SET output_format_json_quote_64bit_integers='true';
SET max_threads=8, max_memory_usage=10000000000, enable_optimize_predicate_expression=false;
SET allow_experimental_analyzer=true;
148 changes: 148 additions & 0 deletions parser/testdata/basic/output/set_statement.sql.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
[
{
"SetPos": 0,
"Settings": {
"SettingsPos": 4,
"ListEnd": 49,
"Items": [
{
"SettingsPos": 4,
"Name": {
"Name": "allow_suspicious_low_cardinality_types",
"QuoteType": 1,
"NamePos": 4,
"NameEnd": 42
},
"Expr": {
"LiteralPos": 45,
"LiteralEnd": 49,
"Literal": "true"
}
}
]
}
},
{
"SetPos": 52,
"Settings": {
"SettingsPos": 56,
"ListEnd": 78,
"Items": [
{
"SettingsPos": 56,
"Name": {
"Name": "max_block_size",
"QuoteType": 1,
"NamePos": 56,
"NameEnd": 70
},
"Expr": {
"NumPos": 73,
"NumEnd": 78,
"Literal": "65536",
"Base": 10
}
}
]
}
},
{
"SetPos": 81,
"Settings": {
"SettingsPos": 85,
"ListEnd": 132,
"Items": [
{
"SettingsPos": 85,
"Name": {
"Name": "output_format_json_quote_64bit_integers",
"QuoteType": 1,
"NamePos": 85,
"NameEnd": 124
},
"Expr": {
"LiteralPos": 128,
"LiteralEnd": 132,
"Literal": "true"
}
}
]
}
},
{
"SetPos": 136,
"Settings": {
"SettingsPos": 140,
"ListEnd": 233,
"Items": [
{
"SettingsPos": 140,
"Name": {
"Name": "max_threads",
"QuoteType": 1,
"NamePos": 140,
"NameEnd": 151
},
"Expr": {
"NumPos": 154,
"NumEnd": 155,
"Literal": "8",
"Base": 10
}
},
{
"SettingsPos": 157,
"Name": {
"Name": "max_memory_usage",
"QuoteType": 1,
"NamePos": 157,
"NameEnd": 173
},
"Expr": {
"NumPos": 176,
"NumEnd": 187,
"Literal": "10000000000",
"Base": 10
}
},
{
"SettingsPos": 189,
"Name": {
"Name": "enable_optimize_predicate_expression",
"QuoteType": 1,
"NamePos": 189,
"NameEnd": 225
},
"Expr": {
"LiteralPos": 228,
"LiteralEnd": 233,
"Literal": "false"
}
}
]
}
},
{
"SetPos": 236,
"Settings": {
"SettingsPos": 240,
"ListEnd": 274,
"Items": [
{
"SettingsPos": 240,
"Name": {
"Name": "allow_experimental_analyzer",
"QuoteType": 1,
"NamePos": 240,
"NameEnd": 267
},
"Expr": {
"LiteralPos": 270,
"LiteralEnd": 274,
"Literal": "true"
}
}
]
}
}
]
Loading