Re: SQL/JSON: functions
Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
On 7/14/20 9:47 PM, Nikita Glukhov wrote: > Attached 49th version of the patches with two new patches #5 and #6. > > On 15.07.2020 00:09, Andrew Dunstan wrote: >> On 7/14/20 1:00 PM, Andrew Dunstan wrote: >>> On 7/5/20 1:29 PM, Justin Pryzby wrote: >>>> On Mon, Mar 23, 2020 at 08:28:52PM +0300, Nikita Glukhov wrote: >>>>> Attached 47th version of the patches. >>>> The patch checker/cfbot says this doesn't apply ; could you send a rebasified >>>> version ? >>>> >>> To keep things moving, I've rebased these patches. However, 1) the docs >>> patches use <replaceble class="parameter"> in many cases where they >>> should now just use <parameter> > I haven't touched <replaceable class="parameter"> yet, because I'm not sure > if <replaceable> or <parameter> is correct here at all. Here's the relevant commit message that explains the policy: commit 47046763c3 Author: Tom Lane <tgl@sss.pgh.pa.us> Date: Mon May 4 13:48:30 2020 -0400 Doc: standardize markup a bit more. We had a mishmash of <replaceable>, <replaceable class="parameter">, and <parameter> markup for operator/function arguments. Use <parameter> consistently for things that are in fact names of parameters (including OUT parameters), reserving <replaceable> for things that aren't. The latter class includes some made-up-by-the-docs type class names, like "numeric_type", as well as placeholders for arguments that don't have well-defined types. Possibly we could do better with those categories as well, but for the moment I'm content not to have parameter names marked up in different ways in different places. > >> Turns out these patches also need to get the message on the new way of >> writing entries in func.sgml - I'll publish some updates on that in the >> next day or so so that "make doc" will succeed. > I can do it by myself, but I just need to understand what to fix and how. Here's an example that shows how it was transformed for json_agg: - <row> - <entry> - <indexterm> - <primary>json_agg</primary> - </indexterm> - <function>json_agg(<replaceable class="parameter">expression</replaceable>)</function> - </entry> - <entry> - <type>any</type> - </entry> - <entry> - <type>json</type> - </entry> - <entry>No</entry> - <entry>aggregates values, including nulls, as a JSON array</entry> - </row> + <row> + <entry role="func_table_entry"><para role="func_signature"> + <indexterm> + <primary>json_agg</primary> + </indexterm> + <function>json_agg</function> ( <type>anyelement</type> ) + <returnvalue>json</returnvalue> + </para> + <para role="func_signature"> + <indexterm> + <primary>jsonb_agg</primary> + </indexterm> + <function>jsonb_agg</function> ( <type>anyelement</type> ) + <returnvalue>jsonb</returnvalue> + </para> + <para> + Collects all the input values, including nulls, into a JSON array. + Values are converted to JSON as per <function>to_json</function> + or <function>to_jsonb</function>. + </para></entry> + <entry>No</entry> + </row> >>> and b) patch 4 fails when run under force_parallel=regress. > Fixed parallel-safety check for RETURNING clause of JSON_EXISTS(). > > On 05.04.2020 19:50, Alexander Korotkov wrote: > > 1) Uniqueness checks using JsonbUniqueCheckContext and > > JsonUniqueCheckContext have quadratic complexity over number of keys. > > That doesn't look good especially for jsonb, which anyway sorts object > > keys before object serialization. > > 2) We have two uniqueness checks for json type, which use > > JsonbUniqueCheckContext and JsonUniqueState. JsonUniqueState uses > > stack of hashes, while JsonbUniqueCheckContext have just plain array > > of keys. I think we can make JsonUniqueState use single hash, where > > object identifies would be part of hash key. And we should replace > > JsonbUniqueCheckContext with JsonUniqueState. That would eliminate > > extra entities and provide reasonable complexity for cases, which now > > use JsonbUniqueCheckContext. > > Unique checks were refactored as Alexander proposed. > > > 3) New SQL/JSON clauses don't use timezone and considered as immutable > > assuming all the children are immutable. Immutability is good, but > > ignoring timezone in all the cases is plain wrong. The first thing we > > can do is to use timezone and make SQL/JSON clauses stable. But that > > limits their usage in functional and partial indexes. I see couple of > > things we can do next (one of them or probably both). > > 3.1) Provide user a way so specify that we should ignore timezone in > > particular case (IGNORE TIMEZONE clause or something like that). Then > > SQL/JSON clause will be considered as immutable. > > 3.2) Automatically detect whether jsonpath might use timezone. If > > jsonpath doesn't use .datetime() method, it doesn't need timezone for > > sure. Also, from the datetime format specifiers we can get that we > > don't compare non-timezoned values to timezoned values. So, if we > > detect this jsonpath never uses timezone, we can consider SQL/JSON > > clause as immutable. > > Implemented second variant with automatic detection. > > I also tried to add explicit IGNORE TIMEZONE / IMMUTABLE clauses, but all of > them lead to shift/reduce conflicts that seem not easy to resolve. Not sure if this helps: https://wiki.postgresql.org/wiki/Debugging_the_PostgreSQL_grammar_(Bison) > > > > Patch #5 implements functions for new JSON type that is expected to appear in > the upcoming SQL/JSON standard: > > - JSON() is for constructing JSON typed values from JSON text. > It is almost equivalent to text::json[b] cast, except that it has additional > ability to specify WITH UNIQUE KEYS constraint. > > - JSON_SCALAR() is for constructing JSON typed values from SQL scalars. > It is equivalent to to_json[b](). > > - JSON_SERIALIZE() is for serializing JSON typed values to character strings. > It is almost equivalent to json[b]::character_type cast, but it also > supports output to bytea. > > Upcoming Oracle 20c will have JSON datatype and these functions [1], so we > decided also to implement them for compatibility, despite that they do not make > sense for real PG users. > Are these functions in the standard, or are they Oracle extensions? If the latter maybe they belong in an options extension. > > Patch #6 allows the user to use PG jsonb type as an effective implementation of > SQL JSON type. By explicitly setting GUC sql_json = jsonb, JSON will be mapped > to jsonb, and JSON TEXT (may be named named differently) will be mapped to json. > > This seems to be a hack, but we failed to propose something more simpler. What is going to be the effect of that on things like index expressions? This strikes me at first glance as something of a potential footgun, but maybe I'm being overcautious. cheers andrew -- Andrew Dunstan https://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
JSON_TABLE: Add support for NESTED paths and columns
- bb766cde63b4 17.0 landed
-
Add basic JSON_TABLE() functionality
- de3600452b61 17.0 landed
-
Add SQL/JSON query functions
- 6185c9737cf4 17.0 landed
-
Add soft error handling to some expression nodes
- aaaf9449ec6b 17.0 landed
-
Adjust populate_record_field() to handle errors softly
- 1edb3b491bee 17.0 landed
-
Refactor code used by jsonpath executor to fetch variables
- faa2b953ba3b 17.0 landed
-
Add more SQL/JSON constructor functions
- 03734a7fed7d 17.0 landed
-
SQL/JSON: support the IS JSON predicate
- 6ee30209a6f1 16.0 landed
-
SQL/JSON: add standard JSON constructor functions
- 7081ac46ace8 16.0 landed
-
Improve readability of SQL/JSON documentation.
- b46bcf7a4b6f 15.0 landed
-
Documentation for SQL/JSON features
- a6baa4baddd5 15.0 landed
-
RETURNING clause for JSON() and JSON_SCALAR()
- 49082c2cc3d8 15.0 landed
-
SQL JSON functions
- 606948b058dc 15.0 landed
-
SQL/JSON query functions
- 1a36bc9dba8e 15.0 landed
-
IS JSON predicate
- 33a377608fc2 15.0 landed
-
SQL/JSON constructors
- f4fb45d15c59 15.0 landed
-
Common SQL/JSON clauses
- f79b803dcc98 15.0 landed
- 865fe4d5df56 15.0 landed
-
Doc: remove obsolete entries in table of BRIN strategy numbers.
- 0ce4cd04da55 14.0 cited
-
Doc: standardize markup a bit more.
- 47046763c3ed 13.0 cited
-
Make pg_regress.c unset PGDATABASE during make installcheck.
- 5ee96b3e2221 13.0 cited
-
Fix handling of GENERATED columns in CREATE TABLE LIKE INCLUDING DEFAULTS.
- b81a9c2fc525 13.0 cited