Merge pull request #5136 from PerformanceVision/basename

Add a basename function
This commit is contained in:
alexey-milovidov 2019-04-30 19:15:35 +03:00 committed by GitHub
commit e7ec3b9eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 0 deletions

View 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>();
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,5 @@
bash
bash
test_file
script.php

View 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'))