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


##########
be/src/util/jsonb_document.h:
##########
@@ -273,35 +276,43 @@ class Stream {
         skip_whitespace();
     }
 
-    void clear_legPtr() { legPtr = nullptr; }
+    void clear_legPtr() { leg_ptr = nullptr; }
 
     void set_legPtr(char* ptr) {

Review Comment:
   set_leg_ptr



##########
be/src/util/jsonb_document.h:
##########
@@ -273,35 +276,43 @@ class Stream {
         skip_whitespace();
     }
 
-    void clear_legPtr() { legPtr = nullptr; }
+    void clear_legPtr() { leg_ptr = nullptr; }
 
     void set_legPtr(char* ptr) {
         clear_legPtr();
-        legPtr = ptr;
+        leg_ptr = ptr;
     }
 
-    char* get_legPtr() { return legPtr; }
+    char* get_legPtr() { return leg_ptr; }
 
-    void clear_legLen() { legLen = 0; }
+    void clear_legLen() { leg_len = 0; }
 
-    void add_legLen() { legLen++; }
+    void add_legLen() { leg_len++; }
 
-    unsigned int get_legLen() { return legLen; }
+    unsigned int get_legLen() const { return leg_len; }

Review Comment:
   get_leg_len



##########
be/src/util/jsonb_document.h:
##########
@@ -1200,52 +1225,125 @@ inline JsonbValue* JsonbValue::findPath(const char* 
key_path, unsigned int kp_le
         stream.clear_legPtr();
         stream.clear_legLen();
 
-        if (!JsonbPath::parsePath(&stream, pval)) {
+        if (!JsonbPath::parsePath(&stream)) {
+            is_invalid_json_path = stream.get_is_invalid_json_path();
             return nullptr;
         }
 
         if (stream.get_legLen() == 0) {
             return nullptr;
         }
 
-        if (LIKELY(pval->type_ == JsonbType::T_Object)) {
-            if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (stream.get_hasEscapes()) {
-                stream.remove_escapes();
-            }
+        if (stream.get_type() == MEMBER_CODE) {
+            if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                return nullptr;
+            } else if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                if (stream.get_legLen() == 1 && *stream.get_legPtr() == 
WILDCARD) {
+                    continue;
+                } else if (stream.get_hasEscapes()) {
+                    stream.remove_escapes();
+                }
 
-            pval = ((ObjectVal*)pval)->find(stream.get_legPtr(), 
stream.get_legLen(), handler);
+                pval = ((ObjectVal*)pval)->find(stream.get_legPtr(), 
stream.get_legLen(), handler);
 
-            if (!pval) return nullptr;
-        } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                if (!pval) return nullptr;
+            } else {
+                return nullptr;
+            }
+        } else if (stream.get_type() == ARRAY_CODE) {
             int index = 0;
-            std::string idx_string(stream.get_legPtr(), stream.get_legLen());
+            std::string_view idx_string(stream.get_legPtr(), 
stream.get_legLen());
 
             if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (std::string(stream.get_legPtr(), 4) == LAST) {
+                if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                    return nullptr;
+                } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                    continue;

Review Comment:
   Why continue, which is different with original code?



##########
be/src/util/jsonb_document.h:
##########
@@ -273,35 +276,43 @@ class Stream {
         skip_whitespace();
     }
 
-    void clear_legPtr() { legPtr = nullptr; }
+    void clear_legPtr() { leg_ptr = nullptr; }
 
     void set_legPtr(char* ptr) {
         clear_legPtr();
-        legPtr = ptr;
+        leg_ptr = ptr;
     }
 
-    char* get_legPtr() { return legPtr; }
+    char* get_legPtr() { return leg_ptr; }
 
-    void clear_legLen() { legLen = 0; }
+    void clear_legLen() { leg_len = 0; }
 
-    void add_legLen() { legLen++; }
+    void add_legLen() { leg_len++; }
 
-    unsigned int get_legLen() { return legLen; }
+    unsigned int get_legLen() const { return leg_len; }
 
     void remove_escapes() {
         int new_len = 0;
-        for (int i = 0; i < legLen; i++) {
-            if (legPtr[i] != '\\') {
-                legPtr[new_len++] = legPtr[i];
+        for (int i = 0; i < leg_len; i++) {
+            if (leg_ptr[i] != '\\') {
+                leg_ptr[new_len++] = leg_ptr[i];
             }
         }
-        legPtr[new_len] = '\0';
-        legLen = new_len;
+        leg_ptr[new_len] = '\0';
+        leg_len = new_len;
     }
 
-    void set_hasEscapes(bool has) { hasEscapes = has; }
+    void set_hasEscapes(bool has) { has_escapes = has; }

Review Comment:
   set_has_escapes



##########
be/src/util/jsonb_document.h:
##########
@@ -273,35 +276,43 @@ class Stream {
         skip_whitespace();
     }
 
-    void clear_legPtr() { legPtr = nullptr; }
+    void clear_legPtr() { leg_ptr = nullptr; }
 
     void set_legPtr(char* ptr) {
         clear_legPtr();
-        legPtr = ptr;
+        leg_ptr = ptr;
     }
 
-    char* get_legPtr() { return legPtr; }
+    char* get_legPtr() { return leg_ptr; }
 
-    void clear_legLen() { legLen = 0; }
+    void clear_legLen() { leg_len = 0; }
 
-    void add_legLen() { legLen++; }
+    void add_legLen() { leg_len++; }

Review Comment:
   add_leg_len



##########
be/src/util/jsonb_document.h:
##########
@@ -273,35 +276,43 @@ class Stream {
         skip_whitespace();
     }
 
-    void clear_legPtr() { legPtr = nullptr; }
+    void clear_legPtr() { leg_ptr = nullptr; }
 
     void set_legPtr(char* ptr) {
         clear_legPtr();
-        legPtr = ptr;
+        leg_ptr = ptr;
     }
 
-    char* get_legPtr() { return legPtr; }
+    char* get_legPtr() { return leg_ptr; }
 
-    void clear_legLen() { legLen = 0; }
+    void clear_legLen() { leg_len = 0; }

Review Comment:
   clear_leg_len



##########
be/src/util/jsonb_document.h:
##########
@@ -1200,52 +1225,125 @@ inline JsonbValue* JsonbValue::findPath(const char* 
key_path, unsigned int kp_le
         stream.clear_legPtr();
         stream.clear_legLen();
 
-        if (!JsonbPath::parsePath(&stream, pval)) {
+        if (!JsonbPath::parsePath(&stream)) {
+            is_invalid_json_path = stream.get_is_invalid_json_path();
             return nullptr;
         }
 
         if (stream.get_legLen() == 0) {
             return nullptr;
         }
 
-        if (LIKELY(pval->type_ == JsonbType::T_Object)) {
-            if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (stream.get_hasEscapes()) {
-                stream.remove_escapes();
-            }
+        if (stream.get_type() == MEMBER_CODE) {
+            if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                return nullptr;
+            } else if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                if (stream.get_legLen() == 1 && *stream.get_legPtr() == 
WILDCARD) {
+                    continue;

Review Comment:
   Why continue? The original code return pval.



##########
be/src/util/jsonb_document.h:
##########
@@ -1200,52 +1225,125 @@ inline JsonbValue* JsonbValue::findPath(const char* 
key_path, unsigned int kp_le
         stream.clear_legPtr();
         stream.clear_legLen();
 
-        if (!JsonbPath::parsePath(&stream, pval)) {
+        if (!JsonbPath::parsePath(&stream)) {
+            is_invalid_json_path = stream.get_is_invalid_json_path();
             return nullptr;
         }
 
         if (stream.get_legLen() == 0) {
             return nullptr;
         }
 
-        if (LIKELY(pval->type_ == JsonbType::T_Object)) {
-            if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (stream.get_hasEscapes()) {
-                stream.remove_escapes();
-            }
+        if (stream.get_type() == MEMBER_CODE) {
+            if (LIKELY(pval->type_ == JsonbType::T_Array)) {

Review Comment:
   It should not be LIKELY. It can be merged to else branch.



##########
be/src/util/jsonb_document.h:
##########
@@ -273,35 +276,43 @@ class Stream {
         skip_whitespace();
     }
 
-    void clear_legPtr() { legPtr = nullptr; }
+    void clear_legPtr() { leg_ptr = nullptr; }

Review Comment:
   clear_leg_ptr



##########
be/src/util/jsonb_document.h:
##########
@@ -1200,52 +1225,125 @@ inline JsonbValue* JsonbValue::findPath(const char* 
key_path, unsigned int kp_le
         stream.clear_legPtr();
         stream.clear_legLen();
 
-        if (!JsonbPath::parsePath(&stream, pval)) {
+        if (!JsonbPath::parsePath(&stream)) {
+            is_invalid_json_path = stream.get_is_invalid_json_path();
             return nullptr;
         }
 
         if (stream.get_legLen() == 0) {
             return nullptr;
         }
 
-        if (LIKELY(pval->type_ == JsonbType::T_Object)) {
-            if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (stream.get_hasEscapes()) {
-                stream.remove_escapes();
-            }
+        if (stream.get_type() == MEMBER_CODE) {
+            if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                return nullptr;
+            } else if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                if (stream.get_legLen() == 1 && *stream.get_legPtr() == 
WILDCARD) {
+                    continue;
+                } else if (stream.get_hasEscapes()) {
+                    stream.remove_escapes();
+                }
 
-            pval = ((ObjectVal*)pval)->find(stream.get_legPtr(), 
stream.get_legLen(), handler);
+                pval = ((ObjectVal*)pval)->find(stream.get_legPtr(), 
stream.get_legLen(), handler);
 
-            if (!pval) return nullptr;
-        } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                if (!pval) return nullptr;
+            } else {
+                return nullptr;
+            }
+        } else if (stream.get_type() == ARRAY_CODE) {
             int index = 0;
-            std::string idx_string(stream.get_legPtr(), stream.get_legLen());
+            std::string_view idx_string(stream.get_legPtr(), 
stream.get_legLen());
 
             if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (std::string(stream.get_legPtr(), 4) == LAST) {
+                if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                    return nullptr;
+                } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                    continue;
+                } else {
+                    return nullptr;
+                }
+            } else if (*stream.get_legPtr() == MINUS && stream.get_legLen() > 
1) {

Review Comment:
   std::from_chars can handle negative numbers. So this branch can be merged.



##########
be/src/util/jsonb_document.h:
##########
@@ -273,35 +276,43 @@ class Stream {
         skip_whitespace();
     }
 
-    void clear_legPtr() { legPtr = nullptr; }
+    void clear_legPtr() { leg_ptr = nullptr; }
 
     void set_legPtr(char* ptr) {
         clear_legPtr();
-        legPtr = ptr;
+        leg_ptr = ptr;
     }
 
-    char* get_legPtr() { return legPtr; }
+    char* get_legPtr() { return leg_ptr; }

Review Comment:
   get_leg_ptr



##########
be/src/util/jsonb_document.h:
##########
@@ -273,35 +276,43 @@ class Stream {
         skip_whitespace();
     }
 
-    void clear_legPtr() { legPtr = nullptr; }
+    void clear_legPtr() { leg_ptr = nullptr; }
 
     void set_legPtr(char* ptr) {
         clear_legPtr();
-        legPtr = ptr;
+        leg_ptr = ptr;
     }
 
-    char* get_legPtr() { return legPtr; }
+    char* get_legPtr() { return leg_ptr; }
 
-    void clear_legLen() { legLen = 0; }
+    void clear_legLen() { leg_len = 0; }
 
-    void add_legLen() { legLen++; }
+    void add_legLen() { leg_len++; }
 
-    unsigned int get_legLen() { return legLen; }
+    unsigned int get_legLen() const { return leg_len; }
 
     void remove_escapes() {
         int new_len = 0;
-        for (int i = 0; i < legLen; i++) {
-            if (legPtr[i] != '\\') {
-                legPtr[new_len++] = legPtr[i];
+        for (int i = 0; i < leg_len; i++) {
+            if (leg_ptr[i] != '\\') {
+                leg_ptr[new_len++] = leg_ptr[i];
             }
         }
-        legPtr[new_len] = '\0';
-        legLen = new_len;
+        leg_ptr[new_len] = '\0';
+        leg_len = new_len;
     }
 
-    void set_hasEscapes(bool has) { hasEscapes = has; }
+    void set_hasEscapes(bool has) { has_escapes = has; }
 
-    bool get_hasEscapes() { return hasEscapes; }
+    bool get_hasEscapes() const { return has_escapes; }

Review Comment:
   get_has_escapes



##########
be/src/util/jsonb_document.h:
##########
@@ -1200,52 +1225,125 @@ inline JsonbValue* JsonbValue::findPath(const char* 
key_path, unsigned int kp_le
         stream.clear_legPtr();
         stream.clear_legLen();
 
-        if (!JsonbPath::parsePath(&stream, pval)) {
+        if (!JsonbPath::parsePath(&stream)) {
+            is_invalid_json_path = stream.get_is_invalid_json_path();
             return nullptr;
         }
 
         if (stream.get_legLen() == 0) {
             return nullptr;
         }
 
-        if (LIKELY(pval->type_ == JsonbType::T_Object)) {
-            if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (stream.get_hasEscapes()) {
-                stream.remove_escapes();
-            }
+        if (stream.get_type() == MEMBER_CODE) {
+            if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                return nullptr;
+            } else if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                if (stream.get_legLen() == 1 && *stream.get_legPtr() == 
WILDCARD) {
+                    continue;
+                } else if (stream.get_hasEscapes()) {
+                    stream.remove_escapes();
+                }
 
-            pval = ((ObjectVal*)pval)->find(stream.get_legPtr(), 
stream.get_legLen(), handler);
+                pval = ((ObjectVal*)pval)->find(stream.get_legPtr(), 
stream.get_legLen(), handler);
 
-            if (!pval) return nullptr;
-        } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                if (!pval) return nullptr;
+            } else {
+                return nullptr;
+            }
+        } else if (stream.get_type() == ARRAY_CODE) {
             int index = 0;
-            std::string idx_string(stream.get_legPtr(), stream.get_legLen());
+            std::string_view idx_string(stream.get_legPtr(), 
stream.get_legLen());
 
             if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (std::string(stream.get_legPtr(), 4) == LAST) {
+                if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                    return nullptr;

Review Comment:
   This branch can be merged to else.



##########
be/src/util/jsonb_document.h:
##########
@@ -1200,52 +1225,125 @@ inline JsonbValue* JsonbValue::findPath(const char* 
key_path, unsigned int kp_le
         stream.clear_legPtr();
         stream.clear_legLen();
 
-        if (!JsonbPath::parsePath(&stream, pval)) {
+        if (!JsonbPath::parsePath(&stream)) {
+            is_invalid_json_path = stream.get_is_invalid_json_path();
             return nullptr;
         }
 
         if (stream.get_legLen() == 0) {
             return nullptr;
         }
 
-        if (LIKELY(pval->type_ == JsonbType::T_Object)) {
-            if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (stream.get_hasEscapes()) {
-                stream.remove_escapes();
-            }
+        if (stream.get_type() == MEMBER_CODE) {
+            if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                return nullptr;
+            } else if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                if (stream.get_legLen() == 1 && *stream.get_legPtr() == 
WILDCARD) {
+                    continue;
+                } else if (stream.get_hasEscapes()) {
+                    stream.remove_escapes();
+                }
 
-            pval = ((ObjectVal*)pval)->find(stream.get_legPtr(), 
stream.get_legLen(), handler);
+                pval = ((ObjectVal*)pval)->find(stream.get_legPtr(), 
stream.get_legLen(), handler);
 
-            if (!pval) return nullptr;
-        } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                if (!pval) return nullptr;
+            } else {
+                return nullptr;
+            }
+        } else if (stream.get_type() == ARRAY_CODE) {
             int index = 0;
-            std::string idx_string(stream.get_legPtr(), stream.get_legLen());
+            std::string_view idx_string(stream.get_legPtr(), 
stream.get_legLen());
 
             if (stream.get_legLen() == 1 && *stream.get_legPtr() == WILDCARD) {
-                return pval;
-            } else if (std::string(stream.get_legPtr(), 4) == LAST) {
+                if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                    return nullptr;
+                } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                    continue;
+                } else {
+                    return nullptr;
+                }
+            } else if (*stream.get_legPtr() == MINUS && stream.get_legLen() > 
1) {
+                auto result = std::from_chars(idx_string.data(),
+                                              idx_string.data() + 
idx_string.size(), index);
+                if (result.ec != std::errc()) {
+                    is_invalid_json_path = true;
+                    return nullptr;
+                }
+
+                if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                    if (index == 0 || index == -1) {
+                        continue;
+                    } else {
+                        return nullptr;
+                    }
+                } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                    size_t num = ((ArrayVal*)pval)->numElem();
+                    if (-index > num) return nullptr;
+                    index = num + index;
+                } else {
+                    return nullptr;
+                }
+            } else if (std::equal(LAST, LAST + 4, stream.get_legPtr(),
+                                  [](char c1, char c2) {
+                                      return std::tolower(c1) == 
std::tolower(c2);
+                                  }) &&
+                       stream.get_legLen() >= 4) {
                 auto pos = idx_string.find(MINUS);
 
                 if (pos != std::string::npos) {
                     idx_string = idx_string.substr(pos + 1);
-                    size_t num = ((ArrayVal*)pval)->numElem();
-                    if (std::stoi(idx_string) > num) {
-                        return nullptr; //invalid json path
+
+                    auto result = std::from_chars(idx_string.data(),
+                                                  idx_string.data() + 
idx_string.size(), index);
+                    if (result.ec != std::errc()) {
+                        is_invalid_json_path = true;
+                        return nullptr;
+                    }
+
+                    if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                        if (index == 0) {
+                            continue;
+                        } else {
+                            return nullptr;
+                        }
+                    } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                        size_t num = ((ArrayVal*)pval)->numElem();
+                        if (index > num) return nullptr;
+                        index = num - 1 - index;
+                    } else {
+                        return nullptr;
                     }
-                    index = num - 1 - std::stoi(idx_string);
                 } else if (stream.get_legLen() == 4) {
-                    index = ((ArrayVal*)pval)->numElem() - 1;
+                    if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                        continue;
+                    } else if (LIKELY(pval->type_ == JsonbType::T_Array)) {
+                        index = ((ArrayVal*)pval)->numElem() - 1;
+                    } else {
+                        return nullptr;
+                    }
+
                 } else {
-                    return nullptr; //invalid json path
+                    is_invalid_json_path = true;
+                    return nullptr;
                 }
             } else {
-                std::string::size_type pos;
-                index = std::stoi(idx_string, &pos, 10);
-                if (pos != idx_string.size()) {
-                    return nullptr; //invalid json path
-                } else if (index >= ((ArrayVal*)pval)->numElem()) {
-                    return nullptr; //invalid json path
+                auto result = std::from_chars(idx_string.data(),
+                                              idx_string.data() + 
idx_string.size(), index);
+                if (result.ec != std::errc()) {
+                    is_invalid_json_path = true;
+                    return nullptr;
+                }
+
+                if (LIKELY(pval->type_ == JsonbType::T_Object)) {
+                    if (index == 0) {
+                        continue;

Review Comment:
   Is it valid to have index in an Object?



-- 
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]

Reply via email to