3939namespace arrow {
4040class Array ;
4141} // namespace arrow
42- namespace avro {
43- class OutputStream ;
44- } // namespace avro
4542struct ArrowArray ;
4643
4744namespace paimon ::avro {
4845
4946AvroFormatWriter::AvroFormatWriter (
5047 const std::shared_ptr<::avro::DataFileWriter<::avro::GenericDatum>>& file_writer,
5148 const ::avro::ValidSchema& avro_schema, const std::shared_ptr<arrow::DataType>& data_type,
52- std::unique_ptr<AvroAdaptor> adaptor)
49+ std::unique_ptr<AvroAdaptor> adaptor, AvroOutputStreamImpl* avro_output_stream )
5350 : writer_(file_writer),
5451 avro_schema_ (avro_schema),
5552 data_type_(data_type),
56- adaptor_(std::move(adaptor)) {}
53+ adaptor_(std::move(adaptor)),
54+ avro_output_stream_(avro_output_stream) {}
5755
5856Result<std::unique_ptr<AvroFormatWriter>> AvroFormatWriter::Create (
59- std::unique_ptr<::avro::OutputStream > out, const std::shared_ptr<arrow::Schema>& schema,
57+ std::unique_ptr<AvroOutputStreamImpl > out, const std::shared_ptr<arrow::Schema>& schema,
6058 const ::avro::Codec codec) {
6159 try {
6260 PAIMON_ASSIGN_OR_RAISE (::avro::ValidSchema avro_schema,
6361 AvroSchemaConverter::ArrowSchemaToAvroSchema (schema));
62+ AvroOutputStreamImpl* avro_output_stream = out.get ();
6463 auto writer = std::make_shared<::avro::DataFileWriter<::avro::GenericDatum>>(
6564 std::move (out), avro_schema, DEFAULT_SYNC_INTERVAL , codec);
6665 auto data_type = arrow::struct_ (schema->fields ());
6766 auto adaptor = std::make_unique<AvroAdaptor>(data_type);
68- return std::unique_ptr<AvroFormatWriter>(
69- new AvroFormatWriter ( writer, avro_schema, data_type, std::move (adaptor)));
67+ return std::unique_ptr<AvroFormatWriter>(new AvroFormatWriter (
68+ writer, avro_schema, data_type, std::move (adaptor), avro_output_stream ));
7069 } catch (const ::avro::Exception& e) {
7170 return Status::Invalid (fmt::format (" avro format writer create failed. {}" , e.what ()));
7271 } catch (const std::exception& e) {
@@ -92,6 +91,7 @@ Status AvroFormatWriter::Flush() {
9291
9392Status AvroFormatWriter::Finish () {
9493 try {
94+ avro_output_stream_->FlushBuffer (); // we need flush buffer before close writer
9595 writer_->close ();
9696 } catch (const ::avro::Exception& e) {
9797 return Status::Invalid (fmt::format (" avro writer close failed. {}" , e.what ()));
0 commit comments