summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane2009-11-06 18:37:55 +0000
committerTom Lane2009-11-06 18:37:55 +0000
commit0772f1e53d2b10021a1c1513a8d44e2eb137a570 (patch)
tree05632d2d8cbfb60df46d1edaab8865ddc0ff7d70 /src/test
parent593f4b854a8bb384547b8fa9854c73dcd88d4876 (diff)
Change plpgsql from using textual substitution to insert variable references
into SQL expressions, to using the newly added parser callback hooks. This allows us to do the substitutions in a more semantically-aware way: a variable reference will only be recognized where it can validly go, ie, a place where a column value or parameter would be legal, instead of the former behavior that would replace any textual match including table names and column aliases (leading to syntax errors later on). A release-note-worthy fine point is that plpgsql variable names that match fully-reserved words will now need to be quoted. This commit preserves the former behavior that variable references take precedence over any possible match to a column name. The infrastructure is in place to support the reverse precedence or throwing an error on ambiguity, but those behaviors aren't accessible yet. Most of the code changes here are associated with making the namespace data structure persist so that it can be consulted at runtime, instead of throwing it away at the end of initial function parsing. The plpgsql scanner is still doing name lookups, but that behavior is now irrelevant for SQL expressions. A future commit will deal with removing unnecessary lookups.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/plpgsql.out42
-rw-r--r--src/test/regress/sql/plpgsql.sql36
2 files changed, 76 insertions, 2 deletions
diff --git a/src/test/regress/expected/plpgsql.out b/src/test/regress/expected/plpgsql.out
index 2e97bec42e9..5846246b7c2 100644
--- a/src/test/regress/expected/plpgsql.out
+++ b/src/test/regress/expected/plpgsql.out
@@ -899,7 +899,7 @@ begin
declare
rec record;
begin
- select into rec * from PLine where slotname = outer.rec.backlink;
+ select into rec * from PLine where slotname = "outer".rec.backlink;
retval := ''Phone line '' || trim(rec.phonenumber);
if rec.comment != '''' then
retval := retval || '' ('';
@@ -3938,3 +3938,43 @@ LINE 1: SELECT rtrim(roomno) AS roomno, foo FROM Room ORDER BY room...
^
QUERY: SELECT rtrim(roomno) AS roomno, foo FROM Room ORDER BY roomno
CONTEXT: PL/pgSQL function "inline_code_block" line 3 at FOR over SELECT rows
+-- Check variable scoping -- a var is not available in its own or prior
+-- default expressions.
+create function scope_test() returns int as $$
+declare x int := 42;
+begin
+ declare y int := x + 1;
+ x int := x + 2;
+ begin
+ return x * 100 + y;
+ end;
+end;
+$$ language plpgsql;
+select scope_test();
+ scope_test
+------------
+ 4443
+(1 row)
+
+drop function scope_test();
+-- Check handling of conflicts between plpgsql vars and table columns.
+create function conflict_test() returns setof int8_tbl as $$
+declare r record;
+ q1 bigint := 42;
+begin
+ for r in select q1,q2 from int8_tbl loop
+ return next r;
+ end loop;
+end;
+$$ language plpgsql;
+select * from conflict_test();
+ q1 | q2
+----+-------------------
+ 42 | 456
+ 42 | 4567890123456789
+ 42 | 123
+ 42 | 4567890123456789
+ 42 | -4567890123456789
+(5 rows)
+
+drop function conflict_test();
diff --git a/src/test/regress/sql/plpgsql.sql b/src/test/regress/sql/plpgsql.sql
index 83cda97d1bf..51bfce2e0c1 100644
--- a/src/test/regress/sql/plpgsql.sql
+++ b/src/test/regress/sql/plpgsql.sql
@@ -1026,7 +1026,7 @@ begin
declare
rec record;
begin
- select into rec * from PLine where slotname = outer.rec.backlink;
+ select into rec * from PLine where slotname = "outer".rec.backlink;
retval := ''Phone line '' || trim(rec.phonenumber);
if rec.comment != '''' then
retval := retval || '' ('';
@@ -3135,3 +3135,37 @@ BEGIN
RAISE NOTICE '%, %', r.roomno, r.comment;
END LOOP;
END$$;
+
+-- Check variable scoping -- a var is not available in its own or prior
+-- default expressions.
+
+create function scope_test() returns int as $$
+declare x int := 42;
+begin
+ declare y int := x + 1;
+ x int := x + 2;
+ begin
+ return x * 100 + y;
+ end;
+end;
+$$ language plpgsql;
+
+select scope_test();
+
+drop function scope_test();
+
+-- Check handling of conflicts between plpgsql vars and table columns.
+
+create function conflict_test() returns setof int8_tbl as $$
+declare r record;
+ q1 bigint := 42;
+begin
+ for r in select q1,q2 from int8_tbl loop
+ return next r;
+ end loop;
+end;
+$$ language plpgsql;
+
+select * from conflict_test();
+
+drop function conflict_test();