From 5757d5ab5daa77484355dcf0eab550a42994d175 Mon Sep 17 00:00:00 2001 From: Tatsuo Ishii Date: Sun, 15 Feb 2026 17:47:48 +0900 Subject: [PATCH v43 2/8] Row pattern recognition patch (parse/analysis). --- src/backend/nodes/copyfuncs.c | 27 +++ src/backend/nodes/equalfuncs.c | 35 +++ src/backend/nodes/outfuncs.c | 51 +++++ src/backend/nodes/readfuncs.c | 85 +++++++ src/backend/parser/Makefile | 1 + src/backend/parser/README | 1 + src/backend/parser/meson.build | 1 + src/backend/parser/parse_agg.c | 7 + src/backend/parser/parse_clause.c | 10 +- src/backend/parser/parse_expr.c | 6 + src/backend/parser/parse_func.c | 3 + src/backend/parser/parse_rpr.c | 367 ++++++++++++++++++++++++++++++ src/include/parser/parse_clause.h | 3 + src/include/parser/parse_rpr.h | 22 ++ 14 files changed, 615 insertions(+), 4 deletions(-) create mode 100644 src/backend/parser/parse_rpr.c create mode 100644 src/include/parser/parse_rpr.h diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index ff22a04abe5..e67ad39bdb8 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -16,6 +16,7 @@ #include "postgres.h" #include "miscadmin.h" +#include "nodes/plannodes.h" #include "utils/datum.h" @@ -166,6 +167,32 @@ _copyBitmapset(const Bitmapset *from) return bms_copy(from); } +static RPRPattern * +_copyRPRPattern(const RPRPattern *from) +{ + RPRPattern *newnode = makeNode(RPRPattern); + + COPY_SCALAR_FIELD(numVars); + COPY_SCALAR_FIELD(maxDepth); + COPY_SCALAR_FIELD(numElements); + + /* Deep copy the varNames array (DEFINE clause is required) */ + Assert(from->numVars > 0); + newnode->varNames = palloc0(from->numVars * sizeof(char *)); + for (int i = 0; i < from->numVars; i++) + newnode->varNames[i] = pstrdup(from->varNames[i]); + + /* Deep copy the elements array (always has at least one element + FIN) */ + Assert(from->numElements >= 2); + newnode->elements = palloc(from->numElements * sizeof(RPRPatternElement)); + memcpy(newnode->elements, from->elements, + from->numElements * sizeof(RPRPatternElement)); + + COPY_SCALAR_FIELD(isAbsorbable); + + return newnode; +} + /* * copyObjectImpl -- implementation of copyObject(); see nodes/nodes.h diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c index 3d1a1adf86e..328199918b8 100644 --- a/src/backend/nodes/equalfuncs.c +++ b/src/backend/nodes/equalfuncs.c @@ -20,6 +20,7 @@ #include "postgres.h" #include "miscadmin.h" +#include "nodes/plannodes.h" #include "utils/datum.h" @@ -149,6 +150,40 @@ _equalBitmapset(const Bitmapset *a, const Bitmapset *b) return bms_equal(a, b); } +static bool +_equalRPRPattern(const RPRPattern *a, const RPRPattern *b) +{ + COMPARE_SCALAR_FIELD(numVars); + COMPARE_SCALAR_FIELD(maxDepth); + COMPARE_SCALAR_FIELD(numElements); + + /* Compare varNames array */ + if (a->numVars > 0) + { + if (a->varNames == NULL || b->varNames == NULL) + return false; + for (int i = 0; i < a->numVars; i++) + { + if (strcmp(a->varNames[i], b->varNames[i]) != 0) + return false; + } + } + + /* Compare elements array */ + if (a->numElements > 0) + { + if (a->elements == NULL || b->elements == NULL) + return false; + if (memcmp(a->elements, b->elements, + a->numElements * sizeof(RPRPatternElement)) != 0) + return false; + } + + COMPARE_SCALAR_FIELD(isAbsorbable); + + return true; +} + /* * Lists are handled specially */ diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 40990143927..aacf8e9f8c7 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -23,6 +23,7 @@ #include "nodes/bitmapset.h" #include "nodes/nodes.h" #include "nodes/pg_list.h" +#include "nodes/plannodes.h" #include "utils/datum.h" /* State flag that determines how nodeToStringInternal() should treat location fields */ @@ -718,6 +719,56 @@ _outA_Const(StringInfo str, const A_Const *node) WRITE_LOCATION_FIELD(location); } +static void +_outRPRPattern(StringInfo str, const RPRPattern *node) +{ + WRITE_NODE_TYPE("RPRPATTERN"); + + WRITE_INT_FIELD(numVars); + WRITE_INT_FIELD(maxDepth); + WRITE_INT_FIELD(numElements); + + /* Write varNames array as list of strings */ + appendStringInfoString(str, " :varNames"); + if (node->numVars > 0 && node->varNames != NULL) + { + appendStringInfoString(str, " ("); + for (int i = 0; i < node->numVars; i++) + { + if (i > 0) + appendStringInfoChar(str, ' '); + outToken(str, node->varNames[i]); + } + appendStringInfoChar(str, ')'); + } + else + appendStringInfoString(str, " <>"); + + /* Write elements array */ + appendStringInfoString(str, " :elements"); + if (node->numElements > 0 && node->elements != NULL) + { + appendStringInfoChar(str, ' '); + for (int i = 0; i < node->numElements; i++) + { + const RPRPatternElement *elem = &node->elements[i]; + + appendStringInfo(str, "(%d %d %u %d %d %d %d)", + (int) elem->varId, + (int) elem->depth, + (unsigned) elem->flags, + (int) elem->min, + (int) elem->max, + (int) elem->next, + (int) elem->jump); + } + } + else + appendStringInfoString(str, " <>"); + + WRITE_BOOL_FIELD(isAbsorbable); +} + /* * outNode - diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index 981ab9c34ef..02be217b167 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -28,6 +28,7 @@ #include "miscadmin.h" #include "nodes/bitmapset.h" +#include "nodes/plannodes.h" #include "nodes/readfuncs.h" @@ -558,6 +559,90 @@ _readExtensibleNode(void) READ_DONE(); } +static RPRPattern * +_readRPRPattern(void) +{ + READ_LOCALS(RPRPattern); + + READ_INT_FIELD(numVars); + READ_INT_FIELD(maxDepth); + READ_INT_FIELD(numElements); + + /* Read varNames array */ + token = pg_strtok(&length); /* skip :varNames */ + token = pg_strtok(&length); /* get '(' or '<>' */ + if (local_node->numVars > 0 && token[0] == '(') + { + local_node->varNames = palloc(local_node->numVars * sizeof(char *)); + for (int i = 0; i < local_node->numVars; i++) + { + token = pg_strtok(&length); + local_node->varNames[i] = debackslash(token, length); + } + token = pg_strtok(&length); /* skip ')' */ + } + else + { + local_node->varNames = NULL; + } + + /* Read elements array */ + token = pg_strtok(&length); /* skip :elements */ + token = pg_strtok(&length); /* get '(' or '<>' */ + if (local_node->numElements > 0 && token[0] == '(') + { + local_node->elements = palloc0(local_node->numElements * sizeof(RPRPatternElement)); + for (int i = 0; i < local_node->numElements; i++) + { + RPRPatternElement *elem = &local_node->elements[i]; + int varId, + flags, + depth, + min, + max, + next, + jump; + + /* Parse "(varId depth flags min max next jump)" */ + token = pg_strtok(&length); + varId = atoi(token); + token = pg_strtok(&length); + depth = atoi(token); + token = pg_strtok(&length); + flags = atoi(token); + token = pg_strtok(&length); + min = atoi(token); + token = pg_strtok(&length); + max = atoi(token); + token = pg_strtok(&length); + next = atoi(token); + token = pg_strtok(&length); + jump = atoi(token); + token = pg_strtok(&length); /* skip ')' */ + + elem->varId = (RPRVarId) varId; + elem->flags = (RPRElemFlags) flags; + elem->depth = (RPRDepth) depth; + elem->min = (RPRQuantity) min; + elem->max = (RPRQuantity) max; + elem->next = (RPRElemIdx) next; + elem->jump = (RPRElemIdx) jump; + + /* Read next element's '(' or end */ + if (i < local_node->numElements - 1) + token = pg_strtok(&length); /* get '(' */ + } + } + else + { + local_node->elements = NULL; + } + + READ_BOOL_FIELD(isAbsorbable); + + READ_DONE(); +} + /* * parseNodeString diff --git a/src/backend/parser/Makefile b/src/backend/parser/Makefile index 8c0fe28d63f..f4c7d605fe3 100644 --- a/src/backend/parser/Makefile +++ b/src/backend/parser/Makefile @@ -29,6 +29,7 @@ OBJS = \ parse_oper.o \ parse_param.o \ parse_relation.o \ + parse_rpr.o \ parse_target.o \ parse_type.o \ parse_utilcmd.o \ diff --git a/src/backend/parser/README b/src/backend/parser/README index e26eb437a9f..2baffa9517e 100644 --- a/src/backend/parser/README +++ b/src/backend/parser/README @@ -26,6 +26,7 @@ parse_node.c create nodes for various structures parse_oper.c handle operators in expressions parse_param.c handle Params (for the cases used in the core backend) parse_relation.c support routines for tables and column handling +parse_rpr.c handle Row Pattern Recognition parse_target.c handle the result list of the query parse_type.c support routines for data type handling parse_utilcmd.c parse analysis for utility commands (done at execution time) diff --git a/src/backend/parser/meson.build b/src/backend/parser/meson.build index 924ee87a453..a07102b37a0 100644 --- a/src/backend/parser/meson.build +++ b/src/backend/parser/meson.build @@ -16,6 +16,7 @@ backend_sources += files( 'parse_oper.c', 'parse_param.c', 'parse_relation.c', + 'parse_rpr.c', 'parse_target.c', 'parse_type.c', 'parse_utilcmd.c', diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 25ee0f87d93..5ed785ea0d5 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -584,6 +584,10 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) errkind = true; break; + case EXPR_KIND_RPR_DEFINE: + errkind = true; + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -1023,6 +1027,9 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_RPR_DEFINE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 06b65d4a605..b30c22933ec 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -37,6 +37,7 @@ #include "parser/parse_func.h" #include "parser/parse_oper.h" #include "parser/parse_relation.h" +#include "parser/parse_rpr.h" #include "parser/parse_target.h" #include "parser/parse_type.h" #include "parser/parser.h" @@ -84,8 +85,6 @@ static void checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName); static TargetEntry *findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind); -static TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node, - List **tlist, ParseExprKind exprKind); static int get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs); static List *resolve_unique_index_expr(ParseState *pstate, InferClause *infer, @@ -97,7 +96,6 @@ static Node *transformFrameOffset(ParseState *pstate, int frameOptions, Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc, Node *clause); - /* * transformFromClause - * Process the FROM clause and add items to the query's range table, @@ -2168,7 +2166,7 @@ findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist, * tlist the target list (passed by reference so we can append to it) * exprKind identifies clause type being processed */ -static TargetEntry * +TargetEntry * findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist, ParseExprKind exprKind) { @@ -3019,6 +3017,10 @@ transformWindowDefinitions(ParseState *pstate, rangeopfamily, rangeopcintype, &wc->endInRangeFunc, windef->endOffset); + + /* Process Row Pattern Recognition related clauses */ + transformRPR(pstate, wc, windef, targetlist); + wc->winref = winref; result = lappend(result, wc); diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index dcfe1acc4c3..219681d6e88 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -577,6 +577,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_COPY_WHERE: case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: + case EXPR_KIND_RPR_DEFINE: /* okay */ break; @@ -1871,6 +1872,9 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_GENERATED_COLUMN: err = _("cannot use subquery in column generation expression"); break; + case EXPR_KIND_RPR_DEFINE: + err = _("cannot use subquery in DEFINE expression"); + break; /* * There is intentionally no default: case here, so that the @@ -3230,6 +3234,8 @@ ParseExprKindName(ParseExprKind exprKind) return "GENERATED AS"; case EXPR_KIND_CYCLE_MARK: return "CYCLE"; + case EXPR_KIND_RPR_DEFINE: + return "DEFINE"; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index 24f6745923b..357b236a818 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2783,6 +2783,9 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_RPR_DEFINE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_rpr.c b/src/backend/parser/parse_rpr.c new file mode 100644 index 00000000000..9e1fa228759 --- /dev/null +++ b/src/backend/parser/parse_rpr.c @@ -0,0 +1,367 @@ +/*------------------------------------------------------------------------- + * + * parse_rpr.c + * Handle Row Pattern Recognition clauses in parser. + * + * This file transforms RPR-related clauses from raw parse tree to planner + * structures during query analysis: + * - Validates frame options (must start at CURRENT ROW, no EXCLUDE) + * - Validates PATTERN variable count (max RPR_VARID_MAX) + * - Transforms DEFINE clause into TargetEntry list + * - Stores PATTERN/SKIP TO/INITIAL clauses for planner + * + * Pattern optimization and compilation to NFA bytecode happens later + * in the planner (see optimizer/plan/rpr.c). + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/parser/parse_rpr.c + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" +#include "optimizer/rpr.h" +#include "parser/parse_clause.h" +#include "parser/parse_collate.h" +#include "parser/parse_coerce.h" +#include "parser/parse_expr.h" +#include "parser/parse_rpr.h" +#include "parser/parse_target.h" + +/* Forward declarations */ +static void validateRPRPatternVarCount(ParseState *pstate, RPRPatternNode *node, + List **varNames); +static List *transformDefineClause(ParseState *pstate, WindowClause *wc, + WindowDef *windef, List **targetlist); + +/* + * transformRPR + * Process Row Pattern Recognition related clauses. + * + * Validates and transforms RPR clauses from parse tree to planner structures: + * - Validates frame options (must start at CURRENT ROW, no EXCLUDE) + * - Stores AFTER MATCH SKIP TO clause + * - Stores SEEK/INITIAL clause + * - Transforms DEFINE clause into TargetEntry list + * - Stores PATTERN AST for deparsing (optimization happens in planner) + * + * Returns early if windef has no rpCommonSyntax (non-RPR window). + */ +void +transformRPR(ParseState *pstate, WindowClause *wc, WindowDef *windef, + List **targetlist) +{ + /* Window definition must exist when called */ + Assert(windef != NULL); + + /* + * Row Pattern Common Syntax clause exists? + */ + if (windef->rpCommonSyntax == NULL) + return; + + /* Check Frame options */ + + /* Frame type must be "ROW" */ + if (wc->frameOptions & FRAMEOPTION_GROUPS) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("FRAME option GROUP is not permitted when row pattern recognition is used"), + errhint("Use: ROWS instead"), + parser_errposition(pstate, + windef->frameLocation >= 0 ? + windef->frameLocation : windef->location))); + if (wc->frameOptions & FRAMEOPTION_RANGE) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("FRAME option RANGE is not permitted when row pattern recognition is used"), + errhint("Use: ROWS instead"), + parser_errposition(pstate, + windef->frameLocation >= 0 ? + windef->frameLocation : windef->location))); + + /* Frame must start at current row */ + if ((wc->frameOptions & FRAMEOPTION_START_CURRENT_ROW) == 0) + { + const char *frameType = "ROWS"; + const char *startBound = "unknown"; + + /* Determine current start bound */ + if (wc->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) + startBound = "UNBOUNDED PRECEDING"; + else if (wc->frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING) + startBound = "offset PRECEDING"; + else if (wc->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING) + startBound = "offset FOLLOWING"; + + /* At least one valid frame start option should be set */ + Assert((wc->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) || + (wc->frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING) || + (wc->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)); + + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("FRAME must start at CURRENT ROW when row pattern recognition is used"), + errdetail("Current frame starts with %s.", startBound), + errhint("Use: %s BETWEEN CURRENT ROW AND ...", frameType), + parser_errposition(pstate, windef->frameLocation >= 0 ? windef->frameLocation : windef->location))); + } + + /* EXCLUDE options are not permitted */ + if ((wc->frameOptions & FRAMEOPTION_EXCLUSION) != 0) + { + const char *excludeType = "EXCLUDE"; + + /* Determine which EXCLUDE option was used */ + if (wc->frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW) + excludeType = "EXCLUDE CURRENT ROW"; + else if (wc->frameOptions & FRAMEOPTION_EXCLUDE_GROUP) + excludeType = "EXCLUDE GROUP"; + else if (wc->frameOptions & FRAMEOPTION_EXCLUDE_TIES) + excludeType = "EXCLUDE TIES"; + + /* At least one valid exclude option should be set */ + Assert((wc->frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW) || + (wc->frameOptions & FRAMEOPTION_EXCLUDE_GROUP) || + (wc->frameOptions & FRAMEOPTION_EXCLUDE_TIES)); + + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("EXCLUDE options are not permitted when row pattern recognition is used"), + errdetail("Frame definition includes %s.", excludeType), + errhint("Remove the EXCLUDE clause from the window definition."), + parser_errposition(pstate, windef->excludeLocation >= 0 ? windef->excludeLocation : windef->location))); + } + + /* Transform AFTER MATCH SKIP TO clause */ + wc->rpSkipTo = windef->rpCommonSyntax->rpSkipTo; + + /* Transform SEEK or INITIAL clause */ + wc->initial = windef->rpCommonSyntax->initial; + + /* Transform DEFINE clause into list of TargetEntry's */ + wc->defineClause = transformDefineClause(pstate, wc, windef, targetlist); + + /* Store PATTERN AST for deparsing */ + wc->rpPattern = windef->rpCommonSyntax->rpPattern; +} + +/* + * validateRPRPatternVarCount + * Validate that PATTERN variables don't exceed RPR_VARID_MAX. + * + * Recursively traverses the pattern tree, collecting unique variable names. + * Throws an error if the number of unique variables exceeds RPR_VARID_MAX. + * + * varNames is both input and output: existing names are preserved, new ones added. + */ +static void +validateRPRPatternVarCount(ParseState *pstate, RPRPatternNode *node, + List **varNames) +{ + ListCell *lc; + + /* Pattern node must exist - parser always provides non-NULL root */ + Assert(node != NULL); + + switch (node->nodeType) + { + case RPR_PATTERN_VAR: + /* Add variable name if not already in list */ + { + bool found = false; + + foreach(lc, *varNames) + { + if (strcmp(strVal(lfirst(lc)), node->varName) == 0) + { + found = true; + break; + } + } + if (!found) + { + /* Check against RPR_VARID_MAX before adding */ + if (list_length(*varNames) >= RPR_VARID_MAX) + ereport(ERROR, + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many pattern variables"), + errdetail("Maximum is %d.", RPR_VARID_MAX), + parser_errposition(pstate, + exprLocation((Node *) node)))); + + *varNames = lappend(*varNames, makeString(pstrdup(node->varName))); + } + } + break; + + case RPR_PATTERN_SEQ: + case RPR_PATTERN_ALT: + case RPR_PATTERN_GROUP: + /* Recurse into children */ + foreach(lc, node->children) + { + validateRPRPatternVarCount(pstate, (RPRPatternNode *) lfirst(lc), + varNames); + } + break; + } +} + +/* + * transformDefineClause + * Process DEFINE clause and transform ResTarget into list of TargetEntry. + * + * For each DEFINE variable: + * 1. Validates PATTERN variable count via validateRPRPatternVarCount + * 2. Checks for duplicate variable names in DEFINE clause + * 3. Transforms expressions and adds to targetlist via findTargetlistEntrySQL99 + * 4. Creates defineClause entry with proper resname (pattern variable name) + * 5. Coerces expressions to boolean type + * 6. Marks column origins and assigns collation information + * + * Note: Variables not in DEFINE are evaluated as TRUE by the executor. + * Variables in DEFINE but not in PATTERN are filtered out by the planner. + * + * XXX we only support column reference in row pattern definition search + * condition, e.g. "price". . is not supported, e.g. "A.price". + */ +static List * +transformDefineClause(ParseState *pstate, WindowClause *wc, WindowDef *windef, + List **targetlist) +{ + ListCell *lc, + *l; + ResTarget *restarget, + *r; + List *restargets; + List *defineClause = NIL; + char *name; + List *patternVarNames = NIL; + + /* + * If Row Definition Common Syntax exists, DEFINE clause must exist. (the + * raw parser should have already checked it.) + */ + Assert(windef->rpCommonSyntax->rpDefs != NULL); + + /* Validate PATTERN variable count (max RPR_VARID_MAX) */ + validateRPRPatternVarCount(pstate, windef->rpCommonSyntax->rpPattern, + &patternVarNames); + + /* + * Check for duplicate row pattern definition variables. The standard + * requires that no two row pattern definition variable names shall be + * equivalent. + */ + restargets = NIL; + foreach(lc, windef->rpCommonSyntax->rpDefs) + { + TargetEntry *te, + *teDefine; + + restarget = (ResTarget *) lfirst(lc); + name = restarget->name; + + foreach(l, restargets) + { + char *n; + + r = (ResTarget *) lfirst(l); + n = r->name; + + if (!strcmp(n, name)) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("row pattern definition variable name \"%s\" appears more than once in DEFINE clause", + name), + parser_errposition(pstate, exprLocation((Node *) r)))); + } + + restargets = lappend(restargets, restarget); + + /* + * Add DEFINE expression (Restarget->val) to the targetlist as a + * TargetEntry if it does not exist yet. Planner will add the column + * ref var node to the outer plan's target list later on. This makes + * DEFINE expression could access the outer tuple while evaluating + * PATTERN. + * + * Note: findTargetlistEntrySQL99 does Expr transformation and clobber + * restarget->val. + */ + + /* + * Save the original expression location before transformation. + * findTargetlistEntrySQL99 may return an existing TargetEntry whose + * location points to where it was originally created (e.g., ORDER + * BY), not the DEFINE clause. We need to preserve the DEFINE location + * for accurate error reporting. + */ + { + int defineExprLocation = exprLocation(restarget->val); + + te = findTargetlistEntrySQL99(pstate, restarget->val, + targetlist, EXPR_KIND_RPR_DEFINE); + + /* ------------------- + * Copy the TargetEntry for defineClause and always set the pattern + * variable name. We use copyObject so the original targetlist entry + * is not modified. + * + * Note: We must always set resname to the pattern variable name. + * findTargetlistEntrySQL99 creates new TEs with resname = NULL + * (resjunk entries), but returns existing TEs unchanged when the + * expression already exists in targetlist. + * + * Example: "SELECT id, flag, ... WINDOW w AS (... DEFINE T AS flag)" + * + * 1. SELECT list processing creates: TE{resname="flag", expr=flag} + * 2. DEFINE T AS flag: findTargetlistEntrySQL99 finds existing TE + * 3. te->resname is "flag" (from SELECT), not NULL + * 4. Without unconditionally setting resname, teDefine->resname + * would remain "flag" instead of pattern variable name "T" + * 5. buildRPRPattern builds defineVariableList from resname, so + * it would contain ["flag"] instead of ["T"] + * 6. Pattern variable "T" not found -> Assert failure crash + */ + teDefine = copyObject(te); + teDefine->resname = pstrdup(name); + + /* + * Update the expression location to point to the DEFINE clause. + * This ensures error messages reference the correct source + * location. + */ + if (defineExprLocation >= 0 && IsA(teDefine->expr, Var)) + ((Var *) teDefine->expr)->location = defineExprLocation; + } + + /* build transformed DEFINE clause (list of TargetEntry) */ + defineClause = lappend(defineClause, teDefine); + } + list_free(restargets); + + /* + * Make sure that the row pattern definition search condition is a boolean + * expression. + */ + foreach_ptr(TargetEntry, te, defineClause) + (void) coerce_to_boolean(pstate, (Node *) te->expr, "DEFINE"); + + /* mark column origins */ + markTargetListOrigins(pstate, defineClause); + + /* mark all nodes in the DEFINE clause tree with collation information */ + assign_expr_collations(pstate, (Node *) defineClause); + + return defineClause; +} diff --git a/src/include/parser/parse_clause.h b/src/include/parser/parse_clause.h index fe234611007..8aaac881f2b 100644 --- a/src/include/parser/parse_clause.h +++ b/src/include/parser/parse_clause.h @@ -52,6 +52,9 @@ extern List *addTargetToSortList(ParseState *pstate, TargetEntry *tle, extern Index assignSortGroupRef(TargetEntry *tle, List *tlist); extern bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList); +extern TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node, + List **tlist, ParseExprKind exprKind); + /* functions in parse_jsontable.c */ extern ParseNamespaceItem *transformJsonTable(ParseState *pstate, JsonTable *jt); diff --git a/src/include/parser/parse_rpr.h b/src/include/parser/parse_rpr.h new file mode 100644 index 00000000000..7fab6f292aa --- /dev/null +++ b/src/include/parser/parse_rpr.h @@ -0,0 +1,22 @@ +/*------------------------------------------------------------------------- + * + * parse_rpr.h + * handle Row Pattern Recognition in parser + * + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/parser/parse_rpr.h + * + *------------------------------------------------------------------------- + */ +#ifndef PARSE_RPR_H +#define PARSE_RPR_H + +#include "parser/parse_node.h" + +extern void transformRPR(ParseState *pstate, WindowClause *wc, + WindowDef *windef, List **targetlist); + +#endif /* PARSE_RPR_H */ -- 2.43.0