Coverage Report

Created: 2026-03-12 17:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/decompressor.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/decompressor.h"
19
20
#include <strings.h>
21
22
#include <memory>
23
#include <ostream>
24
25
#include "common/cast_set.h"
26
#include "common/logging.h"
27
#include "common/status.h"
28
#include "exec/common/endian.h"
29
30
namespace doris {
31
#include "common/compile_check_begin.h"
32
33
Status Decompressor::create_decompressor(CompressType type,
34
6.63k
                                         std::unique_ptr<Decompressor>* decompressor) {
35
6.63k
    switch (type) {
36
2.95k
    case CompressType::UNCOMPRESSED:
37
2.95k
        decompressor->reset(nullptr);
38
2.95k
        break;
39
736
    case CompressType::GZIP:
40
736
        decompressor->reset(new GzipDecompressor(false));
41
736
        break;
42
32
    case CompressType::DEFLATE:
43
32
        decompressor->reset(new GzipDecompressor(true));
44
32
        break;
45
686
    case CompressType::BZIP2:
46
686
        decompressor->reset(new Bzip2Decompressor());
47
686
        break;
48
646
    case CompressType::ZSTD:
49
646
        decompressor->reset(new ZstdDecompressor());
50
646
        break;
51
24
    case CompressType::LZ4FRAME:
52
24
        decompressor->reset(new Lz4FrameDecompressor());
53
24
        break;
54
750
    case CompressType::LZ4BLOCK:
55
750
        decompressor->reset(new Lz4BlockDecompressor());
56
750
        break;
57
770
    case CompressType::SNAPPYBLOCK:
58
770
        decompressor->reset(new SnappyBlockDecompressor());
59
770
        break;
60
34
    case CompressType::LZOP:
61
34
        decompressor->reset(new LzopDecompressor());
62
34
        break;
63
0
    default:
64
0
        return Status::InternalError("Unknown compress type: {}", type);
65
6.63k
    }
66
67
6.63k
    Status st = Status::OK();
68
6.63k
    if (*decompressor != nullptr) {
69
3.67k
        st = (*decompressor)->init();
70
3.67k
    }
71
72
6.63k
    return st;
73
6.63k
}
74
75
Status Decompressor::create_decompressor(TFileCompressType::type type,
76
6.63k
                                         std::unique_ptr<Decompressor>* decompressor) {
77
6.63k
    CompressType compress_type;
78
6.63k
    switch (type) {
79
2.95k
    case TFileCompressType::PLAIN:
80
2.95k
    case TFileCompressType::UNKNOWN:
81
2.95k
        compress_type = CompressType::UNCOMPRESSED;
82
2.95k
        break;
83
736
    case TFileCompressType::GZ:
84
736
        compress_type = CompressType::GZIP;
85
736
        break;
86
0
    case TFileCompressType::LZO:
87
34
    case TFileCompressType::LZOP:
88
34
        compress_type = CompressType::LZOP;
89
34
        break;
90
686
    case TFileCompressType::BZ2:
91
686
        compress_type = CompressType::BZIP2;
92
686
        break;
93
646
    case TFileCompressType::ZSTD:
94
646
        compress_type = CompressType::ZSTD;
95
646
        break;
96
24
    case TFileCompressType::LZ4FRAME:
97
24
        compress_type = CompressType::LZ4FRAME;
98
24
        break;
99
748
    case TFileCompressType::LZ4BLOCK:
100
748
        compress_type = CompressType::LZ4BLOCK;
101
748
        break;
102
32
    case TFileCompressType::DEFLATE:
103
32
        compress_type = CompressType::DEFLATE;
104
32
        break;
105
770
    case TFileCompressType::SNAPPYBLOCK:
106
770
        compress_type = CompressType::SNAPPYBLOCK;
107
770
        break;
108
0
    default:
109
0
        return Status::InternalError<false>("unknown compress type: {}", type);
110
6.63k
    }
111
6.63k
    RETURN_IF_ERROR(Decompressor::create_decompressor(compress_type, decompressor));
112
113
6.63k
    return Status::OK();
114
6.63k
}
115
116
Status Decompressor::create_decompressor(TFileFormatType::type type,
117
0
                                         std::unique_ptr<Decompressor>* decompressor) {
118
0
    CompressType compress_type;
119
0
    switch (type) {
120
0
    case TFileFormatType::FORMAT_PROTO:
121
0
        [[fallthrough]];
122
0
    case TFileFormatType::FORMAT_CSV_PLAIN:
123
0
        compress_type = CompressType::UNCOMPRESSED;
124
0
        break;
125
0
    case TFileFormatType::FORMAT_CSV_GZ:
126
0
        compress_type = CompressType::GZIP;
127
0
        break;
128
0
    case TFileFormatType::FORMAT_CSV_BZ2:
129
0
        compress_type = CompressType::BZIP2;
130
0
        break;
131
0
    case TFileFormatType::FORMAT_CSV_LZ4FRAME:
132
0
        compress_type = CompressType::LZ4FRAME;
133
0
        break;
134
0
    case TFileFormatType::FORMAT_CSV_LZ4BLOCK:
135
0
        compress_type = CompressType::LZ4BLOCK;
136
0
        break;
137
0
    case TFileFormatType::FORMAT_CSV_LZOP:
138
0
        compress_type = CompressType::LZOP;
139
0
        break;
140
0
    case TFileFormatType::FORMAT_CSV_DEFLATE:
141
0
        compress_type = CompressType::DEFLATE;
142
0
        break;
143
0
    case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK:
144
0
        compress_type = CompressType::SNAPPYBLOCK;
145
0
        break;
146
0
    default:
147
0
        return Status::InternalError<false>("unknown compress type: {}", type);
148
0
    }
149
0
    RETURN_IF_ERROR(Decompressor::create_decompressor(compress_type, decompressor));
150
151
0
    return Status::OK();
152
0
}
153
154
0
uint32_t Decompressor::_read_int32(uint8_t* buf) {
155
0
    return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
156
0
}
157
158
0
std::string Decompressor::debug_info() {
159
0
    return "Decompressor";
160
0
}
161
162
// Gzip
163
GzipDecompressor::GzipDecompressor(bool is_deflate)
164
768
        : Decompressor(is_deflate ? CompressType::DEFLATE : CompressType::GZIP),
165
768
          _is_deflate(is_deflate) {}
166
167
768
GzipDecompressor::~GzipDecompressor() {
168
768
    (void)inflateEnd(&_z_strm);
169
768
}
170
171
768
Status GzipDecompressor::init() {
172
768
    _z_strm = {};
173
768
    _z_strm.zalloc = Z_NULL;
174
768
    _z_strm.zfree = Z_NULL;
175
768
    _z_strm.opaque = Z_NULL;
176
177
768
    int window_bits = _is_deflate ? WINDOW_BITS : (WINDOW_BITS | DETECT_CODEC);
178
768
    int ret = inflateInit2(&_z_strm, window_bits);
179
768
    if (ret < 0) {
180
0
        return Status::InternalError("Failed to do gzip decompress. status code: {}", ret);
181
0
    }
182
183
768
    return Status::OK();
184
768
}
185
186
Status GzipDecompressor::decompress(uint8_t* input, uint32_t input_len, size_t* input_bytes_read,
187
                                    uint8_t* output, uint32_t output_max_len,
188
                                    size_t* decompressed_len, bool* stream_end,
189
1.05k
                                    size_t* more_input_bytes, size_t* more_output_bytes) {
190
    // 1. set input and output
191
1.05k
    _z_strm.next_in = input;
192
1.05k
    _z_strm.avail_in = input_len;
193
1.05k
    _z_strm.next_out = output;
194
1.05k
    _z_strm.avail_out = output_max_len;
195
196
3.45k
    while (_z_strm.avail_out > 0 && _z_strm.avail_in > 0) {
197
2.39k
        *stream_end = false;
198
        // inflate() performs one or both of the following actions:
199
        //   Decompress more input starting at next_in and update next_in and avail_in
200
        //       accordingly.
201
        //   Provide more output starting at next_out and update next_out and avail_out
202
        //       accordingly.
203
        // inflate() returns Z_OK if some progress has been made (more input processed
204
        // or more output produced)
205
206
2.39k
        int ret = inflate(&_z_strm, Z_NO_FLUSH);
207
2.39k
        *input_bytes_read = input_len - _z_strm.avail_in;
208
2.39k
        *decompressed_len = output_max_len - _z_strm.avail_out;
209
210
2.39k
        VLOG_TRACE << "gzip dec ret: " << ret << " input_bytes_read: " << *input_bytes_read
211
0
                   << " decompressed_len: " << *decompressed_len;
212
213
2.39k
        if (ret == Z_BUF_ERROR) {
214
            // Z_BUF_ERROR indicates that inflate() could not consume more input or
215
            // produce more output. inflate() can be called again with more output space
216
            // or more available input
217
            // ATTN: even if ret == Z_OK, decompressed_len may also be zero
218
0
            return Status::OK();
219
2.39k
        } else if (ret == Z_STREAM_END) {
220
2.09k
            *stream_end = true;
221
            // reset _z_strm to continue decoding a subsequent gzip stream
222
2.09k
            ret = inflateReset(&_z_strm);
223
2.09k
            if (ret != Z_OK) {
224
0
                if (_is_deflate) {
225
0
                    return Status::InternalError("Failed to do deflate decompress. return code: {}",
226
0
                                                 ret);
227
0
                } else {
228
0
                    return Status::InternalError("Failed to do gzip decompress. return code: {}",
229
0
                                                 ret);
230
0
                }
231
0
            }
232
2.09k
        } else if (ret != Z_OK) {
233
0
            if (_is_deflate) {
234
0
                return Status::InternalError("Failed to do deflate decompress. return code: {}",
235
0
                                             ret);
236
0
            } else {
237
0
                return Status::InternalError("Failed to do gzip decompress. return code: {}", ret);
238
0
            }
239
302
        } else {
240
            // here ret must be Z_OK.
241
            // we continue if avail_out and avail_in > 0.
242
            // this means 'inflate' is not done yet.
243
302
        }
244
2.39k
    }
245
246
1.05k
    return Status::OK();
247
1.05k
}
248
249
0
std::string GzipDecompressor::debug_info() {
250
0
    std::stringstream ss;
251
0
    ss << "GzipDecompressor."
252
0
       << " is_deflate: " << _is_deflate;
253
0
    return ss.str();
254
0
}
255
256
// Bzip2
257
686
Bzip2Decompressor::~Bzip2Decompressor() {
258
686
    BZ2_bzDecompressEnd(&_bz_strm);
259
686
}
260
261
686
Status Bzip2Decompressor::init() {
262
686
    bzero(&_bz_strm, sizeof(_bz_strm));
263
686
    int ret = BZ2_bzDecompressInit(&_bz_strm, 0, 0);
264
686
    if (ret != BZ_OK) {
265
0
        return Status::InternalError("Failed to do bz2 decompress. status code: {}", ret);
266
0
    }
267
268
686
    return Status::OK();
269
686
}
270
271
Status Bzip2Decompressor::decompress(uint8_t* input, uint32_t input_len, size_t* input_bytes_read,
272
                                     uint8_t* output, uint32_t output_max_len,
273
                                     size_t* decompressed_len, bool* stream_end,
274
742
                                     size_t* more_input_bytes, size_t* more_output_bytes) {
275
    // 1. set input and output
276
742
    _bz_strm.next_in = reinterpret_cast<char*>(input);
277
742
    _bz_strm.avail_in = input_len;
278
742
    _bz_strm.next_out = reinterpret_cast<char*>(output);
279
742
    _bz_strm.avail_out = output_max_len;
280
281
2.80k
    while (_bz_strm.avail_out > 0 && _bz_strm.avail_in > 0) {
282
2.05k
        *stream_end = false;
283
        // decompress
284
2.05k
        int ret = BZ2_bzDecompress(&_bz_strm);
285
2.05k
        *input_bytes_read = input_len - _bz_strm.avail_in;
286
2.05k
        *decompressed_len = output_max_len - _bz_strm.avail_out;
287
288
2.05k
        if (ret == BZ_DATA_ERROR || ret == BZ_DATA_ERROR_MAGIC) {
289
0
            LOG(INFO) << "input_bytes_read: " << *input_bytes_read
290
0
                      << " decompressed_len: " << *decompressed_len;
291
0
            return Status::InternalError("Failed to do bz2 decompress. status code: {}", ret);
292
2.05k
        } else if (ret == BZ_STREAM_END) {
293
2.00k
            *stream_end = true;
294
2.00k
            ret = BZ2_bzDecompressEnd(&_bz_strm);
295
2.00k
            if (ret != BZ_OK) {
296
0
                return Status::InternalError("Failed to do bz2 decompress. status code: {}", ret);
297
0
            }
298
299
2.00k
            ret = BZ2_bzDecompressInit(&_bz_strm, 0, 0);
300
2.00k
            if (ret != BZ_OK) {
301
0
                return Status::InternalError("Failed to do bz2 decompress. status code: {}", ret);
302
0
            }
303
2.00k
        } else if (ret != BZ_OK) {
304
0
            return Status::InternalError("Failed to bz2 decompress. status code: {}", ret);
305
58
        } else {
306
            // continue
307
58
        }
308
2.05k
    }
309
310
742
    return Status::OK();
311
742
}
312
313
0
std::string Bzip2Decompressor::debug_info() {
314
0
    std::stringstream ss;
315
0
    ss << "Bzip2Decompressor.";
316
0
    return ss.str();
317
0
}
318
319
646
ZstdDecompressor::~ZstdDecompressor() {
320
646
    ZSTD_freeDStream(_zstd_strm);
321
646
}
322
323
646
Status ZstdDecompressor::init() {
324
646
    _zstd_strm = ZSTD_createDStream();
325
646
    if (!_zstd_strm) {
326
0
        std::stringstream ss;
327
0
        return Status::InternalError("ZSTD_dctx creation error");
328
0
    }
329
646
    auto ret = ZSTD_initDStream(_zstd_strm);
330
646
    if (ZSTD_isError(ret)) {
331
0
        return Status::InternalError("ZSTD_initDStream error: {}", ZSTD_getErrorName(ret));
332
0
    }
333
646
    return Status::OK();
334
646
}
335
336
Status ZstdDecompressor::decompress(uint8_t* input, uint32_t input_len, size_t* input_bytes_read,
337
                                    uint8_t* output, uint32_t output_max_len,
338
                                    size_t* decompressed_len, bool* stream_end,
339
1.94k
                                    size_t* more_input_bytes, size_t* more_output_bytes) {
340
    // 1. set input and output
341
1.94k
    ZSTD_inBuffer inputBuffer = {input, input_len, 0};
342
1.94k
    ZSTD_outBuffer outputBuffer = {output, output_max_len, 0};
343
344
    // decompress
345
1.94k
    size_t ret = ZSTD_decompressStream(_zstd_strm, &outputBuffer, &inputBuffer);
346
1.94k
    *input_bytes_read = inputBuffer.pos;
347
1.94k
    *decompressed_len = outputBuffer.pos;
348
349
1.94k
    if (ZSTD_isError(ret)) {
350
0
        return Status::InternalError("Failed to do zstd decompress: {}", ZSTD_getErrorName(ret));
351
0
    }
352
353
1.94k
    *stream_end = ret == 0;
354
1.94k
    return Status::OK();
355
1.94k
}
356
357
0
std::string ZstdDecompressor::debug_info() {
358
0
    std::stringstream ss;
359
0
    ss << "ZstdDecompressor.";
360
0
    return ss.str();
361
0
}
362
363
// Lz4Frame
364
// Lz4 version: 1.7.5
365
// define LZ4F_VERSION = 100
366
const unsigned Lz4FrameDecompressor::DORIS_LZ4F_VERSION = 100;
367
368
24
Lz4FrameDecompressor::~Lz4FrameDecompressor() {
369
24
    LZ4F_freeDecompressionContext(_dctx);
370
24
}
371
372
24
Status Lz4FrameDecompressor::init() {
373
24
    size_t ret = LZ4F_createDecompressionContext(&_dctx, DORIS_LZ4F_VERSION);
374
24
    if (LZ4F_isError(ret)) {
375
0
        std::stringstream ss;
376
0
        ss << "LZ4F_dctx creation error: " << std::string(LZ4F_getErrorName(ret));
377
0
        return Status::InternalError(ss.str());
378
0
    }
379
380
    // init as -1
381
24
    _expect_dec_buf_size = -1;
382
383
24
    return Status::OK();
384
24
}
385
386
Status Lz4FrameDecompressor::decompress(uint8_t* input, uint32_t input_len,
387
                                        size_t* input_bytes_read, uint8_t* output,
388
                                        uint32_t output_max_len, size_t* decompressed_len,
389
                                        bool* stream_end, size_t* more_input_bytes,
390
24
                                        size_t* more_output_bytes) {
391
24
    uint8_t* src = input;
392
24
    size_t remaining_input_size = input_len;
393
24
    size_t ret = 1;
394
24
    *input_bytes_read = 0;
395
396
24
    if (_expect_dec_buf_size == -1) {
397
        // init expected decompress buf size, and check if output_max_len is large enough
398
        // ATTN: _expect_dec_buf_size is uninit, which means this is the first time to call
399
        //       decompress(), so *input* should point to the head of the compressed file,
400
        //       where lz4 header section is there.
401
402
24
        if (input_len < 15) {
403
0
            return Status::InternalError(
404
0
                    "Lz4 header size is between 7 and 15 bytes. "
405
0
                    "but input size is only: {}",
406
0
                    input_len);
407
0
        }
408
409
24
        LZ4F_frameInfo_t info;
410
24
        ret = LZ4F_getFrameInfo(_dctx, &info, (void*)src, &remaining_input_size);
411
24
        if (LZ4F_isError(ret)) {
412
0
            return Status::InternalError("LZ4F_getFrameInfo error: {}",
413
0
                                         std::string(LZ4F_getErrorName(ret)));
414
0
        }
415
416
24
        _expect_dec_buf_size = get_block_size(&info);
417
24
        if (_expect_dec_buf_size == -1) {
418
0
            return Status::InternalError(
419
0
                    "Impossible lz4 block size unless more block sizes are allowed {}",
420
0
                    std::string(LZ4F_getErrorName(ret)));
421
0
        }
422
423
24
        *input_bytes_read = remaining_input_size;
424
425
24
        src += remaining_input_size;
426
24
        remaining_input_size = input_len - remaining_input_size;
427
428
24
        LOG(INFO) << "lz4 block size: " << _expect_dec_buf_size;
429
24
    }
430
431
    // decompress
432
24
    size_t output_len = output_max_len;
433
24
    ret = LZ4F_decompress(_dctx, (void*)output, &output_len, (void*)src, &remaining_input_size,
434
24
                          /* LZ4F_decompressOptions_t */ nullptr);
435
24
    if (LZ4F_isError(ret)) {
436
0
        return Status::InternalError("Decompression error: {}",
437
0
                                     std::string(LZ4F_getErrorName(ret)));
438
0
    }
439
440
    // update
441
24
    *input_bytes_read += remaining_input_size;
442
24
    *decompressed_len = output_len;
443
24
    if (ret == 0) {
444
24
        *stream_end = true;
445
24
    } else {
446
0
        *stream_end = false;
447
0
    }
448
449
24
    return Status::OK();
450
24
}
451
452
0
std::string Lz4FrameDecompressor::debug_info() {
453
0
    std::stringstream ss;
454
0
    ss << "Lz4FrameDecompressor."
455
0
       << " expect dec buf size: " << _expect_dec_buf_size
456
0
       << " Lz4 Frame Version: " << DORIS_LZ4F_VERSION;
457
0
    return ss.str();
458
0
}
459
460
24
size_t Lz4FrameDecompressor::get_block_size(const LZ4F_frameInfo_t* info) {
461
24
    switch (info->blockSizeID) {
462
0
    case LZ4F_default:
463
0
    case LZ4F_max64KB:
464
0
        return 1 << 16;
465
24
    case LZ4F_max256KB:
466
24
        return 1 << 18;
467
0
    case LZ4F_max1MB:
468
0
        return 1 << 20;
469
0
    case LZ4F_max4MB:
470
0
        return 1 << 22;
471
0
    default:
472
        // error
473
0
        return -1;
474
24
    }
475
24
}
476
477
/// Lz4BlockDecompressor
478
750
Status Lz4BlockDecompressor::init() {
479
750
    return Status::OK();
480
750
}
481
482
// Hadoop lz4codec source :
483
// https://github.com/apache/hadoop/blob/trunk/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-nativetask/src/main/native/src/codec/Lz4Codec.cc
484
// Example:
485
// OriginData(The original data will be divided into several large data block.) :
486
//      large data block1 | large data block2 | large data block3 | ....
487
// The large data block will be divided into several small data block.
488
// Suppose a large data block is divided into three small blocks:
489
// large data block1:            | small block1 | small block2 | small block3 |
490
// CompressData:   <A [B1 compress(small block1) ] [B2 compress(small block1) ] [B3 compress(small block1)]>
491
//
492
// A : original length of the current block of large data block.
493
// sizeof(A) = 4 bytes.
494
// A = length(small block1) + length(small block2) + length(small block3)
495
// Bx : length of  small data block bx.
496
// sizeof(Bx) = 4 bytes.
497
// Bx = length(compress(small blockx))
498
Status Lz4BlockDecompressor::decompress(uint8_t* input, uint32_t input_len,
499
                                        size_t* input_bytes_read, uint8_t* output,
500
                                        uint32_t output_max_len, size_t* decompressed_len,
501
                                        bool* stream_end, size_t* more_input_bytes,
502
2.71k
                                        size_t* more_output_bytes) {
503
2.71k
    auto* input_ptr = input;
504
2.71k
    auto* output_ptr = output;
505
506
5.13k
    while (input_len > 0) {
507
4.28k
        if (input_len < sizeof(uint32_t)) {
508
0
            *more_input_bytes = sizeof(uint32_t) - input_len;
509
0
            break;
510
0
        }
511
512
        //if faild, fall back to large block begin
513
4.28k
        auto* large_block_input_ptr = input_ptr;
514
4.28k
        auto* large_block_output_ptr = output_ptr;
515
516
4.28k
        uint32_t remaining_decompressed_large_block_len = BigEndian::Load32(input_ptr);
517
518
4.28k
        input_ptr += sizeof(uint32_t);
519
4.28k
        input_len -= sizeof(uint32_t);
520
521
4.28k
        auto remaining_output_len = cast_set<uint32_t>(output_max_len - *decompressed_len);
522
523
4.28k
        if (remaining_output_len < remaining_decompressed_large_block_len) {
524
            // Need more output buffer
525
1.86k
            *more_output_bytes = remaining_decompressed_large_block_len - remaining_output_len;
526
1.86k
            input_ptr = large_block_input_ptr;
527
1.86k
            output_ptr = large_block_output_ptr;
528
529
1.86k
            break;
530
1.86k
        }
531
532
2.42k
        std::size_t decompressed_large_block_len = 0;
533
4.88k
        while (remaining_decompressed_large_block_len > 0) {
534
            // Check that input length should not be negative.
535
2.46k
            if (input_len < sizeof(uint32_t)) {
536
0
                *more_input_bytes = sizeof(uint32_t) - input_len;
537
0
                break;
538
0
            }
539
540
            // Read the length of the next lz4 compressed block.
541
2.46k
            uint32_t compressed_small_block_len = BigEndian::Load32(input_ptr);
542
543
2.46k
            input_ptr += sizeof(uint32_t);
544
2.46k
            input_len -= sizeof(uint32_t);
545
546
2.46k
            if (compressed_small_block_len == 0) {
547
0
                continue;
548
0
            }
549
550
2.46k
            if (compressed_small_block_len > input_len) {
551
                // Need more input buffer
552
0
                *more_input_bytes = compressed_small_block_len - input_len;
553
0
                break;
554
0
            }
555
556
            // Decompress this block.
557
2.46k
            auto decompressed_small_block_len = LZ4_decompress_safe(
558
2.46k
                    reinterpret_cast<const char*>(input_ptr), reinterpret_cast<char*>(output_ptr),
559
2.46k
                    compressed_small_block_len, remaining_output_len);
560
2.46k
            if (decompressed_small_block_len < 0) {
561
0
                return Status::InvalidArgument("Failed to do Lz4Block decompress, error = {}",
562
0
                                               LZ4F_getErrorName(decompressed_small_block_len));
563
0
            }
564
2.46k
            input_ptr += compressed_small_block_len;
565
2.46k
            input_len -= compressed_small_block_len;
566
567
2.46k
            output_ptr += decompressed_small_block_len;
568
2.46k
            remaining_decompressed_large_block_len -= decompressed_small_block_len;
569
2.46k
            decompressed_large_block_len += decompressed_small_block_len;
570
2.46k
        };
571
572
2.42k
        if (*more_input_bytes != 0) {
573
            // Need more input buffer
574
0
            input_ptr = large_block_input_ptr;
575
0
            output_ptr = large_block_output_ptr;
576
0
            break;
577
0
        }
578
579
2.42k
        *decompressed_len += decompressed_large_block_len;
580
2.42k
    }
581
2.71k
    *input_bytes_read += (input_ptr - input);
582
    // If no more input and output need, means this is the end of a compressed block
583
2.71k
    *stream_end = (*more_input_bytes == 0 && *more_output_bytes == 0);
584
585
2.71k
    return Status::OK();
586
2.71k
}
587
588
0
std::string Lz4BlockDecompressor::debug_info() {
589
0
    std::stringstream ss;
590
0
    ss << "Lz4BlockDecompressor.";
591
0
    return ss.str();
592
0
}
593
594
/// SnappyBlockDecompressor
595
770
Status SnappyBlockDecompressor::init() {
596
770
    return Status::OK();
597
770
}
598
599
// Hadoop snappycodec source :
600
// https://github.com/apache/hadoop/blob/trunk/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-nativetask/src/main/native/src/codec/SnappyCodec.cc
601
// Example:
602
// OriginData(The original data will be divided into several large data block.) :
603
//      large data block1 | large data block2 | large data block3 | ....
604
// The large data block will be divided into several small data block.
605
// Suppose a large data block is divided into three small blocks:
606
// large data block1:            | small block1 | small block2 | small block3 |
607
// CompressData:   <A [B1 compress(small block1) ] [B2 compress(small block1) ] [B3 compress(small block1)]>
608
//
609
// A : original length of the current block of large data block.
610
// sizeof(A) = 4 bytes.
611
// A = length(small block1) + length(small block2) + length(small block3)
612
// Bx : length of  small data block bx.
613
// sizeof(Bx) = 4 bytes.
614
// Bx = length(compress(small blockx))
615
Status SnappyBlockDecompressor::decompress(uint8_t* input, uint32_t input_len,
616
                                           size_t* input_bytes_read, uint8_t* output,
617
                                           uint32_t output_max_len, size_t* decompressed_len,
618
                                           bool* stream_end, size_t* more_input_bytes,
619
914
                                           size_t* more_output_bytes) {
620
914
    auto* input_ptr = input;
621
914
    auto* output_ptr = output;
622
623
5.04k
    while (input_len > 0) {
624
4.27k
        if (input_len < sizeof(uint32_t)) {
625
0
            *more_input_bytes = sizeof(uint32_t) - input_len;
626
0
            break;
627
0
        }
628
629
        //if faild, fall back to large block begin
630
4.27k
        auto* large_block_input_ptr = input_ptr;
631
4.27k
        auto* large_block_output_ptr = output_ptr;
632
633
4.27k
        uint32_t remaining_decompressed_large_block_len = BigEndian::Load32(input_ptr);
634
635
4.27k
        input_ptr += sizeof(uint32_t);
636
4.27k
        input_len -= sizeof(uint32_t);
637
638
4.27k
        std::size_t remaining_output_len = output_max_len - *decompressed_len;
639
640
4.27k
        if (remaining_output_len < remaining_decompressed_large_block_len) {
641
            // Need more output buffer
642
96
            *more_output_bytes = remaining_decompressed_large_block_len - remaining_output_len;
643
96
            input_ptr = large_block_input_ptr;
644
96
            output_ptr = large_block_output_ptr;
645
646
96
            break;
647
96
        }
648
649
4.18k
        std::size_t decompressed_large_block_len = 0;
650
10.9k
        while (remaining_decompressed_large_block_len > 0) {
651
            // Check that input length should not be negative.
652
6.82k
            if (input_len < sizeof(uint32_t)) {
653
0
                *more_input_bytes = sizeof(uint32_t) - input_len;
654
0
                break;
655
0
            }
656
657
            // Read the length of the next snappy compressed block.
658
6.82k
            size_t compressed_small_block_len = BigEndian::Load32(input_ptr);
659
660
6.82k
            input_ptr += sizeof(uint32_t);
661
6.82k
            input_len -= sizeof(uint32_t);
662
663
6.82k
            if (compressed_small_block_len == 0) {
664
0
                continue;
665
0
            }
666
667
6.82k
            if (compressed_small_block_len > input_len) {
668
                // Need more input buffer
669
52
                *more_input_bytes = compressed_small_block_len - input_len;
670
52
                break;
671
52
            }
672
673
            // Decompress this block.
674
6.77k
            size_t decompressed_small_block_len;
675
6.77k
            if (!snappy::GetUncompressedLength(reinterpret_cast<const char*>(input_ptr),
676
6.77k
                                               compressed_small_block_len,
677
6.77k
                                               &decompressed_small_block_len)) {
678
0
                return Status::InternalError("Failed to do snappy decompress.");
679
0
            }
680
6.77k
            if (!snappy::RawUncompress(reinterpret_cast<const char*>(input_ptr),
681
6.77k
                                       compressed_small_block_len,
682
6.77k
                                       reinterpret_cast<char*>(output_ptr))) {
683
0
                return Status::InternalError(
684
0
                        "Failed to do snappy decompress. uncompressed_len: {}, compressed_len: {}",
685
0
                        decompressed_small_block_len, compressed_small_block_len);
686
0
            }
687
6.77k
            input_ptr += compressed_small_block_len;
688
6.77k
            input_len -= compressed_small_block_len;
689
690
6.77k
            output_ptr += decompressed_small_block_len;
691
6.77k
            remaining_decompressed_large_block_len -= decompressed_small_block_len;
692
6.77k
            decompressed_large_block_len += decompressed_small_block_len;
693
6.77k
        };
694
695
4.18k
        if (*more_input_bytes != 0) {
696
            // Need more input buffer
697
52
            input_ptr = large_block_input_ptr;
698
52
            output_ptr = large_block_output_ptr;
699
52
            break;
700
52
        }
701
702
4.13k
        *decompressed_len += decompressed_large_block_len;
703
4.13k
    }
704
914
    *input_bytes_read += (input_ptr - input);
705
    // If no more input and output need, means this is the end of a compressed block
706
914
    *stream_end = (*more_input_bytes == 0 && *more_output_bytes == 0);
707
708
914
    return Status::OK();
709
914
}
710
711
0
std::string SnappyBlockDecompressor::debug_info() {
712
0
    std::stringstream ss;
713
0
    ss << "SnappyBlockDecompressor.";
714
0
    return ss.str();
715
0
}
716
717
#include "common/compile_check_end.h"
718
} // namespace doris