compress_base_udf.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #pragma once
  2. #include <yql/essentials/public/udf/udf_helpers.h>
  3. #include <library/cpp/streams/brotli/brotli.h>
  4. #include <library/cpp/streams/bzip2/bzip2.h>
  5. #include <library/cpp/streams/zstd/zstd.h>
  6. #include <library/cpp/streams/lzma/lzma.h>
  7. #include <library/cpp/streams/xz/decompress.h>
  8. #include <util/stream/mem.h>
  9. #include <util/stream/zlib.h>
  10. #include <contrib/libs/snappy/snappy.h>
  11. using namespace NYql::NUdf;
  12. namespace NCompress {
  13. SIMPLE_UDF(TGzip, char*(TAutoMap<char*>, ui8)) {
  14. TString result;
  15. TStringOutput output(result);
  16. TZLibCompress compress(&output, ZLib::GZip, args[1].Get<ui8>());
  17. compress.Write(args[0].AsStringRef());
  18. compress.Finish();
  19. return valueBuilder->NewString(result);
  20. }
  21. SIMPLE_UDF(TZlib, char*(TAutoMap<char*>, ui8)) {
  22. TString result;
  23. TStringOutput output(result);
  24. TZLibCompress compress(&output, ZLib::ZLib, args[1].Get<ui8>());
  25. compress.Write(args[0].AsStringRef());
  26. compress.Finish();
  27. return valueBuilder->NewString(result);
  28. }
  29. SIMPLE_UDF(TBrotli, char*(TAutoMap<char*>, ui8)) {
  30. TString result;
  31. TStringOutput output(result);
  32. TBrotliCompress compress(&output, args[1].Get<ui8>());
  33. compress.Write(args[0].AsStringRef());
  34. compress.Finish();
  35. return valueBuilder->NewString(result);
  36. }
  37. SIMPLE_UDF(TLzma, char*(TAutoMap<char*>, ui8)) {
  38. TString result;
  39. TStringOutput output(result);
  40. TLzmaCompress compress(&output, args[1].Get<ui8>());
  41. compress.Write(args[0].AsStringRef());
  42. compress.Finish();
  43. return valueBuilder->NewString(result);
  44. }
  45. SIMPLE_UDF(TBZip2, char*(TAutoMap<char*>, ui8)) {
  46. TString result;
  47. TStringOutput output(result);
  48. TBZipCompress compress(&output, args[1].Get<ui8>());
  49. compress.Write(args[0].AsStringRef());
  50. compress.Finish();
  51. return valueBuilder->NewString(result);
  52. }
  53. SIMPLE_UDF(TSnappy, char*(TAutoMap<char*>)) {
  54. TString result;
  55. const TStringRef& input = args[0].AsStringRef();
  56. snappy::Compress(input.Data(), input.Size(), &result);
  57. return valueBuilder->NewString(result);
  58. }
  59. SIMPLE_UDF(TZstd, char*(TAutoMap<char*>, ui8)) {
  60. TString result;
  61. TStringOutput output(result);
  62. TZstdCompress compress(&output, args[1].Get<ui8>());
  63. compress.Write(args[0].AsStringRef());
  64. compress.Finish();
  65. return valueBuilder->NewString(result);
  66. }
  67. }
  68. namespace NDecompress {
  69. SIMPLE_UDF(TGzip, char*(TAutoMap<char*>)) {
  70. const auto& ref = args->AsStringRef();
  71. TMemoryInput input(ref.Data(), ref.Size());
  72. TZLibDecompress decompress(&input);
  73. return valueBuilder->NewString(decompress.ReadAll());
  74. }
  75. SIMPLE_UDF(TZlib, char*(TAutoMap<char*>)) {
  76. const auto& ref = args->AsStringRef();
  77. TMemoryInput input(ref.Data(), ref.Size());
  78. TZLibDecompress decompress(&input);
  79. return valueBuilder->NewString(decompress.ReadAll());
  80. }
  81. SIMPLE_UDF(TBrotli, char*(TAutoMap<char*>)) {
  82. const auto& ref = args->AsStringRef();
  83. TMemoryInput input(ref.Data(), ref.Size());
  84. TBrotliDecompress decompress(&input);
  85. return valueBuilder->NewString(decompress.ReadAll());
  86. }
  87. SIMPLE_UDF(TLzma, char*(TAutoMap<char*>)) {
  88. const auto& ref = args->AsStringRef();
  89. TMemoryInput input(ref.Data(), ref.Size());
  90. TLzmaDecompress decompress(&input);
  91. return valueBuilder->NewString(decompress.ReadAll());
  92. }
  93. SIMPLE_UDF(TBZip2, char*(TAutoMap<char*>)) {
  94. const auto& ref = args->AsStringRef();
  95. TMemoryInput input(ref.Data(), ref.Size());
  96. TBZipDecompress decompress(&input);
  97. return valueBuilder->NewString(decompress.ReadAll());
  98. }
  99. SIMPLE_UDF(TSnappy, char*(TAutoMap<char*>)) {
  100. TString result;
  101. const auto& value = args->AsStringRef();
  102. if (snappy::Uncompress(value.Data(), value.Size(), &result)) {
  103. return valueBuilder->NewString(result);
  104. }
  105. ythrow yexception() << "failed to decompress message with snappy";
  106. }
  107. SIMPLE_UDF(TZstd, char*(TAutoMap<char*>)) {
  108. const auto& ref = args->AsStringRef();
  109. TMemoryInput input(ref.Data(), ref.Size());
  110. TZstdDecompress decompress(&input);
  111. return valueBuilder->NewString(decompress.ReadAll());
  112. }
  113. SIMPLE_UDF(TXz, char*(TAutoMap<char*>)) {
  114. const auto& ref = args->AsStringRef();
  115. TMemoryInput input(ref.Data(), ref.Size());
  116. TXzDecompress decompress(&input);
  117. return valueBuilder->NewString(decompress.ReadAll());
  118. }
  119. }
  120. namespace NTryDecompress {
  121. SIMPLE_UDF(TGzip, TOptional<char*>(TAutoMap<char*>)) try {
  122. const auto& ref = args->AsStringRef();
  123. TMemoryInput input(ref.Data(), ref.Size());
  124. TZLibDecompress decompress(&input);
  125. return valueBuilder->NewString(decompress.ReadAll());
  126. } catch (const std::exception&) {
  127. return TUnboxedValuePod();
  128. }
  129. SIMPLE_UDF(TZlib, TOptional<char*>(TAutoMap<char*>)) try {
  130. const auto& ref = args->AsStringRef();
  131. TMemoryInput input(ref.Data(), ref.Size());
  132. TZLibDecompress decompress(&input);
  133. return valueBuilder->NewString(decompress.ReadAll());
  134. } catch (const std::exception&) {
  135. return TUnboxedValuePod();
  136. }
  137. SIMPLE_UDF(TBrotli, TOptional<char*>(TAutoMap<char*>)) try {
  138. const auto& ref = args->AsStringRef();
  139. TMemoryInput input(ref.Data(), ref.Size());
  140. TBrotliDecompress decompress(&input);
  141. return valueBuilder->NewString(decompress.ReadAll());
  142. } catch (const std::exception&) {
  143. return TUnboxedValuePod();
  144. }
  145. SIMPLE_UDF(TLzma, TOptional<char*>(TAutoMap<char*>)) try {
  146. const auto& ref = args->AsStringRef();
  147. TMemoryInput input(ref.Data(), ref.Size());
  148. TLzmaDecompress decompress(&input);
  149. return valueBuilder->NewString(decompress.ReadAll());
  150. } catch (const std::exception&) {
  151. return TUnboxedValuePod();
  152. }
  153. SIMPLE_UDF(TBZip2, TOptional<char*>(TAutoMap<char*>)) try {
  154. const auto& ref = args->AsStringRef();
  155. TMemoryInput input(ref.Data(), ref.Size());
  156. TBZipDecompress decompress(&input);
  157. return valueBuilder->NewString(decompress.ReadAll());
  158. } catch (const std::exception&) {
  159. return TUnboxedValuePod();
  160. }
  161. SIMPLE_UDF(TSnappy, TOptional<char*>(TAutoMap<char*>)) {
  162. TString result;
  163. const auto& value = args->AsStringRef();
  164. if (snappy::Uncompress(value.Data(), value.Size(), &result)) {
  165. return valueBuilder->NewString(result);
  166. }
  167. return TUnboxedValuePod();
  168. }
  169. SIMPLE_UDF(TZstd, TOptional<char*>(TAutoMap<char*>)) try {
  170. const auto& ref = args->AsStringRef();
  171. TMemoryInput input(ref.Data(), ref.Size());
  172. TZstdDecompress decompress(&input);
  173. return valueBuilder->NewString(decompress.ReadAll());
  174. } catch (const std::exception&) {
  175. return TUnboxedValuePod();
  176. }
  177. SIMPLE_UDF(TXz, TOptional<char*>(TAutoMap<char*>)) try {
  178. const auto& ref = args->AsStringRef();
  179. TMemoryInput input(ref.Data(), ref.Size());
  180. TXzDecompress decompress(&input);
  181. return valueBuilder->NewString(decompress.ReadAll());
  182. } catch (const std::exception&) {
  183. return TUnboxedValuePod();
  184. }
  185. }
  186. #define EXPORTED_COMPRESS_BASE_UDF TGzip, TZlib, TBrotli, TLzma, TBZip2, TSnappy, TZstd
  187. #define EXPORTED_DECOMPRESS_BASE_UDF TGzip, TZlib, TBrotli, TLzma, TBZip2, TSnappy, TZstd, TXz
  188. #define EXPORTED_TRY_DECOMPRESS_BASE_UDF TGzip, TZlib, TBrotli, TLzma, TBZip2, TSnappy, TZstd, TXz