ClickHouse/programs/odbc-bridge/ODBCBlockInputStream.cpp

159 lines
5.3 KiB
C++
Raw Normal View History

#include "ODBCBlockInputStream.h"
#include <vector>
2021-03-22 11:40:29 +00:00
#include <IO/ReadBufferFromString.h>
#include <DataTypes/DataTypeNullable.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
#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>
#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
{
extern const int UNKNOWN_TYPE;
2016-12-08 02:49:04 +00:00
}
ODBCBlockInputStream::ODBCBlockInputStream(
2021-03-22 11:40:29 +00:00
nanodbc::connection & connection_, const std::string & query_str, const Block & sample_block, const UInt64 max_block_size_)
: log(&Poco::Logger::get("ODBCBlockInputStream"))
2019-08-03 11:02:40 +00:00
, max_block_size{max_block_size_}
2021-03-22 11:40:29 +00:00
, connection(connection_)
, query(query_str)
2016-12-08 02:49:04 +00:00
{
description.init(sample_block);
2021-03-22 11:40:29 +00:00
result = execute(connection, NANODBC_TEXT(query));
2016-12-08 02:49:04 +00:00
}
Block ODBCBlockInputStream::readImpl()
{
2021-03-22 11:40:29 +00:00
if (finished)
return Block();
2021-03-22 11:40:29 +00:00
MutableColumns columns(description.sample_block.cloneEmptyColumns());
size_t num_rows = 0;
2016-12-08 02:49:04 +00:00
2021-03-22 11:40:29 +00:00
while (true)
{
if (!result.next())
{
2021-03-22 11:40:29 +00:00
finished = true;
break;
}
2016-12-08 02:49:04 +00:00
2021-03-22 11:40:29 +00:00
for (int idx = 0; idx < result.columns(); ++idx)
{
const auto & sample = description.sample_block.getByPosition(idx);
2021-03-22 11:40:29 +00:00
if (!result.is_null(idx))
{
bool is_nullable = description.types[idx].second;
if (is_nullable)
{
ColumnNullable & column_nullable = assert_cast<ColumnNullable &>(*columns[idx]);
const auto & data_type = assert_cast<const DataTypeNullable &>(*sample.type);
insertValue(column_nullable.getNestedColumn(), data_type.getNestedType(), description.types[idx].first, result, idx);
column_nullable.getNullMapData().emplace_back(0);
}
else
{
insertValue(*columns[idx], sample.type, description.types[idx].first, result, idx);
}
}
else
insertDefaultValue(*columns[idx], *sample.column);
}
2016-12-08 02:49:04 +00:00
2021-03-22 11:40:29 +00:00
if (++num_rows == max_block_size)
break;
}
2016-12-08 02:49:04 +00:00
return description.sample_block.cloneWithColumns(std::move(columns));
2016-12-08 02:49:04 +00:00
}
2021-03-22 11:40:29 +00:00
void ODBCBlockInputStream::insertValue(
IColumn & column, const DataTypePtr data_type, const ValueType type, nanodbc::result & row, size_t idx)
2021-03-22 11:40:29 +00:00
{
switch (type)
{
case ValueType::vtUInt8:
assert_cast<ColumnUInt8 &>(column).insertValue(row.get<uint16_t>(idx));
break;
case ValueType::vtUInt16:
assert_cast<ColumnUInt16 &>(column).insertValue(row.get<uint16_t>(idx));
break;
case ValueType::vtUInt32:
assert_cast<ColumnUInt32 &>(column).insertValue(row.get<uint32_t>(idx));
break;
case ValueType::vtUInt64:
assert_cast<ColumnUInt64 &>(column).insertValue(row.get<uint64_t>(idx));
break;
case ValueType::vtInt8:
assert_cast<ColumnInt8 &>(column).insertValue(row.get<int16_t>(idx));
break;
case ValueType::vtInt16:
assert_cast<ColumnInt16 &>(column).insertValue(row.get<int16_t>(idx));
break;
case ValueType::vtInt32:
assert_cast<ColumnInt32 &>(column).insertValue(row.get<int32_t>(idx));
break;
case ValueType::vtInt64:
assert_cast<ColumnInt64 &>(column).insertValue(row.get<int64_t>(idx));
break;
case ValueType::vtFloat32:
assert_cast<ColumnFloat32 &>(column).insertValue(row.get<float>(idx));
break;
case ValueType::vtFloat64:
assert_cast<ColumnFloat64 &>(column).insertValue(row.get<double>(idx));
break;
case ValueType::vtFixedString:[[fallthrough]];
case ValueType::vtString:
assert_cast<ColumnString &>(column).insert(row.get<std::string>(idx));
break;
case ValueType::vtUUID:
{
auto value = row.get<std::string>(idx);
assert_cast<ColumnUInt128 &>(column).insert(parse<UUID>(value.data(), value.size()));
break;
}
case ValueType::vtDate:
assert_cast<ColumnUInt16 &>(column).insertValue(UInt16{LocalDate{row.get<std::string>(idx)}.getDayNum()});
break;
case ValueType::vtDateTime:
{
auto value = row.get<std::string>(idx);
ReadBufferFromString in(value);
2021-03-22 11:40:29 +00:00
time_t time = 0;
readDateTimeText(time, in);
if (time < 0)
time = 0;
assert_cast<ColumnUInt32 &>(column).insertValue(time);
break;
}
case ValueType::vtDateTime64:[[fallthrough]];
case ValueType::vtDecimal32: [[fallthrough]];
case ValueType::vtDecimal64: [[fallthrough]];
case ValueType::vtDecimal128: [[fallthrough]];
case ValueType::vtDecimal256:
{
auto value = row.get<std::string>(idx);
ReadBufferFromString istr(value);
2021-03-31 08:42:57 +00:00
data_type->getDefaultSerialization()->deserializeWholeText(column, istr, FormatSettings{});
break;
}
2021-03-22 11:40:29 +00:00
default:
throw Exception("Unsupported value type", ErrorCodes::UNKNOWN_TYPE);
}
}
2016-12-08 02:49:04 +00:00
}