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
16 changes: 9 additions & 7 deletions parser/ast_literal.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package parser

type Ident struct {
Name string
QuoteType int
NamePos Pos
NameEnd Pos
Name string
QuoteType int
NamePos Pos
NameEnd Pos
HeredocDelimiter string `json:",omitempty"`
}

func (i *Ident) Pos() Pos {
Expand Down Expand Up @@ -286,9 +287,10 @@ func (n *NumberLiteral) Accept(visitor ASTVisitor) error {
}

type StringLiteral struct {
LiteralPos Pos
LiteralEnd Pos
Literal string
LiteralPos Pos
LiteralEnd Pos
Literal string
HeredocDelimiter string `json:",omitempty"`
}

func (s *StringLiteral) Pos() Pos {
Expand Down
13 changes: 13 additions & 0 deletions parser/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,8 @@ func (i *Ident) FormatSQL(formatter *Formatter) {
formatter.WriteByte('"')
formatter.WriteString(i.Name)
formatter.WriteByte('"')
case HeredocQuote:
formatter.writeHeredoc(i.HeredocDelimiter, i.Name)
case SingleQuote:
formatter.WriteByte('\'')
formatter.WriteString(i.Name)
Expand Down Expand Up @@ -2513,7 +2515,18 @@ func (s *ShowStmt) FormatSQL(formatter *Formatter) {

}

func (f *Formatter) writeHeredoc(delimiter, body string) {
f.WriteString(delimiter)
// Indentation inside the body would change the string value.
f.builder.WriteString(body)
f.WriteString(delimiter)
}

func (s *StringLiteral) FormatSQL(formatter *Formatter) {
if s.HeredocDelimiter != "" {
formatter.writeHeredoc(s.HeredocDelimiter, s.Literal)
return
}
formatter.WriteByte('\'')
formatter.WriteString(s.Literal)
formatter.WriteByte('\'')
Expand Down
58 changes: 49 additions & 9 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
DoubleQuote
BackTicks
SingleQuote
HeredocQuote
)

type Pos int
Expand All @@ -61,10 +62,11 @@ type Token struct {
Pos Pos
End Pos

Kind TokenKind
String string
Base int // 10 or 16 on TokenKindInt
QuoteType int
Kind TokenKind
String string
Base int // 10 or 16 on TokenKindInt
QuoteType int
heredocDelimiter string
}

func (t *Token) ToString() string {
Expand Down Expand Up @@ -271,6 +273,38 @@ func (l *Lexer) consumeMultiLineComment() error {
return &lexerError{pos: pos, err: errors.New("unclosed multi-line comment")}
}

// tryConsumeHeredoc reads a string between matching dollar-quoted delimiters.
// For example, SELECT $tag$FROM t$tag$ returns the string 'FROM t'.
func (l *Lexer) tryConsumeHeredoc() bool {
i := 1
for l.peekOk(i) && (IsIdentStart(l.peekN(i)) || IsDigit(l.peekN(i))) {
i++
}
if !l.peekOk(i) || l.peekN(i) != '$' {
return false
}

delimiter := l.slice(0, i+1)
bodyStart := l.offset + len(delimiter)
bodyLength := strings.Index(l.input[bodyStart:], delimiter)
if bodyLength < 0 {
return false
}

bodyEnd := bodyStart + bodyLength
end := bodyEnd + len(delimiter)
l.currentToken = &Token{
Kind: TokenKindString,
String: l.input[bodyStart:bodyEnd],
QuoteType: HeredocQuote,
heredocDelimiter: delimiter,
Pos: Pos(l.offset),
End: Pos(end),
}
l.offset = end
return true
}

func (l *Lexer) consumeString() error {
i := 1
endChar := byte('\'')
Expand Down Expand Up @@ -299,10 +333,11 @@ func (l *Lexer) consumeString() error {
return errors.New("invalid string")
}
l.currentToken = &Token{
Kind: TokenKindString,
String: l.slice(1, i),
Pos: Pos(l.offset),
End: Pos(l.offset + i + 1),
Kind: TokenKindString,
QuoteType: SingleQuote,
String: l.slice(1, i),
Pos: Pos(l.offset),
End: Pos(l.offset + i + 1),
}
l.skipN(i + 1)
return nil
Expand Down Expand Up @@ -417,7 +452,12 @@ func (l *Lexer) consumeToken() (err error) {
}
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return l.consumeNumber()
case '`', '$', '"':
case '$':
if l.tryConsumeHeredoc() {
return nil
}
fallthrough
case '`', '"':
return l.consumeIdent(Pos(l.offset))
case '\'':
return l.consumeString()
Expand Down
16 changes: 9 additions & 7 deletions parser/parser_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ func (p *Parser) parseIdentOrString() (*Ident, error) {
curToken := p.current()
_ = p.lexer.consumeToken()
return &Ident{
NamePos: curToken.Pos,
NameEnd: curToken.End,
Name: curToken.String,
QuoteType: SingleQuote, // Treat string literals as single-quoted identifiers
NamePos: curToken.Pos,
NameEnd: curToken.End,
Name: curToken.String,
QuoteType: curToken.QuoteType,
HeredocDelimiter: curToken.heredocDelimiter,
}, nil
default:
return nil, fmt.Errorf("expected <ident> or <string>, but got %q", p.currentTokenKind())
Expand Down Expand Up @@ -401,9 +402,10 @@ func (p *Parser) parseString(pos Pos) (*StringLiteral, error) {
}

str := &StringLiteral{
LiteralPos: pos,
LiteralEnd: curToken.End,
Literal: curToken.String,
LiteralPos: pos,
LiteralEnd: curToken.End,
Literal: curToken.String,
HeredocDelimiter: curToken.heredocDelimiter,
}
return str, nil
}
Expand Down
2 changes: 1 addition & 1 deletion parser/parser_row_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (p *Parser) parseRowPolicyPrincipal() (*RowPolicyPrincipal, error) {
return nil, err
}
principal.PrincipalEnd = rowPolicyIdentEnd(principal.Name)
if principal.Name.QuoteType != SingleQuote && p.matchTokenKind(TokenKindAtSign) {
if principal.Name.QuoteType != SingleQuote && principal.Name.QuoteType != HeredocQuote && p.matchTokenKind(TokenKindAtSign) {
if _, err := p.tryConsumeTokenKind(TokenKindAtSign); err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions parser/testdata/ddl/format/beautify/row_policy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SHOW CREATE ROW POLICIES ON db.*;
SHOW ROW POLICIES;
CREATE ROW POLICY selected_rows ON db.example_table FOR SELECT USING id > 0 TO example_role;
CREATE ROW POLICY all_commands ON db.example_table FOR ALL USING id > 0 TO example_role;
CREATE ROW POLICY $p$example$p$ ON t USING 1;


-- Beautify SQL:
Expand All @@ -26,3 +27,4 @@ SHOW CREATE ROW POLICIES ON db.*;
SHOW ROW POLICIES;
CREATE ROW POLICY selected_rows ON db.example_table FOR SELECT USING id > 0 TO example_role;
CREATE ROW POLICY all_commands ON db.example_table FOR ALL USING id > 0 TO example_role;
CREATE ROW POLICY $p$example$p$ ON t USING 1;
2 changes: 2 additions & 0 deletions parser/testdata/ddl/format/row_policy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SHOW CREATE ROW POLICIES ON db.*;
SHOW ROW POLICIES;
CREATE ROW POLICY selected_rows ON db.example_table FOR SELECT USING id > 0 TO example_role;
CREATE ROW POLICY all_commands ON db.example_table FOR ALL USING id > 0 TO example_role;
CREATE ROW POLICY $p$example$p$ ON t USING 1;


-- Format SQL:
Expand All @@ -26,3 +27,4 @@ SHOW CREATE ROW POLICIES ON db.*;
SHOW ROW POLICIES;
CREATE ROW POLICY selected_rows ON db.example_table FOR SELECT USING id > 0 TO example_role;
CREATE ROW POLICY all_commands ON db.example_table FOR ALL USING id > 0 TO example_role;
CREATE ROW POLICY $p$example$p$ ON t USING 1;
49 changes: 49 additions & 0 deletions parser/testdata/ddl/output/row_policy.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -990,5 +990,54 @@
],
"Except": null
}
},
{
"CreatePos": 1159,
"StatementEnd": 1203,
"IfNotExists": false,
"OrReplace": false,
"Targets": [
{
"PolicyName": {
"Name": "example",
"QuoteType": 5,
"NamePos": 1177,
"NameEnd": 1190,
"HeredocDelimiter": "$p$"
},
"Table": {
"Database": null,
"Table": {
"Name": "t",
"QuoteType": 1,
"NamePos": 1194,
"NameEnd": 1195
}
}
}
],
"OnCluster": null,
"AccessStorageType": null,
"Mode": "",
"Filters": [
{
"FilterPos": 1196,
"FilterEnd": 1203,
"Commands": null,
"Using": {
"ConditionPos": 1202,
"ConditionEnd": 1203,
"None": false,
"Expr": {
"NumPos": 1202,
"NumEnd": 1203,
"Literal": "1",
"Base": 10
}
},
"WithCheck": null
}
],
"To": null
}
]
1 change: 1 addition & 0 deletions parser/testdata/ddl/row_policy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ SHOW CREATE ROW POLICIES ON db.*;
SHOW ROW POLICIES;
CREATE ROW POLICY selected_rows ON db.example_table FOR SELECT USING id > 0 TO example_role;
CREATE ROW POLICY all_commands ON db.example_table FOR ALL USING id > 0 TO example_role;
CREATE ROW POLICY $p$example$p$ ON t USING 1;
20 changes: 20 additions & 0 deletions parser/testdata/query/format/beautify/select_heredoc.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Origin SQL:
SELECT $tag$hello$tag$;
SELECT $x$ AS a FROM t WHERE b = $x$;
SELECT $$it's\raw$$ AS service$$name, 1 AS a$b, 2 AS $open$;
SELECT $t_1$it's\raw
and $$ inside$t_1$;


-- Beautify SQL:
SELECT
$tag$hello$tag$;
SELECT
$x$ AS a FROM t WHERE b = $x$;
SELECT
$$it's\raw$$ AS service$$name,
1 AS a$b,
2 AS $open$;
SELECT
$t_1$it's\raw
and $$ inside$t_1$;
14 changes: 14 additions & 0 deletions parser/testdata/query/format/select_heredoc.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Origin SQL:
SELECT $tag$hello$tag$;
SELECT $x$ AS a FROM t WHERE b = $x$;
SELECT $$it's\raw$$ AS service$$name, 1 AS a$b, 2 AS $open$;
SELECT $t_1$it's\raw
and $$ inside$t_1$;


-- Format SQL:
SELECT $tag$hello$tag$;
SELECT $x$ AS a FROM t WHERE b = $x$;
SELECT $$it's\raw$$ AS service$$name, 1 AS a$b, 2 AS $open$;
SELECT $t_1$it's\raw
and $$ inside$t_1$;
Loading
Loading