Skip to content

Commit 2a5949c

Browse files
authored
[fix](function) json_object can not input null value (#34591) (#34700)
1 parent 998e070 commit 2a5949c

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

be/src/vec/functions/function_json.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "common/compiler_util.h" // IWYU pragma: keep
4343
#include "common/status.h"
4444
#include "exprs/json_functions.h"
45+
#include "util/simd/bits.h"
4546
#include "vec/io/io_helper.h"
4647
#ifdef __AVX2__
4748
#include "util/jsonb_parser_simd.h"
@@ -619,6 +620,7 @@ struct ExecuteReducer {
619620
struct FunctionJsonArrayImpl {
620621
static constexpr auto name = "json_array";
621622

623+
static constexpr auto must_not_null = false;
622624
template <int flag>
623625
using Reducer = ExecuteReducer<flag, FunctionJsonArrayImpl>;
624626

@@ -654,7 +656,7 @@ struct FunctionJsonArrayImpl {
654656

655657
struct FunctionJsonObjectImpl {
656658
static constexpr auto name = "json_object";
657-
659+
static constexpr auto must_not_null = true;
658660
template <int flag>
659661
using Reducer = ExecuteReducer<flag, FunctionJsonObjectImpl>;
660662

@@ -743,6 +745,9 @@ class FunctionJsonAlwaysNotNullable : public IFunction {
743745
data_columns.push_back(assert_cast<const ColumnString*>(column_ptrs.back().get()));
744746
}
745747
}
748+
if (SpecificImpl::must_not_null) {
749+
RETURN_IF_ERROR(check_keys_all_not_null(nullmaps, input_rows_count, arguments.size()));
750+
}
746751
execute(data_columns, *assert_cast<ColumnString*>(result_column.get()), input_rows_count,
747752
nullmaps);
748753
block.get_by_position(result).column = std::move(result_column);
@@ -774,6 +779,24 @@ class FunctionJsonAlwaysNotNullable : public IFunction {
774779
result_column.insert_data(buf.GetString(), buf.GetSize());
775780
}
776781
}
782+
783+
static Status check_keys_all_not_null(const std::vector<const ColumnUInt8*>& nullmaps, int size,
784+
size_t args) {
785+
for (int i = 0; i < args; i += 2) {
786+
const auto* null_map = nullmaps[i];
787+
if (null_map) {
788+
const bool not_null_num =
789+
simd::count_zero_num((int8_t*)null_map->get_data().data(), size);
790+
if (not_null_num < size) {
791+
return Status::InternalError(
792+
"function {} can not input null value , JSON documents may not contain "
793+
"NULL member names.",
794+
name);
795+
}
796+
}
797+
}
798+
return Status::OK();
799+
}
777800
};
778801

779802
struct FunctionJsonQuoteImpl {

regression-test/suites/query_p0/sql_functions/json_function/test_query_json_object.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@ suite("test_query_json_object", "query") {
4141
sql "insert into ${tableName} values(4,null,null,'test','2022-01-01 11:11:11');"
4242
sql "insert into ${tableName} values(5,1,true,'test','2022-01-01 11:11:11');"
4343
qt_sql1 "select json_object('k0',k0,'k1',k1,'k2',k2,'k3',k3,'k4',k4,'k5', null,'k6','k6') from ${tableName} order by k0;"
44+
test {
45+
sql """select k0,json_object(k3,123) from ${tableName} order by k0;"""
46+
exception "function json_object can not input null value , JSON documents may not contain NULL member names."
47+
}
4448
sql "DROP TABLE ${tableName};"
4549
}

0 commit comments

Comments
 (0)