commit bd8ef3d652f3d4bc44012c6db3a018e2d14cd85f Author: Pavel Stehule Date: Tue Mar 31 14:46:54 2015 +0200 plpgsql - multiassing foreach diff --git a/contrib/hstore/expected/hstore.out b/contrib/hstore/expected/hstore.out index 9749e45..e44532e 100644 --- a/contrib/hstore/expected/hstore.out +++ b/contrib/hstore/expected/hstore.out @@ -1148,6 +1148,22 @@ select %% 'aa=>1, cq=>l, b=>g, fg=>NULL'; {b,g,aa,1,cq,l,fg,NULL} (1 row) +-- fast iteration over keys +do $$ +declare + key text; + value text; +begin + foreach key, value in array hstore_to_array('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore) + loop + raise notice 'key: %, value: %', key, value; + end loop; +end; +$$; +NOTICE: key: b, value: g +NOTICE: key: aa, value: 1 +NOTICE: key: cq, value: l +NOTICE: key: fg, value: select hstore_to_matrix('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore); hstore_to_matrix --------------------------------- diff --git a/contrib/hstore/sql/hstore.sql b/contrib/hstore/sql/hstore.sql index 5a9e9ee..7b9eb09 100644 --- a/contrib/hstore/sql/hstore.sql +++ b/contrib/hstore/sql/hstore.sql @@ -257,6 +257,19 @@ select avals(''); select hstore_to_array('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore); select %% 'aa=>1, cq=>l, b=>g, fg=>NULL'; +-- fast iteration over keys +do $$ +declare + key text; + value text; +begin + foreach key, value in array hstore_to_array('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore) + loop + raise notice 'key: %, value: %', key, value; + end loop; +end; +$$; + select hstore_to_matrix('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore); select %# 'aa=>1, cq=>l, b=>g, fg=>NULL'; diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index d36acf6..e4abb97 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -2505,6 +2505,29 @@ NOTICE: row = {7,8,9} NOTICE: row = {10,11,12} + + + FOREACH cycle can be used for iteration over record. You + need a extension. For this case a clause + SLICE should not be used. FOREACH + statements supports list of target variables. When source array is + a array of composites, then composite array element is saved to target + variables. When the array is a array of scalar values, then target + variables are filled item by item. + +CREATE FUNCTION trig_function() RETURNS TRIGGER AS $$ +DECLARE + key text; value text; +BEGIN + FOREACH key, value IN ARRAY hstore_to_array(hstore(NEW)) + LOOP + RAISE NOTICE 'key = %, value = %', key, value; + END LOOP; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + + diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index deefb1f..5c34a03 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -2261,6 +2261,9 @@ exec_stmt_foreach_a(PLpgSQL_execstate *estate, PLpgSQL_stmt_foreach_a *stmt) Datum value; bool isnull; + + bool multiassign = false; + /* get the value of the array expression */ value = exec_eval_expr(estate, stmt->expr, &isnull, &arrtype, &arrtypmod); if (isnull) @@ -2322,6 +2325,21 @@ exec_stmt_foreach_a(PLpgSQL_execstate *estate, PLpgSQL_stmt_foreach_a *stmt) (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("FOREACH loop variable must not be of an array type"))); + /* + * it is multiassign? Don't support slicing yet. + */ + if (loop_var->dtype == PLPGSQL_DTYPE_ROW + && !type_is_rowtype(ARR_ELEMTYPE(arr))) + { + if (stmt->slice != 0) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("cannot to assign non composite value to composite variable"))); + + /* only when target var is composite, SLICE=0 and source is scalar */ + multiassign = true; + } + /* Create an iterator to step through the array */ array_iterator = array_create_iterator(arr, stmt->slice, NULL); @@ -2344,13 +2362,45 @@ exec_stmt_foreach_a(PLpgSQL_execstate *estate, PLpgSQL_stmt_foreach_a *stmt) { found = true; /* looped at least once */ - /* Assign current element/slice to the loop variable */ - exec_assign_value(estate, loop_var, value, isnull, - iterator_result_type, iterator_result_typmod); + if (!multiassign) + { + /* Assign current element/slice to the loop variable */ + exec_assign_value(estate, loop_var, value, isnull, + iterator_result_type, iterator_result_typmod); + + /* In slice case, value is temporary; must free it to avoid leakage */ + if (stmt->slice > 0) + pfree(DatumGetPointer(value)); + } + else + { + int i; + bool first = true; + PLpgSQL_row *row = (PLpgSQL_row *) loop_var; + + for (i = 0; i < row->nfields; i++) + { + int varno = row->varnos[i]; - /* In slice case, value is temporary; must free it to avoid leakage */ - if (stmt->slice > 0) - pfree(DatumGetPointer(value)); + if (varno != -1) + { + PLpgSQL_datum *var = (PLpgSQL_datum *) (estate->datums[varno]); + + if (!first) + { + if (!array_iterate(array_iterator, &value, &isnull)) + ereport(ERROR, + (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), + errmsg("array is not well sized, missing data"))); + } + else + first = false; + + exec_assign_value(estate, var, value, isnull, + iterator_result_type, iterator_result_typmod); + } + } + } /* * Execute the statements diff --git a/src/test/regress/expected/plpgsql.out b/src/test/regress/expected/plpgsql.out index 78e5a85..92d448d 100644 --- a/src/test/regress/expected/plpgsql.out +++ b/src/test/regress/expected/plpgsql.out @@ -5127,6 +5127,59 @@ NOTICE: {"(35,78)","(88,76)"} drop function foreach_test(anyarray); drop type xy_tuple; +-- multiassign (key,value) tests +create or replace function foreach_test_ab(anyarray) +returns void as $$ +declare + a text; b text; +begin + foreach a,b in array $1 + loop + raise notice 'a: %, b: %', a, b; + end loop; +end +$$ language plpgsql; +select foreach_test_ab(array[1,2,3,4]); +NOTICE: a: 1, b: 2 +NOTICE: a: 3, b: 4 + foreach_test_ab +----------------- + +(1 row) + +select foreach_test_ab(array[[1,2],[3,4]]); +NOTICE: a: 1, b: 2 +NOTICE: a: 3, b: 4 + foreach_test_ab +----------------- + +(1 row) + +select foreach_test_ab(array[[1,2,3]]); +NOTICE: a: 1, b: 2 +ERROR: array is not well sized, missing data +CONTEXT: PL/pgSQL function foreach_test_ab(anyarray) line 5 at FOREACH over array +select foreach_test_ab('{{{1,2},{3,4}},{{5,6},{7,8}}}'::int[]); +NOTICE: a: 1, b: 2 +NOTICE: a: 3, b: 4 +NOTICE: a: 5, b: 6 +NOTICE: a: 7, b: 8 + foreach_test_ab +----------------- + +(1 row) + +select foreach_test_ab(array[null,null, 1,null, 1,1, null,1]); +NOTICE: a: , b: +NOTICE: a: 1, b: +NOTICE: a: 1, b: 1 +NOTICE: a: , b: 1 + foreach_test_ab +----------------- + +(1 row) + +drop function foreach_test_ab(anyarray); -- -- Assorted tests for array subscript assignment -- diff --git a/src/test/regress/sql/plpgsql.sql b/src/test/regress/sql/plpgsql.sql index e19e415..7640f5d 100644 --- a/src/test/regress/sql/plpgsql.sql +++ b/src/test/regress/sql/plpgsql.sql @@ -4075,6 +4075,27 @@ select foreach_test(ARRAY[[(10,20),(40,69)],[(35,78),(88,76)]]::xy_tuple[]); drop function foreach_test(anyarray); drop type xy_tuple; +-- multiassign (key,value) tests +create or replace function foreach_test_ab(anyarray) +returns void as $$ +declare + a text; b text; +begin + foreach a,b in array $1 + loop + raise notice 'a: %, b: %', a, b; + end loop; +end +$$ language plpgsql; + +select foreach_test_ab(array[1,2,3,4]); +select foreach_test_ab(array[[1,2],[3,4]]); +select foreach_test_ab(array[[1,2,3]]); +select foreach_test_ab('{{{1,2},{3,4}},{{5,6},{7,8}}}'::int[]); +select foreach_test_ab(array[null,null, 1,null, 1,1, null,1]); + +drop function foreach_test_ab(anyarray); + -- -- Assorted tests for array subscript assignment --