2020-04-16 12:31:57 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include "config_core.h"
|
|
|
|
#endif
|
2017-04-19 00:25:57 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if USE_MYSQL
|
|
|
|
# include <vector>
|
|
|
|
# include <Columns/ColumnNullable.h>
|
|
|
|
# include <Columns/ColumnString.h>
|
|
|
|
# include <Columns/ColumnsNumber.h>
|
2020-09-09 12:18:02 +00:00
|
|
|
# include <Columns/ColumnDecimal.h>
|
|
|
|
# include <DataTypes/IDataType.h>
|
|
|
|
# include <DataTypes/DataTypeNullable.h>
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <IO/ReadHelpers.h>
|
|
|
|
# include <IO/WriteHelpers.h>
|
2020-11-25 05:47:11 +00:00
|
|
|
# include <IO/Operators.h>
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <Common/assert_cast.h>
|
|
|
|
# include <ext/range.h>
|
|
|
|
# include "MySQLBlockInputStream.h"
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int NUMBER_OF_COLUMNS_DOESNT_MATCH;
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 11:12:48 +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
|
|
|
|
|
|
|
MySQLBlockInputStream::MySQLBlockInputStream(
|
2020-09-25 11:12:48 +00:00
|
|
|
const mysqlxx::PoolWithFailover::Entry & entry,
|
|
|
|
const std::string & query_str,
|
|
|
|
const Block & sample_block,
|
|
|
|
const UInt64 max_block_size_,
|
2020-11-24 15:31:43 +00:00
|
|
|
const bool auto_close_,
|
|
|
|
const bool fetch_by_name_)
|
2020-09-25 11:12:48 +00:00
|
|
|
: connection{std::make_unique<Connection>(entry, query_str)}
|
|
|
|
, max_block_size{max_block_size_}
|
|
|
|
, auto_close{auto_close_}
|
2020-11-24 15:31:43 +00:00
|
|
|
, fetch_by_name(fetch_by_name_)
|
2016-12-08 02:49:04 +00:00
|
|
|
{
|
2020-11-24 15:31:43 +00:00
|
|
|
if (!fetch_by_name && sample_block.columns() != connection->result.getNumFields())
|
2020-09-25 11:12:48 +00:00
|
|
|
throw Exception{"mysqlxx::UseQueryResult contains " + toString(connection->result.getNumFields()) + " columns while "
|
2018-12-10 15:25:45 +00:00
|
|
|
+ toString(sample_block.columns()) + " expected",
|
|
|
|
ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH};
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
description.init(sample_block);
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ValueType = ExternalResultDescription::ValueType;
|
|
|
|
|
2020-09-09 12:18:02 +00:00
|
|
|
void insertValue(const IDataType & data_type, IColumn & column, const ValueType type, const mysqlxx::Value & value)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtUInt8:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt8 &>(column).insertValue(value.getUInt());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtUInt16:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt16 &>(column).insertValue(value.getUInt());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtUInt32:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt32 &>(column).insertValue(value.getUInt());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtUInt64:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt64 &>(column).insertValue(value.getUInt());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtInt8:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnInt8 &>(column).insertValue(value.getInt());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtInt16:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnInt16 &>(column).insertValue(value.getInt());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtInt32:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnInt32 &>(column).insertValue(value.getInt());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtInt64:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnInt64 &>(column).insertValue(value.getInt());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtFloat32:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnFloat32 &>(column).insertValue(value.getDouble());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtFloat64:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnFloat64 &>(column).insertValue(value.getDouble());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtString:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnString &>(column).insertData(value.data(), value.size());
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtDate:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt16 &>(column).insertValue(UInt16(value.getDate().getDayNum()));
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtDateTime:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt32 &>(column).insertValue(UInt32(value.getDateTime()));
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case ValueType::vtUUID:
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt128 &>(column).insert(parse<UUID>(value.data(), value.size()));
|
2018-12-10 15:25:45 +00:00
|
|
|
break;
|
2020-09-09 12:18:02 +00:00
|
|
|
case ValueType::vtDateTime64:[[fallthrough]];
|
|
|
|
case ValueType::vtDecimal32: [[fallthrough]];
|
|
|
|
case ValueType::vtDecimal64: [[fallthrough]];
|
2020-09-10 03:37:00 +00:00
|
|
|
case ValueType::vtDecimal128:[[fallthrough]];
|
|
|
|
case ValueType::vtDecimal256:
|
2020-09-09 12:18:02 +00:00
|
|
|
{
|
|
|
|
ReadBuffer buffer(const_cast<char *>(value.data()), value.size(), 0);
|
|
|
|
data_type.deserializeAsWholeText(column, buffer, FormatSettings{});
|
|
|
|
break;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-15 03:12:04 +00:00
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
void insertDefaultValue(IColumn & column, const IColumn & sample_column) { column.insertFrom(sample_column, 0); }
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block MySQLBlockInputStream::readImpl()
|
|
|
|
{
|
2020-09-25 11:12:48 +00:00
|
|
|
auto row = connection->result.fetch();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!row)
|
2019-05-23 13:09:07 +00:00
|
|
|
{
|
2019-05-24 12:30:12 +00:00
|
|
|
if (auto_close)
|
2020-09-25 11:12:48 +00:00
|
|
|
connection->entry.disconnect();
|
2017-04-01 07:20:54 +00:00
|
|
|
return {};
|
2019-05-23 13:09:07 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 03:12:04 +00:00
|
|
|
MutableColumns columns(description.sample_block.columns());
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto i : ext::range(0, columns.size()))
|
2017-12-15 03:12:04 +00:00
|
|
|
columns[i] = description.sample_block.getByPosition(i).column->cloneEmpty();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-11-24 15:31:43 +00:00
|
|
|
if (unlikely(position_mapping.size() != description.sample_block.columns()))
|
|
|
|
{
|
|
|
|
position_mapping.resize(description.sample_block.columns());
|
|
|
|
|
|
|
|
if (!fetch_by_name)
|
|
|
|
{
|
|
|
|
for (const auto idx : ext::range(0, row.size()))
|
|
|
|
position_mapping[idx] = idx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-25 15:52:30 +00:00
|
|
|
const auto & sample_names = description.sample_block.getNames();
|
|
|
|
std::unordered_set<std::string> missing_names(sample_names.begin(), sample_names.end());
|
2020-11-25 05:47:11 +00:00
|
|
|
|
2020-11-24 15:31:43 +00:00
|
|
|
for (const auto idx : ext::range(0, row.size()))
|
|
|
|
{
|
|
|
|
const auto & field_name = row.getFieldName(idx);
|
|
|
|
if (description.sample_block.has(field_name))
|
|
|
|
{
|
|
|
|
const auto & position = description.sample_block.getPositionByName(field_name);
|
|
|
|
position_mapping[position] = idx;
|
2020-11-25 15:52:30 +00:00
|
|
|
missing_names.erase(field_name);
|
2020-11-24 15:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 05:47:11 +00:00
|
|
|
|
|
|
|
if (!missing_names.empty())
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString exception_message;
|
2020-11-25 15:52:30 +00:00
|
|
|
for (auto iter = missing_names.begin(); iter != missing_names.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (iter != missing_names.begin())
|
|
|
|
exception_message << ", ";
|
|
|
|
exception_message << *iter;
|
|
|
|
}
|
2020-11-25 05:47:11 +00:00
|
|
|
|
|
|
|
throw Exception("mysqlxx::UseQueryResult must be contain the" + exception_message.str() + " columns.",
|
|
|
|
ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
}
|
2020-11-24 15:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t num_rows = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
while (row)
|
|
|
|
{
|
2020-11-24 15:31:43 +00:00
|
|
|
for (size_t index = 0; index < position_mapping.size(); ++index)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-11-24 15:31:43 +00:00
|
|
|
const auto value = row[position_mapping[index]];
|
|
|
|
const auto & sample = description.sample_block.getByPosition(index);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!value.isNull())
|
2018-10-12 02:26:48 +00:00
|
|
|
{
|
2020-11-24 15:31:43 +00:00
|
|
|
if (description.types[index].second)
|
2018-10-12 02:26:48 +00:00
|
|
|
{
|
2020-11-24 15:31:43 +00:00
|
|
|
ColumnNullable & column_nullable = assert_cast<ColumnNullable &>(*columns[index]);
|
2020-09-09 12:18:02 +00:00
|
|
|
const auto & data_type = assert_cast<const DataTypeNullable &>(*sample.type);
|
2020-11-24 15:31:43 +00:00
|
|
|
insertValue(*data_type.getNestedType(), column_nullable.getNestedColumn(), description.types[index].first, value);
|
2018-10-12 02:26:48 +00:00
|
|
|
column_nullable.getNullMapData().emplace_back(0);
|
|
|
|
}
|
|
|
|
else
|
2020-11-24 15:31:43 +00:00
|
|
|
insertValue(*sample.type, *columns[index], description.types[index].first, value);
|
2018-10-12 02:26:48 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2020-11-24 15:31:43 +00:00
|
|
|
insertDefaultValue(*columns[index], *sample.column);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++num_rows;
|
|
|
|
if (num_rows == max_block_size)
|
|
|
|
break;
|
|
|
|
|
2020-09-25 11:12:48 +00:00
|
|
|
row = connection->result.fetch();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-12-15 03:12:04 +00:00
|
|
|
return description.sample_block.cloneWithColumns(std::move(columns));
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 11:12:48 +00:00
|
|
|
MySQLBlockInputStream::MySQLBlockInputStream(
|
|
|
|
const Block & sample_block_,
|
|
|
|
UInt64 max_block_size_,
|
2020-11-24 15:31:43 +00:00
|
|
|
bool auto_close_,
|
|
|
|
bool fetch_by_name_)
|
2020-09-25 11:12:48 +00:00
|
|
|
: max_block_size(max_block_size_)
|
|
|
|
, auto_close(auto_close_)
|
2020-11-24 15:31:43 +00:00
|
|
|
, fetch_by_name(fetch_by_name_)
|
2020-09-25 11:12:48 +00:00
|
|
|
{
|
|
|
|
description.init(sample_block_);
|
|
|
|
}
|
|
|
|
|
|
|
|
MySQLLazyBlockInputStream::MySQLLazyBlockInputStream(
|
|
|
|
mysqlxx::Pool & pool_,
|
|
|
|
const std::string & query_str_,
|
|
|
|
const Block & sample_block_,
|
|
|
|
const UInt64 max_block_size_,
|
2020-11-24 15:31:43 +00:00
|
|
|
const bool auto_close_,
|
|
|
|
const bool fetch_by_name_)
|
|
|
|
: MySQLBlockInputStream(sample_block_, max_block_size_, auto_close_, fetch_by_name_)
|
2020-09-25 11:12:48 +00:00
|
|
|
, pool(pool_)
|
|
|
|
, query_str(query_str_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MySQLLazyBlockInputStream::readPrefix()
|
|
|
|
{
|
|
|
|
connection = std::make_unique<Connection>(pool.get(), query_str);
|
2020-11-24 15:31:43 +00:00
|
|
|
if (!fetch_by_name && description.sample_block.columns() != connection->result.getNumFields())
|
2020-09-25 11:12:48 +00:00
|
|
|
throw Exception{"mysqlxx::UseQueryResult contains " + toString(connection->result.getNumFields()) + " columns while "
|
|
|
|
+ toString(description.sample_block.columns()) + " expected",
|
|
|
|
ErrorCodes::NUMBER_OF_COLUMNS_DOESNT_MATCH};
|
|
|
|
}
|
|
|
|
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
2017-04-19 00:25:57 +00:00
|
|
|
|
|
|
|
#endif
|