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
7 changes: 5 additions & 2 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1545,10 +1545,13 @@ func (i *Ident) End() Pos {
}

func (i *Ident) String() string {
if i.QuoteType == BackTicks {
switch i.QuoteType {
case BackTicks:
return "`" + i.Name + "`"
} else if i.QuoteType == DoubleQuote {
case DoubleQuote:
return `"` + i.Name + `"`
case SingleQuote:
return `'` + i.Name + `'`
}
return i.Name
}
Expand Down
1 change: 1 addition & 0 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
Unquoted = iota + 1
DoubleQuote
BackTicks
SingleQuote
)

type Pos int
Expand Down
25 changes: 25 additions & 0 deletions parser/parser_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,38 @@ func (p *Parser) parseIdentOrStar() (*Ident, error) {
}
}

func (p *Parser) parseIdentOrString() (*Ident, error) {
switch {
case p.matchTokenKind(TokenKindIdent):
return p.parseIdent()
case p.matchTokenKind(TokenKindString):
lastToken := p.last()
_ = p.lexer.consumeToken()
return &Ident{
NamePos: lastToken.Pos,
NameEnd: lastToken.End,
Name: lastToken.String,
QuoteType: SingleQuote, // Treat string literals as single-quoted identifiers
}, nil
default:
return nil, fmt.Errorf("expected <ident> or <string>, but got %q", p.lastTokenKind())
}
}

func (p *Parser) tryParseDotIdent(_ Pos) (*Ident, error) {
if p.tryConsumeTokenKind(TokenKindDot) == nil {
return nil, nil // nolint
}
return p.parseIdent()
}

func (p *Parser) tryParseDotIdentOrString(_ Pos) (*Ident, error) {
if p.tryConsumeTokenKind(TokenKindDot) == nil {
return nil, nil // nolint
}
return p.parseIdentOrString()
}

func (p *Parser) parseUUID() (*UUID, error) {
if err := p.expectKeyword(KeywordUuid); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion parser/parser_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (p *Parser) parseJoinOp(_ Pos) []string {

func (p *Parser) parseJoinTableExpr(_ Pos) (Expr, error) {
switch {
case p.matchTokenKind(TokenKindIdent), p.matchTokenKind(TokenKindLParen):
case p.matchTokenKind(TokenKindIdent), p.matchTokenKind(TokenKindString), p.matchTokenKind(TokenKindLParen):
tableExpr, err := p.parseTableExpr(p.Pos())
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions parser/parser_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@ func (p *Parser) parseIdentOrFunction(_ Pos) (Expr, error) {
}

func (p *Parser) parseTableIdentifier(_ Pos) (*TableIdentifier, error) {
ident, err := p.parseIdent()
ident, err := p.parseIdentOrString()
if err != nil {
return nil, err
}
dotIdent, err := p.tryParseDotIdent(p.Pos())
dotIdent, err := p.tryParseDotIdentOrString(p.Pos())
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Origin SQL:
SELECT * FROM 'test_table'


-- Format SQL:
SELECT * FROM 'test_table';
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[
{
"SelectPos": 0,
"StatementEnd": 25,
"With": null,
"Top": null,
"HasDistinct": false,
"DistinctOn": null,
"SelectItems": [
{
"Expr": {
"Name": "*",
"QuoteType": 0,
"NamePos": 7,
"NameEnd": 7
},
"Modifiers": [],
"Alias": null
}
],
"From": {
"FromPos": 9,
"Expr": {
"Table": {
"TablePos": 15,
"TableEnd": 25,
"Alias": null,
"Expr": {
"Database": null,
"Table": {
"Name": "test_table",
"QuoteType": 4,
"NamePos": 15,
"NameEnd": 25
}
},
"HasFinal": false
},
"StatementEnd": 25,
"SampleRatio": null,
"HasFinal": false
}
},
"ArrayJoin": null,
"Window": null,
"Prewhere": null,
"Where": null,
"GroupBy": null,
"WithTotal": false,
"Having": null,
"OrderBy": null,
"LimitBy": null,
"Limit": null,
"Settings": null,
"Format": null,
"UnionAll": null,
"UnionDistinct": null,
"Except": null
}
]
1 change: 1 addition & 0 deletions parser/testdata/query/select_with_single_quote_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM 'test_table'