Re: jsonpath

From: Nikita Glukhov <n(dot)gluhov(at)postgrespro(dot)ru>
To: Alexander Korotkov <a(dot)korotkov(at)postgrespro(dot)ru>
Cc: Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, Stas Kelvich <s(dot)kelvich(at)postgrespro(dot)ru>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com>, David Steele <david(at)pgmasters(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Andrew Dunstan <andrew(dot)dunstan(at)2ndquadrant(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Oleg Bartunov <obartunov(at)gmail(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Subject: Re: jsonpath
Date: 2019-01-21 13:56:46
Message-ID: a37b47a8-d853-b9ba-9b61-2e87ecc783b2@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 20.01.2019 2:45, Alexander Korotkov wrote:

> 3) How do we calculate the "id" property returned by keyvalue()
> function? It's not documented. Even presence of "id" columns isn't
> documented. Standard stands that it's implementation-depended
> indetifier of object holding key-value pair. The way of its
> calculation is also not clear from the code. Why do we need constant
> of 10000000000?
>
> id = jb->type != jbvBinary ? 0 :
> (int64)((char *) jb->val.binary.data -
> (char *) cxt->baseObject.jbc);
> id += (int64) cxt->baseObject.id * INT64CONST(10000000000);

I decided to construct object id from the two parts: base object id and its
binary offset in its base object's jsonb:

object_id = 10000000000 * base_object_id + object_offset_in_base_object

10000000000 (10^10) -- is a first round decimal number greater than 2^32
(maximal offset in jsonb). Decimal multiplier is used here to improve the
readability of identifiers.

Base object is usually a root object of the path: context item '$' or path
variable '$var', literals can't produce objects for now. But if the path
contains generated objects (.keyvalue() itself, for example), then they become
base object for the subsequent .keyvalue(). See example:

'$.a.b.keyvalue().value.keyvalue()' :
- base for the first .keyvalue() is '$'
- base for the second .keyvalue() is '$.a.b.keyvalue()'

Id of '$' is 0.
Id of '$var' is its ordinal (positive) number in the list of variables.
Ids for generated objects are assigned using global counter
'JsonPathExecContext.generatedObjectId' starting from 'number_of_vars + 1'.

Corresponding comments will be added in the upcoming version of the patches.

--
Nikita Glukhov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

In response to

  • Re: jsonpath at 2019-01-19 23:45:10 from Alexander Korotkov

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2019-01-21 14:12:22 Re: House style for DocBook documentation?
Previous Message Andreas Karlsson 2019-01-21 13:40:34 Re: Early WIP/PoC for inlining CTEs