Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix column index style wasn't supported in query #119

Merged
merged 1 commit into from
Feb 18, 2025
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
8 changes: 4 additions & 4 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ func (p *BinaryOperation) Accept(visitor ASTVisitor) error {
}

type IndexOperation struct {
LeftExpr Expr
Object Expr
Operation TokenKind
Index Expr
}

func (i *IndexOperation) Accept(visitor ASTVisitor) error {
visitor.enter(i)
defer visitor.leave(i)
if err := i.LeftExpr.Accept(visitor); err != nil {
if err := i.Object.Accept(visitor); err != nil {
return err
}
if err := i.Index.Accept(visitor); err != nil {
Expand All @@ -206,7 +206,7 @@ func (i *IndexOperation) Accept(visitor ASTVisitor) error {
}

func (i *IndexOperation) Pos() Pos {
return i.LeftExpr.Pos()
return i.Object.Pos()
}

func (i *IndexOperation) End() Pos {
Expand All @@ -215,7 +215,7 @@ func (i *IndexOperation) End() Pos {

func (i *IndexOperation) String() string {
var builder strings.Builder
builder.WriteString(i.LeftExpr.String())
builder.WriteString(i.Object.String())
builder.WriteString(string(i.Operation))
builder.WriteString(i.Index.String())
return builder.String()
Expand Down
4 changes: 3 additions & 1 deletion parser/parser_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (p *Parser) getNextPrecedence() int {
return PrecedenceIs
case p.matchKeyword(KeywordNot):
return PrecedenceNot
case p.matchTokenKind(TokenKindDot):
return PrecedenceDot
case p.matchTokenKind(TokenKindDash):
return PrecedenceDoubleColon
case p.matchTokenKind(TokenKindSingleEQ), p.matchTokenKind(TokenKindLT), p.matchTokenKind(TokenKindLE),
Expand Down Expand Up @@ -155,7 +157,7 @@ func (p *Parser) parseInfix(expr Expr, precedence int) (Expr, error) {
return nil, err
}
return &IndexOperation{
LeftExpr: expr,
Object: expr,
Operation: TokenKindDot,
Index: rightExpr,
}, nil
Expand Down
13 changes: 12 additions & 1 deletion parser/parser_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,19 @@ func (p *Parser) parseIdentOrFunction(_ Pos) (Expr, error) {
Ident: ident,
DotIdent: nextIdent,
}, nil
case p.matchTokenKind(TokenKindInt):
i, err := p.parseNumber(p.Pos())
if err != nil {
return nil, err
}
return &IndexOperation{
Object: ident,
Operation: TokenKindDot,
Index: i,
}, nil
default:
return nil, fmt.Errorf("expected IDENT, NUMBER or *, but got %q", p.lastTokenKind())
}

}
return ident, nil
}
Expand Down
5 changes: 5 additions & 0 deletions parser/testdata/query/format/select_with_number_field.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Origin SQL:
SELECT foo, bar.1, foo.2 FROM foo ARRAY JOIN m as bar

-- Format SQL:
SELECT foo, bar.1, foo.2 FROM foo ARRAY JOIN m AS bar;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"SelectItems": [
{
"Expr": {
"LeftExpr": {
"Object": {
"Name": {
"Name": "tuple",
"QuoteType": 1,
Expand Down Expand Up @@ -291,7 +291,7 @@
},
{
"Expr": {
"LeftExpr": {
"Object": {
"LeftParenPos": 161,
"RightParenPos": 257,
"Items": {
Expand Down Expand Up @@ -485,7 +485,7 @@
},
{
"Expr": {
"LeftExpr": {
"Object": {
"Name": {
"Name": "tuple",
"QuoteType": 1,
Expand Down
120 changes: 120 additions & 0 deletions parser/testdata/query/output/select_with_number_field.sql.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
[
{
"SelectPos": 0,
"StatementEnd": 53,
"With": null,
"Top": null,
"SelectItems": [
{
"Expr": {
"Name": "foo",
"QuoteType": 1,
"NamePos": 7,
"NameEnd": 10
},
"Modifiers": [],
"Alias": null
},
{
"Expr": {
"Object": {
"Name": "bar",
"QuoteType": 1,
"NamePos": 12,
"NameEnd": 15
},
"Operation": ".",
"Index": {
"NumPos": 16,
"NumEnd": 17,
"Literal": "1",
"Base": 10
}
},
"Modifiers": [],
"Alias": null
},
{
"Expr": {
"Object": {
"Name": "foo",
"QuoteType": 1,
"NamePos": 19,
"NameEnd": 22
},
"Operation": ".",
"Index": {
"NumPos": 23,
"NumEnd": 24,
"Literal": "2",
"Base": 10
}
},
"Modifiers": [],
"Alias": null
}
],
"From": {
"FromPos": 25,
"Expr": {
"Table": {
"TablePos": 30,
"TableEnd": 33,
"Alias": null,
"Expr": {
"Database": null,
"Table": {
"Name": "foo",
"QuoteType": 1,
"NamePos": 30,
"NameEnd": 33
}
},
"HasFinal": false
},
"StatementEnd": 33,
"SampleRatio": null,
"HasFinal": false
}
},
"ArrayJoin": {
"ArrayPos": 34,
"Type": "",
"Expr": {
"ListPos": 45,
"ListEnd": 53,
"HasDistinct": false,
"Items": [
{
"Expr": {
"Name": "m",
"QuoteType": 1,
"NamePos": 45,
"NameEnd": 46
},
"Alias": {
"Name": "bar",
"QuoteType": 1,
"NamePos": 50,
"NameEnd": 53
}
}
]
}
},
"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_number_field.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT foo, bar.1, foo.2 FROM foo ARRAY JOIN m as bar
Loading