Skip to content

Commit

Permalink
Support select * for postgres parser (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
hokaccha authored Jul 20, 2023
1 parent c632d5e commit 81cbc53
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
39 changes: 39 additions & 0 deletions database/postgres/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (p PostgresParser) parseSelectStmt(stmt *pgquery.SelectStmt) (parser.Select
}

var fromTable parser.TableName
var aliasName string
if len(stmt.FromClause) == 0 {
fromTable = parser.TableName{
Name: parser.NewTableIdent(""),
Expand All @@ -222,6 +223,9 @@ func (p PostgresParser) parseSelectStmt(stmt *pgquery.SelectStmt) (parser.Select
if err != nil {
return nil, err
}
if node.RangeVar.Alias != nil {
aliasName = node.RangeVar.Alias.Aliasname
}
default:
return nil, fmt.Errorf("unknown node in parseSelectStmt: %#v", node)
}
Expand All @@ -233,12 +237,47 @@ func (p PostgresParser) parseSelectStmt(stmt *pgquery.SelectStmt) (parser.Select
&parser.AliasedTableExpr{
Expr: fromTable,
TableHints: []string{},
As: parser.NewTableIdent(aliasName),
},
},
}, nil
}

func (p PostgresParser) parseResTarget(stmt *pgquery.ResTarget) (parser.SelectExpr, error) {
if node, ok := stmt.Val.Node.(*pgquery.Node_ColumnRef); ok {
fields := node.ColumnRef.Fields
fieldsLen := len(fields)
column := fields[fieldsLen-1]
if _, ok := column.Node.(*pgquery.Node_AStar); ok {
var tableName string
var schemaName string
if fieldsLen >= 2 {
tableField := fields[fieldsLen-2]
tableNode, ok := tableField.Node.(*pgquery.Node_String_)
if !ok {
return nil, fmt.Errorf("Invalid table field node type: %#v", tableField)
}
tableName = tableNode.String_.Sval

if fieldsLen >= 3 {
schemaField := fields[fieldsLen-3]
schemaNode, ok := schemaField.Node.(*pgquery.Node_String_)
if !ok {
return nil, fmt.Errorf("Invalid schema field node type: %#v", schemaField)
}
schemaName = schemaNode.String_.Sval
}
}

return &parser.StarExpr{
TableName: parser.TableName{
Name: parser.NewTableIdent(tableName),
Qualifier: parser.NewTableIdent(schemaName),
},
}, nil
}
}

expr, err := p.parseExpr(stmt.Val)
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions database/postgres/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ CreateViewWithCaseWhen:
CreateViewWithoutFrom:
sql: |
create view v as select 10 as n;
CreateViewWithStarExpr:
compare_with_generic_parser: true
sql: |
create view hoge_view as
select t.* from hoge t;
CreateIndex:
compare_with_generic_parser: true
sql: |
Expand Down

0 comments on commit 81cbc53

Please sign in to comment.