aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/compress_base/lib/compress_base_udf.h
blob: 58709134d6a91f33ceca3b3f7a97b35edb073c14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#pragma once

#include <yql/essentials/public/udf/udf_helpers.h>

#include <library/cpp/streams/brotli/brotli.h>
#include <library/cpp/streams/bzip2/bzip2.h>
#include <library/cpp/streams/zstd/zstd.h>
#include <library/cpp/streams/lzma/lzma.h>
#include <library/cpp/streams/xz/decompress.h>

#include <util/stream/mem.h>
#include <util/stream/zlib.h>

#include <contrib/libs/snappy/snappy.h>

using namespace NYql::NUdf;

namespace NCompress {
    SIMPLE_UDF(TGzip, char*(TAutoMap<char*>, ui8)) {
        TString result;
        TStringOutput output(result);
        TZLibCompress compress(&output, ZLib::GZip, args[1].Get<ui8>());
        compress.Write(args[0].AsStringRef());
        compress.Finish();
        return valueBuilder->NewString(result);
    }

    SIMPLE_UDF(TZlib, char*(TAutoMap<char*>, ui8)) {
        TString result;
        TStringOutput output(result);
        TZLibCompress compress(&output, ZLib::ZLib, args[1].Get<ui8>());
        compress.Write(args[0].AsStringRef());
        compress.Finish();
        return valueBuilder->NewString(result);
    }

    SIMPLE_UDF(TBrotli, char*(TAutoMap<char*>, ui8)) {
        TString result;
        TStringOutput output(result);
        TBrotliCompress compress(&output, args[1].Get<ui8>());
        compress.Write(args[0].AsStringRef());
        compress.Finish();
        return valueBuilder->NewString(result);
    }

    SIMPLE_UDF(TLzma, char*(TAutoMap<char*>, ui8)) {
        TString result;
        TStringOutput output(result);
        TLzmaCompress compress(&output, args[1].Get<ui8>());
        compress.Write(args[0].AsStringRef());
        compress.Finish();
        return valueBuilder->NewString(result);
    }

    SIMPLE_UDF(TBZip2, char*(TAutoMap<char*>, ui8)) {
        TString result;
        TStringOutput output(result);
        TBZipCompress compress(&output, args[1].Get<ui8>());
        compress.Write(args[0].AsStringRef());
        compress.Finish();
        return valueBuilder->NewString(result);
    }

    SIMPLE_UDF(TSnappy, char*(TAutoMap<char*>)) {
        TString result;
        const TStringRef& input = args[0].AsStringRef();
        snappy::Compress(input.Data(), input.Size(), &result);
        return valueBuilder->NewString(result);
    }

    SIMPLE_UDF(TZstd, char*(TAutoMap<char*>, ui8)) {
        TString result;
        TStringOutput output(result);
        TZstdCompress compress(&output, args[1].Get<ui8>());
        compress.Write(args[0].AsStringRef());
        compress.Finish();
        return valueBuilder->NewString(result);
    }
}

namespace NDecompress {
    SIMPLE_UDF(TGzip, char*(TAutoMap<char*>)) {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TZLibDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    }

    SIMPLE_UDF(TZlib, char*(TAutoMap<char*>)) {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TZLibDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    }

    SIMPLE_UDF(TBrotli, char*(TAutoMap<char*>)) {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TBrotliDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    }

    SIMPLE_UDF(TLzma, char*(TAutoMap<char*>)) {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TLzmaDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    }

    SIMPLE_UDF(TBZip2, char*(TAutoMap<char*>)) {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TBZipDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    }

    SIMPLE_UDF(TSnappy, char*(TAutoMap<char*>)) {
        TString result;
        const auto& value = args->AsStringRef();
        if (snappy::Uncompress(value.Data(), value.Size(), &result)) {
            return valueBuilder->NewString(result);
        }

        ythrow yexception() << "failed to decompress message with snappy";
     }

    SIMPLE_UDF(TZstd, char*(TAutoMap<char*>)) {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TZstdDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    }

    SIMPLE_UDF(TXz, char*(TAutoMap<char*>)) {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TXzDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    }
}

namespace NTryDecompress {
    SIMPLE_UDF(TGzip, TOptional<char*>(TAutoMap<char*>)) try {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TZLibDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    } catch (const std::exception&) {
        return TUnboxedValuePod();
    }

    SIMPLE_UDF(TZlib, TOptional<char*>(TAutoMap<char*>)) try {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TZLibDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    } catch (const std::exception&) {
        return TUnboxedValuePod();
    }

    SIMPLE_UDF(TBrotli, TOptional<char*>(TAutoMap<char*>)) try {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TBrotliDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    } catch (const std::exception&) {
        return TUnboxedValuePod();
    }

    SIMPLE_UDF(TLzma, TOptional<char*>(TAutoMap<char*>)) try {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TLzmaDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    } catch (const std::exception&) {
        return TUnboxedValuePod();
    }

    SIMPLE_UDF(TBZip2, TOptional<char*>(TAutoMap<char*>)) try {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TBZipDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    } catch (const std::exception&) {
        return TUnboxedValuePod();
    }

    SIMPLE_UDF(TSnappy, TOptional<char*>(TAutoMap<char*>)) {
        TString result;
        const auto& value = args->AsStringRef();
        if (snappy::Uncompress(value.Data(), value.Size(), &result)) {
            return valueBuilder->NewString(result);
        }
        return TUnboxedValuePod();
    }

    SIMPLE_UDF(TZstd, TOptional<char*>(TAutoMap<char*>)) try {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TZstdDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    } catch (const std::exception&) {
        return TUnboxedValuePod();
    }

    SIMPLE_UDF(TXz, TOptional<char*>(TAutoMap<char*>)) try {
        const auto& ref = args->AsStringRef();
        TMemoryInput input(ref.Data(), ref.Size());
        TXzDecompress decompress(&input);
        return valueBuilder->NewString(decompress.ReadAll());
    } catch (const std::exception&) {
        return TUnboxedValuePod();
    }
}

#define EXPORTED_COMPRESS_BASE_UDF       TGzip, TZlib, TBrotli, TLzma, TBZip2, TSnappy, TZstd
#define EXPORTED_DECOMPRESS_BASE_UDF     TGzip, TZlib, TBrotli, TLzma, TBZip2, TSnappy, TZstd, TXz
#define EXPORTED_TRY_DECOMPRESS_BASE_UDF TGzip, TZlib, TBrotli, TLzma, TBZip2, TSnappy, TZstd, TXz