Re: Row pattern recognition - Mailing list pgsql-hackers

From Tatsuo Ishii
Subject Re: Row pattern recognition
Date
Msg-id 20260227.113733.1986003944279104973.ishii@postgresql.org
Whole thread Raw
In response to Re: Row pattern recognition  (Henson Choi <assam258@gmail.com>)
Responses Re: Row pattern recognition
List pgsql-hackers
Hi Henson,

> Hi Tatsuo,
> 
> Here are ten incremental patches on top of v43.

Thanks.

BTW, I noticed that RPR accepts table name qualified variables in
DEFINE clause.

SELECT company, tdate, price, first_value(price) OVER w, last_value(price) OVER w,
 nth_value(tdate, 2) OVER w AS nth_second
 FROM stock
 WINDOW w AS (
 PARTITION BY company
 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
 INITIAL
 PATTERN (START UP+ DOWN+)
 DEFINE
  START AS TRUE,
  UP AS stock.price > PREV(price),    <-- table name "stock" is uded
  DOWN AS price < PREV(price)
);

The standard does not allow to use range variables, declared in the
FROM clause, in the DEFINE clause (19075-5 6.5). Attached patch raises
an error in this case.

psql:a.sql:13: ERROR:  range var qualified name "stock.price" is not allowed in DEFINE clause
LINE 11:   UP AS stock.price > PREV(price),
                 ^

Also, currently we do not support pattern variable range vars in the
DEFINE caluse (e.g. UP.price). If used, we see a confusing error
message:

ERROR: missing FROM-clause entry for table "UP"
LINE 13: UP AS UP.price > PREV(price),
               ^

The attached patch errors out differently. I believe this is an
enhancement.

psql:a.sql:13: ERROR:  range var qualified name "up.price" is not allowed in DEFINE clause
LINE 11:   UP AS UP.price > PREV(price),
                 ^

Thoughts?
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp
diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index 219681d6e88..f92fa793c10 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -613,6 +613,17 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
             return node;
     }
 
+    /*
+     * DEFINE clause in RPR does not allow to use table names.
+     */
+    if (pstate->p_expr_kind == EXPR_KIND_RPR_DEFINE &&
+        list_length(cref->fields) != 1)
+        ereport(ERROR,
+                (errcode(ERRCODE_SYNTAX_ERROR),
+                 errmsg("range var qualified name \"%s\" is not allowed in DEFINE clause",
+                        NameListToString(cref->fields)),
+                 parser_errposition(pstate, cref->location)));
+
     /*----------
      * The allowed syntaxes are:
      *

pgsql-hackers by date:

Previous
From: wenhui qiu
Date:
Subject: Re: Convert ALL SubLinks to ANY SubLinks
Next
From: Fujii Masao
Date:
Subject: Re: [Patch]Add tab completion for DELETE ... USING