Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added function firstLine to extract the first line from a multiline s…
…tring Fixes #51172
- Loading branch information
Showing
7 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <Functions/FunctionFactory.h> | ||
#include <Functions/FunctionStringToString.h> | ||
#include <Functions/StringHelpers.h> | ||
#include <base/find_symbols.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
struct FirstLine | ||
{ | ||
static size_t getReserveLengthForElement() { return 16; } | ||
|
||
static void execute(Pos data, size_t size, Pos & res_data, size_t & res_size) | ||
{ | ||
res_data = data; | ||
|
||
const Pos end = data + size; | ||
const Pos pos = find_first_symbols<'\r', '\n'>(data, end); | ||
res_size = pos - data; | ||
} | ||
}; | ||
|
||
struct NameFirstLine | ||
{ | ||
static constexpr auto name = "firstLine"; | ||
}; | ||
|
||
using FunctionFirstLine = FunctionStringToString<ExtractSubstringImpl<FirstLine>, NameFirstLine>; | ||
|
||
REGISTER_FUNCTION(FirstLine) | ||
{ | ||
factory.registerFunction<FunctionFirstLine>(FunctionDocumentation{ | ||
.description = "Returns first line of a multi-line string.", | ||
.syntax = "firstLine(string)", | ||
.arguments = {{.name = "string", .description = "The string to process."}}, | ||
.returned_value = {"The first line of the string or the whole string if there is no line separators."}, | ||
.examples = { | ||
{.name = "Return first line", .query = "firstLine('Hello\\nWorld')", .result = "'Hello'"}, | ||
{.name = "Return whole string", .query = "firstLine('Hello World')", .result = "'Hello World'"}, | ||
}}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
foo | ||
foo | ||
foo | ||
foobarbaz | ||
== vector | ||
1 foo | ||
2 quux | ||
3 single line | ||
4 windows |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
select firstLine('foo\nbar\nbaz'); | ||
select firstLine('foo\rbar\rbaz'); | ||
select firstLine('foo\r\nbar\r\nbaz'); | ||
select firstLine('foobarbaz'); | ||
|
||
select '== vector'; | ||
|
||
drop table if exists 02815_first_line_vector; | ||
create table 02815_first_line_vector (n Int32, text String) engine = MergeTree order by n; | ||
|
||
insert into 02815_first_line_vector values (1, 'foo\nbar\nbaz'), (2, 'quux\n'), (3, 'single line'), (4, 'windows\r\nline breaks'); | ||
select n, firstLine(text) from 02815_first_line_vector order by n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters