ClickHouse/src/Functions/URL/FunctionsURL.h

164 lines
4.9 KiB
C++
Raw Normal View History

2012-07-16 03:42:36 +00:00
#pragma once
#include <Columns/ColumnString.h>
#include <Common/memcpySmall.h>
2012-07-16 03:42:36 +00:00
2017-03-12 11:09:25 +00:00
2012-07-16 03:42:36 +00:00
namespace DB
{
/** URL processing functions. See implementation in separate .cpp files.
* All functions are not strictly follow RFC, instead they are maximally simplified for performance reasons.
2012-07-16 03:42:36 +00:00
*
* Functions for extraction parts of URL.
* If URL has nothing like, then empty string is returned.
*
2012-07-16 03:42:36 +00:00
* domain
* domainWithoutWWW
* topLevelDomain
* protocol
* path
* queryString
* fragment
* queryStringAndFragment
2020-06-02 09:10:10 +00:00
* netloc
2012-07-16 03:42:36 +00:00
*
* Functions, removing parts from URL.
2019-01-22 19:56:53 +00:00
* If URL has nothing like, then it is returned unchanged.
*
2012-07-16 03:42:36 +00:00
* cutWWW
* cutFragment
* cutQueryString
* cutQueryStringAndFragment
2012-07-21 03:45:48 +00:00
*
* Extract value of parameter in query string or in fragment identifier. Return empty string, if URL has no such parameter.
* If there are many parameters with same name - return value of first one. Value is not %-decoded.
2012-07-21 03:45:48 +00:00
*
* extractURLParameter(URL, name)
*
* Extract all parameters from URL in form of array of strings name=value.
* extractURLParameters(URL)
*
* Extract names of all parameters from URL in form of array of strings.
* extractURLParameterNames(URL)
*
* Remove specified parameter from URL.
* cutURLParameter(URL, name)
*
* Get array of URL 'hierarchy' as in Yandex.Metrica tree-like reports. See docs.
* URLHierarchy(URL)
2012-07-16 03:42:36 +00:00
*/
namespace ErrorCodes
2012-07-16 03:42:36 +00:00
{
extern const int ILLEGAL_COLUMN;
}
2012-07-16 03:42:36 +00:00
using Pos = const char *;
2017-05-27 15:45:25 +00:00
/** Select part of string using the Extractor.
2012-07-16 03:42:36 +00:00
*/
template <typename Extractor>
struct ExtractSubstringImpl
{
static void vector(const ColumnString::Chars & data, const ColumnString::Offsets & offsets,
ColumnString::Chars & res_data, ColumnString::Offsets & res_offsets)
{
size_t size = offsets.size();
res_offsets.resize(size);
res_data.reserve(size * Extractor::getReserveLengthForElement());
size_t prev_offset = 0;
size_t res_offset = 0;
2017-05-27 15:45:25 +00:00
/// Matched part.
Pos start;
size_t length;
for (size_t i = 0; i < size; ++i)
{
Extractor::execute(reinterpret_cast<const char *>(&data[prev_offset]), offsets[i] - prev_offset - 1, start, length);
res_data.resize(res_data.size() + length + 1);
memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], start, length);
res_offset += length + 1;
res_data[res_offset - 1] = 0;
res_offsets[i] = res_offset;
prev_offset = offsets[i];
}
}
static void constant(const std::string & data,
std::string & res_data)
{
Pos start;
size_t length;
Extractor::execute(data.data(), data.size(), start, length);
res_data.assign(start, length);
}
static void vectorFixed(const ColumnString::Chars &, size_t, ColumnString::Chars &)
{
throw Exception("Column of type FixedString is not supported by URL functions", ErrorCodes::ILLEGAL_COLUMN);
}
2012-07-16 03:42:36 +00:00
};
2017-05-27 15:45:25 +00:00
/** Delete part of string using the Extractor.
2012-07-16 03:42:36 +00:00
*/
template <typename Extractor>
struct CutSubstringImpl
{
static void vector(const ColumnString::Chars & data, const ColumnString::Offsets & offsets,
ColumnString::Chars & res_data, ColumnString::Offsets & res_offsets)
{
res_data.reserve(data.size());
size_t size = offsets.size();
res_offsets.resize(size);
size_t prev_offset = 0;
size_t res_offset = 0;
2017-05-27 15:45:25 +00:00
/// Matched part.
Pos start;
size_t length;
for (size_t i = 0; i < size; ++i)
{
const char * current = reinterpret_cast<const char *>(&data[prev_offset]);
Extractor::execute(current, offsets[i] - prev_offset - 1, start, length);
size_t start_index = start - reinterpret_cast<const char *>(data.data());
res_data.resize(res_data.size() + offsets[i] - prev_offset - length);
memcpySmallAllowReadWriteOverflow15(
&res_data[res_offset], current, start - current);
memcpySmallAllowReadWriteOverflow15(
&res_data[res_offset + start - current], start + length, offsets[i] - start_index - length);
res_offset += offsets[i] - prev_offset - length;
res_offsets[i] = res_offset;
prev_offset = offsets[i];
}
}
static void constant(const std::string & data,
std::string & res_data)
{
Pos start;
size_t length;
Extractor::execute(data.data(), data.size(), start, length);
res_data.reserve(data.size() - length);
res_data.append(data.data(), start);
res_data.append(start + length, data.data() + data.size());
}
static void vectorFixed(const ColumnString::Chars &, size_t, ColumnString::Chars &)
{
throw Exception("Column of type FixedString is not supported by URL functions", ErrorCodes::ILLEGAL_COLUMN);
}
2012-07-16 03:42:36 +00:00
};
}