Add support for label conjunction (&) in SQL/PGQ
Thread notes
1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
@global #feature #moderate #enhancement
Hello everyone,
I'm proposing a patch that adds support for label conjunction in
SQL/PGQ, i'll quote from the commit message since its already a
semi-complete description:
The SQL/PGQ standard allows label expressions to use boolean operators,
such as conjunction of labels: MATCH (a IS label1 & label2). Previously,
only
disjunction (|) was supported in graph element patterns.
Supporting label conjunction, disjunction and in the future ; label
negation, requires an evaluation of a full label expression. Previously
get_path_elements_for_path_factor() considered only flat BoolExpr
evaluation (as it only knew about disjunctions).
As required, this commit adds a recursive label expression evaluator,
get_path_elements_for_path_factor() now fetches all candidate table
elements for the path factor kind once and evaluates each table
element's label OIDs against the label expression tree.
I'm not sure if the current approach of getting all elements and then
iteratively pulling their corresponding labels from catalog ; which
might seem costly (if doing many `table_open`s every time is a lot and
pg_propgraph_element is large enough), we can change it to keep
`pg_propgraph_element_label` open until all element OIDs are treated.
This also allows to support label conjunction for a path factor
appearing with different label expressions in multiple path elements. I
think this is also required for correct support of multi path patterns
later ; example: MATCH (a IS Person)-[]-(), (a IS Gamer) requires path
factor `a` label expressions to conjunct.
We add a new error "ERROR: label expression for variable "a" is not
satisfied in property graph "g1"" when a path factor's label expression
can't be satisfied for any element table possible.
Thoughts ?
Regards,
Ayoub
Hi Ayoub,
Thanks for your interest in SQL/PGQ.
On Wed, Jul 22, 2026 at 3:02 PM <ayoub.kazar@data-bene.io> wrote:
Hello everyone,
I'm proposing a patch that adds support for label conjunction in
SQL/PGQ, i'll quote from the commit message since its already a
semi-complete description:The SQL/PGQ standard allows label expressions to use boolean operators,
such as conjunction of labels: MATCH (a IS label1 & label2). Previously,
only
disjunction (|) was supported in graph element patterns.
As your patch does, the label conjunction requires changes to the grammar.
We need to be careful there. But we can support conjunction without
requiring a change to the grammar - by allowing multiple element patterns
with non-empty label expression to share the same variable name. I would
start with that. You have mentioned that your patch supports it. So, let's
create two patches - one without grammar and other with grammar. The first
one by itself has higher chances of getting committed. Oracle also does not
seem to support explicit label disjunction.
I would also question whether label conjunction is a feature required by
the field just so that we prioritize the features by field demand.
Supporting label conjunction, disjunction and in the future ; label
negation, requires an evaluation of a full label expression. Previously
get_path_elements_for_path_factor() considered only flat BoolExpr
evaluation (as it only knew about disjunctions).As required, this commit adds a recursive label expression evaluator,
get_path_elements_for_path_factor() now fetches all candidate table
elements for the path factor kind once and evaluates each table
element's label OIDs against the label expression tree.I'm not sure if the current approach of getting all elements and then
iteratively pulling their corresponding labels from catalog ; which
might seem costly (if doing many `table_open`s every time is a lot and
pg_propgraph_element is large enough), we can change it to keep
`pg_propgraph_element_label` open until all element OIDs are treated.
I think the way label are resolved into elements works only for
disjunction, conjunction and negation require some refactoring. I would
suggest taking patches from [1]/messages/by-id/CAExHW5uNRS9tcgnHCc03rh5oQhFPz-wrC1YF+_XUhPdwK2fbXw@mail.gmail.com, rebasing all properties supporting patch
from [2]/messages/by-id/CAExHW5tYCE9QyCvVraKUeesKW5RTR+mrzsg3u64qSps-RPJR5A@mail.gmail.com on top of those patches. Implement implicit label conjunction on
top of these patches. I am hoping that patches from [1]/messages/by-id/CAExHW5uNRS9tcgnHCc03rh5oQhFPz-wrC1YF+_XUhPdwK2fbXw@mail.gmail.com would get committed
to PG 19 - hence those are separate patches. For PG 20, we may decide to
distribute the code slightly differently across the patches.
[1]: /messages/by-id/CAExHW5uNRS9tcgnHCc03rh5oQhFPz-wrC1YF+_XUhPdwK2fbXw@mail.gmail.com
/messages/by-id/CAExHW5uNRS9tcgnHCc03rh5oQhFPz-wrC1YF+_XUhPdwK2fbXw@mail.gmail.com
[2]: /messages/by-id/CAExHW5tYCE9QyCvVraKUeesKW5RTR+mrzsg3u64qSps-RPJR5A@mail.gmail.com
/messages/by-id/CAExHW5tYCE9QyCvVraKUeesKW5RTR+mrzsg3u64qSps-RPJR5A@mail.gmail.com
--
Best Wishes,
Ashutosh Bapat
Hello Ashutosh,
Thanks for the review.
On 30/07/2026 09:20, Ashutosh Bapat wrote:
Hi Ayoub,
Thanks for your interest in SQL/PGQ.
On Wed, Jul 22, 2026 at 3:02 PM <ayoub.kazar@data-bene.io> wrote:
Hello everyone,
I'm proposing a patch that adds support for label conjunction in
SQL/PGQ, i'll quote from the commit message since its already a
semi-complete description:The SQL/PGQ standard allows label expressions to use boolean
operators,
such as conjunction of labels: MATCH (a IS label1 & label2).
Previously,
only
disjunction (|) was supported in graph element patterns.As your patch does, the label conjunction requires changes to the
grammar. We need to be careful there. But we can support conjunction
without requiring a change to the grammar - by allowing multiple
element patterns with non-empty label expression to share the same
variable name. I would start with that. You have mentioned that your
patch supports it. So, let's create two patches - one without grammar
and other with grammar. The first one by itself has higher chances of
getting committed. Oracle also does not seem to support explicit
label disjunction.I would also question whether label conjunction is a feature required
by the field just so that we prioritize the features by field demand.Supporting label conjunction, disjunction and in the future ; label
negation, requires an evaluation of a full label expression.
Previously
get_path_elements_for_path_factor() considered only flat BoolExpr
evaluation (as it only knew about disjunctions).As required, this commit adds a recursive label expression evaluator,
get_path_elements_for_path_factor() now fetches all candidate table
elements for the path factor kind once and evaluates each table
element's label OIDs against the label expression tree.I'm not sure if the current approach of getting all elements and then
iteratively pulling their corresponding labels from catalog ; which
might seem costly (if doing many `table_open`s every time is a lot
and
pg_propgraph_element is large enough), we can change it to keep
`pg_propgraph_element_label` open until all element OIDs are treated.I think the way label are resolved into elements works only for
disjunction, conjunction and negation require some refactoring. I
would suggest taking patches from [1], rebasing all properties
supporting patch from [2] on top of those patches. Implement implicit
label conjunction on top of these patches. I am hoping that patches
from [1] would get committed to PG 19 - hence those are separate
patches. For PG 20, we may decide to distribute the code
slightly differently across the patches.[1]
/messages/by-id/CAExHW5uNRS9tcgnHCc03rh5oQhFPz-wrC1YF+_XUhPdwK2fbXw@mail.gmail.com
[2]
/messages/by-id/CAExHW5tYCE9QyCvVraKUeesKW5RTR+mrzsg3u64qSps-RPJR5A@mail.gmail.com--
Best Wishes,
Ashutosh Bapat
I managed to rebase your previous patches that you referenced, then on
top of them i added the implicit label conjunction, the explicit case
can be left for later since its requires just grammar change (having
both at the same time is very good for expressiveness).
I noticed a small issue in your
v20260318-0001-Support-all-properties-reference-in-COLUMN.patch
- BoolExpr *be = castNode(BoolExpr, labelexpr);
+ BoolExpr *be = castNode(BoolExpr, pf->labelexpr);
this would break the recursion since its always getting the top level
expression i guess ?
Attached are all 3 rebased patches on top of each other as you suggested
+ the label conjunction patch on top of them.
Regards,
Ayoub Kazar
Attachments:
t253153_3v1-0004-Add-support-for-label-conjunction-in-SQL-PGQ.patchtext/x-patch; charset=UTF-8; name=v1-0004-Add-support-for-label-conjunction-in-SQL-PGQ.patchDownload+83-34
v1-0001-Empty-label-expression-in-view-definition.patchtext/x-patch; charset=UTF-8; name=v1-0001-Empty-label-expression-in-view-definition.patchDownload+178-64
v1-0003-Support-all-properties-reference-in-COLUMNs-list-of-a-GraphTableRef.patchtext/x-patch; charset=UTF-8; name=v1-0003-Support-all-properties-reference-in-COLUMNs-list-of-a-GraphTableRef.patchDownload+571-347
v1-0002-View-referencing-labels-shared-by-vertex-and-edge-tables.patchtext/x-patch; charset=UTF-8; name=v1-0002-View-referencing-labels-shared-by-vertex-and-edge-tables.patchDownload+33-1
Hi Ayoub,
SQL/PGQ was reverted from master in b1f106c80cb on 2026-09-07, so
GRAPH_TABLE is no longer in the tree and this patch has nothing left
to apply against.
I'm setting the CommitFest entry (#7047) to "Waiting on Author" to
reflect that. Could you let us know how you'd like to handle it --
keep it open in PG20-2 and rebase if and when a reworked SQL/PGQ is
re-proposed, or move the entry to a later CommitFest?
Thanks,
Shihao