pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
GRAPH_TABLE pattern WHERE clause is not coerced to boolean
6+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-09-02 1c8c790ec1 792094a5ce  Coerce GRAPH_TABLE pattern WHERE clauses to boolean
* GRAPH_TABLE pattern WHERE clause is not coerced to boolean
@ 2026-08-31 07:32  Ewan Young <kdbase.hack@gmail.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Ewan Young @ 2026-08-31 07:32 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; +Cc: Peter Eisentraut <peter@eisentraut.org>; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

Hi

The WHERE clauses inside a GRAPH_TABLE pattern -- both the element-level
one (MATCH (c IS customers WHERE ...)) and the graph-pattern-level one
(MATCH ... WHERE ...) -- are transformed with a bare transformExpr()
and never go through coerce_to_boolean().  So a WHERE clause of any
type is accepted, and its raw datum is used as the qual:

    create table customers (id int primary key, name text);
    insert into customers values (1,'alice'),(2,null),(3,'carol');
    create property graph g
      vertex tables (customers key (id) label customer properties (id, name));

    select * from graph_table (g match (c is customer where c.name)
                               columns (c.id));
     id
    ----
      1
      3
    (2 rows)

EXPLAIN shows "Filter: name".  The never-null text pointer is always
taken as true, so the condition silently degenerates to roughly
"name IS NOT NULL": the NULL-name row disappears with no error.
Numeric quals are evaluated by bit pattern ("WHERE 1" is true,
"WHERE 0" is false), and even "WHERE row(1,2)" is accepted.  The same
clause outside GRAPH_TABLE gives the usual

    ERROR:  argument of WHERE must be type boolean, not type text

The attached patch routes both sites through transformWhereClause(),
like every other WHERE clause, and adds regression tests for the
element-level and pattern-level cases.  make check passes.  The code is
the same in REL_19_STABLE, so v19 is affected as well.

-- 
Regards,
Ewan Young

Attachments:

v1-0001-Coerce-GRAPH_TABLE-pattern-WHERE-clauses-to-boolean.patch (application/octet-stream, 4.9K) download From 4cd6a54ef711cccd1f7d7bfb6e32d3ae1aa85411 Mon Sep 17 00:00:00 2001 From: Ewan Young <kdbase.hack@gmail.com> Date: Mon, 31 Aug 2026 22:53:03 +0800 Subject: [PATCH] Coerce GRAPH_TABLE pattern WHERE clauses to boolean transformGraphElementPattern() and transformGraphPattern() ran their WHERE clauses through transformExpr() without ever applying coerce_to_boolean(), so a WHERE clause of any type was accepted and its bare datum was used as the qual. For example, MATCH (c IS customers WHERE c.name) is accepted and EXPLAIN shows "Filter: name"; the never-null text pointer is always taken as true, so the condition silently degenerates to roughly "name IS NOT NULL" instead of raising the usual "argument of WHERE must be type boolean" error. Wrong results, no diagnostic. Route both sites through transformWhereClause(), like every other WHERE clause, and add regression tests for the element-level and pattern-level cases. --- src/backend/parser/parse_graphtable.c | 7 +++++-- src/test/regress/expected/graph_table.out | 4 ++++ src/test/regress/sql/graph_table.sql | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c index 73fbfb541f7..b44f7ccd0a7 100644 --- a/src/backend/parser/parse_graphtable.c +++ b/src/backend/parser/parse_graphtable.c @@ -22,6 +22,7 @@ #include "catalog/pg_propgraph_property.h" #include "miscadmin.h" #include "nodes/makefuncs.h" +#include "parser/parse_clause.h" #include "parser/parse_collate.h" #include "parser/parse_expr.h" #include "parser/parse_graphtable.h" @@ -251,7 +252,8 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep) gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr); - gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE); + gep->whereClause = transformWhereClause(pstate, gep->whereClause, + EXPR_KIND_WHERE, "WHERE"); /* * Assign collations here for the reason mentioned in the prologue of @@ -387,7 +389,8 @@ transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern) transformPathPatternList(pstate, graph_pattern->path_pattern_list)); graph_pattern->path_pattern_list = path_pattern_list; - graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE); + graph_pattern->whereClause = transformWhereClause(pstate, graph_pattern->whereClause, + EXPR_KIND_WHERE, "WHERE"); assign_expr_collations(pstate, graph_pattern->whereClause); return (Node *) graph_pattern; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index 2e862a82ba0..dd051aa4d1c 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -101,6 +101,10 @@ LINE 1: SELECT * FROM GRAPH_TABLE (myshop MATCH COLUMNS (1 AS col)); ^ SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers)->{1,2}(o IS orders) COLUMNS (c.name AS customer_name)); -- error ERROR: element pattern quantifier is not supported +SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.name) COLUMNS (c.name AS customer_name)); -- error, WHERE must yield boolean +ERROR: argument of WHERE must be type boolean, not type character varying +SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE c.customer_id COLUMNS (c.name AS customer_name)); -- error, WHERE must yield boolean +ERROR: argument of WHERE must be type boolean, not type integer SELECT * FROM GRAPH_TABLE (myshop MATCH ((c IS customers)->(o IS orders)) COLUMNS (c.name)); ERROR: unsupported element pattern kind: "nested path pattern" LINE 1: SELECT * FROM GRAPH_TABLE (myshop MATCH ((c IS customers)->(... diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 21e70015f6e..67ea6d2f0d2 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -92,6 +92,8 @@ SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.addr SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers), (o IS orders) COLUMNS (c.name AS customer_name)); -- error SELECT * FROM GRAPH_TABLE (myshop MATCH COLUMNS (1 AS col)); -- error, empty match clause SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers)->{1,2}(o IS orders) COLUMNS (c.name AS customer_name)); -- error +SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.name) COLUMNS (c.name AS customer_name)); -- error, WHERE must yield boolean +SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE c.customer_id COLUMNS (c.name AS customer_name)); -- error, WHERE must yield boolean SELECT * FROM GRAPH_TABLE (myshop MATCH ((c IS customers)->(o IS orders)) COLUMNS (c.name)); -- a property graph can be referenced only from within GRAPH_TABLE clause. -- 2.47.3
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: GRAPH_TABLE pattern WHERE clause is not coerced to boolean
@ 2026-08-31 08:38  Chao Li <li.evan.chao@gmail.com>
  parent: Ewan Young <kdbase.hack@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Chao Li @ 2026-08-31 08:38 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Peter Eisentraut <peter@eisentraut.org>; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>



> On Aug 31, 2026, at 15:32, Ewan Young <kdbase.hack@gmail.com> wrote:
> 
> Hi
> 
> The WHERE clauses inside a GRAPH_TABLE pattern -- both the element-level
> one (MATCH (c IS customers WHERE ...)) and the graph-pattern-level one
> (MATCH ... WHERE ...) -- are transformed with a bare transformExpr()
> and never go through coerce_to_boolean().  So a WHERE clause of any
> type is accepted, and its raw datum is used as the qual:
> 
>    create table customers (id int primary key, name text);
>    insert into customers values (1,'alice'),(2,null),(3,'carol');
>    create property graph g
>      vertex tables (customers key (id) label customer properties (id, name));
> 
>    select * from graph_table (g match (c is customer where c.name)
>                               columns (c.id));
>     id
>    ----
>      1
>      3
>    (2 rows)
> 
> EXPLAIN shows "Filter: name".  The never-null text pointer is always
> taken as true, so the condition silently degenerates to roughly
> "name IS NOT NULL": the NULL-name row disappears with no error.
> Numeric quals are evaluated by bit pattern ("WHERE 1" is true,
> "WHERE 0" is false), and even "WHERE row(1,2)" is accepted.  The same
> clause outside GRAPH_TABLE gives the usual
> 
>    ERROR:  argument of WHERE must be type boolean, not type text
> 
> The attached patch routes both sites through transformWhereClause(),
> like every other WHERE clause, and adds regression tests for the
> element-level and pattern-level cases.  make check passes.  The code is
> the same in REL_19_STABLE, so v19 is affected as well.
> 
> -- 
> Regards,
> Ewan Young
> <v1-0001-Coerce-GRAPH_TABLE-pattern-WHERE-clauses-to-boolean.patch>

The patch looks good to me. As this is a v19 bug, it might be worth noting in the Open Items list.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/










^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: GRAPH_TABLE pattern WHERE clause is not coerced to boolean
@ 2026-08-31 10:08  Ewan Young <kdbase.hack@gmail.com>
  parent: Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Ewan Young @ 2026-08-31 10:08 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Peter Eisentraut <peter@eisentraut.org>; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

Thanks for the review!

On Mon, Aug 31, 2026 at 4:38 PM Chao Li <li.evan.chao@gmail.com> wrote:
>
>
>
> > On Aug 31, 2026, at 15:32, Ewan Young <kdbase.hack@gmail.com> wrote:
> >
> > Hi
> >
> > The WHERE clauses inside a GRAPH_TABLE pattern -- both the element-level
> > one (MATCH (c IS customers WHERE ...)) and the graph-pattern-level one
> > (MATCH ... WHERE ...) -- are transformed with a bare transformExpr()
> > and never go through coerce_to_boolean().  So a WHERE clause of any
> > type is accepted, and its raw datum is used as the qual:
> >
> >    create table customers (id int primary key, name text);
> >    insert into customers values (1,'alice'),(2,null),(3,'carol');
> >    create property graph g
> >      vertex tables (customers key (id) label customer properties (id, name));
> >
> >    select * from graph_table (g match (c is customer where c.name)
> >                               columns (c.id));
> >     id
> >    ----
> >      1
> >      3
> >    (2 rows)
> >
> > EXPLAIN shows "Filter: name".  The never-null text pointer is always
> > taken as true, so the condition silently degenerates to roughly
> > "name IS NOT NULL": the NULL-name row disappears with no error.
> > Numeric quals are evaluated by bit pattern ("WHERE 1" is true,
> > "WHERE 0" is false), and even "WHERE row(1,2)" is accepted.  The same
> > clause outside GRAPH_TABLE gives the usual
> >
> >    ERROR:  argument of WHERE must be type boolean, not type text
> >
> > The attached patch routes both sites through transformWhereClause(),
> > like every other WHERE clause, and adds regression tests for the
> > element-level and pattern-level cases.  make check passes.  The code is
> > the same in REL_19_STABLE, so v19 is affected as well.
> >
> > --
> > Regards,
> > Ewan Young
> > <v1-0001-Coerce-GRAPH_TABLE-pattern-WHERE-clauses-to-boolean.patch>
>
> The patch looks good to me. As this is a v19 bug, it might be worth noting in the Open Items list.

Agreed -- I don't have wiki edit access though; could someone add it
under "Open Issues"?

>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
>
>
>


-- 
Regards,
Ewan Young






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: GRAPH_TABLE pattern WHERE clause is not coerced to boolean
@ 2026-08-31 11:26  Chao Li <li.evan.chao@gmail.com>
  parent: Ewan Young <kdbase.hack@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Chao Li @ 2026-08-31 11:26 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Peter Eisentraut <peter@eisentraut.org>; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>



> On Aug 31, 2026, at 18:08, Ewan Young <kdbase.hack@gmail.com> wrote:
> 
> Thanks for the review!
> 
> On Mon, Aug 31, 2026 at 4:38 PM Chao Li <li.evan.chao@gmail.com> wrote:
>> 
>> 
>> 
>>> On Aug 31, 2026, at 15:32, Ewan Young <kdbase.hack@gmail.com> wrote:
>>> 
>>> Hi
>>> 
>>> The WHERE clauses inside a GRAPH_TABLE pattern -- both the element-level
>>> one (MATCH (c IS customers WHERE ...)) and the graph-pattern-level one
>>> (MATCH ... WHERE ...) -- are transformed with a bare transformExpr()
>>> and never go through coerce_to_boolean().  So a WHERE clause of any
>>> type is accepted, and its raw datum is used as the qual:
>>> 
>>>   create table customers (id int primary key, name text);
>>>   insert into customers values (1,'alice'),(2,null),(3,'carol');
>>>   create property graph g
>>>     vertex tables (customers key (id) label customer properties (id, name));
>>> 
>>>   select * from graph_table (g match (c is customer where c.name)
>>>                              columns (c.id));
>>>    id
>>>   ----
>>>     1
>>>     3
>>>   (2 rows)
>>> 
>>> EXPLAIN shows "Filter: name".  The never-null text pointer is always
>>> taken as true, so the condition silently degenerates to roughly
>>> "name IS NOT NULL": the NULL-name row disappears with no error.
>>> Numeric quals are evaluated by bit pattern ("WHERE 1" is true,
>>> "WHERE 0" is false), and even "WHERE row(1,2)" is accepted.  The same
>>> clause outside GRAPH_TABLE gives the usual
>>> 
>>>   ERROR:  argument of WHERE must be type boolean, not type text
>>> 
>>> The attached patch routes both sites through transformWhereClause(),
>>> like every other WHERE clause, and adds regression tests for the
>>> element-level and pattern-level cases.  make check passes.  The code is
>>> the same in REL_19_STABLE, so v19 is affected as well.
>>> 
>>> --
>>> Regards,
>>> Ewan Young
>>> <v1-0001-Coerce-GRAPH_TABLE-pattern-WHERE-clauses-to-boolean.patch>
>> 
>> The patch looks good to me. As this is a v19 bug, it might be worth noting in the Open Items list.
> 
> Agreed -- I don't have wiki edit access though; could someone add it
> under "Open Issues"?
> 

I just helped add the item.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/










^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: GRAPH_TABLE pattern WHERE clause is not coerced to boolean
@ 2026-09-01 02:34  Ewan Young <kdbase.hack@gmail.com>
  parent: Chao Li <li.evan.chao@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Ewan Young @ 2026-09-01 02:34 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Peter Eisentraut <peter@eisentraut.org>; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

Thanks for adding it!

On Mon, Aug 31, 2026 at 7:27 PM Chao Li <li.evan.chao@gmail.com> wrote:
>
>
>
> > On Aug 31, 2026, at 18:08, Ewan Young <kdbase.hack@gmail.com> wrote:
> >
> > Thanks for the review!
> >
> > On Mon, Aug 31, 2026 at 4:38 PM Chao Li <li.evan.chao@gmail.com> wrote:
> >>
> >>
> >>
> >>> On Aug 31, 2026, at 15:32, Ewan Young <kdbase.hack@gmail.com> wrote:
> >>>
> >>> Hi
> >>>
> >>> The WHERE clauses inside a GRAPH_TABLE pattern -- both the element-level
> >>> one (MATCH (c IS customers WHERE ...)) and the graph-pattern-level one
> >>> (MATCH ... WHERE ...) -- are transformed with a bare transformExpr()
> >>> and never go through coerce_to_boolean().  So a WHERE clause of any
> >>> type is accepted, and its raw datum is used as the qual:
> >>>
> >>>   create table customers (id int primary key, name text);
> >>>   insert into customers values (1,'alice'),(2,null),(3,'carol');
> >>>   create property graph g
> >>>     vertex tables (customers key (id) label customer properties (id, name));
> >>>
> >>>   select * from graph_table (g match (c is customer where c.name)
> >>>                              columns (c.id));
> >>>    id
> >>>   ----
> >>>     1
> >>>     3
> >>>   (2 rows)
> >>>
> >>> EXPLAIN shows "Filter: name".  The never-null text pointer is always
> >>> taken as true, so the condition silently degenerates to roughly
> >>> "name IS NOT NULL": the NULL-name row disappears with no error.
> >>> Numeric quals are evaluated by bit pattern ("WHERE 1" is true,
> >>> "WHERE 0" is false), and even "WHERE row(1,2)" is accepted.  The same
> >>> clause outside GRAPH_TABLE gives the usual
> >>>
> >>>   ERROR:  argument of WHERE must be type boolean, not type text
> >>>
> >>> The attached patch routes both sites through transformWhereClause(),
> >>> like every other WHERE clause, and adds regression tests for the
> >>> element-level and pattern-level cases.  make check passes.  The code is
> >>> the same in REL_19_STABLE, so v19 is affected as well.
> >>>
> >>> --
> >>> Regards,
> >>> Ewan Young
> >>> <v1-0001-Coerce-GRAPH_TABLE-pattern-WHERE-clauses-to-boolean.patch>
> >>
> >> The patch looks good to me. As this is a v19 bug, it might be worth noting in the Open Items list.
> >
> > Agreed -- I don't have wiki edit access though; could someone add it
> > under "Open Issues"?
> >
>
> I just helped add the item.
>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
>
>
>


-- 
Regards,
Ewan Young






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: GRAPH_TABLE pattern WHERE clause is not coerced to boolean
@ 2026-09-02 19:15  Peter Eisentraut <peter@eisentraut.org>
  parent: Ewan Young <kdbase.hack@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Peter Eisentraut @ 2026-09-02 19:15 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; +Cc: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

On 31.08.26 09:32, Ewan Young wrote:
> Hi
> 
> The WHERE clauses inside a GRAPH_TABLE pattern -- both the element-level
> one (MATCH (c IS customers WHERE ...)) and the graph-pattern-level one
> (MATCH ... WHERE ...) -- are transformed with a bare transformExpr()
> and never go through coerce_to_boolean().  So a WHERE clause of any
> type is accepted, and its raw datum is used as the qual:
> 
>      create table customers (id int primary key, name text);
>      insert into customers values (1,'alice'),(2,null),(3,'carol');
>      create property graph g
>        vertex tables (customers key (id) label customer properties (id, name));
> 
>      select * from graph_table (g match (c is customer where c.name)
>                                 columns (c.id));
>       id
>      ----
>        1
>        3
>      (2 rows)
> 
> EXPLAIN shows "Filter: name".  The never-null text pointer is always
> taken as true, so the condition silently degenerates to roughly
> "name IS NOT NULL": the NULL-name row disappears with no error.
> Numeric quals are evaluated by bit pattern ("WHERE 1" is true,
> "WHERE 0" is false), and even "WHERE row(1,2)" is accepted.  The same
> clause outside GRAPH_TABLE gives the usual
> 
>      ERROR:  argument of WHERE must be type boolean, not type text
> 
> The attached patch routes both sites through transformWhereClause(),
> like every other WHERE clause, and adds regression tests for the
> element-level and pattern-level cases.  make check passes.  The code is
> the same in REL_19_STABLE, so v19 is affected as well.

Fixed, thanks.







^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-09-02 19:15 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-31 07:32 GRAPH_TABLE pattern WHERE clause is not coerced to boolean Ewan Young <kdbase.hack@gmail.com>
2026-08-31 08:38 ` Chao Li <li.evan.chao@gmail.com>
2026-08-31 10:08   ` Ewan Young <kdbase.hack@gmail.com>
2026-08-31 11:26     ` Chao Li <li.evan.chao@gmail.com>
2026-09-01 02:34       ` Ewan Young <kdbase.hack@gmail.com>
2026-09-02 19:15 ` Peter Eisentraut <peter@eisentraut.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox