-
-
Notifications
You must be signed in to change notification settings - Fork 71
Fix INTO semantics in plpgsql #3237
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright 2026 Dolthub, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package _go | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| ) | ||
|
|
||
| // TestPlpgsqlSelectInto covers what an INTO clause does to its targets when the query returns something | ||
| // other than exactly one row, and when it has several targets. All expectations were verified against | ||
| // PostgreSQL 16. | ||
| func TestPlpgsqlSelectInto(t *testing.T) { | ||
| RunScripts(t, []ScriptTest{ | ||
| { | ||
| Name: "INTO targets when the query matches no rows", | ||
| SetUpScript: []string{ | ||
| `CREATE TABLE k (id int, nm text);`, | ||
| `INSERT INTO k VALUES (1, 'a'), (2, 'b');`, | ||
| }, | ||
| Assertions: []ScriptTestAssertion{ | ||
| { | ||
| // Without STRICT this is not an error: the target is set to NULL. | ||
| Query: `CREATE FUNCTION f_scalar_miss() RETURNS int LANGUAGE plpgsql AS $$ | ||
| DECLARE v int; | ||
| BEGIN | ||
| SELECT id INTO v FROM k WHERE id = 99; | ||
| RETURN coalesce(v, -1); | ||
| END; $$;`, | ||
| Expected: []sql.Row{}, | ||
| }, | ||
| { | ||
| Query: `SELECT f_scalar_miss();`, | ||
| Expected: []sql.Row{{-1}}, | ||
| }, | ||
| { | ||
| // Every target is set to NULL, not just the first. | ||
| Query: `CREATE FUNCTION f_two_miss() RETURNS text LANGUAGE plpgsql AS $$ | ||
| DECLARE a int; b text; | ||
| BEGIN | ||
| SELECT id, nm INTO a, b FROM k WHERE id = 99; | ||
| RETURN coalesce(a::text, 'NULLa') || '/' || coalesce(b, 'NULLb'); | ||
| END; $$;`, | ||
| Expected: []sql.Row{}, | ||
| }, | ||
| { | ||
| Query: `SELECT f_two_miss();`, | ||
| Expected: []sql.Row{{"NULLa/NULLb"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Name: "INTO with several targets assigns every one of them", | ||
| SetUpScript: []string{ | ||
| `CREATE TABLE k (id int, nm text);`, | ||
| `INSERT INTO k VALUES (1, 'a'), (2, 'b');`, | ||
| }, | ||
| Assertions: []ScriptTestAssertion{ | ||
| { | ||
| Query: `CREATE FUNCTION f_two() RETURNS text LANGUAGE plpgsql AS $$ | ||
| DECLARE a int; b text; | ||
| BEGIN | ||
| SELECT id, nm INTO a, b FROM k WHERE id = 1; | ||
| RETURN coalesce(a::text, 'NULLa') || '/' || coalesce(b, 'NULLb'); | ||
| END; $$;`, | ||
| Expected: []sql.Row{}, | ||
| }, | ||
| { | ||
| Query: `SELECT f_two();`, | ||
| Expected: []sql.Row{{"1/a"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Name: "INTO keeps the first row when the query matches several", | ||
| SetUpScript: []string{ | ||
| `CREATE TABLE k (id int, nm text);`, | ||
| `INSERT INTO k VALUES (1, 'a'), (2, 'b');`, | ||
| }, | ||
| Assertions: []ScriptTestAssertion{ | ||
| { | ||
| // Without STRICT, extra rows are discarded rather than raising. | ||
| Query: `CREATE FUNCTION f_scalar_multi() RETURNS int LANGUAGE plpgsql AS $$ | ||
| DECLARE v int; | ||
| BEGIN | ||
| SELECT id INTO v FROM k ORDER BY id; | ||
| RETURN v; | ||
| END; $$;`, | ||
| Expected: []sql.Row{}, | ||
| }, | ||
| { | ||
| Query: `SELECT f_scalar_multi();`, | ||
| Expected: []sql.Row{{1}}, | ||
| }, | ||
| { | ||
| Query: `CREATE FUNCTION f_two_multi() RETURNS text LANGUAGE plpgsql AS $$ | ||
| DECLARE a int; b text; | ||
| BEGIN | ||
| SELECT id, nm INTO a, b FROM k ORDER BY id; | ||
| RETURN coalesce(a::text, 'NULLa') || '/' || coalesce(b, 'NULLb'); | ||
| END; $$;`, | ||
| Expected: []sql.Row{}, | ||
| }, | ||
| { | ||
| Query: `SELECT f_two_multi();`, | ||
| Expected: []sql.Row{{"1/a"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.