2012-12-21 19:48:47 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2013-03-06 12:03:52 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeArray.h>
|
2013-06-05 13:40:14 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
2015-03-20 15:21:29 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeDate.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeDateTime.h>
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Columns/ColumnVector.h>
|
2013-03-06 12:03:52 +00:00
|
|
|
|
#include <DB/Columns/ColumnArray.h>
|
2013-06-05 13:40:14 +00:00
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Interpreters/Context.h>
|
2015-04-16 06:12:35 +00:00
|
|
|
|
#include <DB/Interpreters/Dictionaries.h>
|
|
|
|
|
#include <DB/Interpreters/ExternalDictionaries.h>
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Functions/IFunction.h>
|
2015-01-29 14:46:15 +00:00
|
|
|
|
#include <DB/Dictionaries/FlatDictionary.h>
|
|
|
|
|
#include <DB/Dictionaries/HashedDictionary.h>
|
2015-02-10 14:50:43 +00:00
|
|
|
|
#include <DB/Dictionaries/CacheDictionary.h>
|
2015-07-13 16:18:28 +00:00
|
|
|
|
#include <DB/Dictionaries/RangeHashedDictionary.h>
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
|
#include <statdaemons/ext/range.hpp>
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Функции, использующие словари Яндекс.Метрики
|
|
|
|
|
* - словари регионов, операционных систем, поисковых систем.
|
|
|
|
|
*
|
2013-03-18 10:49:31 +00:00
|
|
|
|
* Подняться по дереву до определенного уровня.
|
|
|
|
|
* regionToCity, regionToArea, regionToCountry,
|
|
|
|
|
* OSToRoot,
|
|
|
|
|
* SEToRoot,
|
2014-06-26 00:58:14 +00:00
|
|
|
|
*
|
2013-06-05 13:40:14 +00:00
|
|
|
|
* Преобразовать значения в столбце
|
|
|
|
|
* regionToName
|
2012-12-21 19:48:47 +00:00
|
|
|
|
*
|
2013-03-18 10:49:31 +00:00
|
|
|
|
* Является ли первый идентификатор потомком второго.
|
2015-02-02 21:22:07 +00:00
|
|
|
|
* regionIn, SEIn, OSIn.
|
2014-06-26 00:58:14 +00:00
|
|
|
|
*
|
2013-03-18 10:49:31 +00:00
|
|
|
|
* Получить массив идентификаторов регионов, состоящий из исходного и цепочки родителей. Порядок implementation defined.
|
2015-02-02 21:22:07 +00:00
|
|
|
|
* regionHierarchy, OSHierarchy, SEHierarchy.
|
2015-02-10 14:50:43 +00:00
|
|
|
|
*
|
|
|
|
|
* Функции, использующие подключаемые (внешние) словари.
|
|
|
|
|
*
|
|
|
|
|
* Получить значение аттрибута заданного типа.
|
|
|
|
|
* dictGetType(dictionary, attribute, id),
|
|
|
|
|
* Type - placeholder для имени типа, в данный момент поддерживаются любые числовые и строковой типы.
|
|
|
|
|
* Тип должен соответствовать реальному типу аттрибута, с которым он был объявлен в структуре словаря.
|
|
|
|
|
*
|
|
|
|
|
* Получить массив идентификаторов, состоящий из исходного и цепочки родителей.
|
|
|
|
|
* dictGetHierarchy(dictionary, id).
|
|
|
|
|
*
|
|
|
|
|
* Является ли первы йидентификатор потомком второго.
|
|
|
|
|
* dictIsIn(dictionary, child_id, parent_id).
|
2012-12-21 19:48:47 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct RegionToCityImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toCity(x); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct RegionToAreaImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toArea(x); }
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-19 18:51:24 +00:00
|
|
|
|
struct RegionToDistrictImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toDistrict(x); }
|
|
|
|
|
};
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
struct RegionToCountryImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toCountry(x); }
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-22 12:27:55 +00:00
|
|
|
|
struct RegionToContinentImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toContinent(x); }
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-20 00:05:14 +00:00
|
|
|
|
struct RegionToPopulationImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt32 apply(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.getPopulation(x); }
|
|
|
|
|
};
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
struct OSToRootImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt8 apply(UInt8 x, const TechDataHierarchy & hierarchy) { return hierarchy.OSToMostAncestor(x); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct SEToRootImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt8 apply(UInt8 x, const TechDataHierarchy & hierarchy) { return hierarchy.SEToMostAncestor(x); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct RegionInImpl
|
|
|
|
|
{
|
|
|
|
|
static bool apply(UInt32 x, UInt32 y, const RegionsHierarchy & hierarchy) { return hierarchy.in(x, y); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct OSInImpl
|
|
|
|
|
{
|
|
|
|
|
static bool apply(UInt32 x, UInt32 y, const TechDataHierarchy & hierarchy) { return hierarchy.isOSIn(x, y); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct SEInImpl
|
|
|
|
|
{
|
|
|
|
|
static bool apply(UInt32 x, UInt32 y, const TechDataHierarchy & hierarchy) { return hierarchy.isSEIn(x, y); }
|
|
|
|
|
};
|
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
struct RegionHierarchyImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt32 toParent(UInt32 x, const RegionsHierarchy & hierarchy) { return hierarchy.toParent(x); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct OSHierarchyImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt8 toParent(UInt8 x, const TechDataHierarchy & hierarchy) { return hierarchy.OSToParent(x); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct SEHierarchyImpl
|
|
|
|
|
{
|
|
|
|
|
static UInt8 toParent(UInt8 x, const TechDataHierarchy & hierarchy) { return hierarchy.SEToParent(x); }
|
|
|
|
|
};
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
2014-04-04 18:08:01 +00:00
|
|
|
|
/** Вспомогательная вещь, позволяющая достать из словаря конкретный словарь, соответствующий точке зрения
|
|
|
|
|
* (ключу словаря, передаваемому в аргументе функции).
|
|
|
|
|
* Пример: при вызове regionToCountry(x, 'ua'), может быть использован словарь, в котором Крым относится к Украине.
|
|
|
|
|
*/
|
|
|
|
|
struct RegionsHierarchyGetter
|
|
|
|
|
{
|
|
|
|
|
typedef RegionsHierarchies Src;
|
|
|
|
|
typedef RegionsHierarchy Dst;
|
|
|
|
|
|
|
|
|
|
static const Dst & get(const Src & src, const std::string & key)
|
|
|
|
|
{
|
|
|
|
|
return src.get(key);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Для словарей без поддержки ключей. Ничего не делает.
|
|
|
|
|
*/
|
|
|
|
|
template <typename Dict>
|
|
|
|
|
struct IdentityDictionaryGetter
|
|
|
|
|
{
|
|
|
|
|
typedef Dict Src;
|
|
|
|
|
typedef Dict Dst;
|
|
|
|
|
|
|
|
|
|
static const Dst & get(const Src & src, const std::string & key)
|
|
|
|
|
{
|
|
|
|
|
if (key.empty())
|
|
|
|
|
return src;
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Dictionary doesn't support 'point of view' keys.", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
/// Преобразует идентификатор, используя словарь.
|
2014-04-04 23:41:54 +00:00
|
|
|
|
template <typename T, typename Transform, typename DictGetter, typename Name>
|
2012-12-21 19:48:47 +00:00
|
|
|
|
class FunctionTransformWithDictionary : public IFunction
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
|
using base_type = FunctionTransformWithDictionary;
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
private:
|
2014-04-04 23:41:54 +00:00
|
|
|
|
const SharedPtr<typename DictGetter::Src> owned_dict;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
public:
|
2014-04-04 23:41:54 +00:00
|
|
|
|
FunctionTransformWithDictionary(const SharedPtr<typename DictGetter::Src> & owned_dict_)
|
2012-12-21 19:48:47 +00:00
|
|
|
|
: owned_dict(owned_dict_)
|
|
|
|
|
{
|
|
|
|
|
if (!owned_dict)
|
|
|
|
|
throw Exception("Dictionaries was not loaded. You need to check configuration file.", ErrorCodes::DICTIONARIES_WAS_NOT_LOADED);
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
/// Получить имя функции.
|
|
|
|
|
String getName() const
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-12-21 19:48:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const
|
|
|
|
|
{
|
2014-04-04 18:08:01 +00:00
|
|
|
|
if (arguments.size() != 1 && arguments.size() != 2)
|
2012-12-21 19:48:47 +00:00
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2014-04-04 18:08:01 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 1 or 2.",
|
2012-12-21 19:48:47 +00:00
|
|
|
|
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()
|
|
|
|
|
+ " (must be " + TypeName<T>::get() + ")",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
2014-04-04 18:08:01 +00:00
|
|
|
|
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()
|
|
|
|
|
+ " (must be " + TypeName<String>::get() + ")",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
return arguments[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
|
{
|
2014-04-04 18:08:01 +00:00
|
|
|
|
/// Ключ словаря, определяющий "точку зрения".
|
|
|
|
|
std::string dict_key;
|
|
|
|
|
|
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const ColumnConstString * key_col = typeid_cast<const ColumnConstString *>(&*block.getByPosition(arguments[1]).column);
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
|
|
|
|
if (!key_col)
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[1]).column->getName()
|
2014-11-12 17:23:26 +00:00
|
|
|
|
+ " of second ('point of view') argument of function " + name
|
2014-04-04 18:08:01 +00:00
|
|
|
|
+ ". Must be constant string.",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
|
|
dict_key = key_col->getData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const typename DictGetter::Dst & dict = DictGetter::get(*owned_dict, dict_key);
|
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (const ColumnVector<T> * col_from = typeid_cast<const ColumnVector<T> *>(&*block.getByPosition(arguments[0]).column))
|
2012-12-21 19:48:47 +00:00
|
|
|
|
{
|
|
|
|
|
ColumnVector<T> * col_to = new ColumnVector<T>;
|
|
|
|
|
block.getByPosition(result).column = col_to;
|
|
|
|
|
|
|
|
|
|
const typename ColumnVector<T>::Container_t & vec_from = col_from->getData();
|
|
|
|
|
typename ColumnVector<T>::Container_t & vec_to = col_to->getData();
|
|
|
|
|
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);
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
else if (const ColumnConst<T> * col_from = typeid_cast<const ColumnConst<T> *>(&*block.getByPosition(arguments[0]).column))
|
2012-12-21 19:48:47 +00:00
|
|
|
|
{
|
2014-04-04 18:08:01 +00:00
|
|
|
|
block.getByPosition(result).column = new ColumnConst<T>(col_from->size(), Transform::apply(col_from->getData(), dict));
|
2012-12-21 19:48:47 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2014-11-12 17:23:26 +00:00
|
|
|
|
+ " of first argument of function " + name,
|
2012-12-21 19:48:47 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Проверяет принадлежность, используя словарь.
|
2014-04-04 23:41:54 +00:00
|
|
|
|
template <typename T, typename Transform, typename DictGetter, typename Name>
|
2012-12-21 19:48:47 +00:00
|
|
|
|
class FunctionIsInWithDictionary : public IFunction
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
|
using base_type = FunctionIsInWithDictionary;
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
private:
|
2014-04-04 23:41:54 +00:00
|
|
|
|
const SharedPtr<typename DictGetter::Src> owned_dict;
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2014-04-04 23:41:54 +00:00
|
|
|
|
FunctionIsInWithDictionary(const SharedPtr<typename DictGetter::Src> & owned_dict_)
|
2012-12-21 19:48:47 +00:00
|
|
|
|
: owned_dict(owned_dict_)
|
|
|
|
|
{
|
|
|
|
|
if (!owned_dict)
|
|
|
|
|
throw Exception("Dictionaries was not loaded. You need to check configuration file.", ErrorCodes::DICTIONARIES_WAS_NOT_LOADED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить имя функции.
|
|
|
|
|
String getName() const
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-12-21 19:48:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const
|
|
|
|
|
{
|
2014-04-04 18:08:01 +00:00
|
|
|
|
if (arguments.size() != 2 && arguments.size() != 3)
|
2012-12-21 19:48:47 +00:00
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2014-04-04 18:08:01 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 2 or 3.",
|
2012-12-21 19:48:47 +00:00
|
|
|
|
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()
|
|
|
|
|
+ " (must be " + TypeName<T>::get() + ")",
|
|
|
|
|
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()
|
|
|
|
|
+ " (must be " + TypeName<T>::get() + ")",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
2014-04-04 18:08:01 +00:00
|
|
|
|
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()
|
|
|
|
|
+ " (must be " + TypeName<String>::get() + ")",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
return new DataTypeUInt8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
|
{
|
2014-04-04 18:08:01 +00:00
|
|
|
|
/// Ключ словаря, определяющий "точку зрения".
|
|
|
|
|
std::string dict_key;
|
|
|
|
|
|
|
|
|
|
if (arguments.size() == 3)
|
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const ColumnConstString * key_col = typeid_cast<const ColumnConstString *>(&*block.getByPosition(arguments[2]).column);
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
|
|
|
|
if (!key_col)
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[2]).column->getName()
|
2014-11-12 17:23:26 +00:00
|
|
|
|
+ " of third ('point of view') argument of function " + name
|
2014-04-04 18:08:01 +00:00
|
|
|
|
+ ". Must be constant string.",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
|
|
dict_key = key_col->getData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const typename DictGetter::Dst & dict = DictGetter::get(*owned_dict, dict_key);
|
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const ColumnVector<T> * col_vec1 = typeid_cast<const ColumnVector<T> *>(&*block.getByPosition(arguments[0]).column);
|
|
|
|
|
const ColumnVector<T> * col_vec2 = typeid_cast<const ColumnVector<T> *>(&*block.getByPosition(arguments[1]).column);
|
|
|
|
|
const ColumnConst<T> * col_const1 = typeid_cast<const ColumnConst<T> *>(&*block.getByPosition(arguments[0]).column);
|
|
|
|
|
const ColumnConst<T> * col_const2 = typeid_cast<const ColumnConst<T> *>(&*block.getByPosition(arguments[1]).column);
|
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
if (col_vec1 && col_vec2)
|
|
|
|
|
{
|
|
|
|
|
ColumnVector<UInt8> * col_to = new ColumnVector<UInt8>;
|
|
|
|
|
block.getByPosition(result).column = col_to;
|
|
|
|
|
|
|
|
|
|
const typename ColumnVector<T>::Container_t & vec_from1 = col_vec1->getData();
|
|
|
|
|
const typename ColumnVector<T>::Container_t & vec_from2 = col_vec2->getData();
|
|
|
|
|
typename ColumnVector<UInt8>::Container_t & vec_to = col_to->getData();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
else if (col_vec1 && col_const2)
|
|
|
|
|
{
|
|
|
|
|
ColumnVector<UInt8> * col_to = new ColumnVector<UInt8>;
|
|
|
|
|
block.getByPosition(result).column = col_to;
|
|
|
|
|
|
|
|
|
|
const typename ColumnVector<T>::Container_t & vec_from1 = col_vec1->getData();
|
|
|
|
|
const T const_from2 = col_const2->getData();
|
|
|
|
|
typename ColumnVector<UInt8>::Container_t & vec_to = col_to->getData();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
else if (col_const1 && col_vec2)
|
|
|
|
|
{
|
|
|
|
|
ColumnVector<UInt8> * col_to = new ColumnVector<UInt8>;
|
|
|
|
|
block.getByPosition(result).column = col_to;
|
|
|
|
|
|
|
|
|
|
const T const_from1 = col_const1->getData();
|
|
|
|
|
const typename ColumnVector<T>::Container_t & vec_from2 = col_vec2->getData();
|
|
|
|
|
typename ColumnVector<UInt8>::Container_t & vec_to = col_to->getData();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
else if (col_const1 && col_const2)
|
|
|
|
|
{
|
2013-04-10 10:52:38 +00:00
|
|
|
|
block.getByPosition(result).column = new ColumnConst<UInt8>(col_const1->size(),
|
2014-04-04 18:08:01 +00:00
|
|
|
|
Transform::apply(col_const1->getData(), col_const2->getData(), dict));
|
2012-12-21 19:48:47 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Illegal columns " + block.getByPosition(arguments[0]).column->getName()
|
|
|
|
|
+ " and " + block.getByPosition(arguments[1]).column->getName()
|
2014-11-12 17:23:26 +00:00
|
|
|
|
+ " of arguments of function " + name,
|
2012-12-21 19:48:47 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
/// Получает массив идентификаторов, состоящий из исходного и цепочки родителей.
|
2014-04-04 23:41:54 +00:00
|
|
|
|
template <typename T, typename Transform, typename DictGetter, typename Name>
|
2013-03-06 12:03:52 +00:00
|
|
|
|
class FunctionHierarchyWithDictionary : public IFunction
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
|
using base_type = FunctionHierarchyWithDictionary;
|
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
private:
|
2014-04-04 23:41:54 +00:00
|
|
|
|
const SharedPtr<typename DictGetter::Src> owned_dict;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
public:
|
2014-04-04 23:41:54 +00:00
|
|
|
|
FunctionHierarchyWithDictionary(const SharedPtr<typename DictGetter::Src> & owned_dict_)
|
2013-03-06 12:03:52 +00:00
|
|
|
|
: owned_dict(owned_dict_)
|
|
|
|
|
{
|
|
|
|
|
if (!owned_dict)
|
|
|
|
|
throw Exception("Dictionaries was not loaded. You need to check configuration file.", ErrorCodes::DICTIONARIES_WAS_NOT_LOADED);
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
/// Получить имя функции.
|
|
|
|
|
String getName() const
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2013-03-06 12:03:52 +00:00
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const
|
|
|
|
|
{
|
2014-04-04 18:08:01 +00:00
|
|
|
|
if (arguments.size() != 1 && arguments.size() != 2)
|
2013-03-06 12:03:52 +00:00
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2014-04-04 18:08:01 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 1 or 2.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
if (arguments[0]->getName() != TypeName<T>::get())
|
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName()
|
|
|
|
|
+ " (must be " + TypeName<T>::get() + ")",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
+ " (must be " + TypeName<String>::get() + ")",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
return new DataTypeArray(arguments[0]);
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
/// Выполнить функцию над блоком.
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
|
{
|
2014-04-04 18:08:01 +00:00
|
|
|
|
/// Ключ словаря, определяющий "точку зрения".
|
|
|
|
|
std::string dict_key;
|
|
|
|
|
|
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const ColumnConstString * key_col = typeid_cast<const ColumnConstString *>(&*block.getByPosition(arguments[1]).column);
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
|
|
|
|
if (!key_col)
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[1]).column->getName()
|
2014-11-12 17:23:26 +00:00
|
|
|
|
+ " of second ('point of view') argument of function " + name
|
2014-04-04 18:08:01 +00:00
|
|
|
|
+ ". Must be constant string.",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
|
|
dict_key = key_col->getData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const typename DictGetter::Dst & dict = DictGetter::get(*owned_dict, dict_key);
|
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (const ColumnVector<T> * col_from = typeid_cast<const ColumnVector<T> *>(&*block.getByPosition(arguments[0]).column))
|
2013-03-06 12:03:52 +00:00
|
|
|
|
{
|
|
|
|
|
ColumnVector<T> * col_values = new ColumnVector<T>;
|
|
|
|
|
ColumnArray * col_array = new ColumnArray(col_values);
|
|
|
|
|
block.getByPosition(result).column = col_array;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
ColumnArray::Offsets_t & res_offsets = col_array->getOffsets();
|
|
|
|
|
typename ColumnVector<T>::Container_t & res_values = col_values->getData();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
const typename ColumnVector<T>::Container_t & vec_from = col_from->getData();
|
|
|
|
|
size_t size = vec_from.size();
|
|
|
|
|
res_offsets.resize(size);
|
|
|
|
|
res_values.reserve(size * 4);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
T cur = vec_from[i];
|
|
|
|
|
while (cur)
|
|
|
|
|
{
|
|
|
|
|
res_values.push_back(cur);
|
|
|
|
|
cur = Transform::toParent(cur, dict);
|
|
|
|
|
}
|
|
|
|
|
res_offsets[i] = res_values.size();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
else if (const ColumnConst<T> * col_from = typeid_cast<const ColumnConst<T> *>(&*block.getByPosition(arguments[0]).column))
|
2013-03-06 12:03:52 +00:00
|
|
|
|
{
|
|
|
|
|
Array res;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-06 12:03:52 +00:00
|
|
|
|
T cur = col_from->getData();
|
|
|
|
|
while (cur)
|
|
|
|
|
{
|
2013-03-06 12:27:05 +00:00
|
|
|
|
res.push_back(static_cast<typename NearestFieldType<T>::Type>(cur));
|
2013-03-06 12:03:52 +00:00
|
|
|
|
cur = Transform::toParent(cur, dict);
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-27 10:07:23 +00:00
|
|
|
|
block.getByPosition(result).column = new ColumnConstArray(col_from->size(), res, new DataTypeArray(new typename DataTypeFromFieldType<T>::Type));
|
2013-03-06 12:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2014-11-12 17:23:26 +00:00
|
|
|
|
+ " of first argument of function " + name,
|
2013-03-06 12:03:52 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct NameRegionToCity { static constexpr auto name = "regionToCity"; };
|
|
|
|
|
struct NameRegionToArea { static constexpr auto name = "regionToArea"; };
|
|
|
|
|
struct NameRegionToDistrict { static constexpr auto name = "regionToDistrict"; };
|
|
|
|
|
struct NameRegionToCountry { static constexpr auto name = "regionToCountry"; };
|
2015-05-16 23:49:13 +00:00
|
|
|
|
struct NameRegionToContinent { static constexpr auto name = "regionToContinent"; };
|
2014-11-20 00:05:14 +00:00
|
|
|
|
struct NameRegionToPopulation { static constexpr auto name = "regionToPopulation"; };
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct NameOSToRoot { static constexpr auto name = "OSToRoot"; };
|
|
|
|
|
struct NameSEToRoot { static constexpr auto name = "SEToRoot"; };
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct NameRegionIn { static constexpr auto name = "regionIn"; };
|
|
|
|
|
struct NameOSIn { static constexpr auto name = "OSIn"; };
|
|
|
|
|
struct NameSEIn { static constexpr auto name = "SEIn"; };
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct NameRegionHierarchy { static constexpr auto name = "regionHierarchy"; };
|
|
|
|
|
struct NameOSHierarchy { static constexpr auto name = "OSHierarchy"; };
|
|
|
|
|
struct NameSEHierarchy { static constexpr auto name = "SEHierarchy"; };
|
2013-04-17 11:37:34 +00:00
|
|
|
|
|
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionRegionToCity :
|
|
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToCityImpl, RegionsHierarchyGetter, NameRegionToCity>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getRegionsHierarchies()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionRegionToArea :
|
|
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToAreaImpl, RegionsHierarchyGetter, NameRegionToArea>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getRegionsHierarchies()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionRegionToDistrict :
|
|
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToDistrictImpl, RegionsHierarchyGetter, NameRegionToDistrict>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getRegionsHierarchies()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-09-19 18:51:24 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionRegionToCountry :
|
|
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToCountryImpl, RegionsHierarchyGetter, NameRegionToCountry>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getRegionsHierarchies()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionRegionToContinent :
|
|
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToContinentImpl, RegionsHierarchyGetter, NameRegionToContinent>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getRegionsHierarchies()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-20 00:05:14 +00:00
|
|
|
|
struct FunctionRegionToPopulation :
|
|
|
|
|
public FunctionTransformWithDictionary<UInt32, RegionToPopulationImpl, RegionsHierarchyGetter, NameRegionToPopulation>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getRegionsHierarchies()};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionOSToRoot :
|
|
|
|
|
public FunctionTransformWithDictionary<UInt8, OSToRootImpl, IdentityDictionaryGetter<TechDataHierarchy>, NameOSToRoot>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getTechDataHierarchy()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionSEToRoot :
|
|
|
|
|
public FunctionTransformWithDictionary<UInt8, SEToRootImpl, IdentityDictionaryGetter<TechDataHierarchy>, NameSEToRoot>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getTechDataHierarchy()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionRegionIn :
|
|
|
|
|
public FunctionIsInWithDictionary<UInt32, RegionInImpl, RegionsHierarchyGetter, NameRegionIn>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getRegionsHierarchies()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-04-17 11:37:34 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionOSIn :
|
|
|
|
|
public FunctionIsInWithDictionary<UInt8, OSInImpl, IdentityDictionaryGetter<TechDataHierarchy>, NameOSIn>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getTechDataHierarchy()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionSEIn :
|
|
|
|
|
public FunctionIsInWithDictionary<UInt8, SEInImpl, IdentityDictionaryGetter<TechDataHierarchy>, NameSEIn>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getTechDataHierarchy()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionRegionHierarchy :
|
|
|
|
|
public FunctionHierarchyWithDictionary<UInt32, RegionHierarchyImpl, RegionsHierarchyGetter, NameRegionHierarchy>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getRegionsHierarchies()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionOSHierarchy :
|
|
|
|
|
public FunctionHierarchyWithDictionary<UInt8, OSHierarchyImpl, IdentityDictionaryGetter<TechDataHierarchy>, NameOSHierarchy>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getTechDataHierarchy()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
struct FunctionSEHierarchy :
|
|
|
|
|
public FunctionHierarchyWithDictionary<UInt8, SEHierarchyImpl, IdentityDictionaryGetter<TechDataHierarchy>, NameSEHierarchy>
|
|
|
|
|
{
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new base_type{context.getDictionaries().getTechDataHierarchy()};
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
2013-06-05 13:40:14 +00:00
|
|
|
|
|
|
|
|
|
/// Преобразует числовой идентификатор региона в имя на заданном языке, используя словарь.
|
|
|
|
|
class FunctionRegionToName : public IFunction
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "regionToName";
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
|
|
|
|
return new FunctionRegionToName(context.getDictionaries().getRegionsNames());
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-05 13:40:14 +00:00
|
|
|
|
private:
|
|
|
|
|
const SharedPtr<RegionsNames> owned_dict;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-05 13:40:14 +00:00
|
|
|
|
public:
|
|
|
|
|
FunctionRegionToName(const SharedPtr<RegionsNames> & owned_dict_)
|
|
|
|
|
: owned_dict(owned_dict_)
|
|
|
|
|
{
|
|
|
|
|
if (!owned_dict)
|
|
|
|
|
throw Exception("Dictionaries was not loaded. You need to check configuration file.", ErrorCodes::DICTIONARIES_WAS_NOT_LOADED);
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-05 13:40:14 +00:00
|
|
|
|
/// Получить имя функции.
|
|
|
|
|
String getName() const
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2013-06-05 13:40:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const
|
|
|
|
|
{
|
2013-06-05 13:54:05 +00:00
|
|
|
|
if (arguments.size() != 1 && arguments.size() != 2)
|
2013-06-05 13:40:14 +00:00
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 1 or 2.",
|
2013-06-05 13:40:14 +00:00
|
|
|
|
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()
|
|
|
|
|
+ " (must be " + TypeName<UInt32>::get() + ")",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-05 13:40:14 +00:00
|
|
|
|
if (arguments.size() == 2 && arguments[1]->getName() != TypeName<String>::get())
|
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of the second argument of function " + getName()
|
|
|
|
|
+ " (must be " + TypeName<String>::get() + ")",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
|
|
return new DataTypeString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
|
{
|
2014-12-12 18:39:48 +00:00
|
|
|
|
RegionsNames::Language language = RegionsNames::Language::RU;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-05 13:40:14 +00:00
|
|
|
|
/// Если указан язык результата
|
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (const ColumnConstString * col_language = typeid_cast<const ColumnConstString *>(&*block.getByPosition(arguments[1]).column))
|
2013-06-05 13:40:14 +00:00
|
|
|
|
language = RegionsNames::getLanguageEnum(col_language->getData());
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[1]).column->getName()
|
|
|
|
|
+ " of the second argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
2014-04-04 18:08:01 +00:00
|
|
|
|
|
|
|
|
|
const RegionsNames & dict = *owned_dict;
|
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (const ColumnVector<UInt32> * col_from = typeid_cast<const ColumnVector<UInt32> *>(&*block.getByPosition(arguments[0]).column))
|
2013-06-05 13:40:14 +00:00
|
|
|
|
{
|
|
|
|
|
ColumnString * col_to = new ColumnString;
|
|
|
|
|
block.getByPosition(result).column = col_to;
|
|
|
|
|
|
|
|
|
|
const ColumnVector<UInt32>::Container_t & region_ids = col_from->getData();
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < region_ids.size(); ++i)
|
|
|
|
|
{
|
2014-04-28 01:48:24 +00:00
|
|
|
|
const StringRef & name_ref = dict.getRegionName(region_ids[i], language);
|
2013-06-05 13:40:14 +00:00
|
|
|
|
col_to->insertDataWithTerminatingZero(name_ref.data, name_ref.size + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
else if (const ColumnConst<UInt32> * col_from = typeid_cast<const ColumnConst<UInt32> *>(&*block.getByPosition(arguments[0]).column))
|
2013-06-05 13:40:14 +00:00
|
|
|
|
{
|
|
|
|
|
UInt32 region_id = col_from->getData();
|
2014-04-28 01:48:24 +00:00
|
|
|
|
const StringRef & name_ref = dict.getRegionName(region_id, language);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-05 13:40:14 +00:00
|
|
|
|
block.getByPosition(result).column = new ColumnConstString(col_from->size(), name_ref.toString());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
|
|
|
|
+ " of the first argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
2015-05-13 16:11:07 +00:00
|
|
|
|
class FunctionDictGetString final : public IFunction
|
2015-01-21 11:39:48 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "dictGetString";
|
|
|
|
|
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
2015-02-10 17:40:40 +00:00
|
|
|
|
return new FunctionDictGetString{context.getExternalDictionaries()};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
FunctionDictGetString(const ExternalDictionaries & dictionaries) : dictionaries(dictionaries) {}
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (arguments.size() != 3 && arguments.size() != 4)
|
2015-01-21 11:39:48 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed "
|
2015-07-14 13:06:25 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 3 or 4.",
|
2015-01-21 11:39:48 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[0].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-10 14:50:43 +00:00
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of first argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2015-01-21 11:39:48 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[1].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-10 14:50:43 +00:00
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of second argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2015-01-21 11:39:48 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[2].get()))
|
2015-01-21 11:39:48 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-10 14:50:43 +00:00
|
|
|
|
"Illegal type " + arguments[2]->getName() + " of third argument of function " + getName()
|
2015-02-26 14:53:33 +00:00
|
|
|
|
+ ", must be UInt64.",
|
2015-01-21 11:39:48 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (arguments.size() == 4 && !typeid_cast<const DataTypeDate *>(arguments[3].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[3]->getName() + " of fourth argument of function " + getName()
|
|
|
|
|
+ ", must be Date.",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-21 11:39:48 +00:00
|
|
|
|
return new DataTypeString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, const size_t result)
|
|
|
|
|
{
|
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.getByPosition(arguments[0]).column.get());
|
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
auto dict = dictionaries.getDictionary(dict_name_col->getData());
|
2015-01-29 14:46:15 +00:00
|
|
|
|
const auto dict_ptr = dict.get();
|
|
|
|
|
|
|
|
|
|
if (!executeDispatch<FlatDictionary>(block, arguments, result, dict_ptr) &&
|
2015-02-10 14:50:43 +00:00
|
|
|
|
!executeDispatch<HashedDictionary>(block, arguments, result, dict_ptr) &&
|
2015-07-13 16:18:28 +00:00
|
|
|
|
!executeDispatch<CacheDictionary>(block, arguments, result, dict_ptr) &&
|
2015-07-14 13:06:25 +00:00
|
|
|
|
!executeDispatchRange<RangeHashedDictionary>(block, arguments, result, dict_ptr))
|
2015-01-29 14:46:15 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Unsupported dictionary type " + dict_ptr->getTypeName(),
|
|
|
|
|
ErrorCodes::UNKNOWN_TYPE
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
2015-07-14 13:06:25 +00:00
|
|
|
|
bool executeDispatch(
|
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * const dictionary)
|
2015-01-29 14:46:15 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (arguments.size() != 3)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Function " + getName() + " for dictionary of type " + dict->getTypeName() +
|
|
|
|
|
" requires exactly 3 arguments",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-21 11:39:48 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.getByPosition(arguments[1]).column.get());
|
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
const auto id_col_untyped = block.getByPosition(arguments[2]).column.get();
|
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnVector<UInt64> *>(id_col_untyped))
|
2015-01-21 11:39:48 +00:00
|
|
|
|
{
|
|
|
|
|
const auto out = new ColumnString;
|
|
|
|
|
block.getByPosition(result).column = out;
|
2015-07-13 17:10:16 +00:00
|
|
|
|
dict->getString(attr_name, id_col->getData(), out);
|
2015-01-21 11:39:48 +00:00
|
|
|
|
}
|
2015-02-19 14:51:39 +00:00
|
|
|
|
else if (const auto id_col = typeid_cast<const ColumnConst<UInt64> *>(id_col_untyped))
|
2015-01-21 11:39:48 +00:00
|
|
|
|
{
|
2015-07-13 16:18:28 +00:00
|
|
|
|
const PODArray<UInt64> ids(1, id_col->getData());
|
2015-07-10 14:43:49 +00:00
|
|
|
|
auto out = std::make_unique<ColumnString>();
|
2015-07-13 17:10:16 +00:00
|
|
|
|
dict->getString(attr_name, ids, out.get());
|
2015-07-10 14:43:49 +00:00
|
|
|
|
|
2015-01-21 11:39:48 +00:00
|
|
|
|
block.getByPosition(result).column = new ColumnConst<String>{
|
2015-08-12 01:18:30 +00:00
|
|
|
|
id_col->size(), out->getDataAt(0).toString()
|
2015-01-21 11:39:48 +00:00
|
|
|
|
};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be UInt64",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
return true;
|
2015-01-21 11:39:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 13:06:25 +00:00
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatchRange(
|
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * const dictionary)
|
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (arguments.size() != 4)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Function " + getName() + " for dictionary of type " + dict->getTypeName() +
|
|
|
|
|
" requires exactly 4 arguments",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.getByPosition(arguments[1]).column.get());
|
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
|
|
|
|
const auto id_col_untyped = block.getByPosition(arguments[2]).column.get();
|
|
|
|
|
const auto date_col_untyped = block.getByPosition(arguments[3]).column.get();
|
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnVector<UInt64> *>(id_col_untyped))
|
|
|
|
|
executeRange(block, result, dict, attr_name, id_col, date_col_untyped);
|
|
|
|
|
else if (const auto id_col = typeid_cast<const ColumnConst<UInt64> *>(id_col_untyped))
|
|
|
|
|
executeRange(block, result, dict, attr_name, id_col, date_col_untyped);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be UInt64",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeRange(
|
|
|
|
|
Block & block, const size_t result, const DictionaryType * const dictionary, const std::string & attr_name,
|
|
|
|
|
const ColumnVector<UInt64> * const id_col, const IColumn * const date_col_untyped)
|
|
|
|
|
{
|
|
|
|
|
if (const auto date_col = typeid_cast<const ColumnVector<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
const auto out = new ColumnString;
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
dictionary->getString(attr_name, id_col->getData(), date_col->getData(), out);
|
|
|
|
|
}
|
|
|
|
|
else if (const auto date_col = typeid_cast<const ColumnConst<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
auto out = new ColumnString;
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
const PODArray<UInt16> dates(id_col->size(), date_col->getData());
|
|
|
|
|
dictionary->getString(attr_name, id_col->getData(), dates, out);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be Date",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeRange(
|
|
|
|
|
Block & block, const size_t result, const DictionaryType * const dictionary, const std::string & attr_name,
|
|
|
|
|
const ColumnConst<UInt64> * const id_col, const IColumn * const date_col_untyped)
|
|
|
|
|
{
|
|
|
|
|
if (const auto date_col = typeid_cast<const ColumnVector<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
const auto out = new ColumnString;
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
const PODArray<UInt64> ids(date_col->size(), id_col->getData());
|
|
|
|
|
dictionary->getString(attr_name, ids, date_col->getData(), out);
|
|
|
|
|
}
|
|
|
|
|
else if (const auto date_col = typeid_cast<const ColumnConst<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
const PODArray<UInt64> ids(1, id_col->getData());
|
|
|
|
|
const PODArray<UInt16> dates(1, date_col->getData());
|
|
|
|
|
|
|
|
|
|
auto out = std::make_unique<ColumnString>();
|
|
|
|
|
dictionary->getString(attr_name, ids, dates, out.get());
|
|
|
|
|
|
|
|
|
|
block.getByPosition(result).column = new ColumnConst<String>{
|
2015-08-12 01:18:30 +00:00
|
|
|
|
id_col->size(), out->getDataAt(0).toString()
|
2015-07-14 13:06:25 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be Date",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
const ExternalDictionaries & dictionaries;
|
2015-01-21 11:39:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
2015-01-28 15:02:05 +00:00
|
|
|
|
template <typename DataType> struct DictGetTraits;
|
|
|
|
|
#define DECLARE_DICT_GET_TRAITS(TYPE, DATA_TYPE) \
|
|
|
|
|
template <> struct DictGetTraits<DATA_TYPE>\
|
|
|
|
|
{\
|
2015-07-13 17:10:16 +00:00
|
|
|
|
template <typename DictionaryType>\
|
2015-07-14 13:06:25 +00:00
|
|
|
|
static void get(\
|
|
|
|
|
const DictionaryType * const dict, const std::string & name, const PODArray<UInt64> & ids,\
|
|
|
|
|
PODArray<TYPE> & out)\
|
2015-01-28 15:02:05 +00:00
|
|
|
|
{\
|
2015-02-19 14:51:39 +00:00
|
|
|
|
dict->get##TYPE(name, ids, out);\
|
2015-01-28 15:02:05 +00:00
|
|
|
|
}\
|
2015-07-14 13:06:25 +00:00
|
|
|
|
template <typename DictionaryType>\
|
|
|
|
|
static void get(\
|
|
|
|
|
const DictionaryType * const dict, const std::string & name, const PODArray<UInt64> & ids,\
|
|
|
|
|
const PODArray<UInt16> & dates, PODArray<TYPE> & out)\
|
|
|
|
|
{\
|
|
|
|
|
dict->get##TYPE(name, ids, dates, out);\
|
|
|
|
|
}\
|
2015-01-28 15:02:05 +00:00
|
|
|
|
};
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(UInt8, DataTypeUInt8)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(UInt16, DataTypeUInt16)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(UInt32, DataTypeUInt32)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(UInt64, DataTypeUInt64)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(Int8, DataTypeInt8)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(Int16, DataTypeInt16)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(Int32, DataTypeInt32)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(Int64, DataTypeInt64)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(Float32, DataTypeFloat32)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(Float64, DataTypeFloat64)
|
2015-03-20 15:21:29 +00:00
|
|
|
|
DECLARE_DICT_GET_TRAITS(UInt16, DataTypeDate)
|
|
|
|
|
DECLARE_DICT_GET_TRAITS(UInt32, DataTypeDateTime)
|
2015-01-28 15:02:05 +00:00
|
|
|
|
#undef DECLARE_DICT_GET_TRAITS
|
|
|
|
|
|
|
|
|
|
template <typename DataType>
|
|
|
|
|
class FunctionDictGet final : public IFunction
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
2015-01-28 15:02:05 +00:00
|
|
|
|
using Type = typename DataType::FieldType;
|
|
|
|
|
|
2015-01-22 14:32:38 +00:00
|
|
|
|
public:
|
|
|
|
|
static const std::string name;
|
|
|
|
|
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
2015-02-10 17:40:40 +00:00
|
|
|
|
return new FunctionDictGet{context.getExternalDictionaries()};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
FunctionDictGet(const ExternalDictionaries & dictionaries) : dictionaries(dictionaries) {}
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (arguments.size() != 3 && arguments.size() != 4)
|
2015-01-22 14:32:38 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed "
|
2015-07-14 13:06:25 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 3 or 4.",
|
2015-01-22 14:32:38 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[0].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-10 14:50:43 +00:00
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of first argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2015-01-22 14:32:38 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[1].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-10 14:50:43 +00:00
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of second argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2015-01-22 14:32:38 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[2].get()))
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-19 14:51:39 +00:00
|
|
|
|
"Illegal type " + arguments[2]->getName() + " of third argument of function " + getName()
|
|
|
|
|
+ ", must be UInt64.",
|
2015-01-22 14:32:38 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (arguments.size() == 4 && !typeid_cast<const DataTypeDate *>(arguments[3].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[3]->getName() + " of fourth argument of function " + getName()
|
|
|
|
|
+ ", must be Date.",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 15:02:05 +00:00
|
|
|
|
return new DataType;
|
2015-01-22 14:32:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 13:20:20 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.getByPosition(arguments[0]).column.get());
|
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
auto dict = dictionaries.getDictionary(dict_name_col->getData());
|
2015-01-28 15:02:05 +00:00
|
|
|
|
const auto dict_ptr = dict.get();
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
2015-01-29 14:46:15 +00:00
|
|
|
|
if (!executeDispatch<FlatDictionary>(block, arguments, result, dict_ptr) &&
|
2015-02-10 14:50:43 +00:00
|
|
|
|
!executeDispatch<HashedDictionary>(block, arguments, result, dict_ptr) &&
|
2015-07-13 16:18:28 +00:00
|
|
|
|
!executeDispatch<CacheDictionary>(block, arguments, result, dict_ptr) &&
|
2015-07-14 13:06:25 +00:00
|
|
|
|
!executeDispatchRange<RangeHashedDictionary>(block, arguments, result, dict_ptr))
|
2015-01-29 14:46:15 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Unsupported dictionary type " + dict_ptr->getTypeName(),
|
|
|
|
|
ErrorCodes::UNKNOWN_TYPE
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatch(Block & block, const ColumnNumbers & arguments, const size_t result,
|
2015-07-13 17:10:16 +00:00
|
|
|
|
const IDictionaryBase * const dictionary)
|
2015-01-29 14:46:15 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (arguments.size() != 3)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Function " + getName() + " for dictionary of type " + dict->getTypeName() +
|
|
|
|
|
" requires exactly 3 arguments.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-22 14:32:38 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.getByPosition(arguments[1]).column.get());
|
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
const auto id_col_untyped = block.getByPosition(arguments[2]).column.get();
|
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnVector<UInt64> *>(id_col_untyped))
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
2015-01-28 15:02:05 +00:00
|
|
|
|
const auto out = new ColumnVector<Type>;
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
const auto size = ids.size();
|
|
|
|
|
data.resize(size);
|
|
|
|
|
|
2015-07-13 17:10:16 +00:00
|
|
|
|
DictGetTraits<DataType>::get(dict, attr_name, ids, data);
|
2015-01-22 14:32:38 +00:00
|
|
|
|
}
|
2015-02-19 14:51:39 +00:00
|
|
|
|
else if (const auto id_col = typeid_cast<const ColumnConst<UInt64> *>(id_col_untyped))
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
2015-07-13 16:18:28 +00:00
|
|
|
|
const PODArray<UInt64> ids(1, id_col->getData());
|
2015-07-10 14:43:49 +00:00
|
|
|
|
PODArray<Type> data(1);
|
2015-07-13 17:10:16 +00:00
|
|
|
|
DictGetTraits<DataType>::get(dict, attr_name, ids, data);
|
2015-07-10 14:43:49 +00:00
|
|
|
|
|
|
|
|
|
block.getByPosition(result).column = new ColumnConst<Type>{id_col->size(), data.front()};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be UInt64",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
return true;
|
2015-01-22 14:32:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 13:06:25 +00:00
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatchRange(
|
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * const dictionary)
|
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (arguments.size() != 4)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Function " + getName() + " for dictionary of type " + dict->getTypeName() +
|
|
|
|
|
" requires exactly 4 arguments",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.getByPosition(arguments[1]).column.get());
|
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
|
|
|
|
const auto id_col_untyped = block.getByPosition(arguments[2]).column.get();
|
|
|
|
|
const auto date_col_untyped = block.getByPosition(arguments[3]).column.get();
|
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnVector<UInt64> *>(id_col_untyped))
|
|
|
|
|
executeRange(block, result, dict, attr_name, id_col, date_col_untyped);
|
|
|
|
|
else if (const auto id_col = typeid_cast<const ColumnConst<UInt64> *>(id_col_untyped))
|
|
|
|
|
executeRange(block, result, dict, attr_name, id_col, date_col_untyped);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be UInt64",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeRange(
|
|
|
|
|
Block & block, const size_t result, const DictionaryType * const dictionary, const std::string & attr_name,
|
|
|
|
|
const ColumnVector<UInt64> * const id_col, const IColumn * const date_col_untyped)
|
|
|
|
|
{
|
|
|
|
|
if (const auto date_col = typeid_cast<const ColumnVector<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
const auto size = id_col->size();
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
const auto & dates = date_col->getData();
|
|
|
|
|
|
|
|
|
|
const auto out = new ColumnVector<Type>{size};
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
DictGetTraits<DataType>::get(dictionary, attr_name, ids, dates, data);
|
|
|
|
|
}
|
|
|
|
|
else if (const auto date_col = typeid_cast<const ColumnConst<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
const auto size = id_col->size();
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
const PODArray<UInt16> dates(size, date_col->getData());
|
|
|
|
|
|
2015-07-14 15:05:40 +00:00
|
|
|
|
const auto out = new ColumnVector<Type>{size};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
DictGetTraits<DataType>::get(dictionary, attr_name, ids, dates, data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be Date",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeRange(
|
|
|
|
|
Block & block, const size_t result, const DictionaryType * const dictionary, const std::string & attr_name,
|
|
|
|
|
const ColumnConst<UInt64> * const id_col, const IColumn * const date_col_untyped)
|
|
|
|
|
{
|
|
|
|
|
if (const auto date_col = typeid_cast<const ColumnVector<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
const auto size = date_col->size();
|
|
|
|
|
const PODArray<UInt64> ids(size, id_col->getData());
|
|
|
|
|
const auto & dates = date_col->getData();
|
|
|
|
|
|
|
|
|
|
const auto out = new ColumnVector<Type>{size};
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
DictGetTraits<DataType>::get(dictionary, attr_name, ids, dates, data);
|
|
|
|
|
}
|
|
|
|
|
else if (const auto date_col = typeid_cast<const ColumnConst<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
const PODArray<UInt64> ids(1, id_col->getData());
|
|
|
|
|
const PODArray<UInt16> dates(1, date_col->getData());
|
|
|
|
|
PODArray<Type> data(1);
|
|
|
|
|
DictGetTraits<DataType>::get(dictionary, attr_name, ids, dates, data);
|
|
|
|
|
|
|
|
|
|
block.getByPosition(result).column = new ColumnConst<Type>{id_col->size(), data.front()};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be Date",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
const ExternalDictionaries & dictionaries;
|
2015-01-22 14:32:38 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-01-28 15:02:05 +00:00
|
|
|
|
template <typename DataType>
|
2015-03-20 15:21:29 +00:00
|
|
|
|
const std::string FunctionDictGet<DataType>::name = "dictGet" + DataType{}.getName();
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
|
|
|
|
|
2015-01-28 15:02:05 +00:00
|
|
|
|
using FunctionDictGetUInt8 = FunctionDictGet<DataTypeUInt8>;
|
|
|
|
|
using FunctionDictGetUInt16 = FunctionDictGet<DataTypeUInt16>;
|
|
|
|
|
using FunctionDictGetUInt32 = FunctionDictGet<DataTypeUInt32>;
|
|
|
|
|
using FunctionDictGetUInt64 = FunctionDictGet<DataTypeUInt64>;
|
|
|
|
|
using FunctionDictGetInt8 = FunctionDictGet<DataTypeInt8>;
|
|
|
|
|
using FunctionDictGetInt16 = FunctionDictGet<DataTypeInt16>;
|
|
|
|
|
using FunctionDictGetInt32 = FunctionDictGet<DataTypeInt32>;
|
|
|
|
|
using FunctionDictGetInt64 = FunctionDictGet<DataTypeInt64>;
|
|
|
|
|
using FunctionDictGetFloat32 = FunctionDictGet<DataTypeFloat32>;
|
|
|
|
|
using FunctionDictGetFloat64 = FunctionDictGet<DataTypeFloat64>;
|
2015-03-20 15:21:29 +00:00
|
|
|
|
using FunctionDictGetDate = FunctionDictGet<DataTypeDate>;
|
|
|
|
|
using FunctionDictGetDateTime = FunctionDictGet<DataTypeDateTime>;
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
|
|
|
|
class FunctionDictGetHierarchy final : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "dictGetHierarchy";
|
|
|
|
|
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
2015-02-10 17:40:40 +00:00
|
|
|
|
return new FunctionDictGetHierarchy{context.getExternalDictionaries()};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
FunctionDictGetHierarchy(const ExternalDictionaries & dictionaries) : dictionaries(dictionaries) {}
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 2)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
|
+ toString(arguments.size()) + ", should be 2.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[0].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-10 14:50:43 +00:00
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of first argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2015-01-28 13:20:20 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[1].get()))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-19 14:51:39 +00:00
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of second argument of function " + getName()
|
|
|
|
|
+ ", must be UInt64.",
|
2015-01-28 13:20:20 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new DataTypeArray{new DataTypeUInt64};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
|
|
|
|
{
|
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.getByPosition(arguments[0]).column.get());
|
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
auto dict = dictionaries.getDictionary(dict_name_col->getData());
|
2015-01-29 14:46:15 +00:00
|
|
|
|
const auto dict_ptr = dict.get();
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
2015-01-29 14:46:15 +00:00
|
|
|
|
if (!executeDispatch<FlatDictionary>(block, arguments, result, dict_ptr) &&
|
2015-02-10 14:50:43 +00:00
|
|
|
|
!executeDispatch<HashedDictionary>(block, arguments, result, dict_ptr) &&
|
2015-07-13 17:10:16 +00:00
|
|
|
|
!executeDispatch<CacheDictionary>(block, arguments, result, dict_ptr))
|
2015-01-29 14:46:15 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Unsupported dictionary type " + dict_ptr->getTypeName(),
|
|
|
|
|
ErrorCodes::UNKNOWN_TYPE
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatch(Block & block, const ColumnNumbers & arguments, const size_t result,
|
2015-07-13 17:10:16 +00:00
|
|
|
|
const IDictionaryBase * const dictionary)
|
2015-01-29 14:46:15 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2015-07-13 17:10:16 +00:00
|
|
|
|
if (!dict->hasHierarchy())
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Dictionary does not have a hierarchy",
|
|
|
|
|
ErrorCodes::UNSUPPORTED_METHOD
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-10 14:43:49 +00:00
|
|
|
|
const auto get_hierarchies = [&] (const PODArray<UInt64> & in, PODArray<UInt64> & out, PODArray<UInt64> & offsets) {
|
2015-03-04 13:18:07 +00:00
|
|
|
|
const auto size = in.size();
|
|
|
|
|
|
|
|
|
|
/// copy of `in` array
|
|
|
|
|
auto in_array = std::make_unique<PODArray<UInt64>>(std::begin(in), std::end(in));
|
|
|
|
|
/// used for storing and handling result of ::toParent call
|
|
|
|
|
auto out_array = std::make_unique<PODArray<UInt64>>(size);
|
|
|
|
|
/// resulting hierarchies
|
|
|
|
|
std::vector<std::vector<IDictionary::id_t>> hierarchies(size);
|
|
|
|
|
|
|
|
|
|
/// total number of non-zero elements, used for allocating all the required memory upfront
|
|
|
|
|
std::size_t total_count = 0;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
auto all_zeroes = true;
|
|
|
|
|
|
|
|
|
|
/// erase zeroed identifiers, store non-zeroed ones
|
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
|
{
|
|
|
|
|
const auto id = (*in_array)[i];
|
|
|
|
|
if (0 == id)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
all_zeroes = false;
|
|
|
|
|
/// place id at it's corresponding place
|
|
|
|
|
hierarchies[i].push_back(id);
|
|
|
|
|
|
|
|
|
|
++total_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (all_zeroes)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/// translate all non-zero identifiers at once
|
2015-07-13 17:10:16 +00:00
|
|
|
|
dict->toParent(*in_array, *out_array);
|
2015-03-04 13:18:07 +00:00
|
|
|
|
|
|
|
|
|
/// we're going to use the `in_array` from this iteration as `out_array` on the next one
|
|
|
|
|
std::swap(in_array, out_array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.reserve(total_count);
|
2015-01-28 13:20:20 +00:00
|
|
|
|
offsets.resize(size);
|
|
|
|
|
|
2015-03-04 13:18:07 +00:00
|
|
|
|
for (const auto i : ext::range(0, size))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2015-03-04 13:18:07 +00:00
|
|
|
|
const auto & ids = hierarchies[i];
|
|
|
|
|
out.insert_assume_reserved(std::begin(ids), std::end(ids));
|
|
|
|
|
offsets[i] = out.size();
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
2015-07-10 14:43:49 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto id_col_untyped = block.getByPosition(arguments[1]).column.get();
|
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnVector<UInt64> *>(id_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
const auto & in = id_col->getData();
|
|
|
|
|
const auto backend = new ColumnVector<UInt64>;
|
|
|
|
|
const auto array = new ColumnArray{backend};
|
|
|
|
|
block.getByPosition(result).column = array;
|
|
|
|
|
|
|
|
|
|
get_hierarchies(in, backend->getData(), array->getOffsets());
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
2015-02-19 14:51:39 +00:00
|
|
|
|
else if (const auto id_col = typeid_cast<const ColumnConst<UInt64> *>(id_col_untyped))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2015-07-10 14:43:49 +00:00
|
|
|
|
const PODArray<UInt64> in(1, id_col->getData());
|
|
|
|
|
const auto backend = new ColumnVector<UInt64>;
|
|
|
|
|
const auto array = new ColumnArray{backend};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
2015-07-10 14:43:49 +00:00
|
|
|
|
get_hierarchies(in, backend->getData(), array->getOffsets());
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
|
|
|
|
block.getByPosition(result).column = new ColumnConstArray{
|
|
|
|
|
id_col->size(),
|
2015-07-10 14:43:49 +00:00
|
|
|
|
(*array)[0].get<Array>(),
|
2015-01-28 13:20:20 +00:00
|
|
|
|
new DataTypeArray{new DataTypeUInt64}
|
|
|
|
|
};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be UInt64",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
const ExternalDictionaries & dictionaries;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionDictIsIn final : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "dictIsIn";
|
|
|
|
|
|
|
|
|
|
static IFunction * create(const Context & context)
|
|
|
|
|
{
|
2015-02-10 17:40:40 +00:00
|
|
|
|
return new FunctionDictIsIn{context.getExternalDictionaries()};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
FunctionDictIsIn(const ExternalDictionaries & dictionaries) : dictionaries(dictionaries) {}
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 3)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
|
+ toString(arguments.size()) + ", should be 3.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[0].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-10 14:50:43 +00:00
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of first argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2015-01-28 13:20:20 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-20 10:57:30 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[1].get()))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-20 10:57:30 +00:00
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of second argument of function " + getName()
|
|
|
|
|
+ ", must be UInt64.",
|
2015-01-28 13:20:20 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-20 10:57:30 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[2].get()))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
2015-02-20 10:57:30 +00:00
|
|
|
|
"Illegal type " + arguments[2]->getName() + " of third argument of function " + getName()
|
|
|
|
|
+ ", must be UInt64.",
|
2015-01-28 13:20:20 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new DataTypeUInt8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
|
|
|
|
{
|
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.getByPosition(arguments[0]).column.get());
|
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
auto dict = dictionaries.getDictionary(dict_name_col->getData());
|
2015-01-29 14:46:15 +00:00
|
|
|
|
const auto dict_ptr = dict.get();
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
2015-01-29 14:46:15 +00:00
|
|
|
|
if (!executeDispatch<FlatDictionary>(block, arguments, result, dict_ptr) &&
|
2015-02-10 14:50:43 +00:00
|
|
|
|
!executeDispatch<HashedDictionary>(block, arguments, result, dict_ptr) &&
|
2015-07-13 17:10:16 +00:00
|
|
|
|
!executeDispatch<CacheDictionary>(block, arguments, result, dict_ptr))
|
2015-01-29 14:46:15 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Unsupported dictionary type " + dict_ptr->getTypeName(),
|
|
|
|
|
ErrorCodes::UNKNOWN_TYPE
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatch(Block & block, const ColumnNumbers & arguments, const size_t result,
|
2015-07-13 17:10:16 +00:00
|
|
|
|
const IDictionaryBase * const dictionary)
|
2015-01-29 14:46:15 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2015-07-13 17:10:16 +00:00
|
|
|
|
if (!dict->hasHierarchy())
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Dictionary does not have a hierarchy",
|
|
|
|
|
ErrorCodes::UNSUPPORTED_METHOD
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
const auto child_id_col_untyped = block.getByPosition(arguments[1]).column.get();
|
|
|
|
|
const auto ancestor_id_col_untyped = block.getByPosition(arguments[2]).column.get();
|
|
|
|
|
|
|
|
|
|
if (const auto child_id_col = typeid_cast<const ColumnVector<UInt64> *>(child_id_col_untyped))
|
2015-07-13 17:10:16 +00:00
|
|
|
|
execute(block, result, dict, child_id_col, ancestor_id_col_untyped);
|
2015-02-19 14:51:39 +00:00
|
|
|
|
else if (const auto child_id_col = typeid_cast<const ColumnConst<UInt64> *>(child_id_col_untyped))
|
2015-07-13 17:10:16 +00:00
|
|
|
|
execute(block, result, dict, child_id_col, ancestor_id_col_untyped);
|
2015-02-19 14:51:39 +00:00
|
|
|
|
else
|
2015-01-28 13:20:20 +00:00
|
|
|
|
throw Exception{
|
2015-02-19 14:51:39 +00:00
|
|
|
|
"Illegal column " + child_id_col_untyped->getName()
|
2015-01-28 13:20:20 +00:00
|
|
|
|
+ " of second argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
2015-01-29 14:46:15 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
template <typename DictionaryType>
|
2015-01-29 14:46:15 +00:00
|
|
|
|
bool execute(Block & block, const size_t result, const DictionaryType * const dictionary,
|
2015-02-19 14:51:39 +00:00
|
|
|
|
const ColumnVector<UInt64> * const child_id_col, const IColumn * const ancestor_id_col_untyped)
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2015-02-19 14:51:39 +00:00
|
|
|
|
if (const auto ancestor_id_col = typeid_cast<const ColumnVector<UInt64> *>(ancestor_id_col_untyped))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
const auto out = new ColumnVector<UInt8>;
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
const auto & child_ids = child_id_col->getData();
|
|
|
|
|
const auto & ancestor_ids = ancestor_id_col->getData();
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
const auto size = child_id_col->size();
|
|
|
|
|
data.resize(size);
|
|
|
|
|
|
|
|
|
|
for (const auto idx : ext::range(0, size))
|
|
|
|
|
data[idx] = dictionary->in(child_ids[idx], ancestor_ids[idx]);
|
|
|
|
|
}
|
2015-02-19 14:51:39 +00:00
|
|
|
|
else if (const auto ancestor_id_col = typeid_cast<const ColumnConst<UInt64> *>(ancestor_id_col_untyped))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
const auto out = new ColumnVector<UInt8>;
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
const auto & child_ids = child_id_col->getData();
|
|
|
|
|
const auto ancestor_id = ancestor_id_col->getData();
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
const auto size = child_id_col->size();
|
|
|
|
|
data.resize(size);
|
|
|
|
|
|
|
|
|
|
for (const auto idx : ext::range(0, size))
|
|
|
|
|
data[idx] = dictionary->in(child_ids[idx], ancestor_id);
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal column " + ancestor_id_col_untyped->getName()
|
|
|
|
|
+ " of third argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
return true;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
template <typename DictionaryType>
|
2015-01-29 14:46:15 +00:00
|
|
|
|
bool execute(Block & block, const size_t result, const DictionaryType * const dictionary,
|
2015-02-19 14:51:39 +00:00
|
|
|
|
const ColumnConst<UInt64> * const child_id_col, const IColumn * const ancestor_id_col_untyped)
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2015-02-19 14:51:39 +00:00
|
|
|
|
if (const auto ancestor_id_col = typeid_cast<const ColumnVector<UInt64> *>(ancestor_id_col_untyped))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
const auto out = new ColumnVector<UInt8>;
|
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
const auto child_id = child_id_col->getData();
|
|
|
|
|
const auto & ancestor_ids = ancestor_id_col->getData();
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
const auto size = child_id_col->size();
|
|
|
|
|
data.resize(size);
|
|
|
|
|
|
|
|
|
|
for (const auto idx : ext::range(0, size))
|
|
|
|
|
data[idx] = dictionary->in(child_id, ancestor_ids[idx]);
|
|
|
|
|
}
|
2015-02-19 14:51:39 +00:00
|
|
|
|
else if (const auto ancestor_id_col = typeid_cast<const ColumnConst<UInt64> *>(ancestor_id_col_untyped))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
block.getByPosition(result).column = new ColumnConst<UInt8>{
|
|
|
|
|
child_id_col->size(),
|
|
|
|
|
dictionary->in(child_id_col->getData(), ancestor_id_col->getData())
|
|
|
|
|
};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal column " + ancestor_id_col_untyped->getName()
|
|
|
|
|
+ " of third argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
|
return true;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
|
const ExternalDictionaries & dictionaries;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
|
};
|