extend JSON_TABLE top level path expression

Started by jian heabout 1 year ago4 messageshackers

Thread notes

1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
hackorum-bot
2 days ago

@global #feature #moderate #enhancement

#feature#moderate#enhancement
Jump to latest
#1jian he
jian.universality@gmail.com

hi.

now you can
SELECT * FROM JSON_TABLE(jsonb '{"a": 123}', '$' || '.' || 'a' COLUMNS
(foo int PATH '$'));

previously, top level path expression (in the above example ('$' || '.' || 'a'))
is hard coded as String A_Const. see gram.y:

if (!IsA($5, A_Const) ||
castNode(A_Const, $5)->val.node.type != T_String)
ereport(ERROR,
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("only string constants are supported in JSON_TABLE
path specification"),
parser_errposition(@5));

with the attached patch, this restriction is removed.
but JSON_TABLE COLUMN path expression is still required as String constant.
(maybe we can do the same but that requires grammar changes).

In transformJsonTable:
``tf->docexpr = transformExpr(pstate, (Node *) jfe, EXPR_KIND_FROM_FUNCTION);``

already did the parse analytics job for top level path expression, the same way
as JSON_QUERY.
we just need to take care of initializing and evaluating the top level path
expression.

--
jian
https://www.enterprisedb.com/

Attachments:

v1-0001-extend-JSON_TABLE-top-level-path-expression.patchtext/x-patch; charset=US-ASCII; name=v1-0001-extend-JSON_TABLE-top-level-path-expression.patchDownload+227-90
#2jian he
jian.universality@gmail.com
In reply to: jian he (#1)
Re: extend JSON_TABLE top level path expression

hi.

https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-SQLJSON-TABLE
JSON_TABLE (
context_item, path_expression [ AS json_path_name ] [ PASSING {
value AS varname } [, ...] ]

I quickly realized I didn't have tests that path_expression is not String
A_Const and PASSING clause is not NULL. so I changed existing tests
to cover this
scenario.

The rest of the patch remains the same as v1.

--
jian
https://www.enterprisedb.com/

Attachments:

t52766_2
v2-0001-extend-JSON_TABLE-top-level-path-expression.patchtext/x-patch; charset=US-ASCII; name=v2-0001-extend-JSON_TABLE-top-level-path-expression.patchDownload+239-98
#3solai v
solai.cdac@gmail.com
In reply to: jian he (#2)
Re: extend JSON_TABLE top level path expression

Hi Jian,

I reviewed and tested patch v2 .
Before applying the patch JSON_TABLE accepted only literal top-level
patch string such as '$.a' and rejected equivalent path expression
such as '$'||.'a' with:
ERROR :only string constants are supported in JSON_TABLE path specification
After applying the patch ,JSON_TABLE correctly accepts path
expressions and returns the expected results.
I verified cases using string concatenation (e.g. '$' || '.a' and '$'
|| '.' || 'a'),,while existing behavior with literal path strings
remained unchanged.
I also confirmed that JSON_QUERY already accepts such patch
expressions,so the patch makes JSON_TABLE behavior more consistent
with other SQL/JSON functions.
The patch applied successfully and tested queries behaved as expected.
Thanks for working on this improvement.

Regards
Solai

#4Thom Brown
thom@linux.com
In reply to: solai v (#3)
Re: extend JSON_TABLE top level path expression

On Thu, 11 Jun 2026 at 11:32, solai v <solai.cdac@gmail.com> wrote:

Hi Jian,

I reviewed and tested patch v2 .
Before applying the patch JSON_TABLE accepted only literal top-level
patch string such as '$.a' and rejected equivalent path expression
such as '$'||.'a' with:
ERROR :only string constants are supported in JSON_TABLE path specification
After applying the patch ,JSON_TABLE correctly accepts path
expressions and returns the expected results.
I verified cases using string concatenation (e.g. '$' || '.a' and '$'
|| '.' || 'a'),,while existing behavior with literal path strings
remained unchanged.
I also confirmed that JSON_QUERY already accepts such patch
expressions,so the patch makes JSON_TABLE behavior more consistent
with other SQL/JSON functions.
The patch applied successfully and tested queries behaved as expected.
Thanks for working on this improvement.

Hi Jian,

This needs a rebase.

Your code changes:

appendStringInfoString(buf, ", ");

- get_const_expr(root->path->value, context, -1);
+ get_rule_expr(jexpr->path_spec, context, showimplicit);

appendStringInfo(buf, " AS %s", quote_identifier(root->path->name));

But I can see elsewhere in ruleutils.c, this already exists:

get_rule_expr(jexpr->formatted_expr, context, showimplicit);

appendStringInfoString(buf, ", ");

get_json_path_spec(jexpr->path_spec, context, showimplicit);

So should you be using get_json_path_spec() in this case?

Also, should there be any documentation changes with this?

And given the rules have changed for TableFunc.rowexpr and
JsonTablePathScan.path->value, does this need a catversion bump?

Thom