2019-08-21 14:19:47 +00:00
|
|
|
#include "ORCBlockInputFormat.h"
|
|
|
|
#if USE_ORC
|
|
|
|
|
|
|
|
#include <Formats/FormatFactory.h>
|
|
|
|
#include <IO/BufferBase.h>
|
|
|
|
#include <IO/ReadBufferFromMemory.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/copyData.h>
|
|
|
|
#include <arrow/io/memory.h>
|
|
|
|
#include "ArrowColumnToCHColumn.h"
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_READ_ALL_DATA;
|
|
|
|
}
|
2019-12-25 19:17:41 +00:00
|
|
|
ORCBlockInputFormat::ORCBlockInputFormat(ReadBuffer &in_, Block header_) : IInputFormat(std::move(header_), in_)
|
|
|
|
{
|
2019-08-21 14:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk ORCBlockInputFormat::generate()
|
|
|
|
{
|
|
|
|
Chunk res;
|
|
|
|
|
|
|
|
auto &header = getPort().getHeader();
|
|
|
|
|
|
|
|
if (!in.eof())
|
|
|
|
{
|
|
|
|
if (row_group_current < row_group_total)
|
|
|
|
throw Exception{"Got new data, but data from previous chunks was not read " +
|
|
|
|
std::to_string(row_group_current) + "/" + std::to_string(row_group_total),
|
|
|
|
ErrorCodes::CANNOT_READ_ALL_DATA};
|
|
|
|
|
|
|
|
file_data.clear();
|
|
|
|
{
|
|
|
|
WriteBufferFromString file_buffer(file_data);
|
|
|
|
copyData(in, file_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<arrow::Buffer> local_buffer = std::make_unique<arrow::Buffer>(file_data);
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<arrow::io::RandomAccessFile> in_stream(new arrow::io::BufferReader(*local_buffer));
|
|
|
|
|
|
|
|
bool ok = arrow::adapters::orc::ORCFileReader::Open(in_stream, arrow::default_memory_pool(),
|
|
|
|
&file_reader).ok();
|
|
|
|
if (!ok)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
row_group_total = file_reader->NumberOfRows();
|
|
|
|
row_group_current = 0;
|
|
|
|
|
|
|
|
} else
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (row_group_current >= row_group_total)
|
|
|
|
return res;
|
|
|
|
std::shared_ptr<arrow::Table> table;
|
|
|
|
|
|
|
|
arrow::Status read_status = file_reader->Read(&table);
|
|
|
|
|
2019-12-25 19:17:41 +00:00
|
|
|
ArrowColumnToCHColumn::arrowTableToCHChunk(res, table, read_status, header, row_group_current, "ORC");
|
2019-08-21 14:19:47 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:46:19 +00:00
|
|
|
void ORCBlockInputFormat::resetParser()
|
|
|
|
{
|
|
|
|
IInputFormat::resetParser();
|
|
|
|
|
|
|
|
file_reader.reset();
|
|
|
|
file_data.clear();
|
|
|
|
row_group_total = 0;
|
|
|
|
row_group_current = 0;
|
|
|
|
}
|
|
|
|
|
2019-08-21 14:19:47 +00:00
|
|
|
void registerInputFormatProcessorORC(FormatFactory &factory)
|
|
|
|
{
|
|
|
|
factory.registerInputFormatProcessor(
|
|
|
|
"ORC",
|
|
|
|
[](ReadBuffer &buf,
|
|
|
|
const Block &sample,
|
|
|
|
const RowInputFormatParams &,
|
|
|
|
const FormatSettings & /* settings */)
|
|
|
|
{
|
2019-12-25 19:17:41 +00:00
|
|
|
return std::make_shared<ORCBlockInputFormat>(buf, sample);
|
2019-08-21 14:19:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_READ_ALL_DATA;
|
|
|
|
}
|
2019-08-21 14:19:47 +00:00
|
|
|
class FormatFactory;
|
|
|
|
void registerInputFormatProcessorORC(FormatFactory &)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|