SQL/JSON: Fix some oversights in commit b6e1157e7
authorAmit Langote <[email protected]>
Sun, 20 Oct 2024 03:21:03 +0000 (12:21 +0900)
committerAmit Langote <[email protected]>
Sun, 20 Oct 2024 03:21:03 +0000 (12:21 +0900)
commitfa4f11854c8b56e094c12c7c25e23d9d456cd5cb
tree393f07f9e9895031040a84a8fa2950b271315686
parent0f4045814fd2e7200dfdf39b568140d8867c18d8
SQL/JSON: Fix some oversights in commit b6e1157e7

The decision in b6e1157e7 to ignore raw_expr when evaluating a
JsonValueExpr was incorrect.  While its value is not ultimately
used (since formatted_expr's value is), failing to initialize it
can lead to problems, for instance,  when the expression tree in
raw_expr contains Aggref nodes, which must be initialized to
ensure the parent Agg node works correctly.

Also, optimize eval_const_expressions_mutator()'s handling of
JsonValueExpr a bit.  Currently, when formatted_expr cannot be folded
into a constant, we end up processing it twice -- once directly in
eval_const_expressions_mutator() and again recursively via
ece_generic_processing().  This recursive processing is required to
handle raw_expr. To avoid the redundant processing of formatted_expr,
we now  process raw_expr directly in eval_const_expressions_mutator().

Finally, update the comment of JsonValueExpr to describe the roles of
raw_expr and formatted_expr more clearly.

Bug: #18657
Reported-by: Alexander Lakhin <[email protected]>
Diagnosed-by: Fabio R. Sluzala <[email protected]>
Diagnosed-by: Tender Wang <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/18657-1b90ccce2b16bdb8@postgresql.org
Backpatch-through: 16
src/backend/executor/execExpr.c
src/backend/optimizer/util/clauses.c
src/include/nodes/primnodes.h
src/test/regress/expected/sqljson.out
src/test/regress/sql/sqljson.sql