Skip to content

branch-4.1: [[fix](variant)](https://github.com/apache/doris/pull/63792) Preserve json object when casting jsonb to variant - #64662

Merged
yiguolei merged 1 commit into
apache:branch-4.1from
eldenmoon:branch-pick-63792-4.1
Jun 22, 2026
Merged

branch-4.1: [[fix](variant)](https://github.com/apache/doris/pull/63792) Preserve json object when casting jsonb to variant#64662
yiguolei merged 1 commit into
apache:branch-4.1from
eldenmoon:branch-pick-63792-4.1

Conversation

@eldenmoon

Copy link
Copy Markdown
Member

cherry-pick #63792

Issue Number: closeapache#24260
Related PR: #xxx
Problem Summary: Inserting JSONB-producing expressions, such as json_object or a JSON column, into a VARIANT column could materialize the root JSONB through the generic JSONB-to-string cast path. That path unescaped string contents before variant parsing, so JSON objects containing quoted strings could be parsed incorrectly and subpath extraction failed. This change converts root JSONB directly back to JSON text with JsonbToJson before parsing into ColumnVariant, preserving JSON escapes for object string values.
Fix variant insertion from JSON/JSONB expressions for strings containing quotes.
- Test:
- Build: `./build.sh --be --fe`
- Format: `PATH=/mnt/disk1/claude-max/tmp/doris/tmp/clang-format-bin:$PATH build-support/clang-format.sh` with clang-format 16.0.0
- Regression test: `./run-regression-test.sh --run -d variant_p0 -s test_insert_json_object_to_variant -forceGenOut ...`
- Regression test: `./run-regression-test.sh --run -d variant_p0 -s test_insert_json_object_to_variant ...`
- BE UT: `./run-be-ut.sh --run --filter='SchemaUtilTest.TestParseVariantColumnsEdgeCases:SchemaUtilTest.TestParseJsonbRootVariantMaterializesDocument'`
- Manual test: reproduced the affected JSON-to-VARIANT insert SQL before the fix and verified path extraction after the fix
- Static check: `CLANG_TIDY_BINARY=/mnt/disk1/claude-max/.vscode-server/extensions/ms-vscode.cpptools-1.32.2-linux-x64/LLVM/bin/clang-tidy build-support/run-clang-tidy.sh --build-dir be/build_Release --files be/src/exec/common/variant_util.cpp`
- Behavior changed: Yes. JSON/JSONB values inserted into variant now materialize as JSON objects so subpath extraction works when string values contain quotes.
- Does this need documentation: No
(cherry picked from commit b6c8054)
CopilotAI review requested due to automatic review settings June 22, 2026 03:39
@eldenmoon
eldenmoon requested a review from yiguolei as a code ownerJune 22, 2026 03:39
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This cherry-pick fixes VARIANT materialization when inserting JSON/JSONB-derived values whose string contents include quotes/backslashes, by ensuring JSONB roots are converted back to JSON text via a JSONB-aware path before VARIANT parsing/materialization.

Changes:

  • Add a JSONB→JSON-text conversion helper and use it when parsing scalar VARIANT roots of type JSONB.
  • Add a regression test suite and golden output covering json_object(...) and JSON column inserts into VARIANT with embedded escaped content.
  • Strengthen BE unit tests to build real JSONB columns and assert subpath materialization for JSONB-root VARIANT.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

FileDescription
regression-test/suites/variant_p0/test_insert_json_object_to_variant.groovyAdds regression coverage for inserting JSONB-producing expressions/JSON columns into VARIANT and validating subpath extraction.
regression-test/data/variant_p0/test_insert_json_object_to_variant.outAdds expected output for the new regression queries.
be/test/exec/common/schema_util_test.cppUpdates/extends unit tests to construct real JSONB columns and validate VARIANT materialization/subcolumns.
be/src/exec/common/variant_util.cppSwitches JSONB scalar-root parsing to a JSONB-aware conversion path before VARIANT parsing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +249 to +258
auto root_column = root.convert_to_full_column_if_const();
const IColumn* jsonb_column = root_column.get();
const NullMap* null_map = nullptr;
if (root_column->is_nullable()) {
const auto& nullable = assert_cast<const ColumnNullable&>(*root_column);
jsonb_column = &nullable.get_nested_column();
null_map = &nullable.get_null_map_data();
}

const auto& column = assert_cast<const ColumnString&>(*jsonb_column);
@eldenmoon

Copy link
Copy Markdown
MemberAuthor

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

BE UT Coverage Report

Increment line coverage 64.29% (18/28) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage54.19% (20613/38041)
Line Coverage37.75% (196191/519767)
Region Coverage34.13% (153358/449357)
Branch Coverage35.05% (66902/190893)

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 89.29% (25/28) 🎉

Increment coverage report
Complete coverage report

CategoryCoverage
Function Coverage73.75% (27431/37197)
Line Coverage57.38% (297162/517847)
Region Coverage55.04% (249526/453350)
Branch Coverage56.46% (108064/191398)

@yiguolei
yiguolei merged commit 2e22f74 into apache:branch-4.1Jun 22, 2026
29 of 32 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@eldenmoon@hello-stephen@yiguolei