2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/range.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Dictionaries/ExternalResultDescription.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int UNKNOWN_TYPE;
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalResultDescription::init(const Block & sample_block_)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
sample_block = sample_block_;
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto num_columns = sample_block.columns();
|
|
|
|
types.reserve(num_columns);
|
|
|
|
names.reserve(num_columns);
|
|
|
|
sample_columns.reserve(num_columns);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto idx : ext::range(0, num_columns))
|
|
|
|
{
|
|
|
|
const auto & column = sample_block.safeGetByPosition(idx);
|
|
|
|
const auto type = column.type.get();
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt8 *>(type))
|
|
|
|
types.push_back(ValueType::UInt8);
|
|
|
|
else if (typeid_cast<const DataTypeUInt16 *>(type))
|
|
|
|
types.push_back(ValueType::UInt16);
|
|
|
|
else if (typeid_cast<const DataTypeUInt32 *>(type))
|
|
|
|
types.push_back(ValueType::UInt32);
|
|
|
|
else if (typeid_cast<const DataTypeUInt64 *>(type))
|
|
|
|
types.push_back(ValueType::UInt64);
|
|
|
|
else if (typeid_cast<const DataTypeInt8 *>(type))
|
|
|
|
types.push_back(ValueType::Int8);
|
|
|
|
else if (typeid_cast<const DataTypeInt16 *>(type))
|
|
|
|
types.push_back(ValueType::Int16);
|
|
|
|
else if (typeid_cast<const DataTypeInt32 *>(type))
|
|
|
|
types.push_back(ValueType::Int32);
|
|
|
|
else if (typeid_cast<const DataTypeInt64 *>(type))
|
|
|
|
types.push_back(ValueType::Int64);
|
|
|
|
else if (typeid_cast<const DataTypeFloat32 *>(type))
|
|
|
|
types.push_back(ValueType::Float32);
|
|
|
|
else if (typeid_cast<const DataTypeFloat64 *>(type))
|
|
|
|
types.push_back(ValueType::Float64);
|
|
|
|
else if (typeid_cast<const DataTypeString *>(type))
|
|
|
|
types.push_back(ValueType::String);
|
|
|
|
else if (typeid_cast<const DataTypeDate *>(type))
|
|
|
|
types.push_back(ValueType::Date);
|
|
|
|
else if (typeid_cast<const DataTypeDateTime *>(type))
|
|
|
|
types.push_back(ValueType::DateTime);
|
|
|
|
else
|
|
|
|
throw Exception{
|
|
|
|
"Unsupported type " + type->getName(),
|
|
|
|
ErrorCodes::UNKNOWN_TYPE};
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
names.emplace_back(column.name);
|
2017-05-25 20:09:48 +00:00
|
|
|
sample_columns.emplace_back(column.column);
|
2017-12-28 05:15:09 +00:00
|
|
|
|
|
|
|
/// If default value for column was not provided, use default from data type.
|
|
|
|
if (sample_columns.back()->empty())
|
|
|
|
{
|
|
|
|
MutableColumnPtr mutable_column = sample_columns.back()->mutate();
|
|
|
|
column.type->insertDefaultInto(*mutable_column);
|
|
|
|
sample_columns.back() = std::move(mutable_column);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|