From edad6471489d372335fc7a4fc97694e5a53b99a8 Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sun, 15 Nov 2026 17:45:54 +0000 Subject: [PATCH 1/3] Rust PG server: grouping by a FROM subquery's row leaves its columns ungrouped (RP-852) Co-Authored-By: Claude Opus 5.5 --- changelog.d/rp-852-group-by-subquery-row.md | 9 ++ crates/secantus-pgplan/src/lib.rs | 132 ++++++++++++++++++++ tasks/backlog-rust-postgres.md | 16 --- tests/backlog/test_rp_found_rp699.py | 11 +- 4 files changed, 151 insertions(+), 17 deletions(-) create mode 100644 changelog.d/rp-852-group-by-subquery-row.md diff --git a/changelog.d/rp-852-group-by-subquery-row.md b/changelog.d/rp-852-group-by-subquery-row.md new file mode 100644 index 000000000..4fd47e4a7 --- /dev/null +++ b/changelog.d/rp-852-group-by-subquery-row.md @@ -0,0 +1,9 @@ +### Grouping by a FROM subquery's row no longer groups its columns + +A query that groups by a FROM subquery's whole row, as in `select v.a from (values (1), (2)) v(a) group by v`, answered the rows as though `v.a` were grouped. PostgreSQL derives no functional dependency of a column on a subquery's row, so it rejects such a column outside an aggregate with 42803 at the reference, a select-list `v.*` included. The Rust PostgreSQL server now does the same, while a column grouped in its own right alongside the row is still accepted. + +#### Fixed + +- `GROUP BY v` over a FROM subquery: a column of `v` outside an aggregate and not itself grouped is 42803 `column "v.a" must appear in the GROUP BY clause or be used in an aggregate function`, positioned at the reference. + +Fixes RP-852 (tests/backlog/test_rp_found_rp699.py::test_group_by_a_from_subquery_row_leaves_its_columns_ungrouped) diff --git a/crates/secantus-pgplan/src/lib.rs b/crates/secantus-pgplan/src/lib.rs index 34624fd7b..dd90d7b20 100644 --- a/crates/secantus-pgplan/src/lib.rs +++ b/crates/secantus-pgplan/src/lib.rs @@ -43894,6 +43894,137 @@ fn check_ungrouped_whole_row( } } +/// A grouped query over a FROM subquery grouped by the subquery's row +/// (`group by v`): its columns are not grouped by that, as PostgreSQL has +/// no functional dependency of a column on a subquery's row +/// (`check_ungrouped_columns`), so a column of it outside an aggregate -- +/// a select-list `v.*` included, at the star -- is 42803 (RP-852). +fn check_columns_under_row_grouping( + s: &pg_query::protobuf::SelectStmt, + alias: &str, + cols: &[String], +) -> Result<()> { + if cols.iter().any(|c| c == alias) { + return Ok(()); + } + let names = |c: &pg_query::protobuf::ColumnRef| -> Vec> { + c.fields + .iter() + .map(|f| match f.node.as_ref() { + Some(N::String(sv)) => Some(sv.sval.clone()), + Some(N::AStar(_)) => Some("*".into()), + _ => None, + }) + .collect() + }; + let target_val = |i: usize| -> Option<&pg_query::protobuf::Node> { + match s.target_list.get(i)?.node.as_ref() { + Some(N::ResTarget(rt)) => rt.val.as_deref(), + _ => None, + } + }; + let mut by_row = false; + let mut grouped: Vec = Vec::new(); + for g in &s.group_clause { + let g = match g.node.as_ref() { + Some(N::AConst(c)) => match &c.val { + Some(pg_query::protobuf::a_const::Val::Ival(i)) if i.ival >= 1 => { + match target_val(i.ival as usize - 1) { + Some(v) => v, + None => continue, + } + } + _ => continue, + }, + _ => g, + }; + let Some(N::ColumnRef(c)) = g.node.as_ref() else { + // Any other grouping item: leave the statement to the + // grouping check proper. + return Ok(()); + }; + match names(c).as_slice() { + [Some(a)] if a == alias => by_row = true, + [Some(a), Some(star)] if a == alias && star == "*" => by_row = true, + [Some(col)] | [Some(_), Some(col)] => grouped.push(col.clone()), + _ => return Ok(()), + } + } + if !by_row { + return Ok(()); + } + let ungrouped = |col: &str| cols.iter().any(|c| c == col) && !grouped.iter().any(|g| g == col); + let mut found: Option<(i32, String)> = None; + let scan = |n: &pg_query::protobuf::Node, found: &mut Option<(i32, String)>| -> Result<()> { + let mut n = n.clone(); + walk_expr(&mut n, &mut |x| { + let opaque = match x.node.as_ref() { + Some(N::SubLink(_)) | Some(N::GroupingFunc(_)) => true, + Some(N::FuncCall(f)) => { + f.over.is_none() + && func_name(f) + .as_deref() + .is_some_and(|n| aggregate_func(n, f.agg_within_group).is_some()) + } + Some(N::ColumnRef(c)) if found.is_none() => { + let col = match names(c).as_slice() { + [Some(col)] => Some(col.clone()), + [Some(a), Some(col)] if a == alias => Some(col.clone()), + _ => None, + }; + if let Some(col) = col.filter(|c| c != "*" && ungrouped(c)) { + *found = Some((c.location, col)); + } + false + } + _ => false, + }; + if opaque { + *x = pg_query::protobuf::Node { node: None }; + } + Ok(()) + }) + }; + for t in &s.target_list { + let Some(N::ResTarget(rt)) = t.node.as_ref() else { + continue; + }; + let Some(v) = rt.val.as_deref() else { + continue; + }; + if let Some(N::ColumnRef(c)) = v.node.as_ref() { + let star = match names(c).as_slice() { + [Some(a), Some(st)] => a == alias && st == "*", + [Some(st)] => st == "*", + _ => false, + }; + if star { + if found.is_none() { + if let Some(col) = cols.iter().find(|c| ungrouped(c)) { + found = Some((c.location, col.clone())); + } + } + continue; + } + } + scan(v, &mut found)?; + } + if let Some(h) = s.having_clause.as_deref() { + scan(h, &mut found)?; + } + match found { + None => Ok(()), + Some((location, col)) => { + if location >= 0 { + set_error_location(location); + } + Err(Error::Grouping(format!( + "column \"{alias}.{col}\" must appear in the GROUP BY clause or be used in an aggregate function" + ))) + } + } +} + /// Does the FROM function `rf` return one column? Planned for its output /// only, nothing in it called (as a subquery's names are, RP-472). fn function_returns_one_column( @@ -44080,6 +44211,7 @@ fn whole_row_rewrite( rowtype = Some(table_rowtype(table)); } } + check_columns_under_row_grouping(s, &alias, &cols)?; (alias, cols) } _ => return Ok(None), diff --git a/tasks/backlog-rust-postgres.md b/tasks/backlog-rust-postgres.md index 547178275..5798288a0 100644 --- a/tasks/backlog-rust-postgres.md +++ b/tasks/backlog-rust-postgres.md @@ -3241,22 +3241,6 @@ statement is VALUES-only and runs there as written. The gap test is in `tests/backlog/test_rp_found_rp849.py`. -## Found while working RP-704 (2026-11-10) - -Measured on PostgreSQL 15.19 (the shared reference on port 5415): every -statement reads only `VALUES` lists. - -- [ ] **`GROUP BY v` over a FROM subquery groups by its column, not - the row.** `select v.a from (values (1), (2)) v(a) group by v` is - 42803 `column "v.a" must appear in the GROUP BY clause or be used in - an aggregate function` at 8 on PostgreSQL 15.19, and so is `select - v.* ... group by v`. Here both answer the rows: the whole-row - grouping rule (`check_ungrouped_whole_row`, RP-532 / RP-536) covers a - lone table only, and a subquery's `v` resolves as its column. (The - star over a subquery itself now expands, `expand_star_in_grouped`.) - Verify: tests/backlog/test_rp_found_rp699.py::test_group_by_a_from_subquery_row_leaves_its_columns_ungrouped - Backlog-ID: RP-852 - ## Found while working RP-700 (2026-11-10) Measured on PostgreSQL 15.19 (the shared reference on port 5415): every diff --git a/tests/backlog/test_rp_found_rp699.py b/tests/backlog/test_rp_found_rp699.py index c23c7c7cb..6ddb8eab4 100644 --- a/tests/backlog/test_rp_found_rp699.py +++ b/tests/backlog/test_rp_found_rp699.py @@ -184,10 +184,19 @@ def test_a_star_over_a_from_subquery_in_a_grouped_query(query: str, dsn: str) -> 'column "v.a" must appear in the GROUP BY clause or be used in an aggregate function', "8", ), + "select v.b from (values (1, 2)) v(a, b) group by v, a": ( + "42803", + 'column "v.b" must appear in the GROUP BY clause or be used in an aggregate function', + "8", + ), + "select v.a from (values (1), (2)) v(a) group by v, a": ([(1,), (2,)], ["a"]), + "select count(*) from (values (1), (1)) v(a) group by v": ([(2,)], ["count"]), } -@pytest.mark.backlog("RP-852") +@pytest.mark.backlog_fixed("RP-852") @pytest.mark.parametrize("query", list(GROUPED_BY_SUBQUERY_ROW)) def test_group_by_a_from_subquery_row_leaves_its_columns_ungrouped(query: str, dsn: str) -> None: + """`group by v` over a FROM subquery answered its columns as grouped; + they are now 42803 unless grouped themselves, as on PostgreSQL 15.19.""" assert answer(dsn, query) == GROUPED_BY_SUBQUERY_ROW[query] From 41447820a54bf5eba41b0cb52478d90899b891fc Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sun, 15 Nov 2026 18:20:22 +0000 Subject: [PATCH 2/3] Rust PG server: an outer column in a set operation's ORDER BY is 0A000 (RP-767) Co-Authored-By: Claude Opus 5.5 --- changelog.d/rp767-setop-order-by-outer.md | 13 ++++++ crates/secantus-pgplan/src/correlated.rs | 56 +++++++++++++++++++++++ tasks/backlog-rust-postgres.md | 16 ------- tests/backlog/test_rp_found_rp698.py | 4 +- 4 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 changelog.d/rp767-setop-order-by-outer.md diff --git a/changelog.d/rp767-setop-order-by-outer.md b/changelog.d/rp767-setop-order-by-outer.md new file mode 100644 index 000000000..d945204bc --- /dev/null +++ b/changelog.d/rp767-setop-order-by-outer.md @@ -0,0 +1,13 @@ +### A set operation's ORDER BY refuses an outer column as PostgreSQL does + +The ORDER BY of a UNION, INTERSECT or EXCEPT inside a correlated subquery +sees only the set operation's output columns. A name that resolves to the +outer row is a sort expression there, which PostgreSQL refuses as 0A000 +`invalid UNION/INTERSECT/EXCEPT ORDER BY clause`; the Rust PostgreSQL server +now gives the same error, detail and position instead of 42703. + +#### Fixed + +- Rust PG server: `order by b` / `order by v.b` naming an outer column in a + correlated set operation is 0A000 with PostgreSQL's DETAIL, at the column. + Fixes RP-767 (tests/backlog/test_rp_found_rp698.py::test_an_outer_column_in_a_set_operations_order_by) diff --git a/crates/secantus-pgplan/src/correlated.rs b/crates/secantus-pgplan/src/correlated.rs index 3b09d62fe..fc242f0fa 100644 --- a/crates/secantus-pgplan/src/correlated.rs +++ b/crates/secantus-pgplan/src/correlated.rs @@ -843,6 +843,61 @@ fn fromless_outer_names(inner: &pg_query::protobuf::SelectStmt, outer: &[String] names } +/// `transformSetOperationStmt`: a set operation's ORDER BY sees only its +/// output columns, so a name resolving to the outer row is a sort +/// expression, which it refuses (RP-767). A name neither level has stays +/// 42703, found by planning. +fn set_operation_order_by_outer( + inner: &pg_query::protobuf::SelectStmt, + outer: &[String], + outer_refs: &[Vec], +) -> Result<()> { + if inner.larg.is_none() { + return Ok(()); + } + let mut leftmost = inner; + while let Some(l) = leftmost.larg.as_deref() { + leftmost = l; + } + let output: Vec = leftmost + .target_list + .iter() + .filter_map(|t| match t.node.as_ref()? { + N::ResTarget(rt) if !rt.name.is_empty() => Some(rt.name.clone()), + N::ResTarget(rt) => match rt.val.as_ref()?.node.as_ref()? { + N::ColumnRef(c) => parts_of(c)?.pop(), + _ => None, + }, + _ => None, + }) + .collect(); + for sb in &inner.sort_clause { + let Some(N::SortBy(sb)) = sb.node.as_ref() else { + continue; + }; + let Some(N::ColumnRef(c)) = sb.node.as_deref().and_then(|n| n.node.as_ref()) else { + continue; + }; + let Some(parts) = parts_of(c) else { + continue; + }; + let is_outer = match parts.as_slice() { + [name] => !output.contains(name) && outer.contains(name), + _ => outer_refs.contains(&parts), + }; + if is_outer { + crate::set_error_location(c.location); + return Err(Error::Sqlstate( + "0A000", + "invalid UNION/INTERSECT/EXCEPT ORDER BY clause\nDetail: Only result column \ + names can be used, not expressions or functions." + .into(), + )); + } + } + Ok(()) +} + pub(crate) fn correlate( sl: &pg_query::protobuf::SubLink, shape: &pg_query::protobuf::SubLink, @@ -891,6 +946,7 @@ pub(crate) fn correlate( } Ok(()) })?; + set_operation_order_by_outer(inner, outer, &outer_refs)?; // Unqualified ones, found by planning: a name the subquery cannot resolve // that the outer query has is a correlation; anything else is a typo. let first = params.len() + 1; diff --git a/tasks/backlog-rust-postgres.md b/tasks/backlog-rust-postgres.md index 5798288a0..c552959c4 100644 --- a/tasks/backlog-rust-postgres.md +++ b/tasks/backlog-rust-postgres.md @@ -3016,22 +3016,6 @@ test is in `tests/backlog/test_rp_found_rp900.py`. Verify: tests/backlog/test_rp_found_rp900.py::test_the_unnamed_portal_is_not_in_pg_cursors Backlog-ID: RP-936 -## Found while working RP-698 (2026-11-10) - -Measured on PostgreSQL 15.19 (the shared reference on port 5415). - -- [ ] **An outer column in a set operation's ORDER BY is 42703.** `select - (select a from (values (1)) t(a) union select 9 order by b limit 1) - from (values (5, 6)) v(a, b)` is 0A000 `invalid UNION/INTERSECT/EXCEPT - ORDER BY clause`, DETAIL `Only result column names can be used, not - expressions or functions.`, at 65 on PostgreSQL 15.19: the ORDER BY - sees only the output columns, so `b` resolves to the outer row and is - a sort expression (`transformSetOperationStmt`); `order by v.b` is the - same. Here each is 42703 `column "b" does not exist`. A name neither - level has is 42703 on both. - Verify: tests/backlog/test_rp_found_rp698.py::test_an_outer_column_in_a_set_operations_order_by - Backlog-ID: RP-767 - ## Found while working RP-756 (2026-11-10) Measured on PostgreSQL 15.19 (the shared reference on port 5415): every diff --git a/tests/backlog/test_rp_found_rp698.py b/tests/backlog/test_rp_found_rp698.py index f24ebbf82..6ef4210e6 100644 --- a/tests/backlog/test_rp_found_rp698.py +++ b/tests/backlog/test_rp_found_rp698.py @@ -1,6 +1,6 @@ """What working RP-698 (a correlated set operation) found: -- an outer column in a set operation's ORDER BY is 42703 `column "b" does +- an outer column in a set operation's ORDER BY was 42703 `column "b" does not exist`, where PostgreSQL's `transformSetOperationStmt` resolves it to the outer row and refuses it as 0A000 `invalid UNION/INTERSECT/EXCEPT ORDER BY clause` (RP-767). @@ -61,7 +61,7 @@ def answer(dsn: str, query: str) -> Any: } -@pytest.mark.backlog("RP-767") +@pytest.mark.backlog_fixed("RP-767") @pytest.mark.parametrize("query", list(SET_OPERATION_ORDER_BY_OUTER)) def test_an_outer_column_in_a_set_operations_order_by(query: str, dsn: str) -> None: assert answer(dsn, query) == SET_OPERATION_ORDER_BY_OUTER[query] From 92aba52eaeef26e76c3c99996d3a96fec7f87f84 Mon Sep 17 00:00:00 2001 From: Joe Drumgoole Date: Sun, 15 Nov 2026 18:42:00 +0000 Subject: [PATCH 3/3] Rust PG server: a named argument to a built-in without argument names is 42883 (RP-677) Co-Authored-By: Claude Opus 5.5 --- .../rp-677-named-arg-unnamed-builtin.md | 14 +++++ crates/secantus-pgplan/src/lib.rs | 58 +++++++++++++++++-- crates/secantus-pgplan/src/named_args.rs | 5 ++ tasks/backlog-rust-postgres.md | 9 --- tests/backlog/test_rp_found_rp458.py | 7 ++- 5 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 changelog.d/rp-677-named-arg-unnamed-builtin.md diff --git a/changelog.d/rp-677-named-arg-unnamed-builtin.md b/changelog.d/rp-677-named-arg-unnamed-builtin.md new file mode 100644 index 000000000..9d0ec11b9 --- /dev/null +++ b/changelog.d/rp-677-named-arg-unnamed-builtin.md @@ -0,0 +1,14 @@ +### A named argument to a built-in without argument names is 42883 + +A built-in such as `nextval` declares no argument names, so PostgreSQL finds +no function for `nextval(regclass := 'seq')` and says so: 42883 `function +nextval(regclass => unknown) does not exist`, at the call, with the +no-function-matches hint. The Rust PostgreSQL server answered 0A000 +`NamedArgExpr is not supported yet` instead, a gap where PostgreSQL gives a +definite answer. It now gives PostgreSQL's. + +#### Fixed + +- Rust PG server: a named argument to a built-in whose parameters have no + names is 42883 naming the call, each named argument printed `name => type`. + Fixes RP-677 (tests/backlog/test_rp_found_rp458.py::test_named_argument_to_a_builtin_without_argument_names) diff --git a/crates/secantus-pgplan/src/lib.rs b/crates/secantus-pgplan/src/lib.rs index dd90d7b20..788346a2b 100644 --- a/crates/secantus-pgplan/src/lib.rs +++ b/crates/secantus-pgplan/src/lib.rs @@ -4121,21 +4121,66 @@ fn refuse_qualified_builtins(node: &N) -> Result<()> { Ok(()) } +/// A named argument matches only a parameter `pg_proc.proargnames` names +/// (`FuncnameGetCandidates` / `MatchNamedCall`): a built-in with no +/// argument names at all, `nextval` say, has no candidate for such a call, +/// which is 42883 naming it with each named argument as `name => type`. +fn refuse_named_args_to_unnamed_builtins(node: &N) -> Result<()> { + for (n, _, _, _) in node.nodes() { + let pg_query::NodeRef::FuncCall(f) = n else { + continue; + }; + if !f + .args + .iter() + .any(|a| matches!(a.node.as_ref(), Some(N::NamedArgExpr(_)))) + { + continue; + } + let Some(name) = func_name(f) else { + continue; + }; + if f.funcname.len() > 1 && !func_schema_is_pg_catalog(f) { + continue; + } + if !funcsig::is_builtin_function_name(&name) + || named_args::has_argument_names(&name) + || correlated::user_function_named(&name) + { + continue; + } + return Err(no_such_call(f, &name)); + } + Ok(()) +} + +fn func_schema_is_pg_catalog(f: &pg_query::protobuf::FuncCall) -> bool { + matches!( + f.funcname.first().and_then(|n| n.node.as_ref()), + Some(N::String(s)) if s.sval == "pg_catalog" + ) +} + /// 42883 for a call PostgreSQL finds no function for, at the call, its /// arguments' types as it prints them (`unknown` for a bare literal). fn no_such_call(f: &pg_query::protobuf::FuncCall, written: &str) -> Error { - let types: Vec = f - .args - .iter() - .map(|a| match a.node.as_ref() { + fn arg_type(a: &pg_query::protobuf::Node) -> String { + match a.node.as_ref() { Some(N::AConst(c)) if c.isnull || matches!(c.val, Some(pg_query::protobuf::a_const::Val::Sval(_))) => { "unknown".to_string() } + // `funcname_signature_string` prints a named argument as + // `name => type`. + Some(N::NamedArgExpr(na)) => match na.arg.as_deref() { + Some(v) => format!("{} => {}", na.name, arg_type(v)), + None => format!("{} => unknown", na.name), + }, _ => display_type(&static_type(a, &Bson::Null)), - }) - .collect(); + } + } + let types: Vec = f.args.iter().map(arg_type).collect(); set_error_location(f.location); Error::UndefinedFunction(format!( "function {written}({}) does not exist", @@ -6108,6 +6153,7 @@ fn plan_node( schemas::resolve_shadowed_calls(&mut node, lookup)?; collation::citext_operators(&mut node, lookup)?; refuse_qualified_builtins(&node)?; + refuse_named_args_to_unnamed_builtins(&node)?; refuse_invisible_extension_functions(&node)?; if let Some(st) = fdw::plan(&node, lookup, params)? { return Ok(st); diff --git a/crates/secantus-pgplan/src/named_args.rs b/crates/secantus-pgplan/src/named_args.rs index c22f03498..155fdbb7f 100644 --- a/crates/secantus-pgplan/src/named_args.rs +++ b/crates/secantus-pgplan/src/named_args.rs @@ -49,6 +49,11 @@ fn named(a: &pg_query::protobuf::Node) -> Option<&pg_query::protobuf::NamedArgEx } } +/// Does some overload of this built-in name its arguments? +pub(crate) fn has_argument_names(name: &str) -> bool { + signatures().contains_key(name) +} + /// Is this a call to a built-in with a named argument? pub(crate) fn wanted(f: &pg_query::protobuf::FuncCall) -> bool { f.args.iter().any(|a| named(a).is_some()) diff --git a/tasks/backlog-rust-postgres.md b/tasks/backlog-rust-postgres.md index c552959c4..6537fae43 100644 --- a/tasks/backlog-rust-postgres.md +++ b/tasks/backlog-rust-postgres.md @@ -2886,15 +2886,6 @@ keeps; what follows is what that lookup does not yet check. Measured on PostgreSQL 15.19; none needs an object. -- [ ] **A named argument to a built-in whose parameters have no names is - refused as a gap.** `select nextval(regclass := 'nosuchseq')` and - `nextval(x => 'nosuchseq')` are 42883 `function nextval(regclass => - unknown) does not exist` (`x => unknown`) at 8 with the - no-function-matches HINT on PostgreSQL 15.19 (`proargnames` is NULL - for `nextval`, so no candidate takes a named argument); here they are - 0A000 `NamedArgExpr is not supported yet` with no position. - Verify: tests/backlog/test_rp_found_rp458.py::test_named_argument_to_a_builtin_without_argument_names - Backlog-ID: RP-677 ## Found while working RP-697 (2026-11-09) diff --git a/tests/backlog/test_rp_found_rp458.py b/tests/backlog/test_rp_found_rp458.py index 014d4ab61..d5adb5f94 100644 --- a/tests/backlog/test_rp_found_rp458.py +++ b/tests/backlog/test_rp_found_rp458.py @@ -116,9 +116,10 @@ def test_sequence_named_by_its_oid_advances(rust_pg: Any) -> None: } -@pytest.mark.backlog("RP-677") +@pytest.mark.backlog_fixed("RP-677") @pytest.mark.parametrize("query", list(NAMED_ARGUMENTS)) def test_named_argument_to_a_builtin_without_argument_names(query: str, rust_pg: Any) -> None: - """A named argument to `nextval` is 0A000 `NamedArgExpr is not - supported yet` here, with no position.""" + """A named argument to `nextval` was 0A000 `NamedArgExpr is not + supported yet` here, with no position; it is now 42883 naming the call + with the argument as `name => type`, as on PostgreSQL.""" assert answer(rust_pg, query) == NAMED_ARGUMENTS[query]