|
| 1 | +#include "config.h" |
| 2 | + |
| 3 | +#if USE_IDNA |
| 4 | + |
| 5 | +#include <Functions/FunctionFactory.h> |
| 6 | +#include <Functions/FunctionStringToString.h> |
| 7 | + |
| 8 | +#ifdef __clang__ |
| 9 | +# pragma clang diagnostic push |
| 10 | +# pragma clang diagnostic ignored "-Wnewline-eof" |
| 11 | +#endif |
| 12 | +# include <ada/idna/punycode.h> |
| 13 | +# include <ada/idna/unicode_transcoding.h> |
| 14 | +#ifdef __clang__ |
| 15 | +# pragma clang diagnostic pop |
| 16 | +#endif |
| 17 | + |
| 18 | +namespace DB |
| 19 | +{ |
| 20 | + |
| 21 | +namespace ErrorCodes |
| 22 | +{ |
| 23 | + extern const int BAD_ARGUMENTS; |
| 24 | + extern const int ILLEGAL_COLUMN; |
| 25 | +} |
| 26 | + |
| 27 | +struct PunycodeEncodeImpl |
| 28 | +{ |
| 29 | + static void vector( |
| 30 | + const ColumnString::Chars & data, |
| 31 | + const ColumnString::Offsets & offsets, |
| 32 | + ColumnString::Chars & res_data, |
| 33 | + ColumnString::Offsets & res_offsets) |
| 34 | + { |
| 35 | + const size_t rows = offsets.size(); |
| 36 | + res_data.reserve(data.size()); /// just a guess, assuming the input is all-ASCII |
| 37 | + res_offsets.reserve(rows); |
| 38 | + |
| 39 | + size_t prev_offset = 0; |
| 40 | + std::u32string value_utf32; |
| 41 | + std::string value_puny; |
| 42 | + for (size_t row = 0; row < rows; ++row) |
| 43 | + { |
| 44 | + const char * value = reinterpret_cast<const char *>(&data[prev_offset]); |
| 45 | + const size_t value_length = offsets[row] - prev_offset - 1; |
| 46 | + |
| 47 | + const size_t value_utf32_length = ada::idna::utf32_length_from_utf8(value, value_length); |
| 48 | + value_utf32.resize(value_utf32_length); |
| 49 | + ada::idna::utf8_to_utf32(value, value_length, value_utf32.data()); |
| 50 | + |
| 51 | + const bool ok = ada::idna::utf32_to_punycode(value_utf32, value_puny); |
| 52 | + if (!ok) |
| 53 | + throw Exception(ErrorCodes::BAD_ARGUMENTS, "Internal error during Punycode encoding"); |
| 54 | + |
| 55 | + res_data.insert(value_puny.c_str(), value_puny.c_str() + value_puny.size() + 1); |
| 56 | + res_offsets.push_back(res_data.size()); |
| 57 | + |
| 58 | + prev_offset = offsets[row]; |
| 59 | + |
| 60 | + value_utf32.clear(); |
| 61 | + value_puny.clear(); /// utf32_to_punycode() appends to its output string |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + [[noreturn]] static void vectorFixed(const ColumnString::Chars &, size_t, ColumnString::Chars &) |
| 66 | + { |
| 67 | + throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Column of type FixedString is not supported by punycodeEncode function"); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +struct PunycodeDecodeImpl |
| 72 | +{ |
| 73 | + static void vector( |
| 74 | + const ColumnString::Chars & data, |
| 75 | + const ColumnString::Offsets & offsets, |
| 76 | + ColumnString::Chars & res_data, |
| 77 | + ColumnString::Offsets & res_offsets) |
| 78 | + { |
| 79 | + const size_t rows = offsets.size(); |
| 80 | + res_data.reserve(data.size()); /// just a guess, assuming the input is all-ASCII |
| 81 | + res_offsets.reserve(rows); |
| 82 | + |
| 83 | + size_t prev_offset = 0; |
| 84 | + std::u32string value_utf32; |
| 85 | + std::string value_utf8; |
| 86 | + for (size_t row = 0; row < rows; ++row) |
| 87 | + { |
| 88 | + const char * value = reinterpret_cast<const char *>(&data[prev_offset]); |
| 89 | + const size_t value_length = offsets[row] - prev_offset - 1; |
| 90 | + |
| 91 | + const std::string_view value_punycode(value, value_length); |
| 92 | + const bool ok = ada::idna::punycode_to_utf32(value_punycode, value_utf32); |
| 93 | + if (!ok) |
| 94 | + throw Exception(ErrorCodes::BAD_ARGUMENTS, "Internal error during Punycode decoding"); |
| 95 | + |
| 96 | + const size_t utf8_length = ada::idna::utf8_length_from_utf32(value_utf32.data(), value_utf32.size()); |
| 97 | + value_utf8.resize(utf8_length); |
| 98 | + ada::idna::utf32_to_utf8(value_utf32.data(), value_utf32.size(), value_utf8.data()); |
| 99 | + |
| 100 | + res_data.insert(value_utf8.c_str(), value_utf8.c_str() + value_utf8.size() + 1); |
| 101 | + res_offsets.push_back(res_data.size()); |
| 102 | + |
| 103 | + prev_offset = offsets[row]; |
| 104 | + |
| 105 | + value_utf32.clear(); /// punycode_to_utf32() appends to its output string |
| 106 | + value_utf8.clear(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + [[noreturn]] static void vectorFixed(const ColumnString::Chars &, size_t, ColumnString::Chars &) |
| 111 | + { |
| 112 | + throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Column of type FixedString is not supported by punycodeDecode function"); |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +struct NamePunycodeEncode |
| 117 | +{ |
| 118 | + static constexpr auto name = "punycodeEncode"; |
| 119 | +}; |
| 120 | + |
| 121 | +struct NamePunycodeDecode |
| 122 | +{ |
| 123 | + static constexpr auto name = "punycodeDecode"; |
| 124 | +}; |
| 125 | + |
| 126 | +REGISTER_FUNCTION(Punycode) |
| 127 | +{ |
| 128 | + factory.registerFunction<FunctionStringToString<PunycodeEncodeImpl, NamePunycodeEncode>>(FunctionDocumentation{ |
| 129 | + .description=R"( |
| 130 | +Computes a Punycode representation of a string.)", |
| 131 | + .syntax="punycodeEncode(str)", |
| 132 | + .arguments={{"str", "Input string"}}, |
| 133 | + .returned_value="The punycode representation [String](/docs/en/sql-reference/data-types/string.md).", |
| 134 | + .examples={ |
| 135 | + {"simple", |
| 136 | + "SELECT punycodeEncode('München') AS puny;", |
| 137 | + R"( |
| 138 | +┌─puny───────┐ |
| 139 | +│ Mnchen-3ya │ |
| 140 | +└────────────┘ |
| 141 | + )" |
| 142 | + }} |
| 143 | + }); |
| 144 | + |
| 145 | + factory.registerFunction<FunctionStringToString<PunycodeDecodeImpl, NamePunycodeDecode>>(FunctionDocumentation{ |
| 146 | + .description=R"( |
| 147 | +Computes a Punycode representation of a string.)", |
| 148 | + .syntax="punycodeDecode(str)", |
| 149 | + .arguments={{"str", "A Punycode-encoded string"}}, |
| 150 | + .returned_value="The plaintext representation [String](/docs/en/sql-reference/data-types/string.md).", |
| 151 | + .examples={ |
| 152 | + {"simple", |
| 153 | + "SELECT punycodeDecode('Mnchen-3ya') AS plain;", |
| 154 | + R"( |
| 155 | +┌─plain───┐ |
| 156 | +│ München │ |
| 157 | +└─────────┘ |
| 158 | + )" |
| 159 | + }} |
| 160 | + }); |
| 161 | +} |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | +#endif |
0 commit comments