LCOV - code coverage report
Current view: top level - src/backend/parser - parse_graphtable.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 89.0 % 100 89
Test Date: 2026-03-23 09:15:54 Functions: 100.0 % 7 7
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * parse_graphtable.c
       4              :  *    parsing of GRAPH_TABLE
       5              :  *
       6              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7              :  * Portions Copyright (c) 1994, Regents of the University of California
       8              :  *
       9              :  *
      10              :  * IDENTIFICATION
      11              :  *    src/backend/parser/parse_graphtable.c
      12              :  *
      13              :  *-------------------------------------------------------------------------
      14              :  */
      15              : 
      16              : #include "postgres.h"
      17              : 
      18              : #include "access/genam.h"
      19              : #include "access/htup_details.h"
      20              : #include "access/table.h"
      21              : #include "catalog/pg_propgraph_label.h"
      22              : #include "catalog/pg_propgraph_property.h"
      23              : #include "miscadmin.h"
      24              : #include "nodes/makefuncs.h"
      25              : #include "parser/parse_collate.h"
      26              : #include "parser/parse_expr.h"
      27              : #include "parser/parse_graphtable.h"
      28              : #include "parser/parse_node.h"
      29              : #include "utils/fmgroids.h"
      30              : #include "utils/lsyscache.h"
      31              : #include "utils/relcache.h"
      32              : #include "utils/syscache.h"
      33              : 
      34              : 
      35              : /*
      36              :  * Return human-readable name of the type of graph element pattern in
      37              :  * GRAPH_TABLE clause, usually for error message purpose.
      38              :  */
      39              : static const char *
      40            4 : get_gep_kind_name(GraphElementPatternKind gepkind)
      41              : {
      42            4 :     switch (gepkind)
      43              :     {
      44            0 :         case VERTEX_PATTERN:
      45            0 :             return "vertex";
      46            0 :         case EDGE_PATTERN_LEFT:
      47            0 :             return "edge pointing left";
      48            0 :         case EDGE_PATTERN_RIGHT:
      49            0 :             return "edge pointing right";
      50            0 :         case EDGE_PATTERN_ANY:
      51            0 :             return "edge pointing any direction";
      52            4 :         case PAREN_EXPR:
      53            4 :             return "nested path pattern";
      54              :     }
      55              : 
      56              :     /*
      57              :      * When a GraphElementPattern is constructed by the parser, it will set a
      58              :      * value from the GraphElementPatternKind enum. But we may get here if the
      59              :      * GraphElementPatternKind value stored in a catalog is corrupted.
      60              :      */
      61            0 :     return "unknown";
      62              : }
      63              : 
      64              : /*
      65              :  * Transform a property reference.
      66              :  *
      67              :  * A property reference is parsed as a ColumnRef of the form:
      68              :  * <variable>.<property>. If <variable> is one of the variables bound to an
      69              :  * element pattern in the graph pattern and <property> can be resolved as a
      70              :  * property of the property graph, then we return a GraphPropertyRef node
      71              :  * representing the property reference. If the <variable> exists in the graph
      72              :  * pattern but <property> does not exist in the property graph, we raise an
      73              :  * error. However, if <variable> does not exist in the graph pattern, we return
      74              :  * NULL to let the caller handle it as some other kind of ColumnRef. The
      75              :  * variables bound to the element patterns in the graph pattern are expected to
      76              :  * be collected in the GraphTableParseState.
      77              :  */
      78              : Node *
      79        24429 : transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref)
      80              : {
      81        24429 :     GraphTableParseState *gpstate = pstate->p_graph_table_pstate;
      82              : 
      83        24429 :     if (!gpstate)
      84        22796 :         return NULL;
      85              : 
      86         1633 :     if (list_length(cref->fields) == 2)
      87              :     {
      88         1620 :         Node       *field1 = linitial(cref->fields);
      89         1620 :         Node       *field2 = lsecond(cref->fields);
      90              :         char       *elvarname;
      91              :         char       *propname;
      92              : 
      93         1620 :         if (IsA(field1, A_Star) || IsA(field2, A_Star))
      94              :         {
      95            8 :             if (pstate->p_expr_kind == EXPR_KIND_SELECT_TARGET)
      96            4 :                 ereport(ERROR,
      97              :                         errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
      98              :                         errmsg("\"*\" is not supported here"),
      99              :                         parser_errposition(pstate, cref->location));
     100              :             else
     101            4 :                 ereport(ERROR,
     102              :                         errcode(ERRCODE_SYNTAX_ERROR),
     103              :                         errmsg("\"*\" not allowed here"),
     104              :                         parser_errposition(pstate, cref->location));
     105              :         }
     106              : 
     107         1612 :         elvarname = strVal(field1);
     108         1612 :         propname = strVal(field2);
     109              : 
     110         1612 :         if (list_member(gpstate->variables, field1))
     111              :         {
     112         1608 :             GraphPropertyRef *gpr = makeNode(GraphPropertyRef);
     113              :             HeapTuple   pgptup;
     114              :             Form_pg_propgraph_property pgpform;
     115              : 
     116         1608 :             pgptup = SearchSysCache2(PROPGRAPHPROPNAME, ObjectIdGetDatum(gpstate->graphid), CStringGetDatum(propname));
     117         1608 :             if (!HeapTupleIsValid(pgptup))
     118            4 :                 ereport(ERROR,
     119              :                         errcode(ERRCODE_SYNTAX_ERROR),
     120              :                         errmsg("property \"%s\" does not exist", propname));
     121         1604 :             pgpform = (Form_pg_propgraph_property) GETSTRUCT(pgptup);
     122              : 
     123         1604 :             gpr->location = cref->location;
     124         1604 :             gpr->elvarname = elvarname;
     125         1604 :             gpr->propid = pgpform->oid;
     126         1604 :             gpr->typeId = pgpform->pgptypid;
     127         1604 :             gpr->typmod = pgpform->pgptypmod;
     128         1604 :             gpr->collation = pgpform->pgpcollation;
     129              : 
     130         1604 :             ReleaseSysCache(pgptup);
     131              : 
     132         1604 :             return (Node *) gpr;
     133              :         }
     134              :     }
     135              : 
     136           17 :     return NULL;
     137              : }
     138              : 
     139              : /*
     140              :  * Transform a label expression.
     141              :  *
     142              :  * A label expression is parsed as either a ColumnRef with a single field or a
     143              :  * label expression like label disjunction. The single field in the ColumnRef is
     144              :  * treated as a label name and transformed to a GraphLabelRef node. The label
     145              :  * expression is recursively transformed into an expression tree containg
     146              :  * GraphLabelRef nodes corresponding to the names of the labels appearing in the
     147              :  * expression. If any label name cannot be resolved to a label in the property
     148              :  * graph, an error is raised.
     149              :  */
     150              : static Node *
     151         1706 : transformLabelExpr(GraphTableParseState *gpstate, Node *labelexpr)
     152              : {
     153              :     Node       *result;
     154              : 
     155         1706 :     if (labelexpr == NULL)
     156          629 :         return NULL;
     157              : 
     158         1077 :     check_stack_depth();
     159              : 
     160         1077 :     switch (nodeTag(labelexpr))
     161              :     {
     162         1025 :         case T_ColumnRef:
     163              :             {
     164         1025 :                 ColumnRef  *cref = (ColumnRef *) labelexpr;
     165              :                 const char *labelname;
     166              :                 Oid         labelid;
     167              :                 GraphLabelRef *lref;
     168              : 
     169              :                 Assert(list_length(cref->fields) == 1);
     170         1025 :                 labelname = strVal(linitial(cref->fields));
     171              : 
     172         1025 :                 labelid = GetSysCacheOid2(PROPGRAPHLABELNAME, Anum_pg_propgraph_label_oid, ObjectIdGetDatum(gpstate->graphid), CStringGetDatum(labelname));
     173         1025 :                 if (!labelid)
     174            4 :                     ereport(ERROR,
     175              :                             errcode(ERRCODE_UNDEFINED_OBJECT),
     176              :                             errmsg("label \"%s\" does not exist in property graph \"%s\"", labelname, get_rel_name(gpstate->graphid)));
     177              : 
     178         1021 :                 lref = makeNode(GraphLabelRef);
     179         1021 :                 lref->labelid = labelid;
     180         1021 :                 lref->location = cref->location;
     181              : 
     182         1021 :                 result = (Node *) lref;
     183         1021 :                 break;
     184              :             }
     185              : 
     186           52 :         case T_BoolExpr:
     187              :             {
     188           52 :                 BoolExpr   *be = (BoolExpr *) labelexpr;
     189              :                 ListCell   *lc;
     190           52 :                 List       *args = NIL;
     191              : 
     192          156 :                 foreach(lc, be->args)
     193              :                 {
     194          108 :                     Node       *arg = (Node *) lfirst(lc);
     195              : 
     196          108 :                     arg = transformLabelExpr(gpstate, arg);
     197          104 :                     args = lappend(args, arg);
     198              :                 }
     199              : 
     200           48 :                 result = (Node *) makeBoolExpr(be->boolop, args, be->location);
     201           48 :                 break;
     202              :             }
     203              : 
     204            0 :         default:
     205              :             /* should not reach here */
     206            0 :             elog(ERROR, "unsupported label expression node: %d", (int) nodeTag(labelexpr));
     207              :             result = NULL;      /* keep compiler quiet */
     208              :             break;
     209              :     }
     210              : 
     211         1069 :     return result;
     212              : }
     213              : 
     214              : /*
     215              :  * Transform a GraphElementPattern.
     216              :  *
     217              :  * Transform the label expression and the where clause in the element pattern
     218              :  * given by GraphElementPattern. The variable name in the GraphElementPattern is
     219              :  * added to the list of variables in the GraphTableParseState which is used to
     220              :  * resolve property references in this element pattern or elsewhere in the
     221              :  * GRAPH_TABLE.
     222              :  */
     223              : static Node *
     224         1606 : transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep)
     225              : {
     226         1606 :     GraphTableParseState *gpstate = pstate->p_graph_table_pstate;
     227              : 
     228         1606 :     if (gep->kind != VERTEX_PATTERN && !IS_EDGE_PATTERN(gep->kind))
     229            4 :         ereport(ERROR,
     230              :                 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     231              :                 errmsg("unsupported element pattern kind: \"%s\"", get_gep_kind_name(gep->kind)));
     232              : 
     233         1602 :     if (gep->quantifier)
     234            4 :         ereport(ERROR,
     235              :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     236              :                  errmsg("element pattern quantifier is not supported")));
     237              : 
     238         1598 :     if (gep->variable)
     239         1315 :         gpstate->variables = lappend(gpstate->variables, makeString(pstrdup(gep->variable)));
     240              : 
     241         1598 :     gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr);
     242              : 
     243         1594 :     gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE);
     244         1590 :     assign_expr_collations(pstate, gep->whereClause);
     245              : 
     246         1590 :     return (Node *) gep;
     247              : }
     248              : 
     249              : /*
     250              :  * Transform a path term (list of GraphElementPattern's).
     251              :  */
     252              : static Node *
     253          524 : transformPathTerm(ParseState *pstate, List *path_term)
     254              : {
     255          524 :     List       *result = NIL;
     256              : 
     257         2622 :     foreach_node(GraphElementPattern, gep, path_term)
     258         1590 :         result = lappend(result,
     259         1606 :                          transformGraphElementPattern(pstate, gep));
     260              : 
     261          508 :     return (Node *) result;
     262              : }
     263              : 
     264              : /*
     265              :  * Transform a path pattern list (list of path terms).
     266              :  */
     267              : static Node *
     268          528 : transformPathPatternList(ParseState *pstate, List *path_pattern)
     269              : {
     270          528 :     List       *result = NIL;
     271              : 
     272              :     /* Grammar doesn't allow empty path pattern list */
     273              :     Assert(list_length(path_pattern) > 0);
     274              : 
     275              :     /*
     276              :      * We do not support multiple path patterns in one GRAPH_TABLE clause
     277              :      * right now. But we may do so in future.
     278              :      */
     279          528 :     if (list_length(path_pattern) != 1)
     280            4 :         ereport(ERROR,
     281              :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     282              :                  errmsg("multiple path patterns in one GRAPH_TABLE clause not supported")));
     283              : 
     284         1540 :     foreach_node(List, path_term, path_pattern)
     285          524 :         result = lappend(result, transformPathTerm(pstate, path_term));
     286              : 
     287          508 :     return (Node *) result;
     288              : }
     289              : 
     290              : /*
     291              :  * Transform a GraphPattern.
     292              :  *
     293              :  * A GraphPattern consists of a list of one or more path patterns and an
     294              :  * optional where clause. Transform them. We use the previously constructure
     295              :  * list of variables in the GraphTableParseState to resolve property references
     296              :  * in the WHERE clause.
     297              :  */
     298              : Node *
     299          528 : transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern)
     300              : {
     301          528 :     List       *path_pattern_list = castNode(List,
     302              :                                              transformPathPatternList(pstate, graph_pattern->path_pattern_list));
     303              : 
     304          508 :     graph_pattern->path_pattern_list = path_pattern_list;
     305          508 :     graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE);
     306          508 :     assign_expr_collations(pstate, graph_pattern->whereClause);
     307              : 
     308          508 :     return (Node *) graph_pattern;
     309              : }
        

Generated by: LCOV version 2.0-1