mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-22 07:31:57 +00:00
Merge pull request #5136 from PerformanceVision/basename
Add a basename function
This commit is contained in:
commit
e7ec3b9eb8
42
dbms/src/Functions/basename.cpp
Normal file
42
dbms/src/Functions/basename.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include <Functions/FunctionFactory.h>
|
||||
#include <Functions/FunctionStringToString.h>
|
||||
#include <Functions/FunctionsURL.h>
|
||||
#include <common/find_symbols.h>
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
/** Extract substring after the last slash or backslash.
|
||||
* If there are no slashes, return the string unchanged.
|
||||
* It is used to extract filename from path.
|
||||
*/
|
||||
struct ExtractBasename
|
||||
{
|
||||
static size_t getReserveLengthForElement() { return 16; } /// Just a guess.
|
||||
|
||||
static void execute(Pos data, size_t size, Pos & res_data, size_t & res_size)
|
||||
{
|
||||
res_data = data;
|
||||
res_size = size;
|
||||
|
||||
Pos pos = data;
|
||||
Pos end = pos + size;
|
||||
|
||||
if ((pos = find_last_symbols_or_null<'/', '\\'>(pos, end)))
|
||||
{
|
||||
++pos;
|
||||
res_data = pos;
|
||||
res_size = end - pos;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct NameBasename { static constexpr auto name = "basename"; };
|
||||
using FunctionBasename = FunctionStringToString<ExtractSubstringImpl<ExtractBasename>, NameBasename>;
|
||||
|
||||
void registerFunctionBasename(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionBasename>();
|
||||
}
|
||||
|
||||
}
|
@ -42,6 +42,7 @@ void registerFunctionLowCardinalityKeys(FunctionFactory &);
|
||||
void registerFunctionsIn(FunctionFactory &);
|
||||
void registerFunctionJoinGet(FunctionFactory &);
|
||||
void registerFunctionFilesystem(FunctionFactory &);
|
||||
void registerFunctionBasename(FunctionFactory &);
|
||||
|
||||
void registerFunctionsMiscellaneous(FunctionFactory & factory)
|
||||
{
|
||||
@ -84,6 +85,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory)
|
||||
registerFunctionsIn(factory);
|
||||
registerFunctionJoinGet(factory);
|
||||
registerFunctionFilesystem(factory);
|
||||
registerFunctionBasename(factory);
|
||||
}
|
||||
|
||||
}
|
||||
|
5
dbms/tests/queries/0_stateless/00938_basename.reference
Normal file
5
dbms/tests/queries/0_stateless/00938_basename.reference
Normal file
@ -0,0 +1,5 @@
|
||||
bash
|
||||
|
||||
bash
|
||||
test_file
|
||||
script.php
|
5
dbms/tests/queries/0_stateless/00938_basename.sql
Normal file
5
dbms/tests/queries/0_stateless/00938_basename.sql
Normal file
@ -0,0 +1,5 @@
|
||||
SELECT basename('/usr/bin/bash');
|
||||
SELECT basename('/usr/bin/bash/');
|
||||
SELECT basename('bash');
|
||||
SELECT basename('C:\\\\Users\\Documents\\test_file');
|
||||
SELECT basename(path('http://example.com/folder_1/folder_2/script.php'))
|
Loading…
Reference in New Issue
Block a user