Skip to content

Commit

Permalink
[fix](function) json_object can not input null value (#34591) (#34700)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mryange authored May 11, 2024
1 parent 998e070 commit 2a5949c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 24 additions & 1 deletion be/src/vec/functions/function_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "common/compiler_util.h" // IWYU pragma: keep
#include "common/status.h"
#include "exprs/json_functions.h"
#include "util/simd/bits.h"
#include "vec/io/io_helper.h"
#ifdef __AVX2__
#include "util/jsonb_parser_simd.h"
Expand Down Expand Up @@ -619,6 +620,7 @@ struct ExecuteReducer {
struct FunctionJsonArrayImpl {
static constexpr auto name = "json_array";

static constexpr auto must_not_null = false;
template <int flag>
using Reducer = ExecuteReducer<flag, FunctionJsonArrayImpl>;

Expand Down Expand Up @@ -654,7 +656,7 @@ struct FunctionJsonArrayImpl {

struct FunctionJsonObjectImpl {
static constexpr auto name = "json_object";

static constexpr auto must_not_null = true;
template <int flag>
using Reducer = ExecuteReducer<flag, FunctionJsonObjectImpl>;

Expand Down Expand Up @@ -743,6 +745,9 @@ class FunctionJsonAlwaysNotNullable : public IFunction {
data_columns.push_back(assert_cast<const ColumnString*>(column_ptrs.back().get()));
}
}
if (SpecificImpl::must_not_null) {
RETURN_IF_ERROR(check_keys_all_not_null(nullmaps, input_rows_count, arguments.size()));
}
execute(data_columns, *assert_cast<ColumnString*>(result_column.get()), input_rows_count,
nullmaps);
block.get_by_position(result).column = std::move(result_column);
Expand Down Expand Up @@ -774,6 +779,24 @@ class FunctionJsonAlwaysNotNullable : public IFunction {
result_column.insert_data(buf.GetString(), buf.GetSize());
}
}

static Status check_keys_all_not_null(const std::vector<const ColumnUInt8*>& nullmaps, int size,
size_t args) {
for (int i = 0; i < args; i += 2) {
const auto* null_map = nullmaps[i];
if (null_map) {
const bool not_null_num =
simd::count_zero_num((int8_t*)null_map->get_data().data(), size);
if (not_null_num < size) {
return Status::InternalError(
"function {} can not input null value , JSON documents may not contain "
"NULL member names.",
name);
}
}
}
return Status::OK();
}
};

struct FunctionJsonQuoteImpl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ suite("test_query_json_object", "query") {
sql "insert into ${tableName} values(4,null,null,'test','2022-01-01 11:11:11');"
sql "insert into ${tableName} values(5,1,true,'test','2022-01-01 11:11:11');"
qt_sql1 "select json_object('k0',k0,'k1',k1,'k2',k2,'k3',k3,'k4',k4,'k5', null,'k6','k6') from ${tableName} order by k0;"
test {
sql """select k0,json_object(k3,123) from ${tableName} order by k0;"""
exception "function json_object can not input null value , JSON documents may not contain NULL member names."
}
sql "DROP TABLE ${tableName};"
}

0 comments on commit 2a5949c

Please sign in to comment.