From 346bdfac78a69a67c790c61b20add74807f3418d Mon Sep 17 00:00:00 2001 From: Haibo Yan Date: Mon, 6 Apr 2026 23:50:50 -0700 Subject: [PATCH v2 01/10] Parse and deparse DISTINCT in window aggregate calls Add a windistinct field to WindowFunc to carry the DISTINCT qualifier through parse analysis, node serialization, and ruleutils deparse. Move the rejection from parse time to executor init so that stored rules and view definitions preserve the syntax. Execution still raises FEATURE_NOT_SUPPORTED until later patches add real support. --- src/backend/executor/nodeWindowAgg.c | 9 +++++++++ src/backend/optimizer/util/clauses.c | 1 + src/backend/parser/parse_func.c | 6 ++++-- src/backend/utils/adt/ruleutils.c | 2 ++ src/include/nodes/primnodes.h | 2 ++ src/test/regress/expected/window.out | 23 +++++++++++++++++++++++ src/test/regress/sql/window.sql | 18 ++++++++++++++++++ 7 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/backend/executor/nodeWindowAgg.c b/src/backend/executor/nodeWindowAgg.c index f52a7aae843..4087ab1b16e 100644 --- a/src/backend/executor/nodeWindowAgg.c +++ b/src/backend/executor/nodeWindowAgg.c @@ -2956,6 +2956,15 @@ initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc, int i; ListCell *lc; + /* + * Temporary: reject DISTINCT window aggregates until executor support + * lands. Patch 2 will replace this with actual DISTINCT handling. + */ + if (wfunc->windistinct) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("DISTINCT is not yet implemented for window aggregates"))); + numArguments = list_length(wfunc->args); i = 0; diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index fcf6d7fff2a..62d62d7fdef 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -2816,6 +2816,7 @@ eval_const_expressions_mutator(Node *node, newexpr->winref = expr->winref; newexpr->winstar = expr->winstar; newexpr->winagg = expr->winagg; + newexpr->windistinct = expr->windistinct; newexpr->ignore_nulls = expr->ignore_nulls; newexpr->location = expr->location; diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index 35ff6427147..d216d53e530 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -847,6 +847,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, /* winref will be set by transformWindowFuncCall */ wfunc->winstar = agg_star; wfunc->winagg = (fdresult == FUNCDETAIL_AGGREGATE); + wfunc->windistinct = agg_distinct; wfunc->aggfilter = agg_filter; wfunc->ignore_nulls = ignore_nulls; wfunc->runCondition = NIL; @@ -854,11 +855,12 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, /* * agg_star is allowed for aggregate functions but distinct isn't + * allowed for non-aggregate window functions. */ - if (agg_distinct) + if (agg_distinct && !wfunc->winagg) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("DISTINCT is not implemented for window functions"), + errmsg("DISTINCT is not implemented for non-aggregate window functions"), parser_errposition(pstate, location))); /* diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index c781cdc84d3..73dc9f85b40 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -11659,6 +11659,8 @@ get_windowfunc_expr_helper(WindowFunc *wfunc, deparse_context *context, appendStringInfoChar(buf, '*'); else { + if (wfunc->windistinct) + appendStringInfoString(buf, "DISTINCT "); if (is_json_objectagg) { get_rule_expr((Node *) linitial(wfunc->args), context, false); diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 6dfc946c20b..1e984dfbfda 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -614,6 +614,8 @@ typedef struct WindowFunc bool winstar pg_node_attr(query_jumble_ignore); /* is function a simple aggregate? */ bool winagg pg_node_attr(query_jumble_ignore); + /* true if aggregate arguments were marked DISTINCT */ + bool windistinct; /* ignore nulls. One of the Null Treatment options */ int ignore_nulls; /* token location, or -1 if unknown */ diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out index e6aac27a2a9..7e9212c4677 100644 --- a/src/test/regress/expected/window.out +++ b/src/test/regress/expected/window.out @@ -5967,3 +5967,26 @@ WINDOW w AS (ORDER BY x ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING); --cleanup DROP TABLE planets CASCADE; NOTICE: drop cascades to view planets_view +-- +-- Test DISTINCT in window aggregates (parse/deparse plumbing only; +-- execution support is not yet implemented) +-- +-- Should parse successfully and round-trip through a view definition +CREATE TEMP VIEW window_distinct_view AS +SELECT count(DISTINCT four) OVER (PARTITION BY ten) AS cnt +FROM tenk1; +SELECT pg_get_viewdef('window_distinct_view') LIKE '%DISTINCT%' AS has_distinct; + has_distinct +-------------- + t +(1 row) + +DROP VIEW window_distinct_view; +-- DISTINCT on a non-aggregate window function is still a parse error +SELECT ntile(DISTINCT 4) OVER () FROM tenk1; -- error +ERROR: DISTINCT is not implemented for non-aggregate window functions +LINE 1: SELECT ntile(DISTINCT 4) OVER () FROM tenk1; + ^ +-- Execution fails with a clear executor-side error +SELECT count(DISTINCT four) OVER (PARTITION BY ten) FROM tenk1; -- error +ERROR: DISTINCT is not yet implemented for window aggregates diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql index 305549b104d..2c211e67052 100644 --- a/src/test/regress/sql/window.sql +++ b/src/test/regress/sql/window.sql @@ -2159,3 +2159,21 @@ WINDOW w AS (ORDER BY x ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING); --cleanup DROP TABLE planets CASCADE; + +-- +-- Test DISTINCT in window aggregates (parse/deparse plumbing only; +-- execution support is not yet implemented) +-- + +-- Should parse successfully and round-trip through a view definition +CREATE TEMP VIEW window_distinct_view AS +SELECT count(DISTINCT four) OVER (PARTITION BY ten) AS cnt +FROM tenk1; +SELECT pg_get_viewdef('window_distinct_view') LIKE '%DISTINCT%' AS has_distinct; +DROP VIEW window_distinct_view; + +-- DISTINCT on a non-aggregate window function is still a parse error +SELECT ntile(DISTINCT 4) OVER () FROM tenk1; -- error + +-- Execution fails with a clear executor-side error +SELECT count(DISTINCT four) OVER (PARTITION BY ten) FROM tenk1; -- error -- 2.52.0