2012-12-21 19:48:47 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2017-03-12 10:13:45 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypesNumber.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>
|
2015-11-13 01:44:41 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeTuple.h>
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
2017-03-12 10:13:45 +00:00
|
|
|
|
#include <DB/Columns/ColumnsNumber.h>
|
|
|
|
|
#include <DB/Columns/ColumnConst.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>
|
2016-02-14 02:37:42 +00:00
|
|
|
|
#include <DB/Columns/ColumnTuple.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/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-11-13 11:21:40 +00:00
|
|
|
|
#include <DB/Dictionaries/ComplexKeyHashedDictionary.h>
|
2015-11-16 17:27:12 +00:00
|
|
|
|
#include <DB/Dictionaries/ComplexKeyCacheDictionary.h>
|
2015-07-13 16:18:28 +00:00
|
|
|
|
#include <DB/Dictionaries/RangeHashedDictionary.h>
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
2015-10-05 00:33:43 +00:00
|
|
|
|
#include <ext/range.hpp>
|
2015-04-16 06:12:35 +00:00
|
|
|
|
|
2012-12-21 19:48:47 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int DICTIONARIES_WAS_NOT_LOADED;
|
2016-06-07 21:07:44 +00:00
|
|
|
|
extern const int UNSUPPORTED_METHOD;
|
|
|
|
|
extern const int UNKNOWN_TYPE;
|
2016-01-12 02:21:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-25 18:11:09 +00:00
|
|
|
|
/** Функции, использующие подключаемые (внешние) словари.
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2015-11-19 13:15:02 +00:00
|
|
|
|
class FunctionDictHas final : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "dictHas";
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context)
|
2015-11-19 13:15:02 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
return std::make_shared<FunctionDictHas>(context.getExternalDictionaries());
|
2015-11-19 13:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FunctionDictHas(const ExternalDictionaries & dictionaries) : dictionaries(dictionaries) {}
|
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
private:
|
2016-12-29 19:38:10 +00:00
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-11-19 13:15:02 +00:00
|
|
|
|
{
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[0].get()))
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of first argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[1].get()) &&
|
|
|
|
|
!typeid_cast<const DataTypeTuple *>(arguments[1].get()))
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of second argument of function " + getName()
|
|
|
|
|
+ ", must be UInt64 or tuple(...).",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
2015-11-19 13:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2015-11-19 13:15:02 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[0]).column.get());
|
2015-11-19 13:15:02 +00:00
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
|
|
|
|
auto dict = dictionaries.getDictionary(dict_name_col->getData());
|
|
|
|
|
const auto dict_ptr = dict.get();
|
|
|
|
|
|
|
|
|
|
if (!executeDispatchSimple<FlatDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchSimple<HashedDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchSimple<CacheDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchComplex<ComplexKeyHashedDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchComplex<ComplexKeyCacheDictionary>(block, arguments, result, dict_ptr))
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Unsupported dictionary type " + dict_ptr->getTypeName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNKNOWN_TYPE};
|
2015-11-19 13:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatchSimple(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-11-19 13:15:02 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto id_col_untyped = block.safeGetByPosition(arguments[1]).column.get();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnUInt64 *>(id_col_untyped))
|
2015-11-19 13:15:02 +00:00
|
|
|
|
{
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnUInt8>(ext::size(ids));
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
|
|
|
|
dict->has(ids, out->getData());
|
|
|
|
|
}
|
|
|
|
|
else if (const auto id_col = typeid_cast<const ColumnConst<UInt64> *>(id_col_untyped))
|
|
|
|
|
{
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(1, id_col->getData());
|
|
|
|
|
PaddedPODArray<UInt8> out(1);
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
|
|
|
|
dict->has(ids, out);
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConst<UInt8>>(id_col->size(), out.front());
|
2015-11-19 13:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be UInt64",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatchComplex(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-11-19 13:15:02 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto key_col_with_type = block.safeGetByPosition(arguments[1]);
|
2016-07-10 07:24:24 +00:00
|
|
|
|
if (typeid_cast<const ColumnTuple *>(key_col_with_type.column.get())
|
|
|
|
|
|| typeid_cast<const ColumnConstTuple *>(key_col_with_type.column.get()))
|
2015-11-19 13:15:02 +00:00
|
|
|
|
{
|
2016-03-03 01:54:58 +00:00
|
|
|
|
/// Функции у внешних словарей поддерживают только полноценные (не константные) столбцы с ключами.
|
2016-07-10 07:24:24 +00:00
|
|
|
|
const ColumnPtr key_col_materialized = key_col_with_type.column->convertToFullColumnIfConst();
|
2016-03-03 01:54:58 +00:00
|
|
|
|
|
|
|
|
|
const auto key_columns = ext::map<ConstColumnPlainPtrs>(
|
2016-07-10 07:24:24 +00:00
|
|
|
|
static_cast<const ColumnTuple &>(*key_col_materialized.get()).getColumns(),
|
|
|
|
|
[](const ColumnPtr & ptr) { return ptr.get(); });
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
|
|
|
|
const auto & key_types = static_cast<const DataTypeTuple &>(*key_col_with_type.type).getElements();
|
|
|
|
|
|
2016-07-10 07:24:24 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnUInt8>(key_col_with_type.column->size());
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
|
|
|
|
dict->has(key_columns, key_types, out->getData());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be " + dict->getKeyDescription(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::TYPE_MISMATCH};
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ExternalDictionaries & dictionaries;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2017-01-05 16:34:05 +00:00
|
|
|
|
static bool isDictGetFunctionInjective(const ExternalDictionaries & dictionaries, const Block & sample_block)
|
|
|
|
|
{
|
|
|
|
|
if (sample_block.columns() != 3 && sample_block.columns() != 4)
|
|
|
|
|
throw Exception{"Function dictGet... takes 3 or 4 arguments", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
|
|
|
|
|
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(sample_block.getByPosition(0).column.get());
|
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function dictGet... must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
|
|
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(sample_block.getByPosition(1).column.get());
|
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function dictGet... must be a constant string",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
|
|
|
|
|
|
return dictionaries.getDictionary(dict_name_col->getData())->isInjective(attr_name_col->getData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context)
|
2015-01-21 11:39:48 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
return std::make_shared<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:
|
2016-12-29 19:38:10 +00:00
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
2017-01-05 16:34:05 +00:00
|
|
|
|
bool isInjective(const Block & sample_block) override
|
|
|
|
|
{
|
|
|
|
|
return isDictGetFunctionInjective(dictionaries, sample_block);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-01-21 11:39:48 +00:00
|
|
|
|
{
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-21 11:39:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-21 11:39:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 01:44:41 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[2].get()) &&
|
|
|
|
|
!typeid_cast<const DataTypeTuple *>(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-11-13 01:44:41 +00:00
|
|
|
|
+ ", must be UInt64 or tuple(...).",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-21 11:39:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
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()
|
2015-11-13 01:44:41 +00:00
|
|
|
|
+ ", must be Date.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2015-01-21 11:39:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2015-01-21 11:39:48 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[0]).column.get());
|
2015-01-21 11:39:48 +00:00
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
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-11-16 17:27:12 +00:00
|
|
|
|
!executeDispatchComplex<ComplexKeyHashedDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchComplex<ComplexKeyCacheDictionary>(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(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNKNOWN_TYPE};
|
2015-01-29 14:46:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
2015-07-14 13:06:25 +00:00
|
|
|
|
bool executeDispatch(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * 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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-01-21 11:39:48 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto id_col_untyped = block.safeGetByPosition(arguments[2]).column.get();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnUInt64 *>(id_col_untyped))
|
2015-01-21 11:39:48 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dict->getString(attr_name, id_col->getData(), out.get());
|
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
|
|
|
|
{
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<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
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConst<String>>(
|
2016-05-28 05:31:36 +00:00
|
|
|
|
id_col->size(), out->getDataAt(0).toString());
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be UInt64",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
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-11-16 17:27:12 +00:00
|
|
|
|
template <typename DictionaryType>
|
2015-11-13 01:44:41 +00:00
|
|
|
|
bool executeDispatchComplex(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-11-13 01:44:41 +00:00
|
|
|
|
{
|
2015-11-16 17:27:12 +00:00
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
2015-11-13 01:44:41 +00:00
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (arguments.size() != 3)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Function " + getName() + " for dictionary of type " + dict->getTypeName() +
|
|
|
|
|
" requires exactly 3 arguments",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-11-13 01:44:41 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto key_col_with_type = block.safeGetByPosition(arguments[2]);
|
2016-07-10 07:24:24 +00:00
|
|
|
|
if (typeid_cast<const ColumnTuple *>(key_col_with_type.column.get())
|
|
|
|
|
|| typeid_cast<const ColumnConstTuple *>(key_col_with_type.column.get()))
|
2015-11-13 01:44:41 +00:00
|
|
|
|
{
|
2016-07-10 07:24:24 +00:00
|
|
|
|
const ColumnPtr key_col_materialized = key_col_with_type.column->convertToFullColumnIfConst();
|
2016-03-03 01:54:58 +00:00
|
|
|
|
|
|
|
|
|
const auto key_columns = ext::map<ConstColumnPlainPtrs>(
|
2016-07-10 07:24:24 +00:00
|
|
|
|
static_cast<const ColumnTuple &>(*key_col_materialized.get()).getColumns(),
|
|
|
|
|
[](const ColumnPtr & ptr) { return ptr.get(); });
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
|
|
|
|
const auto & key_types = static_cast<const DataTypeTuple &>(*key_col_with_type.type).getElements();
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dict->getString(attr_name, key_columns, key_types, out.get());
|
2015-11-13 01:44:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be " + dict->getKeyDescription(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::TYPE_MISMATCH};
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 13:06:25 +00:00
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatchRange(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
|
|
|
|
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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto id_col_untyped = block.safeGetByPosition(arguments[2]).column.get();
|
|
|
|
|
const auto date_col_untyped = block.safeGetByPosition(arguments[3]).column.get();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnUInt64 *>(id_col_untyped))
|
2015-07-14 13:06:25 +00:00
|
|
|
|
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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeRange(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const size_t result, const DictionaryType * dictionary, const std::string & attr_name,
|
|
|
|
|
const ColumnUInt64 * id_col, const IColumn * date_col_untyped)
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto date_col = typeid_cast<const ColumnUInt16 *>(date_col_untyped))
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dictionary->getString(attr_name, id_col->getData(), date_col->getData(), out.get());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
else if (const auto date_col = typeid_cast<const ColumnConst<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt16> dates(id_col->size(), date_col->getData());
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dictionary->getString(attr_name, id_col->getData(), dates, out.get());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be Date",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeRange(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const size_t result, const DictionaryType * dictionary, const std::string & attr_name,
|
|
|
|
|
const ColumnConst<UInt64> * id_col, const IColumn * date_col_untyped)
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto date_col = typeid_cast<const ColumnUInt16 *>(date_col_untyped))
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(date_col->size(), id_col->getData());
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dictionary->getString(attr_name, ids, date_col->getData(), out.get());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
else if (const auto date_col = typeid_cast<const ColumnConst<UInt16> *>(date_col_untyped))
|
|
|
|
|
{
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(1, id_col->getData());
|
|
|
|
|
const PaddedPODArray<UInt16> dates(1, date_col->getData());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
auto out = std::make_unique<ColumnString>();
|
|
|
|
|
dictionary->getString(attr_name, ids, dates, out.get());
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConst<String>>(
|
2016-05-28 05:31:36 +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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-11-06 14:49:54 +00:00
|
|
|
|
class FunctionDictGetStringOrDefault final : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "dictGetStringOrDefault";
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context)
|
2015-11-06 14:49:54 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
return std::make_shared<FunctionDictGetStringOrDefault>(context.getExternalDictionaries());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FunctionDictGetStringOrDefault(const ExternalDictionaries & dictionaries) : dictionaries(dictionaries) {}
|
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
private:
|
2016-12-29 19:38:10 +00:00
|
|
|
|
size_t getNumberOfArguments() const override { return 4; }
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-11-06 14:49:54 +00:00
|
|
|
|
{
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[0].get()))
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of first argument of function " + getName() +
|
|
|
|
|
", expected a string.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[1].get()))
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of second argument of function " + getName() +
|
|
|
|
|
", expected a string.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
2015-11-18 13:31:29 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[2].get()) &&
|
|
|
|
|
!typeid_cast<const DataTypeTuple *>(arguments[2].get()))
|
|
|
|
|
{
|
2015-11-06 14:49:54 +00:00
|
|
|
|
throw Exception{
|
2015-11-18 13:31:29 +00:00
|
|
|
|
"Illegal type " + arguments[2]->getName() + " of third argument of function " + getName()
|
|
|
|
|
+ ", must be UInt64 or tuple(...).",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-18 13:31:29 +00:00
|
|
|
|
}
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[3].get()))
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[3]->getName() + " of fourth argument of function " + getName() +
|
|
|
|
|
", must be String.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2015-11-06 14:49:54 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[0]).column.get());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
auto dict = dictionaries.getDictionary(dict_name_col->getData());
|
|
|
|
|
const auto dict_ptr = dict.get();
|
|
|
|
|
|
|
|
|
|
if (!executeDispatch<FlatDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatch<HashedDictionary>(block, arguments, result, dict_ptr) &&
|
2015-11-18 13:31:29 +00:00
|
|
|
|
!executeDispatch<CacheDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchComplex<ComplexKeyHashedDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchComplex<ComplexKeyCacheDictionary>(block, arguments, result, dict_ptr))
|
2015-11-06 14:49:54 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Unsupported dictionary type " + dict_ptr->getTypeName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNKNOWN_TYPE};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatch(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-11-06 14:49:54 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto id_col_untyped = block.safeGetByPosition(arguments[2]).column.get();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnUInt64 *>(id_col_untyped))
|
2015-11-06 14:49:54 +00:00
|
|
|
|
executeDispatch(block, arguments, result, dict, attr_name, id_col);
|
|
|
|
|
else if (const auto id_col = typeid_cast<const ColumnConst<UInt64> *>(id_col_untyped))
|
|
|
|
|
executeDispatch(block, arguments, result, dict, attr_name, id_col);
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be UInt64",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeDispatch(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const DictionaryType * dictionary,
|
|
|
|
|
const std::string & attr_name, const ColumnUInt64 * id_col)
|
2015-11-06 14:49:54 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto default_col_untyped = block.safeGetByPosition(arguments[3]).column.get();
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
if (const auto default_col = typeid_cast<const ColumnString *>(default_col_untyped))
|
|
|
|
|
{
|
2015-11-10 09:22:25 +00:00
|
|
|
|
/// vector ids, vector defaults
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dictionary->getString(attr_name, ids, default_col, out.get());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
2015-11-20 15:53:23 +00:00
|
|
|
|
else if (const auto default_col = typeid_cast<const ColumnConst<String> *>(default_col_untyped))
|
2015-11-06 14:49:54 +00:00
|
|
|
|
{
|
2015-11-10 09:22:25 +00:00
|
|
|
|
/// vector ids, const defaults
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
const auto & def = default_col->getData();
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dictionary->getString(attr_name, ids, def, out.get());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be String",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeDispatch(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const DictionaryType * dictionary,
|
|
|
|
|
const std::string & attr_name, const ColumnConst<UInt64> * id_col)
|
2015-11-06 14:49:54 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto default_col_untyped = block.safeGetByPosition(arguments[3]).column.get();
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
if (const auto default_col = typeid_cast<const ColumnString *>(default_col_untyped))
|
|
|
|
|
{
|
2015-11-10 09:22:25 +00:00
|
|
|
|
/// const ids, vector defaults
|
|
|
|
|
/// @todo avoid materialization
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(id_col->size(), id_col->getData());
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dictionary->getString(attr_name, ids, default_col, out.get());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
else if (const auto default_col = typeid_cast<const ColumnConst<String> *>(default_col_untyped))
|
|
|
|
|
{
|
2015-11-10 09:22:25 +00:00
|
|
|
|
/// const ids, const defaults
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(1, id_col->getData());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
auto out = std::make_unique<ColumnString>();
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
const auto & def = default_col->getData();
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
dictionary->getString(attr_name, ids, def, out.get());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConst<String>>(
|
2016-05-28 05:31:36 +00:00
|
|
|
|
id_col->size(), out->getDataAt(0).toString());
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be String",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-06 14:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 13:31:29 +00:00
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatchComplex(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-11-18 13:31:29 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-11-18 13:31:29 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-18 13:31:29 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto key_col_with_type = block.safeGetByPosition(arguments[2]);
|
2015-11-18 13:31:29 +00:00
|
|
|
|
const auto & key_col = typeid_cast<const ColumnTuple &>(*key_col_with_type.column);
|
2016-03-03 01:54:58 +00:00
|
|
|
|
|
|
|
|
|
const ColumnPtr key_col_materialized = key_col.convertToFullColumnIfConst();
|
|
|
|
|
|
|
|
|
|
const auto key_columns = ext::map<ConstColumnPlainPtrs>(
|
|
|
|
|
static_cast<const ColumnTuple &>(*key_col_materialized.get()).getColumns(), [](const ColumnPtr & ptr) { return ptr.get(); });
|
|
|
|
|
|
2015-11-18 13:31:29 +00:00
|
|
|
|
const auto & key_types = static_cast<const DataTypeTuple &>(*key_col_with_type.type).getElements();
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-18 13:31:29 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto default_col_untyped = block.safeGetByPosition(arguments[3]).column.get();
|
2015-11-18 13:31:29 +00:00
|
|
|
|
if (const auto default_col = typeid_cast<const ColumnString *>(default_col_untyped))
|
2016-03-03 01:54:58 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dict->getString(attr_name, key_columns, key_types, default_col, out.get());
|
2016-03-03 01:54:58 +00:00
|
|
|
|
}
|
2015-11-18 13:31:29 +00:00
|
|
|
|
else if (const auto default_col = typeid_cast<const ColumnConst<String> *>(default_col_untyped))
|
|
|
|
|
{
|
2015-11-20 15:53:23 +00:00
|
|
|
|
const auto & def = default_col->getData();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
dict->getString(attr_name, key_columns, key_types, def, out.get());
|
2015-11-18 13:31:29 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be String",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-18 13:31:29 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 14:49:54 +00:00
|
|
|
|
const ExternalDictionaries & dictionaries;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
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(\
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const DictionaryType * dict, const std::string & name, const PaddedPODArray<UInt64> & ids,\
|
2016-04-15 00:33:21 +00:00
|
|
|
|
PaddedPODArray<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>\
|
2015-11-13 01:44:41 +00:00
|
|
|
|
static void get(\
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const DictionaryType * dict, const std::string & name, const ConstColumnPlainPtrs & key_columns,\
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const DataTypes & key_types, PaddedPODArray<TYPE> & out)\
|
2015-11-13 01:44:41 +00:00
|
|
|
|
{\
|
|
|
|
|
dict->get##TYPE(name, key_columns, key_types, out);\
|
|
|
|
|
}\
|
|
|
|
|
template <typename DictionaryType>\
|
2015-07-14 13:06:25 +00:00
|
|
|
|
static void get(\
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const DictionaryType * dict, const std::string & name, const PaddedPODArray<UInt64> & ids,\
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt16> & dates, PaddedPODArray<TYPE> & out)\
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{\
|
|
|
|
|
dict->get##TYPE(name, ids, dates, out);\
|
|
|
|
|
}\
|
2015-11-20 15:53:23 +00:00
|
|
|
|
template <typename DictionaryType, typename DefaultsType>\
|
2015-11-10 09:22:25 +00:00
|
|
|
|
static void getOrDefault(\
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const DictionaryType * dict, const std::string & name, const PaddedPODArray<UInt64> & ids,\
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const DefaultsType & def, PaddedPODArray<TYPE> & out)\
|
2015-11-10 09:22:25 +00:00
|
|
|
|
{\
|
|
|
|
|
dict->get##TYPE(name, ids, def, out);\
|
|
|
|
|
}\
|
2015-11-20 15:53:23 +00:00
|
|
|
|
template <typename DictionaryType, typename DefaultsType>\
|
2015-11-13 01:44:41 +00:00
|
|
|
|
static void getOrDefault(\
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const DictionaryType * dict, const std::string & name, const ConstColumnPlainPtrs & key_columns,\
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const DataTypes & key_types, const DefaultsType & def, PaddedPODArray<TYPE> & out)\
|
2015-11-13 01:44:41 +00:00
|
|
|
|
{\
|
|
|
|
|
dict->get##TYPE(name, key_columns, key_types, def, 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;
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context)
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
return std::make_shared<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:
|
2016-12-29 19:38:10 +00:00
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
2017-01-05 16:34:05 +00:00
|
|
|
|
bool isInjective(const Block & sample_block) override
|
|
|
|
|
{
|
|
|
|
|
return isDictGetFunctionInjective(dictionaries, sample_block);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (arguments.size() != 3 && arguments.size() != 4)
|
2017-01-05 16:34:05 +00:00
|
|
|
|
throw Exception{"Function " + getName() + " takes 3 or 4 arguments", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-22 14:32:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-22 14:32:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 01:44:41 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[2].get()) &&
|
|
|
|
|
!typeid_cast<const DataTypeTuple *>(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()
|
2015-11-13 01:44:41 +00:00
|
|
|
|
+ ", must be UInt64 or tuple(...).",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-22 14:32:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataType>();
|
2015-01-22 14:32:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[0]).column.get());
|
2015-01-22 14:32:38 +00:00
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
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-11-16 17:27:12 +00:00
|
|
|
|
!executeDispatchComplex<ComplexKeyHashedDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchComplex<ComplexKeyCacheDictionary>(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(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNKNOWN_TYPE};
|
2015-01-29 14:46:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatch(Block & block, const ColumnNumbers & arguments, const size_t result,
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const IDictionaryBase * 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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-01-22 14:32:38 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-01-22 14:32:38 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto id_col_untyped = block.safeGetByPosition(arguments[2]).column.get();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnUInt64 *>(id_col_untyped))
|
2015-01-22 14:32:38 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(id_col->size());
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-01-28 15:02:05 +00:00
|
|
|
|
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
|
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
|
|
|
|
{
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(1, id_col->getData());
|
|
|
|
|
PaddedPODArray<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
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
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-11-16 17:27:12 +00:00
|
|
|
|
template <typename DictionaryType>
|
2015-11-13 01:44:41 +00:00
|
|
|
|
bool executeDispatchComplex(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-11-13 01:44:41 +00:00
|
|
|
|
{
|
2015-11-16 17:27:12 +00:00
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
2015-11-13 01:44:41 +00:00
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (arguments.size() != 3)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Function " + getName() + " for dictionary of type " + dict->getTypeName() +
|
|
|
|
|
" requires exactly 3 arguments",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-11-13 01:44:41 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto key_col_with_type = block.safeGetByPosition(arguments[2]);
|
2016-07-10 07:24:24 +00:00
|
|
|
|
if (typeid_cast<const ColumnTuple *>(key_col_with_type.column.get())
|
|
|
|
|
|| typeid_cast<const ColumnConstTuple *>(key_col_with_type.column.get()))
|
2015-11-13 01:44:41 +00:00
|
|
|
|
{
|
2016-07-10 07:24:24 +00:00
|
|
|
|
const ColumnPtr key_col_materialized = key_col_with_type.column->convertToFullColumnIfConst();
|
2016-03-03 01:54:58 +00:00
|
|
|
|
|
|
|
|
|
const auto key_columns = ext::map<ConstColumnPlainPtrs>(
|
2016-07-10 07:24:24 +00:00
|
|
|
|
static_cast<const ColumnTuple &>(*key_col_materialized.get()).getColumns(),
|
|
|
|
|
[](const ColumnPtr & ptr) { return ptr.get(); });
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
|
|
|
|
const auto & key_types = static_cast<const DataTypeTuple &>(*key_col_with_type.type).getElements();
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(key_columns.front()->size());
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
|
|
|
|
|
DictGetTraits<DataType>::get(dict, attr_name, key_columns, key_types, data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be " + dict->getKeyDescription(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::TYPE_MISMATCH};
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 13:06:25 +00:00
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatchRange(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
|
|
|
|
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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto id_col_untyped = block.safeGetByPosition(arguments[2]).column.get();
|
|
|
|
|
const auto date_col_untyped = block.safeGetByPosition(arguments[3]).column.get();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnUInt64 *>(id_col_untyped))
|
2015-07-14 13:06:25 +00:00
|
|
|
|
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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeRange(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const size_t result, const DictionaryType * dictionary, const std::string & attr_name,
|
|
|
|
|
const ColumnUInt64 * id_col, const IColumn * date_col_untyped)
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto date_col = typeid_cast<const ColumnUInt16 *>(date_col_untyped))
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
|
|
|
|
const auto size = id_col->size();
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
const auto & dates = date_col->getData();
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(size);
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
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();
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt16> dates(size, date_col->getData());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(size);
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
DictGetTraits<DataType>::get(dictionary, attr_name, ids, dates, data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be Date",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeRange(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const size_t result, const DictionaryType * dictionary, const std::string & attr_name,
|
|
|
|
|
const ColumnConst<UInt64> * id_col, const IColumn * date_col_untyped)
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto date_col = typeid_cast<const ColumnUInt16 *>(date_col_untyped))
|
2015-07-14 13:06:25 +00:00
|
|
|
|
{
|
|
|
|
|
const auto size = date_col->size();
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(size, id_col->getData());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
const auto & dates = date_col->getData();
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(size);
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-07-14 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(1, id_col->getData());
|
|
|
|
|
const PaddedPODArray<UInt16> dates(1, date_col->getData());
|
|
|
|
|
PaddedPODArray<Type> data(1);
|
2015-07-14 13:06:25 +00:00
|
|
|
|
DictGetTraits<DataType>::get(dictionary, attr_name, ids, dates, data);
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConst<Type>>(id_col->size(), data.front());
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be Date",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-07-14 13:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2015-11-10 09:22:25 +00:00
|
|
|
|
template <typename DataType>
|
|
|
|
|
class FunctionDictGetOrDefault final : public IFunction
|
|
|
|
|
{
|
|
|
|
|
using Type = typename DataType::FieldType;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static const std::string name;
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context)
|
2015-11-10 09:22:25 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
return std::make_shared<FunctionDictGetOrDefault>(context.getExternalDictionaries());
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FunctionDictGetOrDefault(const ExternalDictionaries & dictionaries) : dictionaries(dictionaries) {}
|
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
private:
|
2016-12-29 19:38:10 +00:00
|
|
|
|
size_t getNumberOfArguments() const override { return 4; }
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-11-10 09:22:25 +00:00
|
|
|
|
{
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[0].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of first argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeString *>(arguments[1].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of second argument of function " + getName()
|
|
|
|
|
+ ", expected a string.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 01:44:41 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt64 *>(arguments[2].get()) &&
|
|
|
|
|
!typeid_cast<const DataTypeTuple *>(arguments[2].get()))
|
2015-11-10 09:22:25 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[2]->getName() + " of third argument of function " + getName()
|
2015-11-13 01:44:41 +00:00
|
|
|
|
+ ", must be UInt64 or tuple(...).",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataType *>(arguments[3].get()))
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal type " + arguments[3]->getName() + " of fourth argument of function " + getName()
|
|
|
|
|
+ ", must be " + DataType{}.getName() + ".",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataType>();
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2015-11-10 09:22:25 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[0]).column.get());
|
2015-11-10 09:22:25 +00:00
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
|
|
|
|
auto dict = dictionaries.getDictionary(dict_name_col->getData());
|
|
|
|
|
const auto dict_ptr = dict.get();
|
|
|
|
|
|
|
|
|
|
if (!executeDispatch<FlatDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatch<HashedDictionary>(block, arguments, result, dict_ptr) &&
|
2015-11-18 13:31:29 +00:00
|
|
|
|
!executeDispatch<CacheDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchComplex<ComplexKeyHashedDictionary>(block, arguments, result, dict_ptr) &&
|
|
|
|
|
!executeDispatchComplex<ComplexKeyCacheDictionary>(block, arguments, result, dict_ptr))
|
2015-11-10 09:22:25 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Unsupported dictionary type " + dict_ptr->getTypeName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNKNOWN_TYPE};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatch(Block & block, const ColumnNumbers & arguments, const size_t result,
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const IDictionaryBase * dictionary)
|
2015-11-10 09:22:25 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-11-10 09:22:25 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto id_col_untyped = block.safeGetByPosition(arguments[2]).column.get();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnUInt64 *>(id_col_untyped))
|
2015-11-10 09:22:25 +00:00
|
|
|
|
executeDispatch(block, arguments, result, dict, attr_name, id_col);
|
|
|
|
|
else if (const auto id_col = typeid_cast<const ColumnConst<UInt64> *>(id_col_untyped))
|
|
|
|
|
executeDispatch(block, arguments, result, dict, attr_name, id_col);
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Third argument of function " + getName() + " must be UInt64",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeDispatch(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const DictionaryType * dictionary,
|
|
|
|
|
const std::string & attr_name, const ColumnUInt64 * id_col)
|
2015-11-10 09:22:25 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto default_col_untyped = block.safeGetByPosition(arguments[3]).column.get();
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
|
|
|
|
if (const auto default_col = typeid_cast<const ColumnVector<Type> *>(default_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
/// vector ids, vector defaults
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(id_col->size());
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
const auto & defs = default_col->getData();
|
|
|
|
|
|
|
|
|
|
DictGetTraits<DataType>::getOrDefault(dictionary, attr_name, ids, defs, data);
|
|
|
|
|
}
|
|
|
|
|
else if (const auto default_col = typeid_cast<const ColumnConst<Type> *>(default_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
/// vector ids, const defaults
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(id_col->size());
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
|
|
|
|
const auto & ids = id_col->getData();
|
|
|
|
|
auto & data = out->getData();
|
2015-11-20 15:53:23 +00:00
|
|
|
|
const auto def = default_col->getData();
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
DictGetTraits<DataType>::getOrDefault(dictionary, attr_name, ids, def, data);
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be " + DataType{}.getName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
void executeDispatch(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const DictionaryType * dictionary,
|
|
|
|
|
const std::string & attr_name, const ColumnConst<UInt64> * id_col)
|
2015-11-10 09:22:25 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto default_col_untyped = block.safeGetByPosition(arguments[3]).column.get();
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
|
|
|
|
if (const auto default_col = typeid_cast<const ColumnVector<Type> *>(default_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
/// const ids, vector defaults
|
|
|
|
|
/// @todo avoid materialization
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(id_col->size(), id_col->getData());
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(id_col->size());
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
const auto & defs = default_col->getData();
|
|
|
|
|
|
|
|
|
|
DictGetTraits<DataType>::getOrDefault(dictionary, attr_name, ids, defs, data);
|
|
|
|
|
}
|
|
|
|
|
else if (const auto default_col = typeid_cast<const ColumnConst<Type> *>(default_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
/// const ids, const defaults
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> ids(1, id_col->getData());
|
|
|
|
|
PaddedPODArray<Type> data(1);
|
2015-11-20 15:53:23 +00:00
|
|
|
|
const auto & def = default_col->getData();
|
|
|
|
|
|
|
|
|
|
DictGetTraits<DataType>::getOrDefault(dictionary, attr_name, ids, def, data);
|
2015-11-10 09:22:25 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConst<Type>>(id_col->size(), data.front());
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be " + DataType{}.getName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-10 09:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 13:31:29 +00:00
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatchComplex(
|
2016-12-13 21:15:27 +00:00
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result, const IDictionaryBase * dictionary)
|
2015-11-18 13:31:29 +00:00
|
|
|
|
{
|
|
|
|
|
const auto dict = typeid_cast<const DictionaryType *>(dictionary);
|
|
|
|
|
if (!dict)
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto attr_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[1]).column.get());
|
2015-11-18 13:31:29 +00:00
|
|
|
|
if (!attr_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-18 13:31:29 +00:00
|
|
|
|
|
|
|
|
|
const auto & attr_name = attr_name_col->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto key_col_with_type = block.safeGetByPosition(arguments[2]);
|
2015-11-18 13:31:29 +00:00
|
|
|
|
const auto & key_col = typeid_cast<const ColumnTuple &>(*key_col_with_type.column);
|
2016-03-03 01:54:58 +00:00
|
|
|
|
|
|
|
|
|
const ColumnPtr key_col_materialized = key_col.convertToFullColumnIfConst();
|
|
|
|
|
|
|
|
|
|
const auto key_columns = ext::map<ConstColumnPlainPtrs>(
|
|
|
|
|
static_cast<const ColumnTuple &>(*key_col_materialized.get()).getColumns(), [](const ColumnPtr & ptr) { return ptr.get(); });
|
|
|
|
|
|
2015-11-18 13:31:29 +00:00
|
|
|
|
const auto & key_types = static_cast<const DataTypeTuple &>(*key_col_with_type.type).getElements();
|
|
|
|
|
|
|
|
|
|
/// @todo detect when all key columns are constant
|
|
|
|
|
const auto rows = key_col.size();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnVector<Type>>(rows);
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-11-18 13:31:29 +00:00
|
|
|
|
auto & data = out->getData();
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto default_col_untyped = block.safeGetByPosition(arguments[3]).column.get();
|
2015-11-18 13:31:29 +00:00
|
|
|
|
if (const auto default_col = typeid_cast<const ColumnVector<Type> *>(default_col_untyped))
|
|
|
|
|
{
|
|
|
|
|
/// const defaults
|
|
|
|
|
const auto & defs = default_col->getData();
|
|
|
|
|
|
|
|
|
|
DictGetTraits<DataType>::getOrDefault(dict, attr_name, key_columns, key_types, defs, data);
|
|
|
|
|
}
|
|
|
|
|
else if (const auto default_col = typeid_cast<const ColumnConst<Type> *>(default_col_untyped))
|
|
|
|
|
{
|
2015-11-20 15:53:23 +00:00
|
|
|
|
const auto def = default_col->getData();
|
2015-11-18 13:31:29 +00:00
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
DictGetTraits<DataType>::getOrDefault(dict, attr_name, key_columns, key_types, def, data);
|
2015-11-18 13:31:29 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Fourth argument of function " + getName() + " must be " + DataType{}.getName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-11-18 13:31:29 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-10 09:22:25 +00:00
|
|
|
|
const ExternalDictionaries & dictionaries;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename DataType>
|
|
|
|
|
const std::string FunctionDictGetOrDefault<DataType>::name = "dictGet" + DataType{}.getName() + "OrDefault";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using FunctionDictGetUInt8OrDefault = FunctionDictGetOrDefault<DataTypeUInt8>;
|
|
|
|
|
using FunctionDictGetUInt16OrDefault = FunctionDictGetOrDefault<DataTypeUInt16>;
|
|
|
|
|
using FunctionDictGetUInt32OrDefault = FunctionDictGetOrDefault<DataTypeUInt32>;
|
|
|
|
|
using FunctionDictGetUInt64OrDefault = FunctionDictGetOrDefault<DataTypeUInt64>;
|
|
|
|
|
using FunctionDictGetInt8OrDefault = FunctionDictGetOrDefault<DataTypeInt8>;
|
|
|
|
|
using FunctionDictGetInt16OrDefault = FunctionDictGetOrDefault<DataTypeInt16>;
|
|
|
|
|
using FunctionDictGetInt32OrDefault = FunctionDictGetOrDefault<DataTypeInt32>;
|
|
|
|
|
using FunctionDictGetInt64OrDefault = FunctionDictGetOrDefault<DataTypeInt64>;
|
|
|
|
|
using FunctionDictGetFloat32OrDefault = FunctionDictGetOrDefault<DataTypeFloat32>;
|
|
|
|
|
using FunctionDictGetFloat64OrDefault = FunctionDictGetOrDefault<DataTypeFloat64>;
|
|
|
|
|
using FunctionDictGetDateOrDefault = FunctionDictGetOrDefault<DataTypeDate>;
|
|
|
|
|
using FunctionDictGetDateTimeOrDefault = FunctionDictGetOrDefault<DataTypeDateTime>;
|
|
|
|
|
|
|
|
|
|
|
2017-03-25 23:42:04 +00:00
|
|
|
|
/// Functions to work with hierarchies.
|
|
|
|
|
|
2015-01-28 13:20:20 +00:00
|
|
|
|
class FunctionDictGetHierarchy final : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "dictGetHierarchy";
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context)
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
return std::make_shared<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:
|
2016-12-29 19:38:10 +00:00
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
2017-01-05 16:34:05 +00:00
|
|
|
|
bool isInjective(const Block & sample_block) override { return true; }
|
2016-12-29 19:38:10 +00:00
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt64>());
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[0]).column.get());
|
2015-01-28 13:20:20 +00:00
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
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(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNKNOWN_TYPE};
|
2015-01-29 14:46:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatch(Block & block, const ColumnNumbers & arguments, const size_t result,
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const IDictionaryBase * 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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNSUPPORTED_METHOD};
|
2015-07-13 17:10:16 +00:00
|
|
|
|
|
2017-03-25 23:42:04 +00:00
|
|
|
|
const auto get_hierarchies = [&] (const PaddedPODArray<UInt64> & in, PaddedPODArray<UInt64> & out, PaddedPODArray<UInt64> & offsets)
|
|
|
|
|
{
|
2015-03-04 13:18:07 +00:00
|
|
|
|
const auto size = in.size();
|
|
|
|
|
|
|
|
|
|
/// copy of `in` array
|
2016-04-15 00:33:21 +00:00
|
|
|
|
auto in_array = std::make_unique<PaddedPODArray<UInt64>>(std::begin(in), std::end(in));
|
2015-03-04 13:18:07 +00:00
|
|
|
|
/// used for storing and handling result of ::toParent call
|
2016-04-15 00:33:21 +00:00
|
|
|
|
auto out_array = std::make_unique<PaddedPODArray<UInt64>>(size);
|
2015-03-04 13:18:07 +00:00
|
|
|
|
/// resulting hierarchies
|
2016-12-12 21:37:57 +00:00
|
|
|
|
std::vector<std::vector<IDictionary::Key>> hierarchies(size); /// TODO Bad code, poor performance.
|
2015-03-04 13:18:07 +00:00
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
};
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto id_col_untyped = block.safeGetByPosition(arguments[1]).column.get();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto id_col = typeid_cast<const ColumnUInt64 *>(id_col_untyped))
|
2015-07-10 14:43:49 +00:00
|
|
|
|
{
|
|
|
|
|
const auto & in = id_col->getData();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto backend = std::make_shared<ColumnUInt64>();
|
|
|
|
|
const auto array = std::make_shared<ColumnArray>(backend);
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = array;
|
2015-07-10 14:43:49 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> in(1, id_col->getData());
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto backend = std::make_shared<ColumnUInt64>();
|
|
|
|
|
const auto array = std::make_shared<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
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConstArray>(
|
2015-01-28 13:20:20 +00:00
|
|
|
|
id_col->size(),
|
2015-07-10 14:43:49 +00:00
|
|
|
|
(*array)[0].get<Array>(),
|
2016-05-28 07:48:40 +00:00
|
|
|
|
std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt64>()));
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Second argument of function " + getName() + " must be UInt64",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context)
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2016-05-28 15:42:22 +00:00
|
|
|
|
return std::make_shared<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:
|
2016-12-29 19:38:10 +00:00
|
|
|
|
size_t getNumberOfArguments() const override { return 3; }
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
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.",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto dict_name_col = typeid_cast<const ColumnConst<String> *>(block.safeGetByPosition(arguments[0]).column.get());
|
2015-01-28 13:20:20 +00:00
|
|
|
|
if (!dict_name_col)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"First argument of function " + getName() + " must be a constant string",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
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
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
|
if (!executeDispatch<FlatDictionary>(block, arguments, result, dict_ptr)
|
|
|
|
|
&& !executeDispatch<HashedDictionary>(block, arguments, result, dict_ptr)
|
|
|
|
|
&& !executeDispatch<CacheDictionary>(block, arguments, result, dict_ptr))
|
2015-01-29 14:46:15 +00:00
|
|
|
|
throw Exception{
|
|
|
|
|
"Unsupported dictionary type " + dict_ptr->getTypeName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNKNOWN_TYPE};
|
2015-01-29 14:46:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename DictionaryType>
|
|
|
|
|
bool executeDispatch(Block & block, const ColumnNumbers & arguments, const size_t result,
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const IDictionaryBase * 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",
|
2016-03-03 01:54:58 +00:00
|
|
|
|
ErrorCodes::UNSUPPORTED_METHOD};
|
2015-07-13 17:10:16 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
const auto child_id_col_untyped = block.safeGetByPosition(arguments[1]).column.get();
|
|
|
|
|
const auto ancestor_id_col_untyped = block.safeGetByPosition(arguments[2]).column.get();
|
2015-02-19 14:51:39 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto child_id_col = typeid_cast<const ColumnUInt64 *>(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(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
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>
|
2016-12-13 21:15:27 +00:00
|
|
|
|
bool execute(Block & block, const size_t result, const DictionaryType * dictionary,
|
|
|
|
|
const ColumnUInt64 * child_id_col, const IColumn * ancestor_id_col_untyped)
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto ancestor_id_col = typeid_cast<const ColumnUInt64 *>(ancestor_id_col_untyped))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnUInt8>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2016-12-13 21:15:27 +00:00
|
|
|
|
dictionary->isInVectorVector(child_ids, ancestor_ids, data);
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnUInt8>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2016-12-13 21:15:27 +00:00
|
|
|
|
dictionary->isInVectorConstant(child_ids, ancestor_id, data);
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal column " + ancestor_id_col_untyped->getName()
|
|
|
|
|
+ " of third argument of function " + getName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
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>
|
2016-12-13 21:15:27 +00:00
|
|
|
|
bool execute(Block & block, const size_t result, const DictionaryType * dictionary,
|
|
|
|
|
const ColumnConst<UInt64> * child_id_col, const IColumn * ancestor_id_col_untyped)
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
if (const auto ancestor_id_col = typeid_cast<const ColumnUInt64 *>(ancestor_id_col_untyped))
|
2015-01-28 13:20:20 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnUInt8>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = out;
|
2015-01-28 13:20:20 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2016-12-13 21:15:27 +00:00
|
|
|
|
dictionary->isInConstantVector(child_id, ancestor_ids, data);
|
2015-01-28 13:20:20 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2016-12-13 21:15:27 +00:00
|
|
|
|
const auto child_id = child_id_col->getData();
|
|
|
|
|
const auto ancestor_id = ancestor_id_col->getData();
|
2016-12-12 21:37:57 +00:00
|
|
|
|
UInt8 res = 0;
|
2016-12-13 21:15:27 +00:00
|
|
|
|
|
|
|
|
|
dictionary->isInConstantConstant(child_id, ancestor_id, res);
|
2016-12-12 21:37:57 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConst<UInt8>>(
|
2016-12-12 21:37:57 +00:00
|
|
|
|
child_id_col->size(), res);
|
2015-02-19 14:51:39 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal column " + ancestor_id_col_untyped->getName()
|
|
|
|
|
+ " of third argument of function " + getName(),
|
2016-03-03 01:54:58 +00:00
|
|
|
|
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
|
|
|
|
};
|