Coverage Report

Created: 2026-04-14 13:42

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