Skip to content

Commit

Permalink
Change the compress return type
Browse files Browse the repository at this point in the history
  • Loading branch information
lzyy2024 committed Jan 25, 2025
1 parent c5d6c70 commit 6ef40dd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
9 changes: 7 additions & 2 deletions be/src/util/block_compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,13 @@ class ZlibBlockCompression : public BlockCompressionCodec {
Slice s(*output);

auto zres = ::compress((Bytef*)s.data, &s.size, (Bytef*)input.data, input.size);
if (zres != Z_OK) {
return Status::InvalidArgument("Fail to do ZLib compress, error={}", zError(zres));
if (zres == Z_MEM_ERROR) {
throw Exception(Status::MemoryLimitExceeded(
fmt::format("ZLib compression failed due to memory allocation error. "
"Requested size: {}, Input size: {}, Max output size: {}, ",
max_len, input.size, output->size())));
} else if (zres != Z_OK) {
return Status::InternalError("Fail to do Zlib compress, error={}", zError(zres));
}
output->resize(s.size);
return Status::OK();
Expand Down
22 changes: 4 additions & 18 deletions be/src/vec/functions/function_compress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class FunctionCompress : public IFunction {
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>());
return std::make_shared<DataTypeString>();
}

Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
Expand All @@ -85,24 +85,15 @@ class FunctionCompress : public IFunction {
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();

faststring compressed_str;
Slice data;
for (size_t row = 0; row < input_rows_count; row++) {
null_map[row] = false;
size_t length = arg_offset[row] - arg_offset[row - 1];
data = Slice(arg_begin, 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);

if (!st.ok()) { // Failed to compress. The data should be a valid string or value.
col_offset[row] = col_offset[row - 1];
null_map[row] = true;
continue;
}

size_t idx = col_data.size();
if (!length) { // data is ''
col_data.resize(col_data.size() + 2);
Expand Down Expand Up @@ -133,8 +124,7 @@ class FunctionCompress : public IFunction {
col_offset[row] = col_offset[row - 1] + 10 + compressed_str.size() * 2;
}

block.replace_by_position(
result, ColumnNullable::create(std::move(result_column), std::move(null_column)));
block.replace_by_position(result, std::move(result_column));
return Status::OK();
}
};
Expand Down Expand Up @@ -184,10 +174,6 @@ class FunctionUncompress : public IFunction {
Slice data;
Slice uncompressed_slice;
for (size_t row = 0; row < input_rows_count; row++) {
std::function<bool(char)> check = [](char x) {
return ((x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F'));
};

null_map[row] = false;
data = Slice(arg_begin, arg_offset[row] - arg_offset[row - 1]);
size_t data_length = arg_offset[row] - arg_offset[row - 1];
Expand All @@ -201,7 +187,7 @@ class FunctionUncompress : public IFunction {
illegal = true;
}
for (size_t i = 2; i <= 9; i += 2) {
if (!check(data[i])) {
if (!std::isxdigit(data[i])) {
illegal = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.StringType;
Expand All @@ -34,7 +34,7 @@
* ScalarFunction 'compress'.
*/
public class Compress extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(StringType.INSTANCE).args(StringType.INSTANCE));
Expand Down

0 comments on commit 6ef40dd

Please sign in to comment.