[PATCH] Fix quotation logic for unreserved keywords in window specifications - Mailing list pgsql-hackers

From Kwangwon Seo
Subject [PATCH] Fix quotation logic for unreserved keywords in window specifications
Date
Msg-id CAHJxwBWx_v=aWp7ZrGRFw2r_7MJdxYX2um3ZOmxTg4c_tL1qLA@mail.gmail.com
Whole thread
Responses Re: [PATCH] Fix quotation logic for unreserved keywords in window specifications
List pgsql-hackers
Hi hackers,

I was testing views with pg_dump/pg_restore and found a case where an unreserved keyword is not quoted correctly.

example:

-- Just a base table
CREATE TABLE t (v int);
INSERT INTO t VALUES (1), (2);

-- Create a view
CREATE VIEW v AS
SELECT count(*) OVER w2 FROM t
WINDOW "rows" AS (PARTITION BY v), w2 AS ("rows" ORDER BY v);

But the problem is, here, that quotation is gone.

-- Problem: Quotation is disappared
SELECT pg_get_viewdef('v');
                       pg_get_viewdef                        
-------------------------------------------------------------
  SELECT count(*) OVER w2 AS count                          +
    FROM t                                                  +
   WINDOW rows AS (PARTITION BY v), w2 AS (rows ORDER BY v);
(1 row)

-- If you use this for recovering the view above, You'll face this error.
 SELECT count(*) OVER w2 AS count
        FROM t
        WINDOW rows AS (PARTITION BY v), w2 AS (rows ORDER BY v);
ERROR:  syntax error at or near "ORDER"
LINE 3:  WINDOW rows AS (PARTITION BY v), w2 AS (rows ORDER BY v);

This breaks pg_dump/restore: the view is lost.
As a result, pg_dump/pg_restore cannot restore the view successfully.



Fortunately, I found this comment in the gram.y:

It says:

 * If we see PARTITION, RANGE, ROWS or GROUPS as the first token after the '('
 * of a window_specification, we want the assumption to be that there is
 * no existing_window_name; but those keywords are unreserved and so could
 * be ColIds.

I think that assumption can be broken when inheritance is used across multiple window definitions.
I mean this: w2 AS ("rows" ORDER BY v)


Some comments for patch file:

The attached patch quotes the window name when it is a keyword in get_rule_windowspec().
Output for ordinary window names is unchanged. The window definition itself is a ColId so no change is needed there; only the refname is quoted.

Maybe this is a known thing on the team's side, but I think, at least, should not break 
compatibility with current tools. That is the motivation of this patch...

I'd appreciate any feedback or suggestions...!!


Best regards...
Attachment

pgsql-hackers by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: [PATCH] Fix null pointer dereference in PG19
Next
From: Amit Kapila
Date:
Subject: Re: PSQL - prevent describe listing tables that are already in listed schemas