github-actions[bot] commented on code in PR #34289: URL: https://github.com/apache/doris/pull/34289#discussion_r1590772710
########## be/src/vec/functions/function_jsonb.cpp: ########## @@ -432,6 +431,157 @@ class FunctionJsonbExtract : public IFunction { } }; +class FunctionJsonbExtractPath : public IFunction { +public: + static constexpr auto name = "json_exists_path"; + static constexpr auto alias = "jsonb_exists_path"; + using ColumnType = ColumnVector<uint8_t>; + using Container = typename ColumnType::Container; + static FunctionPtr create() { return std::make_shared<FunctionJsonbExtractPath>(); } + String get_name() const override { return name; } + bool is_variadic() const override { return true; } + size_t get_number_of_arguments() const override { return 2; } + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + // it only needs to indicate existence and does not need to return nullable values. + return std::make_shared<DataTypeUInt8>(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) const override { + DCHECK_GE(arguments.size(), 2); + + ColumnPtr jsonb_data_column; + bool jsonb_data_const = false; + // prepare jsonb data column + std::tie(jsonb_data_column, jsonb_data_const) = + unpack_if_const(block.get_by_position(arguments[0]).column); + const auto& ldata = assert_cast<const ColumnString*>(jsonb_data_column.get())->get_chars(); + const auto& loffsets = + assert_cast<const ColumnString*>(jsonb_data_column.get())->get_offsets(); + + // prepare parse path column prepare + std::vector<const ColumnString*> jsonb_path_columns; + std::vector<bool> path_const(arguments.size() - 1); + for (int i = 0; i < arguments.size() - 1; ++i) { + ColumnPtr path_column; + bool is_const = false; + std::tie(path_column, is_const) = + unpack_if_const(block.get_by_position(arguments[i + 1]).column); + path_const[i] = is_const; + jsonb_path_columns.push_back(assert_cast<const ColumnString*>(path_column.get())); + } + auto res = ColumnType::create(); + + bool is_invalid_json_path = false; + + // not support other extract type for now (e.g. int, double, ...) + DCHECK_EQ(jsonb_path_columns.size(), 1); + const auto& rdata = jsonb_path_columns[0]->get_chars(); + const auto& roffsets = jsonb_path_columns[0]->get_offsets(); + if (jsonb_data_const) { + scalar_vector(context, jsonb_data_column->get_data_at(0), rdata, roffsets, + res->get_data(), is_invalid_json_path); + } else if (path_const[0]) { + vector_scalar(context, ldata, loffsets, jsonb_path_columns[0]->get_data_at(0), + res->get_data(), is_invalid_json_path); + } else { + vector_vector(context, ldata, loffsets, rdata, roffsets, res->get_data(), + is_invalid_json_path); + } + if (is_invalid_json_path) { + return Status::InvalidArgument( + "Json path error: {} for value: {}", + JsonbErrMsg::getErrMsg(JsonbErrType::E_INVALID_JSON_PATH), + std::string_view(reinterpret_cast<const char*>(rdata.data()), rdata.size())); + } + + block.get_by_position(result).column = std::move(res); + return Status::OK(); + } + +private: + static ALWAYS_INLINE void inner_loop_impl(size_t i, Container& res, const char* l_raw_str, + int l_str_size, JsonbPath& path) { + // doc is NOT necessary to be deleted since JsonbDocument will not allocate memory + JsonbDocument* doc = JsonbDocument::createDocument(l_raw_str, l_str_size); + if (UNLIKELY(!doc || !doc->getValue())) { Review Comment: warning: boolean expression can be simplified by DeMorgan's theorem [readability-simplify-boolean-expr] ```cpp if (UNLIKELY(!doc || !doc->getValue())) { ^ ``` <details> <summary>Additional context</summary> **be/src/common/compiler_util.h:35:** expanded from macro 'UNLIKELY' ```cpp #define UNLIKELY(expr) __builtin_expect(!!(expr), 0) ^ ``` </details> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org