github-actions[bot] commented on code in PR #60910:
URL: https://github.com/apache/doris/pull/60910#discussion_r2875848738
##########
be/src/vec/functions/function_fake.cpp:
##########
@@ -239,6 +267,8 @@ void register_function_fake(SimpleFunctionFactory& factory)
{
register_table_function_expand_outer<FunctionExplodeMap>(factory,
"explode_map");
register_table_function_expand_outer<FunctionExplodeJsonObject>(factory,
"explode_json_object");
+ register_function<FunctionJsonEach>(factory, "json_each");
+ register_function<FunctionJsonEachText>(factory, "json_each_text");
Review Comment:
Unlike the adjacent `explode_json_object` which uses
`register_table_function_expand_outer`, `json_each` and `json_each_text` use
plain `register_function`. This means `json_each_outer` /
`json_each_text_outer` won't be recognized by the FE. If outer semantics are
intentionally not supported, consider adding a comment explaining why.
Otherwise, consider using `register_table_function_expand_outer` (and adding
corresponding `JsonEachOuter`/`JsonEachTextOuter` FE classes) for consistency
with the rest of the table function ecosystem.
##########
fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinTableGeneratingFunctions.java:
##########
@@ -63,6 +65,8 @@ public class BuiltinTableGeneratingFunctions implements
FunctionHelper {
tableGenerating(ExplodeMapOuter.class, "explode_map_outer"),
tableGenerating(ExplodeJsonObject.class, "explode_json_object"),
tableGenerating(ExplodeJsonObjectOuter.class,
"explode_json_object_outer"),
+ tableGenerating(JsonEach.class, "json_each"),
+ tableGenerating(JsonEachText.class, "json_each_text"),
tableGenerating(ExplodeNumbers.class, "explode_numbers"),
Review Comment:
Nit: This line has extra leading whitespace (double indentation) compared to
the surrounding entries. Should align with `tableGenerating(JsonEach.class,
"json_each"),` above.
```suggestion
tableGenerating(JsonEachText.class, "json_each_text"),
```
##########
be/src/vec/exprs/table_function/vjson_each.cpp:
##########
@@ -0,0 +1,203 @@
+// 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);
+
+ 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());
Review Comment:
When `_is_const` is true, this code still re-parses JSON and rebuilds
`_kv_pairs` for every row. Consider adding a short-circuit like:
```cpp
if (_is_const && _cur_size > 0) {
return; // reuse _kv_pairs from first row
}
```
before the JSON parsing (after `TableFunction::process_row()`). This would
avoid redundant work for constant/literal inputs like `LATERAL VIEW
json_each('{"a":1}')`.
Note: The base class `process_row` already preserves `_cur_size` when
`_is_const` is true, so the `_kv_pairs` from the first parse would remain valid.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]