ClickHouse/src/Formats/MySQLBlockInputStream.cpp

311 lines
11 KiB
C++
Raw Normal View History

#if !defined(ARCADIA_BUILD)
#include "config_core.h"
#endif
#if USE_MYSQL
#include <vector>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnDecimal.h>
#include <Columns/ColumnFixedString.h>
#include <DataTypes/IDataType.h>
#include <DataTypes/DataTypeNullable.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <IO/Operators.h>
#include <Common/assert_cast.h>
#include <ext/range.h>
#include <common/logger_useful.h>
#include "MySQLBlockInputStream.h"
2016-12-08 02:49:04 +00:00
namespace DB
{
2016-12-08 02:49:04 +00:00
namespace ErrorCodes
{
extern const int NUMBER_OF_COLUMNS_DOESNT_MATCH;
extern const int NOT_IMPLEMENTED;
2016-12-08 02:49:04 +00:00
}
MySQLBlockInputStream::Connection::Connection(
const mysqlxx::PoolWithFailover::Entry & entry_,
const std::string & query_str)
: entry(entry_)
, query{entry->query(query_str)}
, result{query.use()}
{
}
2016-12-08 02:49:04 +00:00
/// Used in MaterializeMySQL and in doInvalidateQuery for dictionary source.
2016-12-08 02:49:04 +00:00
MySQLBlockInputStream::MySQLBlockInputStream(
const mysqlxx::PoolWithFailover::Entry & entry,
const std::string & query_str,
const Block & sample_block,
const UInt64 max_block_size_,
const bool auto_close_,
const bool fetch_by_name_)
: log(&Poco::Logger::get("MySQLBlockInputStream"))
, connection{std::make_unique<Connection>(entry, query_str)}
, max_block_size{max_block_size_}
, auto_close{auto_close_}
, fetch_by_name(fetch_by_name_)
2016-12-08 02:49:04 +00:00
{
description.init(sample_block);
2020-12-09 08:19:09 +00:00
initPositionMappingFromQueryResultStructure();
2016-12-08 02:49:04 +00:00
}
/// As a base class contructor for MySQLWithFailoverBlockInputStream.
MySQLBlockInputStream::MySQLBlockInputStream(
const Block & sample_block_,
UInt64 max_block_size_,
bool auto_close_,
bool fetch_by_name_)
: log(&Poco::Logger::get("MySQLBlockInputStream"))
, max_block_size(max_block_size_)
, auto_close(auto_close_)
, fetch_by_name(fetch_by_name_)
{
description.init(sample_block_);
}
/// Used by MySQL storage / table function and dictionary source.
MySQLWithFailoverBlockInputStream::MySQLWithFailoverBlockInputStream(
2021-03-27 14:35:44 +00:00
mysqlxx::PoolWithFailoverPtr pool_,
const std::string & query_str_,
const Block & sample_block_,
const UInt64 max_block_size_,
const bool auto_close_,
const bool fetch_by_name_,
const size_t max_tries_)
: MySQLBlockInputStream(sample_block_, max_block_size_, auto_close_, fetch_by_name_)
, pool(pool_)
, query_str(query_str_)
, max_tries(max_tries_)
{
}
void MySQLWithFailoverBlockInputStream::readPrefix()
{
size_t count_connection_lost = 0;
/// For recovering from "Lost connection to MySQL server during query" errors
while (true)
{
try
{
2021-03-27 14:35:44 +00:00
connection = std::make_unique<Connection>(pool->get(), query_str);
break;
}
catch (const mysqlxx::ConnectionLost & ecl) /// There are two retriable failures: CR_SERVER_GONE_ERROR, CR_SERVER_LOST
{
LOG_WARNING(log, ecl.displayText());
}
LOG_WARNING(log, "Lost connection ({}/{}). Trying to reconnect...", count_connection_lost, max_tries);
if (++count_connection_lost > max_tries)
{
LOG_ERROR(log, "Failed to create connection to MySQL. ({}/{})", count_connection_lost, max_tries);
throw;
}
}
initPositionMappingFromQueryResultStructure();
}
2016-12-08 02:49:04 +00:00
namespace
{
using ValueType = ExternalResultDescription::ValueType;
void insertValue(const IDataType & data_type, IColumn & column, const ValueType type, const mysqlxx::Value & value)
{
switch (type)
{
2019-08-03 11:02:40 +00:00
case ValueType::vtUInt8:
assert_cast<ColumnUInt8 &>(column).insertValue(value.getUInt());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtUInt16:
assert_cast<ColumnUInt16 &>(column).insertValue(value.getUInt());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtUInt32:
assert_cast<ColumnUInt32 &>(column).insertValue(value.getUInt());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtUInt64:
assert_cast<ColumnUInt64 &>(column).insertValue(value.getUInt());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtInt8:
assert_cast<ColumnInt8 &>(column).insertValue(value.getInt());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtInt16:
assert_cast<ColumnInt16 &>(column).insertValue(value.getInt());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtInt32:
assert_cast<ColumnInt32 &>(column).insertValue(value.getInt());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtInt64:
assert_cast<ColumnInt64 &>(column).insertValue(value.getInt());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtFloat32:
assert_cast<ColumnFloat32 &>(column).insertValue(value.getDouble());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtFloat64:
assert_cast<ColumnFloat64 &>(column).insertValue(value.getDouble());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtString:
assert_cast<ColumnString &>(column).insertData(value.data(), value.size());
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtDate:
assert_cast<ColumnUInt16 &>(column).insertValue(UInt16(value.getDate().getDayNum()));
break;
2019-08-03 11:02:40 +00:00
case ValueType::vtDateTime:
2021-03-15 19:23:27 +00:00
{
ReadBufferFromString in(value);
time_t time = 0;
readDateTimeText(time, in);
2021-03-15 20:40:33 +00:00
if (time < 0)
time = 0;
2021-03-15 19:23:27 +00:00
assert_cast<ColumnUInt32 &>(column).insertValue(time);
break;
2021-03-15 19:23:27 +00:00
}
2019-08-03 11:02:40 +00:00
case ValueType::vtUUID:
assert_cast<ColumnUInt128 &>(column).insert(parse<UUID>(value.data(), value.size()));
break;
case ValueType::vtDateTime64:[[fallthrough]];
case ValueType::vtDecimal32: [[fallthrough]];
case ValueType::vtDecimal64: [[fallthrough]];
case ValueType::vtDecimal128:[[fallthrough]];
case ValueType::vtDecimal256:
{
ReadBuffer buffer(const_cast<char *>(value.data()), value.size(), 0);
data_type.deserializeAsWholeText(column, buffer, FormatSettings{});
break;
}
case ValueType::vtFixedString:
assert_cast<ColumnFixedString &>(column).insertData(value.data(), value.size());
break;
default:
throw Exception("Unsupported value type", ErrorCodes::NOT_IMPLEMENTED);
}
}
void insertDefaultValue(IColumn & column, const IColumn & sample_column) { column.insertFrom(sample_column, 0); }
2016-12-08 02:49:04 +00:00
}
Block MySQLBlockInputStream::readImpl()
{
auto row = connection->result.fetch();
if (!row)
{
2019-05-24 12:30:12 +00:00
if (auto_close)
{
LOG_TRACE(log, "Removing connection, kssenii");
connection->entry.disconnect();
}
LOG_TRACE(log, "Connection disconnected kssenii");
return {};
}
MutableColumns columns(description.sample_block.columns());
for (const auto i : ext::range(0, columns.size()))
columns[i] = description.sample_block.getByPosition(i).column->cloneEmpty();
ColumnConst unification (#1011) * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * Fixed error in ColumnArray::replicateGeneric [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150].
2017-07-21 06:35:58 +00:00
size_t num_rows = 0;
while (row)
{
for (size_t index = 0; index < position_mapping.size(); ++index)
{
const auto value = row[position_mapping[index]];
const auto & sample = description.sample_block.getByPosition(index);
bool is_type_nullable = description.types[index].second;
if (!value.isNull())
{
if (is_type_nullable)
{
ColumnNullable & column_nullable = assert_cast<ColumnNullable &>(*columns[index]);
const auto & data_type = assert_cast<const DataTypeNullable &>(*sample.type);
insertValue(*data_type.getNestedType(), column_nullable.getNestedColumn(), description.types[index].first, value);
column_nullable.getNullMapData().emplace_back(false);
}
else
{
insertValue(*sample.type, *columns[index], description.types[index].first, value);
}
}
else
{
insertDefaultValue(*columns[index], *sample.column);
if (is_type_nullable)
{
ColumnNullable & column_nullable = assert_cast<ColumnNullable &>(*columns[index]);
column_nullable.getNullMapData().back() = true;
}
}
}
++num_rows;
if (num_rows == max_block_size)
break;
row = connection->result.fetch();
}
return description.sample_block.cloneWithColumns(std::move(columns));
2016-12-08 02:49:04 +00:00
}
2020-12-09 08:19:09 +00:00
void MySQLBlockInputStream::initPositionMappingFromQueryResultStructure()
{
position_mapping.resize(description.sample_block.columns());
if (!fetch_by_name)
{
if (description.sample_block.columns() != connection->result.getNumFields())
throw Exception{"mysqlxx::UseQueryResult contains " + toString(connection->result.getNumFields()) + " columns while "
+ toString(description.sample_block.columns()) + " expected", ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH};
for (const auto idx : ext::range(0, connection->result.getNumFields()))
position_mapping[idx] = idx;
}
else
{
const auto & sample_names = description.sample_block.getNames();
std::unordered_set<std::string> missing_names(sample_names.begin(), sample_names.end());
size_t fields_size = connection->result.getNumFields();
for (const size_t & idx : ext::range(0, fields_size))
{
const auto & field_name = connection->result.getFieldName(idx);
if (description.sample_block.has(field_name))
{
const auto & position = description.sample_block.getPositionByName(field_name);
position_mapping[position] = idx;
missing_names.erase(field_name);
}
}
if (!missing_names.empty())
{
WriteBufferFromOwnString exception_message;
for (auto iter = missing_names.begin(); iter != missing_names.end(); ++iter)
{
if (iter != missing_names.begin())
exception_message << ", ";
exception_message << *iter;
}
throw Exception("mysqlxx::UseQueryResult must be contain the" + exception_message.str() + " columns.",
ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH);
}
}
}
2016-12-08 02:49:04 +00:00
}
#endif