diff --git a/src/common/pl/plpgsql/src/gram.y b/src/common/pl/plpgsql/src/gram.y index 17580149..ecd3861e 100755 --- a/src/common/pl/plpgsql/src/gram.y +++ b/src/common/pl/plpgsql/src/gram.y @@ -2963,6 +2963,10 @@ for_control : for_variable K_IN check_assignable((PLpgSQL_datum *)newp->rec ? (PLpgSQL_datum *)newp->rec : (PLpgSQL_datum *)newp->row, @1); } else { + /* check the sql */ + if (u_sess->attr.attr_sql.sql_compatibility == A_FORMAT && ALLOW_PROCEDURE_COMPILE_CHECK) { + (void)getCursorTupleDesc(expr1, false, true); + } newp->rec = $1.rec; check_assignable((PLpgSQL_datum *) newp->rec, @1); } @@ -2979,6 +2983,10 @@ for_control : for_variable K_IN check_assignable((PLpgSQL_datum *)newp->rec ? (PLpgSQL_datum *)newp->rec : (PLpgSQL_datum *)newp->row, @1); } else { + /* check the sql */ + if (u_sess->attr.attr_sql.sql_compatibility == A_FORMAT && ALLOW_PROCEDURE_COMPILE_CHECK) { + (void)getCursorTupleDesc(expr1, false, true); + } newp->row = $1.row; check_assignable((PLpgSQL_datum *) newp->row, @1); } @@ -2995,6 +3003,10 @@ for_control : for_variable K_IN check_assignable((PLpgSQL_datum *)newp->rec ? (PLpgSQL_datum *)newp->rec : (PLpgSQL_datum *)newp->row, @1); } else { + /* check the sql */ + if (u_sess->attr.attr_sql.sql_compatibility == A_FORMAT && ALLOW_PROCEDURE_COMPILE_CHECK) { + (void)getCursorTupleDesc(expr1, false, true); + } /* convert single scalar to list */ newp->row = make_scalar_list1($1.name, $1.scalar, $1.dno, $1.lineno, @1); /* no need for check_assignable */ @@ -4342,6 +4354,7 @@ stmt_open : K_OPEN cursor_variable yyerror("syntax error"); } } +#ifndef ENABLE_MULTIPLE_NODES if (newp->query != NULL && u_sess->attr.attr_sql.sql_compatibility == A_FORMAT && ALLOW_PROCEDURE_COMPILE_CHECK) { (void)getCursorTupleDesc(newp->query, false, true); } @@ -4349,6 +4362,7 @@ stmt_open : K_OPEN cursor_variable { (void)getCursorTupleDesc(newp->dynquery, false, true); } +#endif } else { @@ -8773,9 +8787,11 @@ make_execsql_stmt(int firsttoken, int location) pfree_ext(ds.data); check_sql_expr(expr->query, location, 0); +#ifndef ENABLE_MULTIPLE_NODES if (firsttoken == K_SELECT && u_sess->attr.attr_sql.sql_compatibility == A_FORMAT && ALLOW_PROCEDURE_COMPILE_CHECK) { (void)getCursorTupleDesc(expr, false, true); } +#endif execsql = (PLpgSQL_stmt_execsql *)palloc(sizeof(PLpgSQL_stmt_execsql)); execsql->cmd_type = PLPGSQL_STMT_EXECSQL; execsql->lineno = plpgsql_location_to_lineno(location); diff --git a/src/test/regress/expected/hw_package_variable.out b/src/test/regress/expected/hw_package_variable.out index 6c4e85a3..983680c9 100644 --- a/src/test/regress/expected/hw_package_variable.out +++ b/src/test/regress/expected/hw_package_variable.out @@ -2732,6 +2732,61 @@ NOTICE: drop cascades to function pkg_val_2.p1() DROP PACKAGE pck2; NOTICE: drop cascades to function pkg_val_2.p1() DROP TABLE t1; +-- test check sql in for rec in query loop +create or replace function check_f04 return int as +declare +v_2 number(10,2):=-213.989; +v_3 float:=22.34; +v_4 char(1):='q'; +v_6 date:='1999-09-09'; +v_7 clob:='dddd'; +v_8 blob:='ad'; +begin +raise info '1-0 %', v_6; +for v_6 in select c3 from check_tab1 order by c3 limit 6 +loop +raise info '1-1 %', v_6; +end loop; +raise info '1-2 %', v_6; + +return 1; +exception +when others then +raise info 'error is %',sqlerrm; +return 0; +end; +/ +ERROR: compile failed when parse the query: select c3 from check_tab1 order by c3 limit 6 +DETAIL: relation "check_tab1" does not exist on datanode1 +CONTEXT: compilation of PL/pgSQL function "check_f04" near line 11 +create table check_tab1(a int); +create or replace function check_f04 return int as +declare +v_2 number(10,2):=-213.989; +v_3 float:=22.34; +v_4 char(1):='q'; +v_6 date:='1999-09-09'; +v_7 clob:='dddd'; +v_8 blob:='ad'; +begin +raise info '1-0 %', v_6; +for v_6 in select c3 from check_tab1 order by c3 limit 6 +loop +raise info '1-1 %', v_6; +end loop; +raise info '1-2 %', v_6; + +return 1; +exception +when others then +raise info 'error is %',sqlerrm; +return 0; +end; +/ +ERROR: compile failed when parse the query: select c3 from check_tab1 order by c3 limit 6 +DETAIL: column "c3" does not exist +CONTEXT: compilation of PL/pgSQL function "check_f04" near line 11 +drop table check_tab1; reset behavior_compat_options; -- ref package cursor attr at first create table t1(a int, b int); diff --git a/src/test/regress/sql/hw_package_variable.sql b/src/test/regress/sql/hw_package_variable.sql index 08c0f596..2316610b 100644 --- a/src/test/regress/sql/hw_package_variable.sql +++ b/src/test/regress/sql/hw_package_variable.sql @@ -2139,6 +2139,60 @@ end pck1; DROP PACKAGE pck1; DROP PACKAGE pck2; DROP TABLE t1; + +-- test check sql in for rec in query loop +create or replace function check_f04 return int as +declare +v_2 number(10,2):=-213.989; +v_3 float:=22.34; +v_4 char(1):='q'; +v_6 date:='1999-09-09'; +v_7 clob:='dddd'; +v_8 blob:='ad'; +begin +raise info '1-0 %', v_6; +for v_6 in select c3 from check_tab1 order by c3 limit 6 +loop +raise info '1-1 %', v_6; +end loop; +raise info '1-2 %', v_6; + +return 1; +exception +when others then +raise info 'error is %',sqlerrm; +return 0; +end; +/ + +create table check_tab1(a int); + +create or replace function check_f04 return int as +declare +v_2 number(10,2):=-213.989; +v_3 float:=22.34; +v_4 char(1):='q'; +v_6 date:='1999-09-09'; +v_7 clob:='dddd'; +v_8 blob:='ad'; +begin +raise info '1-0 %', v_6; +for v_6 in select c3 from check_tab1 order by c3 limit 6 +loop +raise info '1-1 %', v_6; +end loop; +raise info '1-2 %', v_6; + +return 1; +exception +when others then +raise info 'error is %',sqlerrm; +return 0; +end; +/ + +drop table check_tab1; + reset behavior_compat_options; -- ref package cursor attr at first