zclllyybb commented on code in PR #47307: URL: https://github.com/apache/doris/pull/47307#discussion_r1930088671
########## be/src/vec/functions/function_compress.cpp: ########## @@ -0,0 +1,251 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +#include <glog/logging.h> + +#include <cctype> +#include <cstddef> +#include <cstring> +#include <functional> +#include <memory> +#include <string> +#include <utility> + +#include "common/status.h" +#include "util/block_compression.h" +#include "util/faststring.h" +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/columns/column.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/columns/columns_number.h" +#include "vec/common/assert_cast.h" +#include "vec/core/block.h" +#include "vec/core/column_numbers.h" +#include "vec/core/column_with_type_and_name.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_nullable.h" +#include "vec/data_types/data_type_number.h" +#include "vec/data_types/data_type_string.h" +#include "vec/functions/function.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris { +class FunctionContext; +} // namespace doris + +namespace doris::vectorized { + +class FunctionCompress : public IFunction { + string hex_itoc = "0123456789ABCDEF"; + +public: + static constexpr auto name = "compress"; + static FunctionPtr create() { return std::make_shared<FunctionCompress>(); } + + 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 std::make_shared<DataTypeString>(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + uint32_t result, size_t input_rows_count) const override { + // Get the compression algorithm object + BlockCompressionCodec* compression_codec; + RETURN_IF_ERROR(get_block_compression_codec(segment_v2::CompressionTypePB::ZLIB, + &compression_codec)); + + const auto& arg_column = + assert_cast<const ColumnString&>(*block.get_by_position(arguments[0]).column); + auto result_column = ColumnString::create(); + + auto& arg_data = arg_column.get_chars(); + auto& arg_offset = arg_column.get_offsets(); + const char* arg_begin = reinterpret_cast<const char*>(arg_data.data()); + + auto& col_data = result_column->get_chars(); + auto& col_offset = result_column->get_offsets(); + col_offset.resize(input_rows_count); + + faststring compressed_str; + Slice data; + + // When the original string is large, the result is roughly this value + size_t total = arg_offset[input_rows_count - 1]; + col_data.reserve(total / 1000); + + for (size_t row = 0; row < input_rows_count; row++) { + size_t length = arg_offset[row] - arg_offset[row - 1]; + data = Slice(arg_begin + arg_offset[row - 1], length); + + // Z_MEM_ERROR and Z_BUF_ERROR are already handled in compress, making sure st is always Z_OK + auto st = compression_codec->compress(data, &compressed_str); + + size_t idx = col_data.size(); + if (!length) { // data is '' + col_data.resize(col_data.size() + 2); + col_data[idx] = '0', col_data[idx + 1] = 'x'; + col_offset[row] = col_offset[row - 1] + 2; + continue; + } + + // first ten digits represent the length of the uncompressed string + col_data.resize(col_data.size() + 10); + col_data[idx] = '0', col_data[idx + 1] = 'x'; + for (size_t i = 0; i < 4; i++) { + unsigned char byte = (length >> (i * 8)) & 0xFF; + col_data[idx + 2 + i * 2] = hex_itoc[byte >> 4]; // higher four + col_data[idx + 3 + i * 2] = hex_itoc[byte & 0x0F]; + } + idx += 10; + + col_data.resize(col_data.size() + 2 * compressed_str.size()); + + unsigned char* src = compressed_str.data(); + for (size_t i = 0; i < compressed_str.size(); i++) { + col_data[idx] = hex_itoc[(*src >> 4) & 0x0F]; + col_data[idx + 1] = hex_itoc[(*src & 0x0F)]; + idx += 2; + src++; + } + col_offset[row] = col_offset[row - 1] + 10 + compressed_str.size() * 2; + } + + block.replace_by_position(result, std::move(result_column)); + return Status::OK(); + } +}; + +class FunctionUncompress : public IFunction { +public: + static constexpr auto name = "uncompress"; + static FunctionPtr create() { return std::make_shared<FunctionUncompress>(); } + + 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>()); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + uint32_t result, size_t input_rows_count) const override { + // Get the compression algorithm object + BlockCompressionCodec* compression_codec; + RETURN_IF_ERROR(get_block_compression_codec(segment_v2::CompressionTypePB::ZLIB, + &compression_codec)); + + const auto& arg_column = + assert_cast<const ColumnString&>(*block.get_by_position(arguments[0]).column); + + auto& arg_data = arg_column.get_chars(); + auto& arg_offset = arg_column.get_offsets(); + const char* arg_begin = reinterpret_cast<const char*>(arg_data.data()); + + auto result_column = ColumnString::create(); + auto& col_data = result_column->get_chars(); + auto& col_offset = result_column->get_offsets(); + col_offset.resize(input_rows_count); + + auto null_column = ColumnUInt8::create(input_rows_count); + auto& null_map = null_column->get_data(); + + std::string uncompressed; + Slice data; + Slice uncompressed_slice; + + size_t total = arg_offset[input_rows_count - 1]; + col_data.reserve(total * 1000); + + for (size_t row = 0; row < input_rows_count; row++) { + null_map[row] = false; + data = Slice(arg_begin + arg_offset[row - 1], arg_offset[row] - arg_offset[row - 1]); + size_t data_length = arg_offset[row] - arg_offset[row - 1]; + + bool illegal = false; + // The first ten digits are "0x" and length, followed by hexadecimal, each two digits is a byte + if (data_length < 10 || data_length % 2 == 1) { + illegal = true; + } else { + if (data[0] != '0' || data[1] != 'x') { + illegal = true; + } + for (size_t i = 2; i <= 9; i += 2) { Review Comment: why `+=2` here? should be `++`? ########## be/src/vec/functions/function_compress.cpp: ########## @@ -0,0 +1,251 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +#include <glog/logging.h> + +#include <cctype> +#include <cstddef> +#include <cstring> +#include <functional> +#include <memory> +#include <string> +#include <utility> + +#include "common/status.h" +#include "util/block_compression.h" +#include "util/faststring.h" +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/columns/column.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/columns/columns_number.h" +#include "vec/common/assert_cast.h" +#include "vec/core/block.h" +#include "vec/core/column_numbers.h" +#include "vec/core/column_with_type_and_name.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_nullable.h" +#include "vec/data_types/data_type_number.h" +#include "vec/data_types/data_type_string.h" +#include "vec/functions/function.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris { +class FunctionContext; +} // namespace doris + +namespace doris::vectorized { + +class FunctionCompress : public IFunction { + string hex_itoc = "0123456789ABCDEF"; + +public: + static constexpr auto name = "compress"; + static FunctionPtr create() { return std::make_shared<FunctionCompress>(); } + + 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 std::make_shared<DataTypeString>(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + uint32_t result, size_t input_rows_count) const override { + // Get the compression algorithm object + BlockCompressionCodec* compression_codec; + RETURN_IF_ERROR(get_block_compression_codec(segment_v2::CompressionTypePB::ZLIB, + &compression_codec)); + + const auto& arg_column = + assert_cast<const ColumnString&>(*block.get_by_position(arguments[0]).column); + auto result_column = ColumnString::create(); + + auto& arg_data = arg_column.get_chars(); + auto& arg_offset = arg_column.get_offsets(); + const char* arg_begin = reinterpret_cast<const char*>(arg_data.data()); + + auto& col_data = result_column->get_chars(); + auto& col_offset = result_column->get_offsets(); + col_offset.resize(input_rows_count); + + faststring compressed_str; + Slice data; + + // When the original string is large, the result is roughly this value + size_t total = arg_offset[input_rows_count - 1]; + col_data.reserve(total / 1000); + + for (size_t row = 0; row < input_rows_count; row++) { + size_t length = arg_offset[row] - arg_offset[row - 1]; + data = Slice(arg_begin + arg_offset[row - 1], length); + + // Z_MEM_ERROR and Z_BUF_ERROR are already handled in compress, making sure st is always Z_OK + auto st = compression_codec->compress(data, &compressed_str); + + size_t idx = col_data.size(); + if (!length) { // data is '' + col_data.resize(col_data.size() + 2); + col_data[idx] = '0', col_data[idx + 1] = 'x'; + col_offset[row] = col_offset[row - 1] + 2; + continue; + } + + // first ten digits represent the length of the uncompressed string + col_data.resize(col_data.size() + 10); Review Comment: L111 and L120's resize could be merged. ########## be/src/vec/functions/function_compress.cpp: ########## @@ -0,0 +1,251 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +#include <glog/logging.h> + +#include <cctype> +#include <cstddef> +#include <cstring> +#include <functional> +#include <memory> +#include <string> +#include <utility> + +#include "common/status.h" +#include "util/block_compression.h" +#include "util/faststring.h" +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/columns/column.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/columns/columns_number.h" +#include "vec/common/assert_cast.h" +#include "vec/core/block.h" +#include "vec/core/column_numbers.h" +#include "vec/core/column_with_type_and_name.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_nullable.h" +#include "vec/data_types/data_type_number.h" +#include "vec/data_types/data_type_string.h" +#include "vec/functions/function.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris { +class FunctionContext; +} // namespace doris + +namespace doris::vectorized { + +class FunctionCompress : public IFunction { + string hex_itoc = "0123456789ABCDEF"; + +public: + static constexpr auto name = "compress"; + static FunctionPtr create() { return std::make_shared<FunctionCompress>(); } + + 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 std::make_shared<DataTypeString>(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + uint32_t result, size_t input_rows_count) const override { + // Get the compression algorithm object + BlockCompressionCodec* compression_codec; + RETURN_IF_ERROR(get_block_compression_codec(segment_v2::CompressionTypePB::ZLIB, + &compression_codec)); + + const auto& arg_column = + assert_cast<const ColumnString&>(*block.get_by_position(arguments[0]).column); + auto result_column = ColumnString::create(); + + auto& arg_data = arg_column.get_chars(); + auto& arg_offset = arg_column.get_offsets(); + const char* arg_begin = reinterpret_cast<const char*>(arg_data.data()); + + auto& col_data = result_column->get_chars(); + auto& col_offset = result_column->get_offsets(); + col_offset.resize(input_rows_count); + + faststring compressed_str; + Slice data; + + // When the original string is large, the result is roughly this value + size_t total = arg_offset[input_rows_count - 1]; + col_data.reserve(total / 1000); + + for (size_t row = 0; row < input_rows_count; row++) { + size_t length = arg_offset[row] - arg_offset[row - 1]; + data = Slice(arg_begin + arg_offset[row - 1], length); + + // Z_MEM_ERROR and Z_BUF_ERROR are already handled in compress, making sure st is always Z_OK + auto st = compression_codec->compress(data, &compressed_str); + + size_t idx = col_data.size(); + if (!length) { // data is '' + col_data.resize(col_data.size() + 2); + col_data[idx] = '0', col_data[idx + 1] = 'x'; + col_offset[row] = col_offset[row - 1] + 2; + continue; + } + + // first ten digits represent the length of the uncompressed string + col_data.resize(col_data.size() + 10); + col_data[idx] = '0', col_data[idx + 1] = 'x'; + for (size_t i = 0; i < 4; i++) { + unsigned char byte = (length >> (i * 8)) & 0xFF; + col_data[idx + 2 + i * 2] = hex_itoc[byte >> 4]; // higher four + col_data[idx + 3 + i * 2] = hex_itoc[byte & 0x0F]; + } + idx += 10; + + col_data.resize(col_data.size() + 2 * compressed_str.size()); + + unsigned char* src = compressed_str.data(); + for (size_t i = 0; i < compressed_str.size(); i++) { + col_data[idx] = hex_itoc[(*src >> 4) & 0x0F]; + col_data[idx + 1] = hex_itoc[(*src & 0x0F)]; + idx += 2; + src++; + } + col_offset[row] = col_offset[row - 1] + 10 + compressed_str.size() * 2; + } + + block.replace_by_position(result, std::move(result_column)); + return Status::OK(); + } +}; + +class FunctionUncompress : public IFunction { +public: + static constexpr auto name = "uncompress"; + static FunctionPtr create() { return std::make_shared<FunctionUncompress>(); } + + 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>()); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + uint32_t result, size_t input_rows_count) const override { + // Get the compression algorithm object + BlockCompressionCodec* compression_codec; + RETURN_IF_ERROR(get_block_compression_codec(segment_v2::CompressionTypePB::ZLIB, + &compression_codec)); + + const auto& arg_column = + assert_cast<const ColumnString&>(*block.get_by_position(arguments[0]).column); + + auto& arg_data = arg_column.get_chars(); + auto& arg_offset = arg_column.get_offsets(); + const char* arg_begin = reinterpret_cast<const char*>(arg_data.data()); + + auto result_column = ColumnString::create(); + auto& col_data = result_column->get_chars(); + auto& col_offset = result_column->get_offsets(); + col_offset.resize(input_rows_count); + + auto null_column = ColumnUInt8::create(input_rows_count); + auto& null_map = null_column->get_data(); + + std::string uncompressed; + Slice data; + Slice uncompressed_slice; + + size_t total = arg_offset[input_rows_count - 1]; + col_data.reserve(total * 1000); + + for (size_t row = 0; row < input_rows_count; row++) { + null_map[row] = false; + data = Slice(arg_begin + arg_offset[row - 1], arg_offset[row] - arg_offset[row - 1]); + size_t data_length = arg_offset[row] - arg_offset[row - 1]; + + bool illegal = false; + // The first ten digits are "0x" and length, followed by hexadecimal, each two digits is a byte + if (data_length < 10 || data_length % 2 == 1) { + illegal = true; + } else { + if (data[0] != '0' || data[1] != 'x') { + illegal = true; + } + for (size_t i = 2; i <= 9; i += 2) { + if (!std::isxdigit(data[i])) { + illegal = true; + } + } + } + + if (illegal) { // The top ten don't fit the rules + col_offset[row] = col_offset[row - 1]; + // if data == "0x" return '', null_map[row] = false + if (!(data.size == 2 && data[0] == '0' && data[1] == 'x')) { + null_map[row] = true; + } + continue; + } + + unsigned int length = 0; + for (size_t i = 2; i <= 9; i += 2) { + unsigned char byte; + std::from_chars(data.data + i, data.data + i + 2, byte, 16); + length += (byte << (8 * (i / 2 - 1))); //Little Endian : 0x01000000 -> 1 + } + + uncompressed.resize(length); + uncompressed_slice = Slice(uncompressed); Review Comment: remove the `uncompressed`, just resize `col_data` to add its length with `length`. so just point `uncompressed_slice` into `col_data`. then we can do decompress inplace and no need to do heavily `memcpy`(at L237) ########## be/test/vec/function/function_string_test.cpp: ########## @@ -3339,8 +3339,59 @@ TEST(function_string_test, function_rpad_test) { {{Null(), std::int32_t(0), std::string("TVl0ZXN0U1RS")}, Null()}, {{Null(), std::int32_t(0), Null()}, Null()}, }; - check_function_all_arg_comb<DataTypeString, true>(func_name, input_types, data_set); } +TEST(function_string_test, function_compress_uncompress_test) { + { + std::string func_name = "compress"; + BaseInputTypeSet input_types = {TypeIndex::String}; + + // Compress multiple different strings + DataSet data_set = { + {{Null()}, Null()}, + // Example 1: Compress a regular string + {{std::string("Hello, world!")}, + std::string("0x0D000000789CF348CDC9C9D75128CF2FCA49510400205E048A")}, + // Example 2: Compress an empty string + {{std::string("")}, std::string("0x")}, + // Example 3: Compress a string with special characters + {{std::string("String with special characters! @#$%^&*()")}, + std::string("0x29000000789C0B2E29CACC4B5728CF2CC950282E484DCE4CCC5148CE482C4A4C2E4" + "92D2A5654705056518D53D3D2D004003C2A0D81")}, + // Example 4: Compress a string with leading and trailing spaces + {{std::string(" This is a string with leading and trailing spaces ")}, + std::string("0x37000000789C15C7510A00101045D1ADDC45D9C00B3125C94CD93EEAFC1C2075731" + "EE1B16D368E456754951FCD426CD9F8F1A55C1DB8054B12C9")}}; + + check_function_all_arg_comb<DataTypeString, true>(func_name, input_types, data_set); + } + + { + std::string func_name = "uncompress"; Review Comment: we should add some case of uncompressing a string which is making from a valid compressed string with minor modifications. like, for valid '0x1204', try to uncompress '0x12F4' which in invalid to get the `NULL` result. -- 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