From 1380b3dcf2692f2c3bb04af164e8126de47d2d5c Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Sun, 15 Nov 2026 23:38:54 +0900 Subject: [PATCH 1/9] Apply review comments on RPR deparse, node output and docs Copy the name reserve_colname() keeps rather than holding the caller's string. No caller passes a literal today, so the copy only keeps the list from depending on the caller's string. Say what makeRPRPattern() guarantees in the varNames comment of _outRPRPattern(). Walk the window clause list in get_query_def() with foreach_node(), as suggested, and for the same reason do so, or use foreach_ptr(), in the other loops the row pattern code added to ruleutils.c and planner.c. Only the loop in optimizeGroupPattern(), which replaces list elements in place, keeps foreach(). List scan.l among the parser files in README.rpr, as suggested, and with it kwlist.h and the psql and ecpg lexers, which were left out for the same reason: they carry the row pattern keywords and the '|' self token. List execExpr.h and llvmjit_types.c too, which hold the navigation opcodes and register their functions for JIT. In perform.sgml, mark up the two "Under ANALYZE" places as EXPLAIN ANALYZE rather than ANALYZE, which would name the statistics command; this follows the file's own usage. Mark up SELECT in advanced.sgml as a command as well. Reviewed-by: Tatsuo Ishii --- doc/src/sgml/advanced.sgml | 2 +- doc/src/sgml/perform.sgml | 14 +++++------ src/backend/executor/README.rpr | 6 +++++ src/backend/nodes/outfuncs.c | 4 ++-- src/backend/optimizer/plan/planner.c | 8 ++----- src/backend/utils/adt/ruleutils.c | 36 +++++++--------------------- 6 files changed, 27 insertions(+), 43 deletions(-) diff --git a/doc/src/sgml/advanced.sgml b/doc/src/sgml/advanced.sgml index 224bac63525..ef7de636b04 100644 --- a/doc/src/sgml/advanced.sgml +++ b/doc/src/sgml/advanced.sgml @@ -614,7 +614,7 @@ DEFINE when the pattern as a whole can match zero rows (for example A* or A?): such a match contains no rows, so the frame is empty. - Example of a SELECT using + Example of a SELECT using the DEFINE and PATTERN clause is as follows. diff --git a/doc/src/sgml/perform.sgml b/doc/src/sgml/perform.sgml index 767fb1a0170..615ff67da89 100644 --- a/doc/src/sgml/perform.sgml +++ b/doc/src/sgml/perform.sgml @@ -764,17 +764,17 @@ WINDOW w AS ( computed and every row of the frame has to be kept; and Nav Mark Lookahead reports runtime or infinite. - Under ANALYZE, a node that was executed shows the - bound resolved for its last scan instead of runtime: - a row count, retain all or + Under EXPLAIN ANALYZE, a node that was executed shows + the bound resolved for its last scan instead of + runtime: a row count, retain all or infinite. - Under ANALYZE, further lines report counters for the - matcher's internal states and contexts. Those are diagnostics for the - implementation rather than a stable interface; their names and contents - may change. + Under EXPLAIN ANALYZE, further lines report counters + for the matcher's internal states and contexts. Those are diagnostics + for the implementation rather than a stable interface; their names and + contents may change. diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index 0463da20671..26d10a6816b 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -17,6 +17,10 @@ Parser - src/backend/parser/gram.y (grammar and keywords) + - src/backend/parser/scan.l ('|' as a self token) + - src/include/parser/kwlist.h (row pattern keywords) + - src/fe_utils/psqlscan.l (psql lexer, kept in sync) + - src/interfaces/ecpg/preproc/pgc.l (ecpg lexer, kept in sync) - src/backend/parser/parse_rpr.c (PATTERN/DEFINE analysis) - src/include/parser/parse_rpr.h (parser entry points) - src/include/parser/parse_node.h (DEFINE parse state) @@ -46,8 +50,10 @@ - src/backend/executor/execRPR.c (NFA engine) - src/include/executor/execRPR.h (NFA public API) - src/backend/executor/execExpr.c (navigation opcode compile) + - src/include/executor/execExpr.h (navigation opcode steps) - src/backend/executor/execExprInterp.c (navigation opcode eval) - src/backend/jit/llvm/llvmjit_expr.c (JIT path for those opcodes) + - src/backend/jit/llvm/llvmjit_types.c (functions the JIT calls) Node support - src/include/nodes/parsenodes.h (parse node definitions) diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 0a99e602942..2627a55d2c8 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -729,8 +729,8 @@ _outRPRPattern(StringInfo str, const RPRPattern *node) /* * Write varNames array as list of strings. makeRPRPattern() guarantees - * the array, so the list has exactly one spelling and the read side has - * no second shape to interpret. + * the array exists, so the list has exactly one spelling and the read + * side has no second shape to interpret. */ appendStringInfoString(str, " :varNames"); Assert(node->numVars > 0 && node->varNames != NULL); diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 8be248fb01f..8fd89744504 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1257,10 +1257,8 @@ subquery_planner(PlannerGlobal *glob, Query *parse, char *plan_name, * set attached survive onto the replacement -- setrefs.c matches the * DEFINE copy against the target list copy and insists they agree. */ - foreach(l, parse->windowClause) + foreach_node(WindowClause, wc, parse->windowClause) { - WindowClause *wc = lfirst_node(WindowClause, l); - if (wc->defineClause != NIL) wc->defineClause = (List *) flatten_group_exprs(root, root->parse, @@ -6718,10 +6716,8 @@ make_window_input_target(PlannerInfo *root, * Add what is missing now, once the clause has the shape it will be * executed with. */ - foreach(lc, activeWindows) + foreach_node(WindowClause, wc, activeWindows) { - WindowClause *wc = lfirst_node(WindowClause, lc); - if (wc->defineClause != NIL) add_define_inputs_walker((Node *) wc->defineClause, input_target); } diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index d43d0377b6a..72d0e414275 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -4083,27 +4083,22 @@ static void collapse_define_join_vars(Query *query) { collapse_define_context context; - ListCell *lc; int rtindex = 0; /* Collect the merge expressions once; most queries have none */ context.exprs = NIL; context.vars = NIL; context.relids = bms_add_range(NULL, 1, list_length(query->rtable)); - foreach(lc, query->rtable) + foreach_node(RangeTblEntry, rte, query->rtable) { - RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc); - ListCell *lc2; AttrNumber attno = 0; rtindex++; if (rte->rtekind != RTE_JOIN) continue; - foreach(lc2, rte->joinaliasvars) + foreach_ptr(Node, aliasvar, rte->joinaliasvars) { - Node *aliasvar = (Node *) lfirst(lc2); - if (++attno > rte->joinmergedcols) break; @@ -4125,10 +4120,8 @@ collapse_define_join_vars(Query *query) if (context.exprs == NIL) return; - foreach(lc, query->windowClause) + foreach_node(WindowClause, wc, query->windowClause) { - WindowClause *wc = lfirst_node(WindowClause, lc); - if (wc->defineClause != NIL) wc->defineClause = (List *) collapse_define_join_vars_mutator((Node *) wc->defineClause, @@ -4578,11 +4571,8 @@ set_using_names(deparse_namespace *dpns, Node *jtnode, List *parentUsing) static void mark_define_columns(deparse_namespace *dpns, Query *query) { - ListCell *lc; - - foreach(lc, query->windowClause) + foreach_node(WindowClause, wc, query->windowClause) { - WindowClause *wc = lfirst_node(WindowClause, lc); List *vars; /* DEFINE has no outer-level Vars or sub-selects */ @@ -4712,11 +4702,9 @@ preset_input_colname(deparse_namespace *dpns, int varno, AttrNumber attno) { Node *aliasvar = (Node *) list_nth(rte->joinaliasvars, attno - 1); List *vars = pull_var_clause(aliasvar, 0); - ListCell *lc; - foreach(lc, vars) + foreach_node(Var, var, vars) { - Var *var = (Var *) lfirst(lc); char *colname; colname = preset_input_colname(dpns, var->varno, var->varattno); @@ -4734,14 +4722,12 @@ preset_input_colname(deparse_namespace *dpns, int varno, AttrNumber attno) static void reserve_colname(deparse_namespace *dpns, char *colname) { - ListCell *lc; - - foreach(lc, dpns->using_names) + foreach_ptr(char, name, dpns->using_names) { - if (strcmp((char *) lfirst(lc), colname) == 0) + if (strcmp(name, colname) == 0) return; } - dpns->using_names = lappend(dpns->using_names, colname); + dpns->using_names = lappend(dpns->using_names, pstrdup(colname)); } /* @@ -6048,8 +6034,6 @@ get_query_def(Query *query, StringInfo buf, List *parentnamespace, */ if (query->hasGroupRTE) { - ListCell *lc; - query->targetList = (List *) flatten_group_exprs(NULL, query, (Node *) query->targetList); query->havingQual = @@ -6060,10 +6044,8 @@ get_query_def(Query *query, StringInfo buf, List *parentnamespace, * them, or the deparsed text would name the grouping step rather than * the expression the user wrote, and the view would not re-parse. */ - foreach(lc, query->windowClause) + foreach_node(WindowClause, wc, query->windowClause) { - WindowClause *wc = lfirst_node(WindowClause, lc); - if (wc->defineClause != NIL) wc->defineClause = (List *) flatten_group_exprs(NULL, query, (Node *) wc->defineClause); -- 2.54.0 (Apple Git-157)