pgsql: Improve our ability to regurgitate SQL-syntax function calls.

Поиск
Список
Период
Сортировка
От Tom Lane
Тема pgsql: Improve our ability to regurgitate SQL-syntax function calls.
Дата
Msg-id E1kaMga-0004aH-7I@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Improve our ability to regurgitate SQL-syntax function calls.

The SQL spec calls out nonstandard syntax for certain function calls,
for example substring() with numeric position info is supposed to be
spelled "SUBSTRING(string FROM start FOR count)".  We accept many
of these things, but up to now would not print them in the same format,
instead simplifying down to "substring"(string, start, count).
That's long annoyed me because it creates an interoperability
problem: we're gratuitously injecting Postgres-specific syntax into
what might otherwise be a perfectly spec-compliant view definition.
However, the real reason for addressing it right now is to support
a planned change in the semantics of EXTRACT() a/k/a date_part().
When we switch that to returning numeric, we'll have the parser
translate EXTRACT() to some new function name (might as well be
"extract" if you ask me) and then teach ruleutils.c to reverse-list
that per SQL spec.  In this way existing calls to date_part() will
continue to have the old semantics.

To implement this, invent a new CoercionForm value COERCE_SQL_SYNTAX,
and make the parser insert that rather than COERCE_EXPLICIT_CALL when
the input has SQL-spec decoration.  (But if the input has the form of
a plain function call, continue to mark it COERCE_EXPLICIT_CALL, even
if it's calling one of these functions.)  Then ruleutils.c recognizes
COERCE_SQL_SYNTAX as a cue to emit SQL call syntax.  It can know
which decoration to emit using hard-wired knowledge about the
functions that could be called this way.  (While this solution isn't
extensible without manual additions, neither is the grammar, so this
doesn't seem unmaintainable.)  Notice that this solution will
reverse-list a function call with SQL decoration only if it was
entered that way; so dump-and-reload will not by itself produce any
changes in the appearance of views.

This requires adding a CoercionForm field to struct FuncCall.
(I couldn't resist the temptation to rearrange that struct's
field order a tad while I was at it.)  FuncCall doesn't appear
in stored rules, so that change isn't a reason for a catversion
bump, but I did one anyway because the new enum value for
CoercionForm fields could confuse old backend code.

Possible future work:

* Perhaps CoercionForm should now be renamed to DisplayForm,
or something like that, to reflect its more general meaning.
This'd require touching a couple hundred places, so it's not
clear it's worth the code churn.

* The SQLValueFunction node type, which was invented partly for
the same goal of improving SQL-compatibility of view output,
could perhaps be replaced with regular function calls marked
with COERCE_SQL_SYNTAX.  It's unclear if this would be a net
code savings, however.

Discussion: https://postgr.es/m/42b73d2d-da12-ba9f-570a-420e0cce19d9@phystech.edu

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/40c24bfef92530bd846e111c1742c2a54441c62c

Modified Files
--------------
src/backend/nodes/copyfuncs.c                    |   3 +-
src/backend/nodes/equalfuncs.c                   |   3 +-
src/backend/nodes/makefuncs.c                    |   5 +-
src/backend/nodes/outfuncs.c                     |   3 +-
src/backend/parser/gram.y                        | 194 ++++++++++++++-----
src/backend/parser/parse_clause.c                |   4 +-
src/backend/parser/parse_func.c                  |   6 +-
src/backend/parser/parse_utilcmd.c               |   1 +
src/backend/utils/adt/ruleutils.c                | 232 ++++++++++++++++++++++-
src/include/catalog/catversion.h                 |   2 +-
src/include/nodes/makefuncs.h                    |   3 +-
src/include/nodes/parsenodes.h                   |   5 +-
src/include/nodes/primnodes.h                    |   8 +-
src/test/modules/test_rls_hooks/test_rls_hooks.c |  10 +-
src/test/regress/expected/create_view.out        |  44 ++++-
src/test/regress/expected/timestamptz.out        |   6 +-
src/test/regress/sql/create_view.sql             |  22 +++
17 files changed, 480 insertions(+), 71 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: pgsql: Remove useless entries for aggregate functions from fmgrtab.c.
Следующее
От: Tom Lane
Дата:
Сообщение: pgsql: Declare lead() and lag() using anycompatible not anyelement.