From dd7969adcdab9a5459c58e13508513d48af1f32e Mon Sep 17 00:00:00 2001 From: Rohit Satardekar Date: Wed, 13 Mar 2024 04:05:59 +0530 Subject: [PATCH] [bugfix]json_length() BE crash fix --- be/src/vec/functions/function_jsonb.cpp | 5 +++-- .../data/jsonb_p0/test_jsonb_load_and_function.out | 3 +++ .../suites/jsonb_p0/test_jsonb_load_and_function.groovy | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/be/src/vec/functions/function_jsonb.cpp b/be/src/vec/functions/function_jsonb.cpp index 4048e5c7799f026..f8dcb0036c37ebd 100644 --- a/be/src/vec/functions/function_jsonb.cpp +++ b/be/src/vec/functions/function_jsonb.cpp @@ -1080,7 +1080,8 @@ struct JsonbLengthUtil { MutableColumnPtr res = return_type->create_column(); for (size_t i = 0; i < input_rows_count; ++i) { - if (jsonb_data_column->is_null_at(i) || path_column->is_null_at(i)) { + if (jsonb_data_column->is_null_at(i) || path_column->is_null_at(i) + || (jsonb_data_column->get_data_at(i).size == 0)) { null_map->get_data()[i] = 1; res->insert_data(nullptr, 0); continue; @@ -1100,7 +1101,7 @@ struct JsonbLengthUtil { // 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()->findValue(path, nullptr); - if (UNLIKELY(jsonb_value.size == 0 || !value)) { + if (UNLIKELY(!value)) { null_map->get_data()[i] = 1; res->insert_data(nullptr, 0); continue; diff --git a/regression-test/data/jsonb_p0/test_jsonb_load_and_function.out b/regression-test/data/jsonb_p0/test_jsonb_load_and_function.out index 215f798df9b68ff..8a3b573cebdba2b 100644 --- a/regression-test/data/jsonb_p0/test_jsonb_load_and_function.out +++ b/regression-test/data/jsonb_p0/test_jsonb_load_and_function.out @@ -7494,6 +7494,9 @@ false -- !sql_json_length -- 2 +-- !sql_json_length -- +\N + -- !select_length -- 1 \N \N 2 null 1 diff --git a/regression-test/suites/jsonb_p0/test_jsonb_load_and_function.groovy b/regression-test/suites/jsonb_p0/test_jsonb_load_and_function.groovy index e6a9ca62dc94feb..bc63071f66a115b 100644 --- a/regression-test/suites/jsonb_p0/test_jsonb_load_and_function.groovy +++ b/regression-test/suites/jsonb_p0/test_jsonb_load_and_function.groovy @@ -552,6 +552,7 @@ suite("test_jsonb_load_and_function", "p0") { qt_sql_json_length """SELECT json_length('{"k1":"v31","k2":300}')""" qt_sql_json_length """SELECT json_length('{"a.b.c":{"k1.a1":"v31", "k2": 300},"a":"niu"}')""" qt_sql_json_length """SELECT json_length('{"a":{"k1.a1":"v31", "k2": 300},"b":"niu"}','\$.a')""" + qt_sql_json_length """SELECT json_length('abc','\$.k1')""" qt_select_length """SELECT id, j, json_length(j) FROM ${testTable} ORDER BY id""" qt_select_length """SELECT id, j, json_length(j, '\$[1]') FROM ${testTable} ORDER BY id"""