-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Feature](func) Support table function json_each, json_each_text #60910
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
Open
linrrzqqq
wants to merge
2
commits into
apache:master
Choose a base branch
from
linrrzqqq:json-each
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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 "vec/exprs/table_function/vjson_each.h" | ||
|
|
||
| #include <glog/logging.h> | ||
|
|
||
| #include <ostream> | ||
| #include <string> | ||
|
|
||
| #include "common/status.h" | ||
| #include "util/jsonb_document.h" | ||
| #include "util/jsonb_utils.h" | ||
| #include "util/jsonb_writer.h" | ||
| #include "vec/columns/column.h" | ||
| #include "vec/columns/column_const.h" | ||
| #include "vec/columns/column_struct.h" | ||
| #include "vec/common/assert_cast.h" | ||
| #include "vec/common/string_ref.h" | ||
| #include "vec/core/block.h" | ||
| #include "vec/core/column_with_type_and_name.h" | ||
| #include "vec/exprs/vexpr.h" | ||
| #include "vec/exprs/vexpr_context.h" | ||
|
|
||
| namespace doris::vectorized { | ||
| #include "common/compile_check_begin.h" | ||
|
|
||
| template <bool TEXT_MODE> | ||
| VJsonEachTableFunction<TEXT_MODE>::VJsonEachTableFunction() { | ||
| _fn_name = TEXT_MODE ? "vjson_each_text" : "vjson_each"; | ||
| } | ||
|
|
||
| template <bool TEXT_MODE> | ||
| Status VJsonEachTableFunction<TEXT_MODE>::process_init(Block* block, RuntimeState* /*state*/) { | ||
| int value_column_idx = -1; | ||
| RETURN_IF_ERROR(_expr_context->root()->children()[0]->execute(_expr_context.get(), block, | ||
| &value_column_idx)); | ||
| auto [col, is_const] = unpack_if_const(block->get_by_position(value_column_idx).column); | ||
| _json_column = col; | ||
| _is_const = is_const; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| // Helper: insert one JsonbValue as plain text into a ColumnNullable<ColumnString>. | ||
| // For strings: raw blob content (quotes stripped, matching json_each_text PG semantics). | ||
| // For null JSON values: SQL NULL (insert_default). | ||
| // For all others (numbers, bools, objects, arrays): JSON text representation. | ||
| static void insert_value_as_text(const JsonbValue* value, MutableColumnPtr& col) { | ||
| if (value == nullptr || value->isNull()) { | ||
| col->insert_default(); | ||
| return; | ||
| } | ||
| if (value->isString()) { | ||
| const auto* str_val = value->unpack<JsonbStringVal>(); | ||
| col->insert_data(str_val->getBlob(), str_val->getBlobLen()); | ||
| } else { | ||
| JsonbToJson converter; | ||
| std::string text = converter.to_json_string(value); | ||
| col->insert_data(text.data(), text.size()); | ||
| } | ||
| } | ||
|
|
||
| // Helper: insert one JsonbValue in JSONB binary form into a ColumnNullable<ColumnString>. | ||
| // For null JSON values: SQL NULL (insert_default). | ||
| // For all others: write JSONB binary via JsonbWriter. | ||
| static void insert_value_as_json(const JsonbValue* value, MutableColumnPtr& col, | ||
| JsonbWriter& writer) { | ||
| if (value == nullptr || value->isNull()) { | ||
| col->insert_default(); | ||
| return; | ||
| } | ||
| writer.reset(); | ||
| writer.writeValue(value); | ||
| const auto* buf = writer.getOutput()->getBuffer(); | ||
| size_t len = writer.getOutput()->getSize(); | ||
| col->insert_data(buf, len); | ||
| } | ||
|
|
||
| template <bool TEXT_MODE> | ||
| void VJsonEachTableFunction<TEXT_MODE>::process_row(size_t row_idx) { | ||
| TableFunction::process_row(row_idx); | ||
| if (_is_const && _cur_size > 0) { | ||
| return; | ||
| } | ||
|
|
||
| StringRef text; | ||
| const size_t idx = _is_const ? 0 : row_idx; | ||
| if (const auto* nullable_col = check_and_get_column<ColumnNullable>(*_json_column)) { | ||
| if (nullable_col->is_null_at(idx)) { | ||
| return; | ||
| } | ||
| text = assert_cast<const ColumnString&>(nullable_col->get_nested_column()).get_data_at(idx); | ||
| } else { | ||
| text = assert_cast<const ColumnString&>(*_json_column).get_data_at(idx); | ||
| } | ||
|
|
||
| const JsonbDocument* doc = nullptr; | ||
| auto st = JsonbDocument::checkAndCreateDocument(text.data, text.size, &doc); | ||
| if (!st.ok() || !doc || !doc->getValue()) [[unlikely]] { | ||
| return; | ||
| } | ||
|
|
||
| const JsonbValue* jv = doc->getValue(); | ||
| if (!jv->isObject()) { | ||
| return; | ||
| } | ||
|
|
||
| const auto* obj = jv->unpack<ObjectVal>(); | ||
| _cur_size = obj->numElem(); | ||
| if (_cur_size == 0) { | ||
| return; | ||
| } | ||
|
|
||
| _kv_pairs.first = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create()); | ||
| _kv_pairs.second = ColumnNullable::create(ColumnString::create(), ColumnUInt8::create()); | ||
| _kv_pairs.first->reserve(_cur_size); | ||
| _kv_pairs.second->reserve(_cur_size); | ||
|
|
||
| if constexpr (TEXT_MODE) { | ||
| for (const auto& kv : *obj) { | ||
| _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen()); | ||
| insert_value_as_text(kv.value(), _kv_pairs.second); | ||
| } | ||
| } else { | ||
| JsonbWriter writer; | ||
| for (const auto& kv : *obj) { | ||
| _kv_pairs.first->insert_data(kv.getKeyStr(), kv.klen()); | ||
| insert_value_as_json(kv.value(), _kv_pairs.second, writer); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template <bool TEXT_MODE> | ||
| void VJsonEachTableFunction<TEXT_MODE>::process_close() { | ||
| _json_column = nullptr; | ||
| _kv_pairs.first = nullptr; | ||
| _kv_pairs.second = nullptr; | ||
| } | ||
|
|
||
| template <bool TEXT_MODE> | ||
| void VJsonEachTableFunction<TEXT_MODE>::get_same_many_values(MutableColumnPtr& column, int length) { | ||
| if (current_empty()) { | ||
| column->insert_many_defaults(length); | ||
| return; | ||
| } | ||
|
|
||
| ColumnStruct* ret; | ||
| if (_is_nullable) { | ||
| auto* nullable = assert_cast<ColumnNullable*>(column.get()); | ||
| ret = assert_cast<ColumnStruct*>(nullable->get_nested_column_ptr().get()); | ||
| assert_cast<ColumnUInt8*>(nullable->get_null_map_column_ptr().get()) | ||
| ->insert_many_defaults(length); | ||
| } else { | ||
| ret = assert_cast<ColumnStruct*>(column.get()); | ||
| } | ||
|
|
||
| ret->get_column(0).insert_many_from(*_kv_pairs.first, _cur_offset, length); | ||
| ret->get_column(1).insert_many_from(*_kv_pairs.second, _cur_offset, length); | ||
| } | ||
|
|
||
| template <bool TEXT_MODE> | ||
| int VJsonEachTableFunction<TEXT_MODE>::get_value(MutableColumnPtr& column, int max_step) { | ||
| max_step = std::min(max_step, (int)(_cur_size - _cur_offset)); | ||
|
|
||
| if (current_empty()) { | ||
| column->insert_default(); | ||
| max_step = 1; | ||
| } else { | ||
| ColumnStruct* struct_col = nullptr; | ||
| if (_is_nullable) { | ||
| auto* nullable_col = assert_cast<ColumnNullable*>(column.get()); | ||
| struct_col = assert_cast<ColumnStruct*>(nullable_col->get_nested_column_ptr().get()); | ||
| assert_cast<ColumnUInt8*>(nullable_col->get_null_map_column_ptr().get()) | ||
| ->insert_many_defaults(max_step); | ||
| } else { | ||
| struct_col = assert_cast<ColumnStruct*>(column.get()); | ||
| } | ||
|
|
||
| struct_col->get_column(0).insert_range_from(*_kv_pairs.first, _cur_offset, max_step); | ||
| struct_col->get_column(1).insert_range_from(*_kv_pairs.second, _cur_offset, max_step); | ||
| } | ||
|
|
||
| forward(max_step); | ||
| return max_step; | ||
| } | ||
|
|
||
| // // Explicit template instantiations | ||
| template class VJsonEachTableFunction<false>; // json_each | ||
| template class VJsonEachTableFunction<true>; // json_each_text | ||
|
|
||
| #include "common/compile_check_end.h" | ||
| } // namespace doris::vectorized | ||
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,68 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| #include "common/status.h" | ||
| #include "vec/data_types/data_type.h" | ||
| #include "vec/exprs/table_function/table_function.h" | ||
|
|
||
| namespace doris::vectorized { | ||
| #include "common/compile_check_begin.h" | ||
| class Block; | ||
|
|
||
| // json_each('{"a":"foo","b":123}') → | ||
| // | key | value | | ||
| // | a | "foo" (JSON) | | ||
| // | b | 123 (JSON) | | ||
| // | ||
| // json_each_text('{"a":"foo","b":123}') → | ||
| // | key | value | | ||
| // | a | foo | ← string unquoted | ||
| // | b | 123 | ← number as text | ||
| // | ||
| // TEXT_MODE=false → json_each (value column type: JSONB binary) | ||
| // TEXT_MODE=true → json_each_text (value column type: plain STRING) | ||
| template <bool TEXT_MODE> | ||
| class VJsonEachTableFunction : public TableFunction { | ||
| ENABLE_FACTORY_CREATOR(VJsonEachTableFunction); | ||
|
|
||
| public: | ||
| VJsonEachTableFunction(); | ||
|
|
||
| ~VJsonEachTableFunction() override = default; | ||
|
|
||
| Status process_init(Block* block, RuntimeState* state) override; | ||
| void process_row(size_t row_idx) override; | ||
| void process_close() override; | ||
| void get_same_many_values(MutableColumnPtr& column, int length) override; | ||
| int get_value(MutableColumnPtr& column, int max_step) override; | ||
|
|
||
| private: | ||
| ColumnPtr _json_column; | ||
| // _kv_pairs.first : ColumnNullable<ColumnString> key (always plain text) | ||
| // _kv_pairs.second : ColumnNullable<ColumnString> value (JSONB bytes or plain text) | ||
| std::pair<MutableColumnPtr, MutableColumnPtr> _kv_pairs; | ||
| }; | ||
|
|
||
| using VJsonEachTableFn = VJsonEachTableFunction<false>; | ||
| using VJsonEachTextTableFn = VJsonEachTableFunction<true>; | ||
|
|
||
| #include "common/compile_check_end.h" | ||
| } // namespace doris::vectorized |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.