From 4a117800caafec402df061a7cf95a2edd733cee9 Mon Sep 17 00:00:00 2001 From: lihangyu <15605149486@163.com> Date: Tue, 18 Jun 2024 10:20:45 +0800 Subject: [PATCH] [Bug](Function) fix json contains with empty value (#36320) (#36418) --- be/src/vec/functions/function_jsonb.cpp | 7 ++++++- .../sql_functions/json_functions/test_json_function.out | 9 +++++++++ .../json_functions/test_json_function.groovy | 4 +++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/be/src/vec/functions/function_jsonb.cpp b/be/src/vec/functions/function_jsonb.cpp index 5618612d27..425145d240 100644 --- a/be/src/vec/functions/function_jsonb.cpp +++ b/be/src/vec/functions/function_jsonb.cpp @@ -1362,6 +1362,11 @@ struct JsonbContainsUtil { auto jsonb_value1 = jsonb_data1_column->get_data_at(i); auto jsonb_value2 = jsonb_data2_column->get_data_at(i); + if (jsonb_value1.size == 0 || jsonb_value2.size == 0) { + null_map->get_data()[i] = 1; + res->insert_data(nullptr, 0); + continue; + } // doc is NOT necessary to be deleted since JsonbDocument will not allocate memory JsonbDocument* doc1 = JsonbDocument::createDocument(jsonb_value1.data, jsonb_value1.size); @@ -1370,7 +1375,7 @@ struct JsonbContainsUtil { JsonbValue* value1 = doc1->getValue()->findValue(path, nullptr); JsonbValue* value2 = doc2->getValue(); - if (UNLIKELY(jsonb_value1.size == 0 || jsonb_value2.size == 0 || !value1 || !value2)) { + if (!value1 || !value2) { null_map->get_data()[i] = 1; res->insert_data(nullptr, 0); continue; diff --git a/regression-test/data/query_p0/sql_functions/json_functions/test_json_function.out b/regression-test/data/query_p0/sql_functions/json_functions/test_json_function.out index 2aa6c3e7d3..bd14394d37 100644 --- a/regression-test/data/query_p0/sql_functions/json_functions/test_json_function.out +++ b/regression-test/data/query_p0/sql_functions/json_functions/test_json_function.out @@ -146,3 +146,12 @@ true -- !sql -- true +-- !sql -- +\N + +-- !sql -- +\N + +-- !sql -- +\N + diff --git a/regression-test/suites/query_p0/sql_functions/json_functions/test_json_function.groovy b/regression-test/suites/query_p0/sql_functions/json_functions/test_json_function.groovy index 0bbbdb9c01..4bd88bf131 100644 --- a/regression-test/suites/query_p0/sql_functions/json_functions/test_json_function.groovy +++ b/regression-test/suites/query_p0/sql_functions/json_functions/test_json_function.groovy @@ -74,5 +74,7 @@ suite("test_json_function", "arrow_flight_sql") { qt_sql "SELECT JSON_CONTAINS('{\"name\": \"John\", \"age\": 30, \"city\": \"New York\", \"hobbies\": [\"reading\", \"travelling\"]}', '{\"age\": 31, \"hobbies\": [\"reading\"]}', '\$');" qt_sql "SELECT JSON_CONTAINS('{\"name\": \"John\", \"age\": 30, \"projects\": [{\"name\": \"Project A\", \"year\": 2020}, {\"name\": \"Project B\", \"year\": 2021}]}', '{\"projects\": [{\"name\": \"Project A\"}]}', '\$');" qt_sql "SELECT JSON_CONTAINS('{\"name\": \"John\", \"age\": 30, \"address\": {\"city\": \"New York\", \"country\": \"USA\"}}', '{\"address\": {\"city\": \"New York\"}}', '\$');" - + qt_sql """SELECT JSON_CONTAINS('','1','\$.a')""" + qt_sql """SELECT JSON_CONTAINS('""','1','\$.a')""" + qt_sql """SELECT JSON_CONTAINS("",'1','\$.a')""" }