Mellorsssss commented on code in PR #18037: URL: https://github.com/apache/doris/pull/18037#discussion_r1148391884
########## be/src/vec/functions/function_json.cpp: ########## @@ -754,11 +754,80 @@ class FunctionJsonValid : public IFunction { } }; +class FunctionJsonUnquote : public IFunction { +public: + static constexpr auto name = "json_unquote"; + static FunctionPtr create() { return std::make_shared<FunctionJsonUnquote>(); } + + String get_name() const override { return name; } + + size_t get_number_of_arguments() const override { return 1; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + return make_nullable(std::make_shared<DataTypeString>()); + } + + bool use_default_implementation_for_nulls() const override { return false; } + + bool use_default_implementation_for_constants() const override { return true; } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) override { + const IColumn& col_from = *(block.get_by_position(arguments[0]).column); + + auto null_map = ColumnUInt8::create(input_rows_count, 0); + + const ColumnString* col_from_string = check_and_get_column<ColumnString>(col_from); + if (auto* nullable = check_and_get_column<ColumnNullable>(col_from)) { + col_from_string = + check_and_get_column<ColumnString>(*nullable->get_nested_column_ptr()); + } + + if (!col_from_string) { + return Status::RuntimeError("Illegal column {} should be ColumnString", + col_from.get_name()); + } + + auto col_to = ColumnString::create(); + col_to->reserve(input_rows_count); + + // parser can be reused for performance + rapidjson::Document document; + for (size_t i = 0; i < input_rows_count; ++i) { + if (col_from.is_null_at(i)) { + null_map->get_data()[i] = 1; + col_to->insert_data(nullptr, 0); + continue; + } + + const auto& data = col_from_string->get_data_at(i); + if (data.size < 2 || data.data[0] != '"' || data.data[data.size - 1] != '"') { Review Comment: If the data.size == 0, then data.data[0] will not be evaluated since data.size < 2 will be true. I add a test case for the empty string ''. ########## be/src/vec/functions/function_json.cpp: ########## @@ -754,11 +754,80 @@ class FunctionJsonValid : public IFunction { } }; +class FunctionJsonUnquote : public IFunction { +public: + static constexpr auto name = "json_unquote"; + static FunctionPtr create() { return std::make_shared<FunctionJsonUnquote>(); } + + String get_name() const override { return name; } + + size_t get_number_of_arguments() const override { return 1; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + return make_nullable(std::make_shared<DataTypeString>()); + } + + bool use_default_implementation_for_nulls() const override { return false; } + + bool use_default_implementation_for_constants() const override { return true; } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) override { + const IColumn& col_from = *(block.get_by_position(arguments[0]).column); + + auto null_map = ColumnUInt8::create(input_rows_count, 0); + + const ColumnString* col_from_string = check_and_get_column<ColumnString>(col_from); + if (auto* nullable = check_and_get_column<ColumnNullable>(col_from)) { + col_from_string = + check_and_get_column<ColumnString>(*nullable->get_nested_column_ptr()); + } + + if (!col_from_string) { + return Status::RuntimeError("Illegal column {} should be ColumnString", + col_from.get_name()); + } + + auto col_to = ColumnString::create(); + col_to->reserve(input_rows_count); + + // parser can be reused for performance + rapidjson::Document document; + for (size_t i = 0; i < input_rows_count; ++i) { + if (col_from.is_null_at(i)) { + null_map->get_data()[i] = 1; + col_to->insert_data(nullptr, 0); + continue; + } + + const auto& data = col_from_string->get_data_at(i); Review Comment: Thanks xiaokang! I will rename it. -- 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