1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionStringToString.h>
3#include "domain.h"
4
5namespace DB
6{
7
8struct ExtractTopLevelDomain
9{
10 static size_t getReserveLengthForElement() { return 5; }
11
12 static void execute(Pos data, size_t size, Pos & res_data, size_t & res_size)
13 {
14 StringRef host = getURLHost(data, size);
15
16 res_data = data;
17 res_size = 0;
18
19 if (host.size != 0)
20 {
21 if (host.data[host.size - 1] == '.')
22 host.size -= 1;
23
24 auto host_end = host.data + host.size;
25
26 Pos last_dot = find_last_symbols_or_null<'.'>(host.data, host_end);
27 if (!last_dot)
28 return;
29
30 /// For IPv4 addresses select nothing.
31 if (last_dot[1] <= '9')
32 return;
33
34 res_data = last_dot + 1;
35 res_size = host_end - res_data;
36 }
37 }
38};
39
40struct NameTopLevelDomain { static constexpr auto name = "topLevelDomain"; };
41using FunctionTopLevelDomain = FunctionStringToString<ExtractSubstringImpl<ExtractTopLevelDomain>, NameTopLevelDomain>;
42
43void registerFunctionTopLevelDomain(FunctionFactory & factory)
44{
45 factory.registerFunction<FunctionTopLevelDomain>();
46}
47
48}
49