2020-05-03 02:46:36 +00:00
|
|
|
#include "ArrowBlockOutputFormat.h"
|
|
|
|
|
|
|
|
#if USE_ARROW
|
|
|
|
|
|
|
|
#include <Formats/FormatFactory.h>
|
|
|
|
#include <arrow/ipc/writer.h>
|
2020-05-03 18:12:14 +00:00
|
|
|
#include <arrow/table.h>
|
2020-07-13 18:25:49 +00:00
|
|
|
#include <arrow/result.h>
|
2020-05-03 18:12:14 +00:00
|
|
|
#include "ArrowBufferedStreams.h"
|
2020-05-03 02:46:36 +00:00
|
|
|
#include "CHColumnToArrowColumn.h"
|
|
|
|
|
2020-07-13 01:11:35 +00:00
|
|
|
|
2020-05-03 02:46:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNKNOWN_EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2020-05-21 04:07:47 +00:00
|
|
|
ArrowBlockOutputFormat::ArrowBlockOutputFormat(WriteBuffer & out_, const Block & header_, bool stream_, const FormatSettings & format_settings_)
|
|
|
|
: IOutputFormat(header_, out_), stream{stream_}, format_settings{format_settings_}, arrow_ostream{std::make_shared<ArrowBufferedOutputStream>(out_)}
|
2020-05-03 02:46:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrowBlockOutputFormat::consume(Chunk chunk)
|
|
|
|
{
|
|
|
|
const Block & header = getPort(PortKind::Main).getHeader();
|
|
|
|
const size_t columns_num = chunk.getNumColumns();
|
|
|
|
std::shared_ptr<arrow::Table> arrow_table;
|
|
|
|
|
2020-05-03 12:25:53 +00:00
|
|
|
CHColumnToArrowColumn::chChunkToArrowTable(arrow_table, header, chunk, columns_num, "Arrow");
|
2020-05-03 02:46:36 +00:00
|
|
|
|
|
|
|
if (!writer)
|
2020-05-21 04:07:47 +00:00
|
|
|
prepareWriter(arrow_table->schema());
|
2020-05-03 02:46:36 +00:00
|
|
|
|
|
|
|
// TODO: calculate row_group_size depending on a number of rows and table size
|
2020-05-03 12:26:39 +00:00
|
|
|
auto status = writer->WriteTable(*arrow_table, format_settings.arrow.row_group_size);
|
2020-05-03 02:46:36 +00:00
|
|
|
|
|
|
|
if (!status.ok())
|
2020-07-13 18:25:49 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_EXCEPTION,
|
|
|
|
"Error while writing a table: {}", status.ToString());
|
2020-05-03 02:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArrowBlockOutputFormat::finalize()
|
|
|
|
{
|
2020-08-06 05:58:08 +00:00
|
|
|
if (!writer)
|
2020-05-03 02:46:36 +00:00
|
|
|
{
|
2020-08-06 05:58:08 +00:00
|
|
|
const Block & header = getPort(PortKind::Main).getHeader();
|
|
|
|
|
2020-08-07 07:40:05 +00:00
|
|
|
consume(Chunk(header.getColumns(), 0));
|
2020-05-03 02:46:36 +00:00
|
|
|
}
|
2020-08-06 05:58:08 +00:00
|
|
|
|
|
|
|
auto status = writer->Close();
|
|
|
|
if (!status.ok())
|
|
|
|
throw Exception(ErrorCodes::UNKNOWN_EXCEPTION,
|
|
|
|
"Error while closing a table: {}", status.ToString());
|
2020-05-03 02:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 04:07:47 +00:00
|
|
|
void ArrowBlockOutputFormat::prepareWriter(const std::shared_ptr<arrow::Schema> & schema)
|
|
|
|
{
|
2020-07-13 18:25:49 +00:00
|
|
|
arrow::Result<std::shared_ptr<arrow::ipc::RecordBatchWriter>> writer_status;
|
2020-05-21 04:07:47 +00:00
|
|
|
|
|
|
|
// TODO: should we use arrow::ipc::IpcOptions::alignment?
|
|
|
|
if (stream)
|
2020-07-13 18:25:49 +00:00
|
|
|
writer_status = arrow::ipc::NewStreamWriter(arrow_ostream.get(), schema);
|
2020-05-21 04:07:47 +00:00
|
|
|
else
|
2020-07-13 18:25:49 +00:00
|
|
|
writer_status = arrow::ipc::NewFileWriter(arrow_ostream.get(), schema);
|
2020-05-21 04:07:47 +00:00
|
|
|
|
2020-07-13 18:25:49 +00:00
|
|
|
if (!writer_status.ok())
|
|
|
|
throw Exception(ErrorCodes::UNKNOWN_EXCEPTION,
|
|
|
|
"Error while opening a table writer: {}", writer_status.status().ToString());
|
|
|
|
|
|
|
|
writer = *writer_status;
|
2020-05-21 04:07:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 02:46:36 +00:00
|
|
|
void registerOutputFormatProcessorArrow(FormatFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerOutputFormatProcessor(
|
|
|
|
"Arrow",
|
|
|
|
[](WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
FormatFactory::WriteCallback,
|
|
|
|
const FormatSettings & format_settings)
|
|
|
|
{
|
2020-05-21 04:07:47 +00:00
|
|
|
return std::make_shared<ArrowBlockOutputFormat>(buf, sample, false, format_settings);
|
|
|
|
});
|
|
|
|
|
|
|
|
factory.registerOutputFormatProcessor(
|
|
|
|
"ArrowStream",
|
|
|
|
[](WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
FormatFactory::WriteCallback,
|
|
|
|
const FormatSettings & format_settings)
|
|
|
|
{
|
|
|
|
return std::make_shared<ArrowBlockOutputFormat>(buf, sample, true, format_settings);
|
2020-05-03 02:46:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class FormatFactory;
|
|
|
|
void registerOutputFormatProcessorArrow(FormatFactory &)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|