aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/normalizeString.cpp
blob: 8bab55e8d8f49192a716f9db1fd6b798abeda212 (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
#include "clickhouse_config.h"

#if USE_ICU
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionStringToString.h>
#include <unicode/rep.h>
#include <unicode/unistr.h>
#include <unicode/unorm2.h>
#include <unicode/ustring.h>
#include <unicode/utypes.h>
#include <Common/logger_useful.h>
#include <Columns/ColumnString.h>
#include <Parsers/IAST_fwd.h>

namespace DB
{

namespace ErrorCodes
{
    extern const int ILLEGAL_COLUMN;
    extern const int CANNOT_NORMALIZE_STRING;
}

namespace
{

// Expansion factors are specified for UTF-32, since icu uses UTF-32 for normalization
// Maximum expansion factors for different normalization forms
// https://unicode.org/faq/normalization.html#12

struct NormalizeNFCImpl
{
    static constexpr auto name = "normalizeUTF8NFC";

    static constexpr auto expansionFactor = 3;

    static const UNormalizer2 *getNormalizer(UErrorCode *err)
    {
        return unorm2_getNFCInstance(err);
    }
};

struct NormalizeNFDImpl
{
    static constexpr auto name = "normalizeUTF8NFD";

    static constexpr auto expansionFactor = 4;

    static const UNormalizer2 *getNormalizer(UErrorCode *err)
    {
        return unorm2_getNFDInstance(err);
    }
};

struct NormalizeNFKCImpl
{
    static constexpr auto name = "normalizeUTF8NFKC";

    static constexpr auto expansionFactor = 18;

    static const UNormalizer2 *getNormalizer(UErrorCode *err)
    {
        return unorm2_getNFKCInstance(err);
    }
};


struct NormalizeNFKDImpl
{
    static constexpr auto name = "normalizeUTF8NFKD";

    static constexpr auto expansionFactor = 18;

    static const UNormalizer2 *getNormalizer(UErrorCode *err)
    {
        return unorm2_getNFKDInstance(err);
    }
};

template<typename NormalizeImpl>
struct NormalizeUTF8Impl
{

    static void vector(const ColumnString::Chars & data,
        const ColumnString::Offsets & offsets,
        ColumnString::Chars & res_data,
        ColumnString::Offsets & res_offsets)
    {
        UErrorCode err = U_ZERO_ERROR;

        const UNormalizer2 *normalizer = NormalizeImpl::getNormalizer(&err);
        if (U_FAILURE(err))
            throw Exception(ErrorCodes::CANNOT_NORMALIZE_STRING, "Normalization failed (getNormalizer): {}", u_errorName(err));

        size_t size = offsets.size();
        res_offsets.resize(size);

        res_data.reserve(data.size() * 2);

        ColumnString::Offset current_from_offset = 0;
        ColumnString::Offset current_to_offset = 0;

        PODArray<UChar> from_uchars;
        PODArray<UChar> to_uchars;

        for (size_t i = 0; i < size; ++i)
        {
            size_t from_size = offsets[i] - current_from_offset - 1;

            from_uchars.resize(from_size + 1);
            int32_t from_code_points = 0;
            u_strFromUTF8(
                from_uchars.data(),
                from_uchars.size(),
                &from_code_points,
                reinterpret_cast<const char*>(&data[current_from_offset]),
                from_size,
                &err);
            if (U_FAILURE(err))
                throw Exception(ErrorCodes::CANNOT_NORMALIZE_STRING, "Normalization failed (strFromUTF8): {}", u_errorName(err));

            to_uchars.resize(from_code_points * NormalizeImpl::expansionFactor + 1);

            int32_t to_code_points = unorm2_normalize(
                normalizer,
                from_uchars.data(),
                from_code_points,
                to_uchars.data(),
                to_uchars.size(),
                &err);
            if (U_FAILURE(err))
                throw Exception(ErrorCodes::CANNOT_NORMALIZE_STRING, "Normalization failed (normalize): {}", u_errorName(err));

            size_t max_to_size = current_to_offset + 4 * to_code_points + 1;
            if (res_data.size() < max_to_size)
                res_data.resize(max_to_size);

            int32_t to_size = 0;
            u_strToUTF8(
                reinterpret_cast<char*>(&res_data[current_to_offset]),
                res_data.size() - current_to_offset,
                &to_size,
                to_uchars.data(),
                to_code_points,
                &err);
            if (U_FAILURE(err))
                throw Exception(ErrorCodes::CANNOT_NORMALIZE_STRING, "Normalization failed (strToUTF8): {}", u_errorName(err));

            current_to_offset += to_size;
            res_data[current_to_offset] = 0;
            ++current_to_offset;
            res_offsets[i] = current_to_offset;

            current_from_offset = offsets[i];
        }

        res_data.resize(current_to_offset);
    }

    [[noreturn]] static void vectorFixed(const ColumnString::Chars &, size_t, ColumnString::Chars &)
    {
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot apply function normalizeUTF8 to fixed string.");
    }
};

using FunctionNormalizeUTF8NFC = FunctionStringToString<NormalizeUTF8Impl<NormalizeNFCImpl>, NormalizeNFCImpl>;
using FunctionNormalizeUTF8NFD = FunctionStringToString<NormalizeUTF8Impl<NormalizeNFDImpl>, NormalizeNFDImpl>;
using FunctionNormalizeUTF8NFKC = FunctionStringToString<NormalizeUTF8Impl<NormalizeNFKCImpl>, NormalizeNFKCImpl>;
using FunctionNormalizeUTF8NFKD = FunctionStringToString<NormalizeUTF8Impl<NormalizeNFKDImpl>, NormalizeNFKDImpl>;
}

REGISTER_FUNCTION(NormalizeUTF8)
{
    factory.registerFunction<FunctionNormalizeUTF8NFC>();
    factory.registerFunction<FunctionNormalizeUTF8NFD>();
    factory.registerFunction<FunctionNormalizeUTF8NFKC>();
    factory.registerFunction<FunctionNormalizeUTF8NFKD>();
}

}

#endif