This is an automated email from the ASF dual-hosted git repository.

dataroaring pushed a commit to branch branch-3.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 37678cbd9a7 branch-3.0: [fix](json) Json Path supports the $.[0] 
format #50427 (#50448)
37678cbd9a7 is described below

commit 37678cbd9a71a1fe409434148700092b2a54bebb
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Apr 29 14:59:42 2025 +0800

    branch-3.0: [fix](json) Json Path supports the $.[0] format #50427 (#50448)
    
    Cherry-picked from #50427
    
    Co-authored-by: Sun Chenyang <suncheny...@selectdb.com>
---
 be/src/util/jsonb_document.h                       |  20 +++++++++++++++-----
 .../data/json_p0/test_json_load_and_function.out   | Bin 261467 -> 261689 bytes
 .../json_p0/test_json_load_and_function.groovy     |  20 ++++++++++++++++++++
 3 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/be/src/util/jsonb_document.h b/be/src/util/jsonb_document.h
index ed778843f0d..804221cc014 100644
--- a/be/src/util/jsonb_document.h
+++ b/be/src/util/jsonb_document.h
@@ -1461,10 +1461,23 @@ inline JsonbValue* JsonbValue::findValue(JsonbPath& 
path, hDictFind handler) {
 }
 
 inline bool JsonbPath::parsePath(Stream* stream, JsonbPath* path) {
+    // $[0]
     if (stream->peek() == BEGIN_ARRAY) {
         return parse_array(stream, path);
-    } else if (stream->peek() == BEGIN_MEMBER) {
-        return parse_member(stream, path);
+    }
+    // $.a or $.[0]
+    else if (stream->peek() == BEGIN_MEMBER) {
+        // advance past the .
+        stream->skip(1);
+
+        // $.[0]
+        if (stream->peek() == BEGIN_ARRAY) {
+            return parse_array(stream, path);
+        }
+        // $.a
+        else {
+            return parse_member(stream, path);
+        }
     } else {
         return false; //invalid json path
     }
@@ -1546,9 +1559,6 @@ inline bool JsonbPath::parse_array(Stream* stream, 
JsonbPath* path) {
 }
 
 inline bool JsonbPath::parse_member(Stream* stream, JsonbPath* path) {
-    // advance past the .
-    assert(stream->peek() == BEGIN_MEMBER);
-    stream->skip(1);
     if (stream->exhausted()) {
         return false;
     }
diff --git a/regression-test/data/json_p0/test_json_load_and_function.out 
b/regression-test/data/json_p0/test_json_load_and_function.out
index f28d78bf1a7..aa8c6805696 100644
Binary files a/regression-test/data/json_p0/test_json_load_and_function.out and 
b/regression-test/data/json_p0/test_json_load_and_function.out differ
diff --git a/regression-test/suites/json_p0/test_json_load_and_function.groovy 
b/regression-test/suites/json_p0/test_json_load_and_function.groovy
index 6875a2acfdb..fb94eb4ce56 100644
--- a/regression-test/suites/json_p0/test_json_load_and_function.groovy
+++ b/regression-test/suites/json_p0/test_json_load_and_function.groovy
@@ -726,4 +726,24 @@ suite("test_json_load_and_function", "p0") {
     qt_sql_json_parse """SELECT/*+SET_VAR(enable_fold_constant_by_be=false)*/ 
json_parse('{"":1, "":"v1"}')"""
     qt_sql_json_parse """SELECT/*+SET_VAR(enable_fold_constant_by_be=false)*/ 
json_parse('{"":1, "ab":"v1", "":"v1", "": 2}')"""
 
+    // test $.[0]
+    qt_sql """select json_extract_string('["aaa", "bbb", "c"]', '\$.[0]');"""
+    qt_sql """select json_extract_string('["aaa", "bbb", "c"]', '\$[0]');"""
+    qt_sql """select json_extract_string('["aaa", "bbb", "c"]', '\$.[1]');"""
+    qt_sql """select json_extract_string('["aaa", "bbb", "c"]', '\$[1]');"""
+
+    qt_sql """select json_extract_string('{"a" : 123}', '\$.[0]');"""
+    qt_sql """select json_extract_string('{"a" : 123}', '\$[0]');"""
+    qt_sql """select json_extract_string('{"a" : 123}', '\$.[1]');"""
+    qt_sql """select json_extract_string('{"a" : 123}', '\$[1]');"""
+
+
+    qt_sql """select json_extract_int('[1, 2, 3]', '\$.[0]');"""
+    qt_sql """select json_extract_int('[1, 2, 3]', '\$[0]');"""
+    qt_sql """select json_extract_int('[1, 2, 3]', '\$.[1]');"""
+    qt_sql """select json_extract_int('[1, 2, 3]', '\$[1]');"""
+
+    qt_sql """select jsonb_extract('[1, 2, 3]', '\$.[1]');"""
+    qt_sql """select jsonb_extract('[1, 2, 3]', '\$[1]');"""
+
 }


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

Reply via email to