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

importer: disallow subqueries in function arguments for pg_dump #118569

Merged
merged 1 commit into from
Feb 2, 2024
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
1 change: 1 addition & 0 deletions pkg/sql/importer/read_import_pgdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ func readPostgresStmt(
case *tree.FuncExpr:
// Look for function calls that mutate schema (this is actually a thing).
semaCtx := tree.MakeSemaContext()
semaCtx.Properties.Require("pg_dump function arguments", tree.RejectSubqueries)
if _, err := expr.TypeCheck(ctx, &semaCtx, nil /* desired */); err != nil {
// If the expression does not type check, it may be a case of using
// a column that does not exist yet in a setval call (as is the case
Expand Down
31 changes: 27 additions & 4 deletions pkg/sql/importer/read_import_pgdump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ COPY done;
}
}

func TestImportArrayData(t *testing.T) {
func TestImportPGDump(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
Expand All @@ -184,6 +184,7 @@ func TestImportArrayData(t *testing.T) {
data string
verifyQuery string
expected [][]string
errorRE string
}{
{
name: "text arrays",
Expand Down Expand Up @@ -211,7 +212,7 @@ COPY t (array_column) FROM STDIN;
},

{
name: "multiple columns",
name: "multiple array columns",
data: `
CREATE TABLE t (
array_column int[],
Expand All @@ -226,6 +227,24 @@ COPY t (array_column, array_column2) FROM STDIN;
{"{1,2,3}", "{cat,dog}"},
},
},

{
name: "function",
data: `
CREATE TABLE t (x INT);

SELECT addgeometrycolumn('t', 'foo', 4326, 'POINT', 2)`,
expected: [][]string{},
},

{
name: "function with subquery",
data: `
CREATE TABLE t (x INT);

SELECT addgeometrycolumn('t', 'foo', 4326, (SELECT 'POINT'), 2)`,
errorRE: ".*subqueries are not allowed in pg_dump function arguments.*",
},
}

for _, test := range tests {
Expand All @@ -237,8 +256,12 @@ COPY t (array_column, array_column2) FROM STDIN;
data = test.data

// Import PGDump and verify expected behavior.
sqlDB.Exec(t, importDumpQuery, srv.URL)
sqlDB.CheckQueryResults(t, test.verifyQuery, test.expected)
if test.errorRE != "" {
sqlDB.ExpectErr(t, test.errorRE, importDumpQuery, srv.URL)
} else {
sqlDB.Exec(t, importDumpQuery, srv.URL)
sqlDB.CheckQueryResults(t, test.verifyQuery, test.expected)
}
})
}
}
Loading