Add SQL/JSON ON MISMATCH clause to JSON_VALUE

Started by Florents Tselai10 months ago3 messageshackers

Thread notes

1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
hackorum-bot
1 day ago

@global #feature #moderate #enhancement

#feature#moderate#enhancement
Jump to latest
#1Florents Tselai
florents.tselai@gmail.com

Hello hackers,

Here's a patch that attempts to $subject.

My original motivation was to implement this for JSON_TABLE,
but I realized it would be better to start with a smaller scope
by targeting the basic query functions first.

Currently this v1 passes the tests I've put for JSON_VALUE.

I've had trouble making JSON_QUERY work too,
so I suspect that although my tests pass,
there may be something I'm missing in the executor side of things.
While I troubleshoot that it'd be nice to get some feedback for this
version.

Regards,
Flo

Attachments:

v1-0001-Add-SQL-JSON-ON-MISMATCH-clause-to-JSON_VALUE.patchapplication/octet-stream; name=v1-0001-Add-SQL-JSON-ON-MISMATCH-clause-to-JSON_VALUE.patchDownload+371-21
#2Florents Tselai
florents.tselai@gmail.com
In reply to: Florents Tselai (#1)
Re: Add SQL/JSON ON MISMATCH clause to JSON_VALUE

I've had trouble making JSON_QUERY work too,
so I suspect that although my tests pass,
there may be something I'm missing in the executor side of things.

"For those curious, I found the issue: in ExecEvalJsonExprPath, I
had misplaced the check for jsexpr->on_mismatch.
I wasn't setting jsestate->escontext.details_wanted = true early enough.
Without this, the soft error context wasn't capturing the specific SQL
error code needed to identify the mismatch.

Attaching a v2 that implements ON MISMATCH for JSON_QUERY and JSON_TABLE
too.

That said, the semantics of ON ERROR / MISMATCH / EMPTY are complex.
I expect someone with access to and detailed knowledge of the standard
might be able to poke holes in certain edge cases, particularly regarding
precedence.

Attachments:

t53168_2
v2-0001-Add-SQL-JSON-ON-MISMATCH-clause-to-JSON_VALUE-JSO.patchapplication/octet-stream; name=v2-0001-Add-SQL-JSON-ON-MISMATCH-clause-to-JSON_VALUE-JSO.patchDownload+747-53
#3Florents Tselai
florents.tselai@gmail.com
In reply to: Florents Tselai (#2)
Re: Add SQL/JSON ON MISMATCH clause to JSON_VALUE

On Sun, Jan 25, 2026 at 3:15 AM Florents Tselai <florents.tselai@gmail.com>
wrote:

I've had trouble making JSON_QUERY work too,
so I suspect that although my tests pass,
there may be something I'm missing in the executor side of things.

"For those curious, I found the issue: in ExecEvalJsonExprPath, I
had misplaced the check for jsexpr->on_mismatch.
I wasn't setting jsestate->escontext.details_wanted = true early enough.
Without this, the soft error context wasn't capturing the specific SQL
error code needed to identify the mismatch.

Attaching a v2 that implements ON MISMATCH for JSON_QUERY and JSON_TABLE
too.

That said, the semantics of ON ERROR / MISMATCH / EMPTY are complex.
I expect someone with access to and detailed knowledge of the standard
might be able to poke holes in certain edge cases, particularly regarding
precedence.

2 amendments to the commit message:

- This is isn't in the SQL/JSON standard

But aligns with the semantics of Oracle that already provides such a clause
[0]: https://docs.oracle.com/en/database/oracle/oracle-database/26/adjsn/mismatch-clause-sql-json-query-functions.html#GUID-21FE887D-5D7B-43BB-A1A8-B26B8924B290

- A TYPE ERROR is defined as "A JSON scalar value has a data type that is
incompatible with the corresponding return SQL scalar data type."
- Precedence logic: ON MISMATCH applies only when neither of the clauses ON
EMPTY and ON ERROR applies.
- Fallback Behavior: if the user doesn't specify ON MISMATCH, the coercion
error is caught by ON ERROR.

[0]: https://docs.oracle.com/en/database/oracle/oracle-database/26/adjsn/mismatch-clause-sql-json-query-functions.html#GUID-21FE887D-5D7B-43BB-A1A8-B26B8924B290
https://docs.oracle.com/en/database/oracle/oracle-database/26/adjsn/mismatch-clause-sql-json-query-functions.html#GUID-21FE887D-5D7B-43BB-A1A8-B26B8924B290