diff --git a/src/common/backend/parser/parser.cpp b/src/common/backend/parser/parser.cpp index e4fe525536ee34d69c7476f51a65d4b6de38cfe9..f35542d0b0aca5bd1db3d739f0006f02b4ea9dcb 100644 --- a/src/common/backend/parser/parser.cpp +++ b/src/common/backend/parser/parser.cpp @@ -741,30 +741,37 @@ int base_yylex(YYSTYPE* lvalp, YYLTYPE* llocp, core_yyscan_t yyscanner) break; } break; - case CURSOR: - GET_NEXT_TOKEN(); - core_yystype_1 = cur_yylval; // the value of cursor - cur_yylloc_1 = cur_yylloc; // the lloc of cursor - next_token_1 = next_token; // the token after curosr - GET_NEXT_TOKEN(); - core_yystype_2 = cur_yylval; // the value after cursor - cur_yylloc_2 = cur_yylloc; // the lloc after cursor - next_token_2 = next_token; // the token after after curosr - - if (next_token_1 == '(' && (is_select_stmt_definitely(next_token))) { - PARSE_CURSOR_PARENTHESES_AS_EXPR(); - } else if (is_prefer_parse_cursor_parentheses_as_expr() && !is_cursor_function_exist()) { - PARSE_CURSOR_PARENTHESES_AS_EXPR(); - } else { - PARSE_CURSOR_PARENTHESES_AS_FUNCTION(); - } - - if (t_thrd.proc->workingVersionNum < CURSOR_EXPRESSION_VERSION_NUMBER && - cur_token == CURSOR_EXPR) { - ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("Unsupported feature: cursor expression during the upgrade"))); - } - break; + case CURSOR: + GET_NEXT_TOKEN(); + core_yystype_1 = cur_yylval; // the value of cursor + cur_yylloc_1 = cur_yylloc; // the lloc of cursor + next_token_1 = next_token; // the token after curosr + if (next_token_1 != '(') { + /* save the lookahead token for next time */ + SET_LOOKAHEAD_TOKEN(); + /* and back up the output info to cur_token */ + lvalp->core_yystype = cur_yylval; + *llocp = cur_yylloc; + } else { + GET_NEXT_TOKEN(); + core_yystype_2 = cur_yylval; // the value after cursor + cur_yylloc_2 = cur_yylloc; // the lloc after cursor + next_token_2 = next_token; // the token after after curosr + + if (next_token_1 == '(' && (is_select_stmt_definitely(next_token))) { + PARSE_CURSOR_PARENTHESES_AS_EXPR(); + } else if (is_prefer_parse_cursor_parentheses_as_expr() && !is_cursor_function_exist()) { + PARSE_CURSOR_PARENTHESES_AS_EXPR(); + } else { + PARSE_CURSOR_PARENTHESES_AS_FUNCTION(); + } + if (t_thrd.proc->workingVersionNum < CURSOR_EXPRESSION_VERSION_NUMBER && + cur_token == CURSOR_EXPR) { + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("Unsupported feature: cursor expression during the upgrade"))); + } + } + break; default: break; } diff --git a/src/test/regress/expected/cursor_expression.out b/src/test/regress/expected/cursor_expression.out index fe470f5703326ea867bf504b2fa7c165a294f4b5..1e7b0a4d18b6124e677875f8f369d4737734aa2a 100644 --- a/src/test/regress/expected/cursor_expression.out +++ b/src/test/regress/expected/cursor_expression.out @@ -938,7 +938,20 @@ NOTICE: CONTEXT: PL/pgSQL function inline_code_block line 10 at FETCH NOTICE: employee_name : zhangsan set enable_auto_explain = off; +create table abort_test(cid int,fid int); +-- expect error +start transaction; +cursor 'abort' for select * from abort_test order by 1; +ERROR: syntax error at or near "'abort'" +LINE 1: cursor 'abort' for select * from abort_test order by 1; + ^ +close 'abort'; +ERROR: syntax error at or near "'abort'" +LINE 1: close 'abort'; + ^ +commit; -- clean +drop table abort_test; drop table test_insert; drop procedure pro_cursor_0011_02; drop table t_cursor_0011_01; diff --git a/src/test/regress/sql/cursor_expression.sql b/src/test/regress/sql/cursor_expression.sql index 435c59fb3bc87f17f9512f8998adda21fb2752ac..ac563df18b860867ecc500c890a86639423b867d 100644 --- a/src/test/regress/sql/cursor_expression.sql +++ b/src/test/regress/sql/cursor_expression.sql @@ -550,7 +550,15 @@ END; / set enable_auto_explain = off; +create table abort_test(cid int,fid int); +-- expect error +start transaction; +cursor 'abort' for select * from abort_test order by 1; +close 'abort'; +commit; + -- clean +drop table abort_test; drop table test_insert; drop procedure pro_cursor_0011_02; drop table t_cursor_0011_01;