2018-11-28 11:37:12 +00:00
|
|
|
#include "ODBCBlockInputStream.h"
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <vector>
|
2018-10-12 02:26:48 +00:00
|
|
|
#include <Columns/ColumnNullable.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2018-09-05 16:20:12 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2018-06-05 19:46:49 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2017-05-15 14:16:10 +00:00
|
|
|
#include <common/logger_useful.h>
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/range.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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ODBCBlockInputStream::ODBCBlockInputStream(
|
2019-08-03 11:02:40 +00:00
|
|
|
Poco::Data::Session && session_, const std::string & query_str, const Block & sample_block, const UInt64 max_block_size_)
|
|
|
|
: session{session_}
|
2018-12-10 15:25:45 +00:00
|
|
|
, statement{(this->session << query_str, Poco::Data::Keywords::now)}
|
|
|
|
, result{statement}
|
|
|
|
, iterator{result.begin()}
|
2019-08-03 11:02:40 +00:00
|
|
|
, max_block_size{max_block_size_}
|
2020-05-30 21:57:37 +00:00
|
|
|
, log(&Poco::Logger::get("ODBCBlockInputStream"))
|
2016-12-08 02:49:04 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (sample_block.columns() != result.columnCount())
|
2018-12-10 15:25:45 +00:00
|
|
|
throw Exception{"RecordSet contains " + toString(result.columnCount()) + " columns while " + 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-03-09 03:38:43 +00:00
|
|
|
void insertValue(IColumn & column, const ValueType type, const Poco::Dynamic::Var & 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.convert<UInt64>());
|
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.convert<UInt64>());
|
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.convert<UInt64>());
|
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.convert<UInt64>());
|
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.convert<Int64>());
|
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.convert<Int64>());
|
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.convert<Int64>());
|
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.convert<Int64>());
|
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.convert<Float64>());
|
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.convert<Float64>());
|
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).insert(value.convert<String>());
|
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{LocalDate{value.convert<String>()}.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(time_t{LocalDateTime{value.convert<String>()}});
|
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.convert<std::string>()));
|
2018-12-10 15:25:45 +00:00
|
|
|
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 ODBCBlockInputStream::readImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (iterator == result.end())
|
|
|
|
return {};
|
2016-12-08 02:49:04 +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-05-15 14:16:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t num_rows = 0;
|
|
|
|
while (iterator != result.end())
|
|
|
|
{
|
|
|
|
Poco::Data::Row & row = *iterator;
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto idx : ext::range(0, row.fieldCount()))
|
|
|
|
{
|
|
|
|
const Poco::Dynamic::Var & value = row[idx];
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!value.isEmpty())
|
2018-10-12 02:26:48 +00:00
|
|
|
{
|
|
|
|
if (description.types[idx].second)
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
ColumnNullable & column_nullable = assert_cast<ColumnNullable &>(*columns[idx]);
|
2018-10-12 02:26:48 +00:00
|
|
|
insertValue(column_nullable.getNestedColumn(), description.types[idx].first, value);
|
|
|
|
column_nullable.getNullMapData().emplace_back(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
insertValue(*columns[idx], description.types[idx].first, value);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2018-10-12 02:09:47 +00:00
|
|
|
insertDefaultValue(*columns[idx], *description.sample_block.getByPosition(idx).column);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-05-22 16:38:24 +00:00
|
|
|
++iterator;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++num_rows;
|
|
|
|
if (num_rows == max_block_size)
|
|
|
|
break;
|
|
|
|
}
|
2016-12-08 02:49:04 +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
|
|
|
}
|
|
|
|
|
|
|
|
}
|