2015-01-27 13:00:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Core/Block.h>
|
|
|
|
#include <DB/DataStreams/IProfilingBlockInputStream.h>
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
2015-03-20 15:21:29 +00:00
|
|
|
#include <DB/DataTypes/DataTypeDate.h>
|
|
|
|
#include <DB/DataTypes/DataTypeDateTime.h>
|
2015-03-23 13:24:56 +00:00
|
|
|
#include <DB/Columns/ColumnString.h>
|
2015-10-05 00:33:43 +00:00
|
|
|
#include <ext/range.hpp>
|
2015-01-27 13:00:20 +00:00
|
|
|
#include <mysqlxx/Query.h>
|
2015-04-16 06:12:35 +00:00
|
|
|
#include <mysqlxx/PoolWithFailover.h>
|
2015-01-27 13:00:20 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// Allows processing results of a MySQL query as a sequence of Blocks, simplifies chaining
|
|
|
|
class MySQLBlockInputStream final : public IProfilingBlockInputStream
|
2015-01-27 13:00:20 +00:00
|
|
|
{
|
2015-03-20 15:21:29 +00:00
|
|
|
enum struct value_type_t
|
|
|
|
{
|
|
|
|
UInt8,
|
|
|
|
UInt16,
|
|
|
|
UInt32,
|
|
|
|
UInt64,
|
|
|
|
Int8,
|
|
|
|
Int16,
|
|
|
|
Int32,
|
|
|
|
Int64,
|
|
|
|
Float32,
|
|
|
|
Float64,
|
|
|
|
String,
|
|
|
|
Date,
|
|
|
|
DateTime
|
|
|
|
};
|
|
|
|
|
2015-01-27 13:00:20 +00:00
|
|
|
public:
|
2015-10-13 15:38:08 +00:00
|
|
|
MySQLBlockInputStream(
|
|
|
|
const mysqlxx::PoolWithFailover::Entry & entry, const std::string & query_str, const Block & sample_block_,
|
2015-03-18 12:09:49 +00:00
|
|
|
const std::size_t max_block_size)
|
|
|
|
: entry{entry}, query{this->entry->query(query_str)}, result{query.use()},
|
2015-10-13 15:38:08 +00:00
|
|
|
sample_block{sample_block_}, max_block_size{max_block_size}
|
2015-01-27 13:00:20 +00:00
|
|
|
{
|
2015-03-23 13:24:56 +00:00
|
|
|
if (sample_block.columns() != result.getNumFields())
|
|
|
|
throw Exception{
|
|
|
|
"mysqlxx::UseQueryResult contains " + toString(result.getNumFields()) + " columns while " +
|
|
|
|
toString(sample_block.columns()) + " expected",
|
|
|
|
ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH
|
|
|
|
};
|
|
|
|
|
2015-10-13 15:38:08 +00:00
|
|
|
const auto num_columns = sample_block.columns();
|
|
|
|
types.reserve(num_columns);
|
|
|
|
sample_columns.reserve(num_columns);
|
2015-01-27 13:00:20 +00:00
|
|
|
|
2015-10-13 15:38:08 +00:00
|
|
|
for (const auto idx : ext::range(0, num_columns))
|
2015-01-27 13:00:20 +00:00
|
|
|
{
|
2015-10-13 15:38:08 +00:00
|
|
|
const auto & column = sample_block.getByPosition(idx);
|
|
|
|
const auto type = column.type.get();
|
|
|
|
|
2015-01-27 13:00:20 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt8 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::UInt8);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeUInt16 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::UInt16);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeUInt32 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::UInt32);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeUInt64 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::UInt64);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeInt8 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::Int8);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeInt16 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::Int16);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeInt32 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::Int32);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeInt64 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::Int64);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeFloat32 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::Float32);
|
2015-12-07 16:06:18 +00:00
|
|
|
else if (typeid_cast<const DataTypeFloat64 *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
types.push_back(value_type_t::Float64);
|
2015-01-27 13:00:20 +00:00
|
|
|
else if (typeid_cast<const DataTypeString *>(type))
|
2015-03-20 15:21:29 +00:00
|
|
|
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);
|
2015-01-27 13:00:20 +00:00
|
|
|
else
|
|
|
|
throw Exception{
|
|
|
|
"Unsupported type " + type->getName(),
|
|
|
|
ErrorCodes::UNKNOWN_TYPE
|
|
|
|
};
|
2015-10-13 15:38:08 +00:00
|
|
|
|
|
|
|
sample_columns.emplace_back(column.column.get());
|
2015-01-27 13:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 20:22:02 +00:00
|
|
|
String getName() const override { return "MySQL"; }
|
2015-01-27 13:00:20 +00:00
|
|
|
|
|
|
|
String getID() const override
|
|
|
|
{
|
2015-02-10 14:50:43 +00:00
|
|
|
return "MySQL(" + query.str() + ")";
|
2015-01-27 13:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Block readImpl() override
|
|
|
|
{
|
2015-03-23 13:24:56 +00:00
|
|
|
auto row = result.fetch();
|
|
|
|
if (!row)
|
|
|
|
return {};
|
|
|
|
|
2015-01-27 13:00:20 +00:00
|
|
|
auto block = sample_block.cloneEmpty();
|
|
|
|
|
2015-03-23 13:24:56 +00:00
|
|
|
/// cache pointers returned by the calls to getByPosition
|
|
|
|
std::vector<IColumn *> columns(block.columns());
|
|
|
|
for (const auto i : ext::range(0, columns.size()))
|
|
|
|
columns[i] = block.getByPosition(i).column.get();
|
2015-01-27 13:00:20 +00:00
|
|
|
|
2015-03-23 13:24:56 +00:00
|
|
|
std::size_t num_rows = 0;
|
|
|
|
while (row)
|
2015-01-27 13:00:20 +00:00
|
|
|
{
|
|
|
|
for (const auto idx : ext::range(0, row.size()))
|
2015-03-23 13:24:56 +00:00
|
|
|
{
|
|
|
|
const auto value = row[idx];
|
|
|
|
if (!value.isNull())
|
|
|
|
insertValue(columns[idx], types[idx], value);
|
|
|
|
else
|
2015-10-13 15:38:08 +00:00
|
|
|
insertDefaultValue(columns[idx], *sample_columns[idx]);
|
2015-03-23 13:24:56 +00:00
|
|
|
}
|
2015-01-27 13:00:20 +00:00
|
|
|
|
2015-03-23 13:24:56 +00:00
|
|
|
++num_rows;
|
|
|
|
if (num_rows == max_block_size)
|
2015-01-27 13:00:20 +00:00
|
|
|
break;
|
2015-03-23 13:24:56 +00:00
|
|
|
|
|
|
|
row = result.fetch();
|
2015-01-27 13:00:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-23 13:24:56 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void insertValue(IColumn * const column, const value_type_t type, const mysqlxx::Value & value)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case value_type_t::UInt8: static_cast<ColumnUInt8 *>(column)->insert(value.getUInt()); break;
|
|
|
|
case value_type_t::UInt16: static_cast<ColumnUInt16 *>(column)->insert(value.getUInt()); break;
|
|
|
|
case value_type_t::UInt32: static_cast<ColumnUInt32 *>(column)->insert(value.getUInt()); break;
|
|
|
|
case value_type_t::UInt64: static_cast<ColumnUInt64 *>(column)->insert(value.getUInt()); break;
|
|
|
|
case value_type_t::Int8: static_cast<ColumnInt8 *>(column)->insert(value.getInt()); break;
|
|
|
|
case value_type_t::Int16: static_cast<ColumnInt16 *>(column)->insert(value.getInt()); break;
|
|
|
|
case value_type_t::Int32: static_cast<ColumnInt32 *>(column)->insert(value.getInt()); break;
|
|
|
|
case value_type_t::Int64: static_cast<ColumnInt64 *>(column)->insert(value.getInt()); break;
|
|
|
|
case value_type_t::Float32: static_cast<ColumnFloat32 *>(column)->insert(value.getDouble()); break;
|
|
|
|
case value_type_t::Float64: static_cast<ColumnFloat64 *>(column)->insert(value.getDouble()); break;
|
|
|
|
case value_type_t::String:
|
|
|
|
{
|
|
|
|
const auto string = value.getString();
|
|
|
|
static_cast<ColumnString *>(column)->insertDataWithTerminatingZero(string.data(), string.size() + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case value_type_t::Date: static_cast<ColumnUInt16 *>(column)->insert(UInt16{value.getDate().getDayNum()}); break;
|
|
|
|
case value_type_t::DateTime: static_cast<ColumnUInt32 *>(column)->insert(time_t{value.getDateTime()}); break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 13:00:20 +00:00
|
|
|
|
2015-10-13 15:38:08 +00:00
|
|
|
static void insertDefaultValue(IColumn * const column, const IColumn & sample_column)
|
2015-01-27 13:00:20 +00:00
|
|
|
{
|
2015-10-13 15:38:08 +00:00
|
|
|
column->insertFrom(sample_column, 0);
|
2015-01-27 13:00:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 12:09:49 +00:00
|
|
|
mysqlxx::PoolWithFailover::Entry entry;
|
2015-01-27 13:00:20 +00:00
|
|
|
mysqlxx::Query query;
|
|
|
|
mysqlxx::UseQueryResult result;
|
|
|
|
Block sample_block;
|
2015-02-10 14:50:43 +00:00
|
|
|
const std::size_t max_block_size;
|
2015-03-20 15:21:29 +00:00
|
|
|
std::vector<value_type_t> types;
|
2015-10-13 15:38:08 +00:00
|
|
|
std::vector<const IColumn *> sample_columns;
|
2015-01-27 13:00:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|