xiaokang commented on code in PR #24801:
URL: https://github.com/apache/doris/pull/24801#discussion_r1390329050


##########
be/src/util/jsonb_document.h:
##########
@@ -1264,6 +1267,56 @@ inline int JsonbValue::length() const {
     }
 }
 
+inline int JsonbValue::depth() const {
+    switch (type_) {
+    case JsonbType::T_Int8:
+    case JsonbType::T_Int16:
+    case JsonbType::T_Int32:
+    case JsonbType::T_Int64:
+    case JsonbType::T_Double:
+    case JsonbType::T_Float:
+    case JsonbType::T_Int128:
+    case JsonbType::T_String:
+    case JsonbType::T_Binary:
+    case JsonbType::T_Null:
+    case JsonbType::T_True:
+    case JsonbType::T_False: {
+        return 1;
+    }
+    case JsonbType::T_Object: {
+        int depth = 1;
+        int numElem = ((ObjectVal*)this)->numElem();
+        if (numElem == 0) return depth;
+
+        JsonbKeyValue* first_key = ((ObjectVal*)this)->getJsonbKeyValue(0);

Review Comment:
   why do special check for first element?



##########
be/src/vec/functions/function_jsonb.cpp:
##########
@@ -1288,6 +1288,56 @@ struct JsonbContainsAndPathImpl {
     }
 };
 
+class FunctionJsonbDepth : public IFunction {
+public:
+    static constexpr auto name = "json_depth";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionJsonbDepth>(); }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        return make_nullable(std::make_shared<DataTypeInt32>());
+    }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        DCHECK_GE(arguments.size(), 1);
+        auto jsonb_data_column = block.get_by_position(arguments[0]).column;
+
+        auto null_map = ColumnUInt8::create(input_rows_count, 0);
+        auto return_type = block.get_data_type(result);
+        MutableColumnPtr res = return_type->create_column();
+
+        //if(jsonb_data_column->)

Review Comment:
   delete unused code



##########
gensrc/script/doris_builtins_functions.py:
##########
@@ -1771,6 +1771,7 @@
         [['json_contains'], 'BOOLEAN', ['JSONB', 'JSONB'], 'ALWAYS_NULLABLE'],
         [['json_contains'], 'BOOLEAN', ['JSONB', 'JSONB', 'VARCHAR'], 
'ALWAYS_NULLABLE'],
         [['json_contains'], 'BOOLEAN', ['JSONB', 'JSONB', 'STRING'], 
'ALWAYS_NULLABLE'],
+        [['json_depth'], 'INT', ['JSONB'], 'ALWAYS_NULLABLE'],

Review Comment:
   why not depends on argument?



##########
docs/zh-CN/docs/sql-manual/sql-functions/json-functions/json-depth.md:
##########
@@ -0,0 +1,94 @@
+---
+{
+"title": "JSON_DEPTH",
+"language": "zh-CN"
+}
+---
+
+<!-- 
+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.
+-->
+
+## json_depth
+### description
+#### Syntax
+
+`INT json_depth(JSON json_str)`
+
+返回 JSON 文档的最大深度。
+
+JSON_DEPTH()根据以下规则计算深度:
+
+* 空数组、空对象或标量的深度为 1。
+* 仅包含深度为 1 的元素的非空数组的深度为2。
+* 仅包含深度为 1 的成员值的非空对象的深度为2。

Review Comment:
   add explanation for depth > 2



##########
be/src/util/jsonb_document.h:
##########
@@ -1264,6 +1267,56 @@ inline int JsonbValue::length() const {
     }
 }
 
+inline int JsonbValue::depth() const {
+    switch (type_) {
+    case JsonbType::T_Int8:
+    case JsonbType::T_Int16:
+    case JsonbType::T_Int32:
+    case JsonbType::T_Int64:
+    case JsonbType::T_Double:
+    case JsonbType::T_Float:
+    case JsonbType::T_Int128:
+    case JsonbType::T_String:
+    case JsonbType::T_Binary:
+    case JsonbType::T_Null:
+    case JsonbType::T_True:
+    case JsonbType::T_False: {
+        return 1;
+    }
+    case JsonbType::T_Object: {
+        int depth = 1;

Review Comment:
   suggest a better name base_depth



##########
be/src/util/jsonb_document.h:
##########
@@ -1264,6 +1267,56 @@ inline int JsonbValue::length() const {
     }
 }
 
+inline int JsonbValue::depth() const {
+    switch (type_) {
+    case JsonbType::T_Int8:
+    case JsonbType::T_Int16:
+    case JsonbType::T_Int32:
+    case JsonbType::T_Int64:
+    case JsonbType::T_Double:
+    case JsonbType::T_Float:
+    case JsonbType::T_Int128:
+    case JsonbType::T_String:
+    case JsonbType::T_Binary:
+    case JsonbType::T_Null:
+    case JsonbType::T_True:
+    case JsonbType::T_False: {
+        return 1;
+    }
+    case JsonbType::T_Object: {
+        int depth = 1;
+        int numElem = ((ObjectVal*)this)->numElem();
+        if (numElem == 0) return depth;
+
+        JsonbKeyValue* first_key = ((ObjectVal*)this)->getJsonbKeyValue(0);

Review Comment:
   just set max_depth = depth and for loop from 0



##########
be/src/vec/functions/function_jsonb.cpp:
##########
@@ -1288,6 +1288,56 @@ struct JsonbContainsAndPathImpl {
     }
 };
 
+class FunctionJsonbDepth : public IFunction {
+public:
+    static constexpr auto name = "json_depth";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionJsonbDepth>(); }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        return make_nullable(std::make_shared<DataTypeInt32>());
+    }
+
+    size_t get_number_of_arguments() const override { return 1; }
+
+    bool use_default_implementation_for_nulls() const override { return false; 
}
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        DCHECK_GE(arguments.size(), 1);
+        auto jsonb_data_column = block.get_by_position(arguments[0]).column;
+
+        auto null_map = ColumnUInt8::create(input_rows_count, 0);
+        auto return_type = block.get_data_type(result);
+        MutableColumnPtr res = return_type->create_column();
+
+        //if(jsonb_data_column->)
+
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            if (jsonb_data_column->is_null_at(i)) {
+                null_map->get_data()[i] = 1;
+                res->insert_data(nullptr, 0);
+                continue;
+            }
+
+            auto jsonb_value = jsonb_data_column->get_data_at(i);
+            // doc is NOT necessary to be deleted since JsonbDocument will not 
allocate memory
+            JsonbDocument* doc = 
JsonbDocument::createDocument(jsonb_value.data, jsonb_value.size);
+            JsonbValue* value = doc->getValue();
+            if (UNLIKELY(jsonb_value.size == 0 || !value)) {
+                null_map->get_data()[i] = 1;
+                res->insert_data(nullptr, 0);
+                continue;
+            }
+            auto depth = value->depth();
+            res->insert_data(const_cast<const char*>((char*)&depth), 0);

Review Comment:
   it's unsafe. Add a int32 to column is safe and efficient.



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to