Skip to content
/ server Public

MDEV-38873 json_extract returns NULL through derived table#4680

Open
varundeepsaini wants to merge 1 commit intoMariaDB:10.11from
varundeepsaini:MDEV-38873-json-extract-derived-table
Open

MDEV-38873 json_extract returns NULL through derived table#4680
varundeepsaini wants to merge 1 commit intoMariaDB:10.11from
varundeepsaini:MDEV-38873-json-extract-derived-table

Conversation

@varundeepsaini
Copy link
Contributor

  • The Jira issue number for this PR is: MDEV-38873

Description

json_extract() passes its result through json_nice() with LOOSE format, which expands the output by adding spaces after commas and colons (e.g. ["a",1]["a", 1]). The max_length in Item_func_json_extract::fix_length_and_dec() was not accounting for this expansion. When the result passed through a derived table, the field was created with the underestimated length, truncating the JSON string and causing the outer json_extract() to fail with "Unexpected end of JSON text".

Fixed by multiplying max_length by 2 to account for the LOOSE format expansion, matching JSON_FORMAT(LOOSE) behavior.

Release Notes

JSON_EXTRACT() no longer returns NULL with "Unexpected end of JSON text" when its result is used through a derived table.

How can this PR be tested?

./mysql-test/mtr --suite=main func_json

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the main branch.
  • This is a bug fix.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

@varundeepsaini varundeepsaini force-pushed the MDEV-38873-json-extract-derived-table branch from 95b0d9b to 8653482 Compare February 22, 2026 15:21
@varundeepsaini varundeepsaini marked this pull request as draft February 22, 2026 18:48
@varundeepsaini varundeepsaini force-pushed the MDEV-38873-json-extract-derived-table branch from 6267e55 to 8500024 Compare February 23, 2026 06:36
@varundeepsaini varundeepsaini marked this pull request as ready for review February 23, 2026 06:36
@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Feb 23, 2026
Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution.
This is a preliminary review.

@varundeepsaini varundeepsaini changed the base branch from main to 10.11 February 23, 2026 10:44
@varundeepsaini varundeepsaini changed the base branch from 10.11 to main February 23, 2026 10:44
@varundeepsaini varundeepsaini force-pushed the MDEV-38873-json-extract-derived-table branch from 8500024 to 6720fd8 Compare February 26, 2026 06:37
@CLAassistant
Copy link

CLAassistant commented Feb 26, 2026

CLA assistant check
All committers have signed the CLA.

@varundeepsaini varundeepsaini changed the base branch from main to 10.6 February 26, 2026 06:38
@varundeepsaini varundeepsaini force-pushed the MDEV-38873-json-extract-derived-table branch from 6720fd8 to fe97d5b Compare February 26, 2026 06:46
Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10.11 is the correct target at this point. Please rebase to that.

@varundeepsaini varundeepsaini force-pushed the MDEV-38873-json-extract-derived-table branch from fe97d5b to 318c6c2 Compare February 27, 2026 05:48
@varundeepsaini varundeepsaini changed the base branch from 10.6 to 10.11 February 27, 2026 05:48
@varundeepsaini varundeepsaini force-pushed the MDEV-38873-json-extract-derived-table branch from 318c6c2 to 4e46538 Compare February 27, 2026 05:53
Item_func_json_extract::fix_length_and_dec() underestimated
max_length. The result is passed through json_nice() with LOOSE
formatting which expands separators (e.g. ["a",1] -> ["a", 1]),
but max_length was computed from the input size alone. When the
result flows through a materialized derived table, the field is
created with this too-small max_length and the output gets truncated.

Fix: multiply by 2 to account for LOOSE expansion, add array
framing overhead for multi-path extractions, and use
fix_char_length_ulonglong() for proper character-to-byte
conversion and overflow handling.

Signed-off-by: Varun Deep Saini <varun.23bcs10048@ms.sst.scaler.com>
@varundeepsaini varundeepsaini force-pushed the MDEV-38873-json-extract-derived-table branch from 4e46538 to 1a9425e Compare February 27, 2026 05:58
Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please stand by for the final review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

5 participants