Preparation [#METR-19470].

This commit is contained in:
Alexey Milovidov 2016-04-10 05:32:59 +03:00
parent 54d1ce01a6
commit 0bcccb720f
4 changed files with 108 additions and 16 deletions

View File

@ -279,7 +279,7 @@ add_library (dbms
include/DB/Dictionaries/Embedded/RegionsHierarchies.h
include/DB/Dictionaries/Embedded/RegionsNames.h
include/DB/Dictionaries/Embedded/TechDataHierarchy.h
include/DB/Dictionaries/ExternalDatabaseHelper.h
include/DB/Dictionaries/ExternalResultDescription.h
include/DB/Interpreters/InterpreterAlterQuery.h
include/DB/Interpreters/AggregationCommon.h
include/DB/Interpreters/ProcessList.h

View File

@ -0,0 +1,92 @@
#pragma once
#include <ext/range.hpp>
#include <DB/Core/Block.h>
namespace DB
{
namespace ErrorCodes
{
extern const int UNKNOWN_TYPE;
}
/** Общая часть реализации MySQLBlockInputStream, MongoDBBlockInputStream, ODBCBlockInputStream.
*/
struct ExternalResultDescription
{
enum struct value_type_t
{
UInt8,
UInt16,
UInt32,
UInt64,
Int8,
Int16,
Int32,
Int64,
Float32,
Float64,
String,
Date,
DateTime
};
Block sample_block;
std::vector<value_type_t> types;
std::vector<std::string> names;
ConstColumnPlainPtrs sample_columns;
void init(const Block & sample_block_)
{
sample_block = sample_block_;
const auto num_columns = sample_block.columns();
types.reserve(num_columns);
names.reserve(num_columns);
sample_columns.reserve(num_columns);
for (const auto idx : ext::range(0, num_columns))
{
const auto & column = sample_block.getByPosition(idx);
const auto type = column.type.get();
if (typeid_cast<const DataTypeUInt8 *>(type))
types.push_back(value_type_t::UInt8);
else if (typeid_cast<const DataTypeUInt16 *>(type))
types.push_back(value_type_t::UInt16);
else if (typeid_cast<const DataTypeUInt32 *>(type))
types.push_back(value_type_t::UInt32);
else if (typeid_cast<const DataTypeUInt64 *>(type))
types.push_back(value_type_t::UInt64);
else if (typeid_cast<const DataTypeInt8 *>(type))
types.push_back(value_type_t::Int8);
else if (typeid_cast<const DataTypeInt16 *>(type))
types.push_back(value_type_t::Int16);
else if (typeid_cast<const DataTypeInt32 *>(type))
types.push_back(value_type_t::Int32);
else if (typeid_cast<const DataTypeInt64 *>(type))
types.push_back(value_type_t::Int64);
else if (typeid_cast<const DataTypeFloat32 *>(type))
types.push_back(value_type_t::Float32);
else if (typeid_cast<const DataTypeFloat64 *>(type))
types.push_back(value_type_t::Float64);
else if (typeid_cast<const DataTypeString *>(type))
types.push_back(value_type_t::String);
else if (typeid_cast<const DataTypeDate *>(type))
types.push_back(value_type_t::Date);
else if (typeid_cast<const DataTypeDateTime *>(type))
types.push_back(value_type_t::DateTime);
else
throw Exception{
"Unsupported type " + type->getName(),
ErrorCodes::UNKNOWN_TYPE};
names.emplace_back(column.name);
sample_columns.emplace_back(column.column.get());
}
}
};
}

View File

@ -8,7 +8,7 @@
#include <DB/DataTypes/DataTypeDate.h>
#include <DB/DataTypes/DataTypeDateTime.h>
#include <DB/Columns/ColumnString.h>
#include <DB/Dictionaries/ExternalDatabaseHelper.h>
#include <DB/Dictionaries/ExternalResultDescription.h>
#include <ext/range.hpp>
#include <mongo/client/dbclient.h>
#include <vector>
@ -31,7 +31,7 @@ public:
if (!cursor->more())
return;
helper.init(sample_block);
description.init(sample_block);
}
String getName() const override { return "MongoDB"; }
@ -43,7 +43,7 @@ public:
}
private:
using value_type_t = ExternalDatabaseHelper::value_type_t;
using value_type_t = ExternalResultDescription::value_type_t;
Block readImpl() override
{
@ -51,7 +51,7 @@ private:
if (!cursor->more())
return {};
auto block = helper.sample_block.cloneEmpty();
auto block = description.sample_block.cloneEmpty();
/// cache pointers returned by the calls to getByPosition
std::vector<IColumn *> columns(block.columns());
@ -67,12 +67,12 @@ private:
for (const auto idx : ext::range(0, size))
{
const auto & name = helper.names[idx];
const auto & name = description.names[idx];
const auto value = row[name];
if (value.ok())
insertValue(columns[idx], helper.types[idx], value, name);
insertValue(columns[idx], description.types[idx], value, name);
else
insertDefaultValue(columns[idx], *helper.sample_columns[idx]);
insertDefaultValue(columns[idx], *description.sample_columns[idx]);
}
++num_rows;
@ -230,7 +230,7 @@ private:
std::unique_ptr<mongo::DBClientCursor> cursor;
const std::size_t max_block_size;
ExternalDatabaseHelper helper;
ExternalResultDescription description;
};
}

View File

@ -7,7 +7,7 @@
#include <DB/DataTypes/DataTypeDate.h>
#include <DB/DataTypes/DataTypeDateTime.h>
#include <DB/Columns/ColumnString.h>
#include <DB/Dictionaries/ExternalDatabaseHelper.h>
#include <DB/Dictionaries/ExternalResultDescription.h>
#include <ext/range.hpp>
#include <mysqlxx/Query.h>
#include <mysqlxx/PoolWithFailover.h>
@ -38,7 +38,7 @@ public:
toString(sample_block.columns()) + " expected",
ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH};
helper.init(sample_block);
description.init(sample_block);
}
String getName() const override { return "MySQL"; }
@ -49,7 +49,7 @@ public:
}
private:
using value_type_t = ExternalDatabaseHelper::value_type_t;
using value_type_t = ExternalResultDescription::value_type_t;
Block readImpl() override
{
@ -57,7 +57,7 @@ private:
if (!row)
return {};
auto block = helper.sample_block.cloneEmpty();
auto block = description.sample_block.cloneEmpty();
/// cache pointers returned by the calls to getByPosition
std::vector<IColumn *> columns(block.columns());
@ -71,9 +71,9 @@ private:
{
const auto value = row[idx];
if (!value.isNull())
insertValue(columns[idx], helper.types[idx], value);
insertValue(columns[idx], description.types[idx], value);
else
insertDefaultValue(columns[idx], *helper.sample_columns[idx]);
insertDefaultValue(columns[idx], *description.sample_columns[idx]);
}
++num_rows;
@ -115,7 +115,7 @@ private:
mysqlxx::Query query;
mysqlxx::UseQueryResult result;
const std::size_t max_block_size;
ExternalDatabaseHelper helper;
ExternalResultDescription description;
};
}