Skip to content

Commit

Permalink
feat: Allow use of table and column aliases for table functions retur…
Browse files Browse the repository at this point in the history
…ning unknown types

Signed-off-by: Andrew Haines <andrew@haines.org.nz>
  • Loading branch information
haines committed Apr 27, 2023
1 parent e4b1c18 commit c7a10a7
Show file tree
Hide file tree
Showing 31 changed files with 480 additions and 4 deletions.
33 changes: 29 additions & 4 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
case *ast.SelectStmt:
list = astutils.Search(n.FromClause, func(node ast.Node) bool {
switch node.(type) {
case *ast.RangeVar, *ast.RangeSubselect, *ast.FuncName:
case *ast.RangeVar, *ast.RangeSubselect, *ast.RangeFunction:
return true
default:
return false
Expand All @@ -455,10 +455,20 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
for _, item := range list.Items {
switch n := item.(type) {

case *ast.FuncName:
case *ast.RangeFunction:
// If the function or table can't be found, don't error out. There
// are many queries that depend on functions unknown to sqlc.
fn, err := qc.GetFunc(n)
var funcCall *ast.FuncCall
switch f := n.Functions.Items[0].(type) {
case *ast.List:
funcCall = f.Items[0].(*ast.FuncCall)
case *ast.FuncCall:
funcCall = f
default:
return nil, fmt.Errorf("sourceTables: unsupported function call type %T", n.Functions.Items[0])
}

fn, err := qc.GetFunc(funcCall.Func)
if err != nil {
continue
}
Expand All @@ -468,7 +478,22 @@ func sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
Name: fn.ReturnType.Name,
})
if err != nil {
continue
if n.Alias == nil || len(n.Alias.Colnames.Items) == 0 {
continue
}

table = &Table{}
for _, colName := range n.Alias.Colnames.Items {
table.Columns = append(table.Columns, &Column{
Name: colName.(*ast.String).Str,
DataType: "any",
})
}
}
if n.Alias != nil {
table.Rel = &ast.TableName{
Name: *n.Alias.Aliasname,
}
}
tables = append(tables, table)

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/unnest/postgresql/pgx/v4/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ SELECT
unnest(@vampire_id::uuid[]) AS vampire_id
RETURNING
*;

-- name: GetVampireIDs :many
SELECT vampires.id::uuid FROM unnest(@vampire_id::uuid[]) AS vampires (id);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/unnest/postgresql/pgx/v5/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ SELECT
unnest(@vampire_id::uuid[]) AS vampire_id
RETURNING
*;

-- name: GetVampireIDs :many
SELECT vampires.id::uuid FROM unnest(@vampire_id::uuid[]) AS vampires (id);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/unnest/postgresql/stdlib/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ SELECT
unnest(@vampire_id::uuid[]) AS vampire_id
RETURNING
*;

-- name: GetVampireIDs :many
SELECT vampires.id::uuid FROM unnest(@vampire_id::uuid[]) AS vampires (id);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- name: GetValues :many
SELECT id, index::bigint, value::text
FROM array_values AS x, unnest(values) WITH ORDINALITY AS y (value, index);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE array_values (
id bigserial PRIMARY KEY,
values text[] NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql",
"emit_interface": true
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c7a10a7

Please sign in to comment.