diff --git a/dbms/CMakeLists.txt b/dbms/CMakeLists.txt index f83a6590b9f..ae1b09c4bb8 100644 --- a/dbms/CMakeLists.txt +++ b/dbms/CMakeLists.txt @@ -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 diff --git a/dbms/include/DB/Dictionaries/ExternalResultDescription.h b/dbms/include/DB/Dictionaries/ExternalResultDescription.h new file mode 100644 index 00000000000..453ddc3cd93 --- /dev/null +++ b/dbms/include/DB/Dictionaries/ExternalResultDescription.h @@ -0,0 +1,92 @@ +#pragma once + +#include +#include + + +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 types; + std::vector 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(type)) + types.push_back(value_type_t::UInt8); + else if (typeid_cast(type)) + types.push_back(value_type_t::UInt16); + else if (typeid_cast(type)) + types.push_back(value_type_t::UInt32); + else if (typeid_cast(type)) + types.push_back(value_type_t::UInt64); + else if (typeid_cast(type)) + types.push_back(value_type_t::Int8); + else if (typeid_cast(type)) + types.push_back(value_type_t::Int16); + else if (typeid_cast(type)) + types.push_back(value_type_t::Int32); + else if (typeid_cast(type)) + types.push_back(value_type_t::Int64); + else if (typeid_cast(type)) + types.push_back(value_type_t::Float32); + else if (typeid_cast(type)) + types.push_back(value_type_t::Float64); + else if (typeid_cast(type)) + types.push_back(value_type_t::String); + else if (typeid_cast(type)) + types.push_back(value_type_t::Date); + else if (typeid_cast(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()); + } + } +}; + +} diff --git a/dbms/include/DB/Dictionaries/MongoDBBlockInputStream.h b/dbms/include/DB/Dictionaries/MongoDBBlockInputStream.h index 3e20385f8c9..e95e963510e 100644 --- a/dbms/include/DB/Dictionaries/MongoDBBlockInputStream.h +++ b/dbms/include/DB/Dictionaries/MongoDBBlockInputStream.h @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include @@ -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 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 cursor; const std::size_t max_block_size; - ExternalDatabaseHelper helper; + ExternalResultDescription description; }; } diff --git a/dbms/include/DB/Dictionaries/MySQLBlockInputStream.h b/dbms/include/DB/Dictionaries/MySQLBlockInputStream.h index 96acd92a1c9..f967f55f7c4 100644 --- a/dbms/include/DB/Dictionaries/MySQLBlockInputStream.h +++ b/dbms/include/DB/Dictionaries/MySQLBlockInputStream.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -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 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; }; }