-
Notifications
You must be signed in to change notification settings - Fork 412
Support json_object pushdown #10744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ti-chi-bot
merged 6 commits into
pingcap:master
from
windtalker:json_object_pushdown_master_pr
Mar 17, 2026
Merged
Support json_object pushdown #10744
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ea1226a
support json_object pushdown
windtalker 2ca4e2f
disable json_object explain test temporarily
windtalker 3c587a6
fix json_object code format
windtalker 932e071
improve json_object execution errors and allocations
windtalker 2559791
Update dbms/src/Functions/tests/gtest_json_object.cpp
windtalker 293e2f6
address comments
windtalker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright 2026 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <TestUtils/FunctionTestUtils.h> | ||
| #include <TestUtils/TiFlashTestBasic.h> | ||
| #include <TiDB/Schema/TiDBTypes.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <limits> | ||
|
|
||
| namespace DB::ErrorCodes | ||
| { | ||
| extern const int ARGUMENT_OUT_OF_BOUND; | ||
| extern const int BAD_ARGUMENTS; | ||
| } // namespace DB::ErrorCodes | ||
|
|
||
| namespace DB::tests | ||
| { | ||
| class TestJsonObject : public DB::tests::FunctionTest | ||
| { | ||
| public: | ||
| ColumnWithTypeAndName castStringToJson(const ColumnWithTypeAndName & column) | ||
| { | ||
| assert(removeNullable(column.type)->isString()); | ||
| ColumnsWithTypeAndName inputs{column}; | ||
| return executeFunction("cast_string_as_json", inputs, nullptr, true); | ||
| } | ||
|
|
||
| ColumnWithTypeAndName executeFunctionWithCast( | ||
| const ColumnNumbers & argument_column_numbers, | ||
| const ColumnsWithTypeAndName & columns) | ||
| { | ||
| auto json_column = executeFunction("json_object", argument_column_numbers, columns); | ||
| tipb::FieldType field_type; | ||
| field_type.set_flen(-1); | ||
| field_type.set_collate(TiDB::ITiDBCollator::BINARY); | ||
| field_type.set_tp(TiDB::TypeString); | ||
| return executeCastJsonAsStringFunction(json_column, field_type); | ||
| } | ||
| }; | ||
|
|
||
| template <typename Fn> | ||
| void assertThrowsCode(Fn && fn, int expected_code) | ||
| { | ||
| try | ||
| { | ||
| fn(); | ||
| FAIL() << "Expected DB::Exception to be thrown"; | ||
| } | ||
| catch (const Exception & e) | ||
| { | ||
| ASSERT_EQ(expected_code, e.code()) << e.displayText(); | ||
| } | ||
| } | ||
|
|
||
| TEST_F(TestJsonObject, TestBasicSemantics) | ||
| try | ||
| { | ||
| constexpr size_t rows_count = 2; | ||
|
|
||
| { | ||
| ColumnsWithTypeAndName inputs{createColumn<String>({"placeholder", "placeholder"})}; | ||
| auto res = executeFunctionWithCast({}, inputs); | ||
| ASSERT_COLUMN_EQ(createConstColumn<Nullable<String>>(rows_count, "{}"), res); | ||
| } | ||
|
|
||
| { | ||
| ColumnsWithTypeAndName inputs{ | ||
| createColumn<String>({"b", "b"}), | ||
| castStringToJson(createColumn<String>({"1", "1"})), | ||
| createColumn<String>({"a", "a"}), | ||
| castStringToJson(createColumn<Nullable<String>>({{}, "\"x\""})), | ||
| }; | ||
| auto res = executeFunctionWithCast({0, 1, 2, 3}, inputs); | ||
| auto expect = createColumn<Nullable<String>>({R"({"a": null, "b": 1})", R"({"a": "x", "b": 1})"}); | ||
| ASSERT_COLUMN_EQ(expect, res); | ||
| } | ||
|
|
||
| { | ||
| ColumnsWithTypeAndName inputs{ | ||
| createConstColumn<String>(rows_count, "dup"), | ||
| castStringToJson(createConstColumn<String>(rows_count, "1")), | ||
| createConstColumn<String>(rows_count, "dup"), | ||
| castStringToJson(createColumn<String>({"2", "3"})), | ||
| }; | ||
| auto res = executeFunctionWithCast({0, 1, 2, 3}, inputs); | ||
| auto expect = createColumn<Nullable<String>>({R"({"dup": 2})", R"({"dup": 3})"}); | ||
| ASSERT_COLUMN_EQ(expect, res); | ||
| } | ||
| } | ||
| CATCH | ||
|
|
||
| TEST_F(TestJsonObject, TestErrors) | ||
| try | ||
| { | ||
| ASSERT_THROW(executeFunction("json_object", {createColumn<String>({"a"})}), Exception); | ||
|
|
||
| auto value = castStringToJson(createColumn<String>({"1"})); | ||
| assertThrowsCode( | ||
| [&] { | ||
| executeFunction("json_object", {createOnlyNullColumn(1), value}); | ||
| }, | ||
| ErrorCodes::BAD_ARGUMENTS); | ||
|
|
||
| assertThrowsCode( | ||
| [&] { | ||
| executeFunction("json_object", {createColumn<Nullable<String>>({{}}), value}); | ||
| }, | ||
| ErrorCodes::BAD_ARGUMENTS); | ||
|
|
||
| String too_long_key(std::numeric_limits<UInt16>::max() + 1, 'a'); | ||
| assertThrowsCode( | ||
| [&] { | ||
| executeFunction("json_object", {createColumn<String>({too_long_key}), value}); | ||
| }, | ||
| ErrorCodes::ARGUMENT_OUT_OF_BOUND); | ||
| } | ||
| CATCH | ||
|
|
||
| } // namespace DB::tests |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
arg_idx % 2 == 0andarguments[arg_idx]->onlyNull() == true, I think we can throw exception here asJSON documents may not contain NULL member names.