Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE

Поиск
Список
Период
Сортировка
Искать

Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE

От:
Sami Imseih <samimseih@gmail.com>
Дата:
Вложения:
Hi,

Commit f585671055d1 [1] disallowed aggregates, window functions, and SRFs in a
GRAPH_TABLE COLUMNS list. For the aggregate case it tests pstate->p_hasAggs
after transforming the columns. That is not enough. An aggregate that
references an outer query is attributed to a parent query level, so
check_agglevels_and_constraints sets p_hasAggs on that parent ParseState and
not on the GRAPH_TABLE's own, so the aggregate is not caught.

There are two places this shows up.

1. An outer-referencing aggregate in the COLUMNS list.

```
  postgres=# CREATE TABLE customers (customer_id int PRIMARY KEY, name text);
  CREATE TABLE
  postgres=# CREATE PROPERTY GRAPH myshop VERTEX TABLES (customers);
  CREATE PROPERTY GRAPH
  postgres=# SELECT (SELECT num
  postgres(#         FROM GRAPH_TABLE (myshop MATCH (c IS customers)
  postgres(#                           COLUMNS (count(o.customer_id) AS num)) t)
  postgres-# FROM customers o;
  ERROR:  Aggref found in non-Agg plan node
```

2. The graph pattern WHERE clause was not checked at all in f585671055d1. A
same-level aggregate there is already rejected with "aggregate functions are
not allowed in WHERE", but an outer-referencing one is attributed to a parent
level is not caught, failing instead with "Aggref found in non-Agg plan node".

```
  postgres=# -- same-level aggregate
  postgres=# SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers
  postgres(#                            WHERE count(c.customer_id) > 0)
  postgres(#                            COLUMNS (c.name AS nm));
  ERROR:  aggregate functions are not allowed in WHERE

  postgres=# -- outer-referencing aggregate
  postgres=# SELECT (SELECT nm
  postgres(#         FROM GRAPH_TABLE (myshop MATCH (c IS customers
  postgres(#                           WHERE count(o.customer_id) > 0)
  postgres(#                           COLUMNS (c.name AS nm)) t)
  postgres-# FROM customers o;
  ERROR:  Aggref found in non-Agg plan node
```

Unlike the repro in f585671055d1, neither case results in an assertion
failure. But surfacing an internal planner error from user SQL is wrong on its
own.

The attached patch closes both gaps by walking the transformed COLUMNS list and
the graph pattern for Aggref and GroupingFunc nodes. Unlike p_hasAggs, the walk
detects an aggregate by its presence in those the trees. Window functions
and SRFs only mark the local ParseState, so their checks introduced in
f585671055d1 remain in place.

Thoughts?

[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=f585671055d1c56d6fba0bc5835d28e68248ccfe

--
Sami Imseih
Amazon Web Services (AWS)

Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE

От:
Sami Imseih <samimseih@gmail.com>
Дата:
Вложения:
> I find that argument pretty shaky.

You are right. I was wrong to assume a GraphPropertyRef can only ever refer
to its own GRAPH_TABLE's level. Written inside a sub-select it does not.

> If I can write
>     MATCH WHERE c.customer_id > 0
> why can't I write
>     MATCH WHERE (SELECT c.customer_id > 0)
> ?  I see that that in fact doesn't work, but that seems like a bug
> in itself.

Disallowing subqueries in GRAPH_TABLE is the current intent of the
feature. The code
in upstream today only checks p_hasSubLinks at the end of
transformRangeGraphTable, after the WHERE and COLUMNS list is transformed, so
it only catches subqueries that transform cleanly, i.e. "(SELECT 1)" gets the
intended error, but "(SELECT c.customer_id > 0)" fails with "missing FROM-clause
entry"

The attached v6 does two things. First, it rejects a SubLink under the
GRAPH_TABLE
expr kinds in transformSubLink, the moment it is seen and before the
sub-select body
is analyzed. So MATCH WHERE (SELECT c.customer_id > 0) now reports

```
ERROR:  subqueries within GRAPH_TABLE reference are not supported
```

instead of

```
ERROR:  missing FROM-clause entry for table "c"
```

Also, because GRAPH_TABLE now rejects subqueries, a property reference
can
never sit inside one, so it is always at its own GRAPH_TABLE's level. That lets
check_agg_arguments_walker treat a GraphPropertyRef as a Var with
varlevelsup 0,
which it otherwise cannot see since a GraphPropertyRef is not a Var.
The walker still
adjusts for the level it is found at, the same as for a Var.

This corrects the aggregate case I showed earlier. The patch includes
regression tests
for it.

> More generally, what this suggests to me is that we have a ton of
> other bugs-of-omission in places that process Vars and don't know that
> a GraphPropertyRef acts like a Var.

Agreed, and v6 is an example of this. It teaches check_agg_arguments_walker
that a GraphPropertyRef behaves like a Var.

Besides other potential bugs, It also limits the feature in the future.
Keeping subqueries out of GRAPH_TABLE is part of what makes some
of the bugs discovered fixable with the current GraphPropertyRef, but
I suspect it will be a real limitation if we try to relax these restrictions.

> So what I'm thinking right now is
> that this is a fundamental design error, and that we should nuke
> GraphPropertyRef altogether in favor of using a Var that references
> the appropriate column of the RTE_GRAPH_TABLE relation.

I spent time today on this and I think the direction is right. In what
I have running
locally, a property reference is emitted as a plain Var over the
RTE_GRAPH_TABLE relation,
so the Var handles leveling and the rest for free. GraphPropertyRef
does not go away
entirely, though. It moves onto a side list on the RTE that the
rewriter uses to resolve
each property Var back to its graph property.

The design has some open questions, but I will share if this is the direction
to go.

--
Sami Imseih
Amazon Web Services (AWS)

Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE

От:
Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Дата:
Вложения:
Hi Sami,

Thanks for working on this. I agree that the changes in
transformRangeGraphTable() to reject aggregates, window functions, and
set-returning functions in GRAPH_TABLE were not sufficient. This patch
fixes it properly.

The patch has two parts
1. Rework the parser to reject aggregates, window functions, and
set-returning functions in GRAPH_TABLE COLUMNS list and graph pattern
WHERE clause using ParseExprKind. It correctly reverts the changes in
transformRangeGraphTable() which tried to do the same but did not
cover all the cases. It adds new ParseExprKind values for GRAPH_TABLE
COLUMNS and GRAPH_TABLE WHERE. I see that they are added to all the
necessary places. We need two separate ParseExprKind values to report
appropriate "all properties reference" error when it appears in
GRAPH_TABLE COLUMNS list versus in the WHERE clause. Otherwise the two
values are used in the same way to reject aggregates, window
functions, and set-returning functions.

2. Treat GraphPropertyRef as a Var and handle all Var-like nodes in
replace_property_refs(). I will cover this below.

On Tue, Aug 18, 2026 at 4:59 AM Sami Imseih  wrote:
>
> Besides other potential bugs, It also limits the feature in the future.
> Keeping subqueries out of GRAPH_TABLE is part of what makes some
> of the bugs discovered fixable with the current GraphPropertyRef, but
> I suspect it will be a real limitation if we try to relax these restrictions.
>

This is a temporary restriction. Sooner or later we will support
subqueries nested under GraphPropertyRef.

> > So what I'm thinking right now is
> > that this is a fundamental design error, and that we should nuke
> > GraphPropertyRef altogether in favor of using a Var that references
> > the appropriate column of the RTE_GRAPH_TABLE relation.
>
> I spent time today on this and I think the direction is right. In what
> I have running
> locally, a property reference is emitted as a plain Var over the
> RTE_GRAPH_TABLE relation,
> so the Var handles leveling and the rest for free. GraphPropertyRef
> does not go away
> entirely, though. It moves onto a side list on the RTE that the
> rewriter uses to resolve
> each property Var back to its graph property.
>

I share the concern raised by Tom about bugs due to missing
GraphPropertyRef handling. However, I don't think we can use Var
instead of GraphPropertyRef. It represents a reference to a property
of all the graph elements that are bound to an element pattern in a
GRAPH_TABLE. The element pattern is not represented by a range table
and Var node does not have a field to represent an element pattern
variable. Further the same GraphPropertyRef may be rewritten as
different expressions depending on the element table it gets
associated with when generating a subquery for a given path. Achieving
this with Var will be quite complex, if not impossible. The question
is whether we can find all the places where we need to treat
GraphPropertyRef as a Var. Given that the GraphPropertyRefs are
generated during transformation and vanish after rewrite, the places
where we need to handle them are limited, as noted by Tom already.
Many of those are already covered by the patch. Going through all the
places, rather painfully, where we use levelsup for Var-like nodes,
looking at var.c, I see that most of that code is applicable to
planning, optimization and execution. The additional places which I
think we need to handle are:
a. locate_var_of_level_walker() to handle GraphPropertyRef so that the
correct error location can be reported. We have separate
locate_aggref_of_level_walker(). So first I thought that it would be
appropriate to add a separate
locate_graphpropertyref_of_level_walker() function. But we want to
report an error from a place where the distinction between Var and
GraphPropertyRef is lost. So a separate function doesn't make sense.
b. contain_vars_of_level() should not see GraphPropertyRef since the
latter lives within GRAPH_TABLE only. But the function is used during
query transformation, so it will be good to add a GraphPropertyRef
case and Assert()/throw error if it is seen. We should add a comment
there to expect GraphPropertyRef there once we start supporting nested
subqueries in GRAPH_TABLE.
c. IncrementVarSublevelsUp() will need to handle GraphPropertyRef once
we support nested subqueries in GRAPH_TABLE. For now we may want to
add a GraphPropertyRef case and Assert()/throw error if it is seen. We
should add a comment there to expect GraphPropertyRef there once we
start supporting nested subqueries in GRAPH_TABLE. Please note, I
don't expect ChangeVarNodes to handle GraphPropertyRef since there is
no rtindex in GraphPropertyRef.

I didn't find any other place where we need to handle
GraphPropertyRef. But there are just many places where we use Vars and
varlevelsup. So there is a non-zero possibility that I may have missed
a few. I wish we could have differentiated between Var in the planner,
optimizer code and ColumnRef in the parser, transformer and rewriter
code. That would have made it easier to find the places where we need
to handle GraphPropertyRef. But the ship has sailed long back.

Here are some comments on v6 patches.

@@ -769,9 +775,17 @@ check_agg_arguments_walker(Node *node,
 {
  if (node == NULL)
  return false;
- if (IsA(node, Var))
+ if (IsA(node, Var) || IsA(node, GraphPropertyRef))
  {

I would rather add a separate GraphPropertyRef case instead of
combining it with Var. When we start supporting nested subqueries in
GRAPH_TABLE, we will need to add levelsup to GraphPropertyRef and
handle it appropriately. I am leaning towards doing that now and
setting it to 0. Then use here instead of hardcoded 0. Also pass it on
to the Vars in the expression that replaces the GraphPropertyRef by
calling ChangeVarNodes() with sublevelsup = GraphPropertyRef::levelsup
instead of 0. Once subquery support is added, we will set
GraphPropertyRef::levelsup to the appropriate value and the
ChangeVarNodes() will propagate it to the Vars in the replacement
expression. If we go about it this way, we should add an Assert in
replace_property_refs() to make sure that GraphPropertyRef::levelsup
== 0. But I am also fine with implicitly treating GraphPropertyRef as
level 0 with a comment that we should pass on the levelsup to the Vars
in the replacement expression once we support nested subqueries in
GRAPH_TABLE.

@@ -1845,6 +1847,10 @@ transformSubLink(ParseState *pstate, SubLink *sublink)
  case EXPR_KIND_CYCLE_MARK:
  /* okay */
  break;
+ case EXPR_KIND_GRAPH_TABLE_COLUMNS:
+ case EXPR_KIND_GRAPH_TABLE_WHERE:
+ err = _("subqueries within GRAPH_TABLE reference are not supported");

I would use "cannot use subquery in GRAPH_TABLE reference" to be
consistent with the other error messages in this function.

@@ -1039,6 +1039,40 @@ replace_property_refs_mutator(Node *node,
struct replace_property_refs_context *

  return (Node *) newvar;
  }
+ else if (IsA(node, Aggref))
+ {
+ Aggref   *aggref;
+
+ /* Copy the Aggref node and mutate its sub-structure */
+ aggref = (Aggref *) expression_tree_mutator(node,
+ replace_property_refs_mutator,
+ context);
+
+ /*
+ * An aggregate is allowed in a graph table expression, but only if
+ * it's an outer aggregate.  Since it will be in a subquery after the
+ * rewrite, we have to increase the level by one.
+ */
+ Assert(aggref->agglevelsup > 0);
+ aggref->agglevelsup++;
+
+ return (Node *) aggref;
+ }
+ else if (IsA(node, GroupingFunc))
+ {
+ GroupingFunc *grp;
+
+ /* Copy the GroupingFunc node and mutate its sub-structure */
+ grp = (GroupingFunc *) expression_tree_mutator(node,
+   replace_property_refs_mutator,
+   context);
+
+ /* Like Aggref, this should be an outer-level reference */
+ Assert(grp->agglevelsup > 0);
+ grp->agglevelsup++;
+
+ return (Node *) grp;
+ }

I think we could make this function lean by letting
IncrementVarSublevelsUp() do the levelsup adjustment before the
GraphPropertyRef is replaced. It will require traversing the
expression tree twice, but it will eliminate the possibility of
replace_property_refs_mutator() missing a node type that has levelsup.

@@ -84,6 +84,8 @@ typedef enum ParseExprKind
  EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */
  EXPR_KIND_CYCLE_MARK, /* cycle mark value */
  EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */
+ EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */
+ EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE pattern */

Nit. I would just write WHERE in a GRAPH_TABLE since GRAPH_TABLE is
not a pattern, but a table function.

-- 
Best Wishes,
Ashutosh Bapat


FAQ