Skip to content

Commit 783b2f6

Browse files
Cyberaxkyleconroy
andcommitted
Ignore unknown functions during SQL validation (#226)
Co-authored-by: Kyle Conroy <[email protected]>
1 parent bd9af4b commit 783b2f6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

internal/dinosql/parser.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,14 @@ func resolveCatalogRefs(c core.Catalog, rvs []nodes.RangeVar, args []paramRef) (
12541254
}
12551255
fun, err := c.LookupFunctionN(fqn, len(n.Args.Items))
12561256
if err != nil {
1257-
return nil, err
1257+
// Synthesize a function on the fly to avoid returning with an error
1258+
// for an unknown Postgres function (e.g. defined in an extension)
1259+
fun = core.Function{
1260+
Name: fqn.Rel,
1261+
ArgN: len(n.Args.Items),
1262+
Arguments: nil,
1263+
ReturnType: "any",
1264+
}
12581265
}
12591266
for i, item := range n.Args.Items {
12601267
pr, ok := item.(nodes.ParamRef)

internal/dinosql/query_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,18 @@ func TestComparisonOperators(t *testing.T) {
927927
}
928928
}
929929

930+
func TestUnknownFunctions(t *testing.T) {
931+
stmt := `
932+
CREATE TABLE foo (id text not null);
933+
-- name: ListFoos :one
934+
SELECT id FROM foo WHERE id = frobnicate($1);
935+
`
936+
_, err := parseSQL(stmt)
937+
if err != nil {
938+
t.Fatal(err)
939+
}
940+
}
941+
930942
func TestInvalidQueries(t *testing.T) {
931943
for i, tc := range []struct {
932944
stmt string

0 commit comments

Comments
 (0)