2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/RemoteBlockOutputStream.h>
|
2017-01-31 18:39:01 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Client/Connection.h>
|
2017-01-31 18:39:01 +00:00
|
|
|
#include <common/logger_useful.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/NetException.h>
|
2017-01-31 18:39:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int UNEXPECTED_PACKET_FROM_SERVER;
|
|
|
|
extern const int LOGICAL_ERROR;
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RemoteBlockOutputStream::RemoteBlockOutputStream(Connection & connection_, const String & query_, const Settings * settings_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: connection(connection_), query(query_), settings(settings_)
|
2017-01-31 18:39:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteBlockOutputStream::writePrefix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Send query and receive "sample block", that describe table structure.
|
|
|
|
* Sample block is needed to know, what structure is required for blocks to be passed to 'write' method.
|
|
|
|
*/
|
|
|
|
|
2018-02-14 15:11:39 +00:00
|
|
|
query_sent = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
connection.sendQuery(query, "", QueryProcessingStage::Complete, settings, nullptr);
|
|
|
|
|
|
|
|
Connection::Packet packet = connection.receivePacket();
|
|
|
|
|
|
|
|
if (Protocol::Server::Data == packet.type)
|
|
|
|
{
|
|
|
|
sample_block = packet.block;
|
|
|
|
|
|
|
|
if (!sample_block)
|
|
|
|
throw Exception("Logical error: empty block received as table structure", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
else if (Protocol::Server::Exception == packet.type)
|
|
|
|
{
|
|
|
|
packet.exception->rethrow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw NetException("Unexpected packet from server (expected Data or Exception, got "
|
|
|
|
+ String(Protocol::Server::toString(packet.type)) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteBlockOutputStream::write(const Block & block)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!sample_block)
|
|
|
|
throw Exception("You must call IBlockOutputStream::writePrefix before IBlockOutputStream::write", ErrorCodes::LOGICAL_ERROR);
|
2017-01-31 18:39:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!blocksHaveEqualStructure(block, sample_block))
|
|
|
|
{
|
|
|
|
std::stringstream message;
|
|
|
|
message << "Block structure is different from table structure.\n"
|
|
|
|
<< "\nTable structure:\n(" << sample_block.dumpStructure() << ")\nBlock structure:\n(" << block.dumpStructure() << ")\n";
|
2017-01-31 18:39:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_ERROR(&Logger::get("RemoteBlockOutputStream"), message.str());
|
|
|
|
throw DB::Exception(message.str());
|
|
|
|
}
|
2017-01-31 18:39:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
connection.sendData(block);
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteBlockOutputStream::writePrepared(ReadBuffer & input, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// We cannot use 'sample_block'. Input must contain block with proper structure.
|
|
|
|
connection.sendPreparedData(input, size);
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteBlockOutputStream::writeSuffix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Empty block means end of data.
|
|
|
|
connection.sendData(Block());
|
|
|
|
|
|
|
|
/// Receive EndOfStream packet.
|
|
|
|
Connection::Packet packet = connection.receivePacket();
|
|
|
|
|
|
|
|
if (Protocol::Server::EndOfStream == packet.type)
|
|
|
|
{
|
|
|
|
/// Do nothing.
|
|
|
|
}
|
|
|
|
else if (Protocol::Server::Exception == packet.type)
|
|
|
|
packet.exception->rethrow();
|
|
|
|
else
|
|
|
|
throw NetException("Unexpected packet from server (expected EndOfStream or Exception, got "
|
|
|
|
+ String(Protocol::Server::toString(packet.type)) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
|
2018-02-14 15:11:39 +00:00
|
|
|
|
|
|
|
finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteBlockOutputStream::~RemoteBlockOutputStream()
|
|
|
|
{
|
|
|
|
/// If interrupted in the middle of the loop of communication with the server, then interrupt the connection
|
|
|
|
if (query_sent && !finished)
|
|
|
|
connection.disconnect();
|
2017-01-31 18:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|