ClickHouse
42 строки · 1.3 Кб
1#include <Functions/FunctionFactory.h>
2#include <Functions/FunctionStringToString.h>
3#include <Functions/StringHelpers.h>
4#include <base/find_symbols.h>
5
6namespace DB
7{
8
9struct FirstLine
10{
11static size_t getReserveLengthForElement() { return 16; }
12
13static void execute(Pos data, size_t size, Pos & res_data, size_t & res_size)
14{
15res_data = data;
16
17const Pos end = data + size;
18const Pos pos = find_first_symbols<'\r', '\n'>(data, end);
19res_size = pos - data;
20}
21};
22
23struct NameFirstLine
24{
25static constexpr auto name = "firstLine";
26};
27
28using FunctionFirstLine = FunctionStringToString<ExtractSubstringImpl<FirstLine>, NameFirstLine>;
29
30REGISTER_FUNCTION(FirstLine)
31{
32factory.registerFunction<FunctionFirstLine>(FunctionDocumentation{
33.description = "Returns first line of a multi-line string.",
34.syntax = "firstLine(string)",
35.arguments = {{.name = "string", .description = "The string to process."}},
36.returned_value = {"The first line of the string or the whole string if there is no line separators."},
37.examples = {
38{.name = "Return first line", .query = "firstLine('Hello\\nWorld')", .result = "'Hello'"},
39{.name = "Return whole string", .query = "firstLine('Hello World')", .result = "'Hello World'"},
40}});
41}
42}
43