Moved DataTypeWithDictionary implementation to DataTypeWithDictionary.cpp

This commit is contained in:
Nikolai Kochetov 2018-05-03 18:17:10 +03:00
parent 04a056f804
commit ed83798dd2
4 changed files with 117 additions and 110 deletions

View File

@ -20,6 +20,24 @@ namespace ErrorCodes
extern const int ILLEGAL_TYPE_OF_ARGUMENT; extern const int ILLEGAL_TYPE_OF_ARGUMENT;
} }
namespace
{
const ColumnWithDictionary & getColumnWithDictionary(const IColumn & column) const
{
return typeid_cast<const ColumnWithDictionary &>(column);
}
ColumnWithDictionary & getColumnWithDictionary(IColumn & column) const
{
return typeid_cast<ColumnWithDictionary &>(column);
}
IColumn & getNestedUniqueColumn(ColumnWithDictionary & column_with_dictionary) const
{
return column_with_dictionary.getUnique()->getNestedColumn()->assumeMutableRef();
}
}
DataTypeWithDictionary::DataTypeWithDictionary(DataTypePtr dictionary_type_, DataTypePtr indexes_type_) DataTypeWithDictionary::DataTypeWithDictionary(DataTypePtr dictionary_type_, DataTypePtr indexes_type_)
: dictionary_type(std::move(dictionary_type_)), indexes_type(std::move(indexes_type_)) : dictionary_type(std::move(dictionary_type_)), indexes_type(std::move(indexes_type_))
{ {

View File

@ -137,20 +137,6 @@ public:
bool withDictionary() const override { return true; } bool withDictionary() const override { return true; }
private: private:
const ColumnWithDictionary & getColumnWithDictionary(const IColumn & column) const
{
return typeid_cast<const ColumnWithDictionary &>(column);;
}
ColumnWithDictionary & getColumnWithDictionary(IColumn & column) const
{
return typeid_cast<ColumnWithDictionary &>(column);;
}
IColumn & getNestedUniqueColumn(ColumnWithDictionary & column_with_dictionary) const
{
return column_with_dictionary.getUnique()->getNestedColumn()->assumeMutableRef();
}
template <typename ... Args> template <typename ... Args>
using SerealizeFunctionPtr = void (IDataType::*)(const IColumn &, size_t, WriteBuffer &, Args & ...) const; using SerealizeFunctionPtr = void (IDataType::*)(const IColumn &, size_t, WriteBuffer &, Args & ...) const;

View File

@ -11,6 +11,7 @@
#include <Columns/ColumnString.h> #include <Columns/ColumnString.h>
#include <Columns/ColumnTuple.h> #include <Columns/ColumnTuple.h>
#include <Columns/ColumnArray.h> #include <Columns/ColumnArray.h>
#include <Columns/ColumnWithDictionary.h>
#include <Functions/FunctionHelpers.h> #include <Functions/FunctionHelpers.h>
#include <Common/UnicodeBar.h> #include <Common/UnicodeBar.h>
#include <Common/UTF8Helpers.h> #include <Common/UTF8Helpers.h>
@ -23,6 +24,7 @@
#include <DataTypes/DataTypeTuple.h> #include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypesNumber.h> #include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeEnum.h> #include <DataTypes/DataTypeEnum.h>
#include <DataTypes/DataTypeWithDictionary.h>
#include <DataTypes/NumberTraits.h> #include <DataTypes/NumberTraits.h>
#include <Functions/FunctionFactory.h> #include <Functions/FunctionFactory.h>
#include <Interpreters/Cluster.h> #include <Interpreters/Cluster.h>
@ -1833,6 +1835,103 @@ public:
}; };
class FunctionMakeDictionary: public IFunction
{
public:
static constexpr auto name = "makeDictionary";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionMakeDictionary>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
return std::make_shared<DataTypeWithDictionary>(arguments[0], std::make_shared<DataTypeUInt16>());
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
{
auto arg_num = arguments[0];
const auto & arg = block.getByPosition(arg_num);
auto & res = block.getByPosition(result);
auto column = res.type->createColumn();
column->insertRangeFrom(*arg.column, 0, arg.column->size());
res.column = std::move(column);
}
};
class FunctionDictionaryIndexes: public IFunction
{
public:
static constexpr auto name = "dictionaryIndexes";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionDictionaryIndexes>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForColumnsWithDictionary() const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
auto * type = typeid_cast<const DataTypeWithDictionary *>(arguments[0].get());
if (!type)
throw Exception("First first argument of function dictionaryIndexes must be ColumnWithDictionary, but got"
+ arguments[0]->getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return type->getIndexesType();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
{
auto arg_num = arguments[0];
const auto & arg = block.getByPosition(arg_num);
auto & res = block.getByPosition(result);
res.column = typeid_cast<const ColumnWithDictionary *>(arg.column.get())->getIndexesPtr();
}
};
class FunctionDictionaryValues: public IFunction
{
public:
static constexpr auto name = "dictionaryValues";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionDictionaryValues>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForColumnsWithDictionary() const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
auto * type = typeid_cast<const DataTypeWithDictionary *>(arguments[0].get());
if (!type)
throw Exception("First first argument of function dictionaryValues must be ColumnWithDictionary, but got"
+ arguments[0]->getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return type->getDictionaryType();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
{
auto arg_num = arguments[0];
const auto & arg = block.getByPosition(arg_num);
auto & res = block.getByPosition(result);
const auto * column_with_dictionary = typeid_cast<const ColumnWithDictionary *>(arg.column.get());
res.column = column_with_dictionary->getUnique()->getNestedColumn()->cloneResized(arg.column->size());
}
};
std::string FunctionVersion::getVersion() const std::string FunctionVersion::getVersion() const
{ {
std::ostringstream os; std::ostringstream os;

View File

@ -7,7 +7,6 @@
#include <IO/Operators.h> #include <IO/Operators.h>
#include <Columns/ColumnFunction.h> #include <Columns/ColumnFunction.h>
#include <DataTypes/DataTypesNumber.h> #include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeWithDictionary.h>
namespace DB namespace DB
{ {
@ -37,101 +36,6 @@ public:
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override; void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override;
}; };
class FunctionMakeDictionary: public IFunction
{
public:
static constexpr auto name = "makeDictionary";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionMakeDictionary>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
return std::make_shared<DataTypeWithDictionary>(arguments[0], std::make_shared<DataTypeUInt16>());
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
{
auto arg_num = arguments[0];
const auto & arg = block.getByPosition(arg_num);
auto & res = block.getByPosition(result);
auto column = res.type->createColumn();
column->insertRangeFrom(*arg.column, 0, arg.column->size());
res.column = std::move(column);
}
};
class FunctionDictionaryIndexes: public IFunction
{
public:
static constexpr auto name = "dictionaryIndexes";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionDictionaryIndexes>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForColumnsWithDictionary() const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
auto * type = typeid_cast<const DataTypeWithDictionary *>(arguments[0].get());
if (!type)
throw Exception("First first argument of function dictionaryIndexes must be ColumnWithDictionary, but got"
+ arguments[0]->getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return type->getIndexesType();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
{
auto arg_num = arguments[0];
const auto & arg = block.getByPosition(arg_num);
auto & res = block.getByPosition(result);
res.column = typeid_cast<const ColumnWithDictionary *>(arg.column.get())->getIndexesPtr();
}
};
class FunctionDictionaryValues: public IFunction
{
public:
static constexpr auto name = "dictionaryValues";
static FunctionPtr create(const Context &) { return std::make_shared<FunctionDictionaryValues>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForColumnsWithDictionary() const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
auto * type = typeid_cast<const DataTypeWithDictionary *>(arguments[0].get());
if (!type)
throw Exception("First first argument of function dictionaryValues must be ColumnWithDictionary, but got"
+ arguments[0]->getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return type->getDictionaryType();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
{
auto arg_num = arguments[0];
const auto & arg = block.getByPosition(arg_num);
auto & res = block.getByPosition(result);
const auto * column_with_dictionary = typeid_cast<const ColumnWithDictionary *>(arg.column.get());
res.column = column_with_dictionary->getUnique()->getNestedColumn()->cloneResized(arg.column->size());
}
};
/// Executes expression. Uses for lambda functions implementation. Can't be created from factory. /// Executes expression. Uses for lambda functions implementation. Can't be created from factory.
class FunctionExpression : public IFunctionBase, public IPreparedFunction, class FunctionExpression : public IFunctionBase, public IPreparedFunction,