2017-03-25 18:11:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/EmbeddedDictionaries.h>
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Dictionaries/Embedded/RegionsHierarchy.h>
|
|
|
|
#include <Dictionaries/Embedded/RegionsHierarchies.h>
|
|
|
|
#include <Dictionaries/Embedded/RegionsNames.h>
|
2019-06-24 11:17:15 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2020-04-20 02:39:43 +00:00
|
|
|
#include <Core/Defines.h>
|
2017-04-19 00:25:57 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include <Common/config.h>
|
|
|
|
#endif
|
|
|
|
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int DICTIONARIES_WAS_NOT_LOADED;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
2019-03-14 23:10:51 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2017-06-13 02:06:53 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2017-03-25 18:11:09 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** Functions using Yandex.Metrica dictionaries
|
|
|
|
* - dictionaries of regions, operating systems, search engines.
|
2017-03-25 18:11:09 +00:00
|
|
|
*
|
2017-05-13 22:19:04 +00:00
|
|
|
* Climb up the tree to a certain level.
|
2017-03-25 18:11:09 +00:00
|
|
|
* regionToCity, regionToArea, regionToCountry, ...
|
|
|
|
*
|
2017-05-13 22:19:04 +00:00
|
|
|
* Convert values of a column
|
2017-03-25 18:11:09 +00:00
|
|
|
* regionToName
|
|
|
|
*
|
2017-05-13 22:19:04 +00:00
|
|
|
* Whether the first identifier is a descendant of the second.
|
2017-03-28 04:59:05 +00:00
|
|
|
* regionIn
|
2017-03-25 18:11:09 +00:00
|
|
|
*
|
2017-05-13 22:19:04 +00:00
|
|
|
* Get an array of region identifiers, consisting of the source and the parents chain. Order implementation defined.
|
2017-03-28 04:59:05 +00:00
|
|
|
* regionHierarchy
|
2017-03-25 18:11:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
struct RegionToCityImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toCity(x); }
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionToAreaImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toArea(x); }
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionToDistrictImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toDistrict(x); }
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionToCountryImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toCountry(x); }
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionToContinentImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toContinent(x); }
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionToTopContinentImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toTopContinent(x); }
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionToPopulationImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.getPopulation(x); }
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 00:25:57 +00:00
|
|
|
struct RegionInImpl
|
|
|
|
{
|
|
|
|
static bool apply(UInt32 x, UInt32 y, const RegionsHierarchy & hierarchy) { return hierarchy.in(x, y); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionHierarchyImpl
|
|
|
|
{
|
|
|
|
static UInt32 toParent(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toParent(x); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** Auxiliary thing, allowing to get from the dictionary a specific dictionary, corresponding to the point of view
|
|
|
|
* (the dictionary key passed as function argument).
|
|
|
|
* Example: when calling regionToCountry(x, 'ua'), a dictionary can be used, in which Crimea refers to Ukraine.
|
2017-03-25 18:11:09 +00:00
|
|
|
*/
|
|
|
|
struct RegionsHierarchyGetter
|
|
|
|
{
|
2017-12-11 18:05:16 +00:00
|
|
|
using Src = const RegionsHierarchies;
|
|
|
|
using Dst = const RegionsHierarchy;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
2017-12-11 18:05:16 +00:00
|
|
|
static Dst & get(Src & src, const std::string & key)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return src.get(key);
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** For dictionaries without key support. Doing nothing.
|
2017-03-25 18:11:09 +00:00
|
|
|
*/
|
|
|
|
template <typename Dict>
|
|
|
|
struct IdentityDictionaryGetter
|
|
|
|
{
|
2017-12-11 18:05:16 +00:00
|
|
|
using Src = const Dict;
|
|
|
|
using Dst = const Dict;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
2017-12-11 18:05:16 +00:00
|
|
|
static Dst & get(Src & src, const std::string & key)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (key.empty())
|
|
|
|
return src;
|
|
|
|
else
|
|
|
|
throw Exception("Dictionary doesn't support 'point of view' keys.", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Converts an identifier using a dictionary.
|
2017-03-25 18:11:09 +00:00
|
|
|
template <typename T, typename Transform, typename DictGetter, typename Name>
|
|
|
|
class FunctionTransformWithDictionary : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
using base_type = FunctionTransformWithDictionary;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::shared_ptr<typename DictGetter::Src> owned_dict;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionTransformWithDictionary(const std::shared_ptr<typename DictGetter::Src> & owned_dict_)
|
|
|
|
: owned_dict(owned_dict_)
|
|
|
|
{
|
|
|
|
if (!owned_dict)
|
2019-02-04 19:50:21 +00:00
|
|
|
throw Exception("Embedded dictionaries were not loaded. You need to check configuration file.", ErrorCodes::DICTIONARIES_WAS_NOT_LOADED);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 1 && arguments.size() != 2)
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be 1 or 2.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
if (arguments[0]->getName() != TypeName<T>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<T>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
if (arguments.size() == 2 && arguments[1]->getName() != TypeName<String>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[1]->getName() + " of the second ('point of view') argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<T>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return arguments[0];
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
2018-08-08 11:26:18 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
2018-02-21 17:05:21 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-05-13 22:19:04 +00:00
|
|
|
/// The dictionary key that defines the "point of view".
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string dict_key;
|
|
|
|
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const ColumnConst * key_col = checkAndGetColumnConst<ColumnString>(arguments[1].column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!key_col)
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[1].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of second ('point of view') argument of function " + name
|
|
|
|
+ ". Must be constant string.",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
dict_key = key_col->getValue<String>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const typename DictGetter::Dst & dict = DictGetter::get(*owned_dict, dict_key);
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
if (const ColumnVector<T> * col_from = checkAndGetColumn<ColumnVector<T>>(arguments[0].column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnVector<T>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & vec_from = col_from->getData();
|
|
|
|
typename ColumnVector<T>::Container & vec_to = col_to->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from.size();
|
|
|
|
vec_to.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
vec_to[i] = Transform::apply(vec_from[i], dict);
|
2017-12-16 05:21:04 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_to;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of first argument of function " + name,
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Checks belonging using a dictionary.
|
2017-03-25 18:11:09 +00:00
|
|
|
template <typename T, typename Transform, typename DictGetter, typename Name>
|
|
|
|
class FunctionIsInWithDictionary : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
using base_type = FunctionIsInWithDictionary;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::shared_ptr<typename DictGetter::Src> owned_dict;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionIsInWithDictionary(const std::shared_ptr<typename DictGetter::Src> & owned_dict_)
|
|
|
|
: owned_dict(owned_dict_)
|
|
|
|
{
|
|
|
|
if (!owned_dict)
|
2019-02-04 19:50:21 +00:00
|
|
|
throw Exception("Embedded dictionaries were not loaded. You need to check configuration file.", ErrorCodes::DICTIONARIES_WAS_NOT_LOADED);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 2 && arguments.size() != 3)
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be 2 or 3.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
if (arguments[0]->getName() != TypeName<T>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<T>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
if (arguments[1]->getName() != TypeName<T>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[1]->getName() + " of second argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<T>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
if (arguments.size() == 3 && arguments[2]->getName() != TypeName<String>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[2]->getName() + " of the third ('point of view') argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<String>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
2018-08-08 11:26:18 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
2018-02-21 17:05:21 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-05-13 22:19:04 +00:00
|
|
|
/// The dictionary key that defines the "point of view".
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string dict_key;
|
|
|
|
|
|
|
|
if (arguments.size() == 3)
|
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const ColumnConst * key_col = checkAndGetColumnConst<ColumnString>(arguments[2].column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!key_col)
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[2].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of third ('point of view') argument of function " + name
|
|
|
|
+ ". Must be constant string.",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
dict_key = key_col->getValue<String>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const typename DictGetter::Dst & dict = DictGetter::get(*owned_dict, dict_key);
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
const ColumnVector<T> * col_vec1 = checkAndGetColumn<ColumnVector<T>>(arguments[0].column.get());
|
|
|
|
const ColumnVector<T> * col_vec2 = checkAndGetColumn<ColumnVector<T>>(arguments[1].column.get());
|
|
|
|
const ColumnConst * col_const1 = checkAndGetColumnConst<ColumnVector<T>>(arguments[0].column.get());
|
|
|
|
const ColumnConst * col_const2 = checkAndGetColumnConst<ColumnVector<T>>(arguments[1].column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (col_vec1 && col_vec2)
|
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnUInt8::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & vec_from1 = col_vec1->getData();
|
|
|
|
const typename ColumnVector<T>::Container & vec_from2 = col_vec2->getData();
|
|
|
|
typename ColumnUInt8::Container & vec_to = col_to->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from1.size();
|
|
|
|
vec_to.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
vec_to[i] = Transform::apply(vec_from1[i], vec_from2[i], dict);
|
2017-12-16 05:21:04 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_to;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (col_vec1 && col_const2)
|
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnUInt8::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & vec_from1 = col_vec1->getData();
|
2017-07-21 06:35:58 +00:00
|
|
|
const T const_from2 = col_const2->template getValue<T>();
|
2017-12-15 21:32:25 +00:00
|
|
|
typename ColumnUInt8::Container & vec_to = col_to->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from1.size();
|
|
|
|
vec_to.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
vec_to[i] = Transform::apply(vec_from1[i], const_from2, dict);
|
2017-12-16 05:21:04 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_to;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (col_const1 && col_vec2)
|
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnUInt8::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const T const_from1 = col_const1->template getValue<T>();
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & vec_from2 = col_vec2->getData();
|
|
|
|
typename ColumnUInt8::Container & vec_to = col_to->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from2.size();
|
|
|
|
vec_to.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
vec_to[i] = Transform::apply(const_from1, vec_from2[i], dict);
|
2017-12-16 05:21:04 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_to;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (col_const1 && col_const2)
|
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
return DataTypeUInt8().createColumnConst(col_const1->size(),
|
2017-07-21 06:35:58 +00:00
|
|
|
toField(Transform::apply(col_const1->template getValue<T>(), col_const2->template getValue<T>(), dict)));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal columns " + arguments[0].column->getName()
|
|
|
|
+ " and " + arguments[1].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of arguments of function " + name,
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Gets an array of identifiers consisting of the source and the parents chain.
|
2017-03-25 18:11:09 +00:00
|
|
|
template <typename T, typename Transform, typename DictGetter, typename Name>
|
|
|
|
class FunctionHierarchyWithDictionary : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
using base_type = FunctionHierarchyWithDictionary;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::shared_ptr<typename DictGetter::Src> owned_dict;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionHierarchyWithDictionary(const std::shared_ptr<typename DictGetter::Src> & owned_dict_)
|
|
|
|
: owned_dict(owned_dict_)
|
|
|
|
{
|
|
|
|
if (!owned_dict)
|
2019-02-04 19:50:21 +00:00
|
|
|
throw Exception("Embedded dictionaries were not loaded. You need to check configuration file.", ErrorCodes::DICTIONARIES_WAS_NOT_LOADED);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 1 && arguments.size() != 2)
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be 1 or 2.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
if (arguments[0]->getName() != TypeName<T>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<T>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
if (arguments.size() == 2 && arguments[1]->getName() != TypeName<String>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[1]->getName() + " of the second ('point of view') argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<String>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeArray>(arguments[0]);
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
2018-08-08 11:26:18 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
2018-02-21 17:05:21 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-05-13 22:19:04 +00:00
|
|
|
/// The dictionary key that defines the "point of view".
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string dict_key;
|
|
|
|
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const ColumnConst * key_col = checkAndGetColumnConst<ColumnString>(arguments[1].column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!key_col)
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[1].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of second ('point of view') argument of function " + name
|
|
|
|
+ ". Must be constant string.",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
dict_key = key_col->getValue<String>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const typename DictGetter::Dst & dict = DictGetter::get(*owned_dict, dict_key);
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
if (const ColumnVector<T> * col_from = checkAndGetColumn<ColumnVector<T>>(arguments[0].column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_values = ColumnVector<T>::create();
|
2017-12-16 05:34:07 +00:00
|
|
|
auto col_offsets = ColumnArray::ColumnOffsets::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 05:34:07 +00:00
|
|
|
auto & res_offsets = col_offsets->getData();
|
|
|
|
auto & res_values = col_values->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & vec_from = col_from->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from.size();
|
|
|
|
res_offsets.resize(size);
|
|
|
|
res_values.reserve(size * 4);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
T cur = vec_from[i];
|
2020-04-20 02:39:43 +00:00
|
|
|
for (size_t depth = 0; cur && depth < DBMS_HIERARCHICAL_DICTIONARY_MAX_DEPTH; ++depth)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
res_values.push_back(cur);
|
|
|
|
cur = Transform::toParent(cur, dict);
|
|
|
|
}
|
|
|
|
res_offsets[i] = res_values.size();
|
|
|
|
}
|
2017-12-16 05:34:07 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return ColumnArray::create(std::move(col_values), std::move(col_offsets));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2017-12-16 05:34:07 +00:00
|
|
|
+ " of first argument of function " + name,
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct NameRegionToCity { static constexpr auto name = "regionToCity"; };
|
|
|
|
struct NameRegionToArea { static constexpr auto name = "regionToArea"; };
|
|
|
|
struct NameRegionToDistrict { static constexpr auto name = "regionToDistrict"; };
|
2017-04-19 00:25:57 +00:00
|
|
|
struct NameRegionToCountry { static constexpr auto name = "regionToCountry"; };
|
|
|
|
struct NameRegionToContinent { static constexpr auto name = "regionToContinent"; };
|
2017-04-01 07:20:54 +00:00
|
|
|
struct NameRegionToTopContinent { static constexpr auto name = "regionToTopContinent"; };
|
2017-04-19 00:25:57 +00:00
|
|
|
struct NameRegionToPopulation { static constexpr auto name = "regionToPopulation"; };
|
|
|
|
struct NameRegionHierarchy { static constexpr auto name = "regionHierarchy"; };
|
|
|
|
struct NameRegionIn { static constexpr auto name = "regionIn"; };
|
|
|
|
|
|
|
|
|
2017-03-25 18:11:09 +00:00
|
|
|
struct FunctionRegionToCity :
|
2017-04-01 07:20:54 +00:00
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToCityImpl, RegionsHierarchyGetter, NameRegionToCity>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionRegionToArea :
|
2017-04-01 07:20:54 +00:00
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToAreaImpl, RegionsHierarchyGetter, NameRegionToArea>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionRegionToDistrict :
|
2017-04-01 07:20:54 +00:00
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToDistrictImpl, RegionsHierarchyGetter, NameRegionToDistrict>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionRegionToCountry :
|
2017-04-01 07:20:54 +00:00
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToCountryImpl, RegionsHierarchyGetter, NameRegionToCountry>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionRegionToContinent :
|
2017-04-01 07:20:54 +00:00
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToContinentImpl, RegionsHierarchyGetter, NameRegionToContinent>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionRegionToTopContinent :
|
2017-04-01 07:20:54 +00:00
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToTopContinentImpl, RegionsHierarchyGetter, NameRegionToTopContinent>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionRegionToPopulation :
|
2017-04-01 07:20:54 +00:00
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToPopulationImpl, RegionsHierarchyGetter, NameRegionToPopulation>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 00:25:57 +00:00
|
|
|
struct FunctionRegionIn :
|
|
|
|
public FunctionIsInWithDictionary<UInt32, RegionInImpl, RegionsHierarchyGetter, NameRegionIn>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
2017-04-19 00:25:57 +00:00
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 00:25:57 +00:00
|
|
|
struct FunctionRegionHierarchy :
|
|
|
|
public FunctionHierarchyWithDictionary<UInt32, RegionHierarchyImpl, RegionsHierarchyGetter, NameRegionHierarchy>
|
2017-03-25 18:11:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
2017-04-19 00:25:57 +00:00
|
|
|
return std::make_shared<base_type>(context.getEmbeddedDictionaries().getRegionsHierarchies());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 00:25:57 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Converts a region's numeric identifier to a name in the specified language using a dictionary.
|
2017-03-25 18:11:09 +00:00
|
|
|
class FunctionRegionToName : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "regionToName";
|
|
|
|
static FunctionPtr create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionRegionToName>(context.getEmbeddedDictionaries().getRegionsNames());
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
private:
|
2017-12-11 18:05:16 +00:00
|
|
|
const MultiVersion<RegionsNames>::Version owned_dict;
|
2017-03-25 18:11:09 +00:00
|
|
|
|
|
|
|
public:
|
2017-12-11 18:05:16 +00:00
|
|
|
FunctionRegionToName(const MultiVersion<RegionsNames>::Version & owned_dict_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: owned_dict(owned_dict_)
|
|
|
|
{
|
|
|
|
if (!owned_dict)
|
2019-02-04 19:50:21 +00:00
|
|
|
throw Exception("Embedded dictionaries were not loaded. You need to check configuration file.", ErrorCodes::DICTIONARIES_WAS_NOT_LOADED);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
/// For the purpose of query optimization, we assume this function to be injective
|
|
|
|
/// even in face of fact that there are many different cities named Moscow.
|
2020-10-09 07:41:28 +00:00
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 1 && arguments.size() != 2)
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be 1 or 2.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
if (arguments[0]->getName() != TypeName<UInt32>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of the first argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<UInt32>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
if (arguments.size() == 2 && arguments[1]->getName() != TypeName<String>::get())
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of the second argument of function " + getName()
|
2017-07-16 03:05:40 +00:00
|
|
|
+ " (must be " + String(TypeName<String>::get()) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
2018-08-08 11:26:18 +00:00
|
|
|
bool isDeterministic() const override { return false; }
|
2018-02-21 17:05:21 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-12-12 19:34:58 +00:00
|
|
|
RegionsNames::Language language = RegionsNames::Language::ru;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// If the result language is specified
|
2017-04-01 07:20:54 +00:00
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
if (const ColumnConst * col_language = checkAndGetColumnConst<ColumnString>(arguments[1].column.get()))
|
2017-07-21 06:35:58 +00:00
|
|
|
language = RegionsNames::getLanguageEnum(col_language->getValue<String>());
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[1].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of the second argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
const RegionsNames & dict = *owned_dict;
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
if (const ColumnUInt32 * col_from = typeid_cast<const ColumnUInt32 *>(arguments[0].column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnString::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnUInt32::Container & region_ids = col_from->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < region_ids.size(); ++i)
|
|
|
|
{
|
|
|
|
const StringRef & name_ref = dict.getRegionName(region_ids[i], language);
|
|
|
|
col_to->insertDataWithTerminatingZero(name_ref.data, name_ref.size + 1);
|
|
|
|
}
|
2017-12-16 05:21:04 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_to;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of the first argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2017-03-25 18:11:09 +00:00
|
|
|
};
|
|
|
|
|
2018-08-26 00:52:58 +00:00
|
|
|
}
|