|
16 | 16 |
|
17 | 17 | #include "paimon/core/operation/append_only_file_store_write.h" |
18 | 18 |
|
| 19 | +#include <functional> |
19 | 20 | #include <vector> |
20 | 21 |
|
| 22 | +#include "arrow/c/bridge.h" |
| 23 | +#include "arrow/c/helpers.h" |
21 | 24 | #include "paimon/common/data/binary_row.h" |
| 25 | +#include "paimon/common/data/shredding/map_shared_shredding_batch_converter.h" |
| 26 | +#include "paimon/common/data/shredding/map_shared_shredding_utils.h" |
| 27 | +#include "paimon/common/data/shredding/map_shredding_defs.h" |
22 | 28 | #include "paimon/common/table/special_fields.h" |
23 | 29 | #include "paimon/common/utils/arrow/arrow_utils.h" |
24 | 30 | #include "paimon/core/append/append_only_writer.h" |
@@ -111,10 +117,14 @@ Result<std::vector<std::shared_ptr<DataFileMeta>>> AppendOnlyFileStoreWrite::Com |
111 | 117 |
|
112 | 118 | PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<BatchReader> reader, |
113 | 119 | CreateFilesReader(partition, bucket, dv_factory, to_compact)); |
| 120 | + PAIMON_ASSIGN_OR_RAISE( |
| 121 | + std::shared_ptr<MapSharedShreddingContext> shredding_context, |
| 122 | + MapSharedShreddingUtils::CreateShreddingContext(write_schema_, options_)); |
114 | 123 | auto rewriter = |
115 | 124 | std::make_unique<RollingFileWriter<::ArrowArray*, std::shared_ptr<DataFileMeta>>>( |
116 | 125 | options_.GetTargetFileSize(/*has_primary_key=*/false), |
117 | | - GetDataFileWriterCreator(partition, bucket, write_schema_, write_cols_, to_compact)); |
| 126 | + GetDataFileWriterCreator(partition, bucket, write_schema_, write_cols_, to_compact, |
| 127 | + shredding_context)); |
118 | 128 |
|
119 | 129 | ScopeGuard reader_guard([&]() { |
120 | 130 | if (reader) { |
@@ -210,34 +220,58 @@ AppendOnlyFileStoreWrite::SingleFileWriterCreator |
210 | 220 | AppendOnlyFileStoreWrite::GetDataFileWriterCreator( |
211 | 221 | const BinaryRow& partition, int32_t bucket, const std::shared_ptr<arrow::Schema>& schema, |
212 | 222 | const std::optional<std::vector<std::string>>& write_cols, |
213 | | - const std::vector<std::shared_ptr<DataFileMeta>>& to_compact) const { |
| 223 | + const std::vector<std::shared_ptr<DataFileMeta>>& to_compact, |
| 224 | + const std::shared_ptr<MapSharedShreddingContext>& shredding_context) const { |
214 | 225 | return |
215 | | - [this, partition, bucket, schema, write_cols, to_compact]() |
| 226 | + [this, partition, bucket, schema, write_cols, to_compact, shredding_context]() |
216 | 227 | -> Result< |
217 | 228 | std::unique_ptr<SingleFileWriter<::ArrowArray*, std::shared_ptr<DataFileMeta>>>> { |
| 229 | + PAIMON_ASSIGN_OR_RAISE(MapSharedShreddingBatchConverter::ConverterBundle bundle, |
| 230 | + MapSharedShreddingBatchConverter::CreateConverter( |
| 231 | + schema, shredding_context, pool_)); |
| 232 | + std::shared_ptr<arrow::Schema> file_schema = |
| 233 | + bundle.physical_schema ? bundle.physical_schema : schema; |
| 234 | + |
218 | 235 | ::ArrowSchema arrow_schema; |
219 | 236 | ScopeGuard guard([&arrow_schema]() { ArrowSchemaRelease(&arrow_schema); }); |
220 | | - PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*schema, &arrow_schema)); |
| 237 | + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*file_schema, &arrow_schema)); |
221 | 238 | auto format = options_.GetFileFormat(); |
222 | 239 | PAIMON_ASSIGN_OR_RAISE( |
223 | 240 | std::shared_ptr<WriterBuilder> writer_builder, |
224 | 241 | format->CreateWriterBuilder(&arrow_schema, options_.GetWriteBatchSize())); |
225 | 242 | writer_builder->WithMemoryPool(pool_); |
226 | 243 |
|
227 | | - PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*schema, &arrow_schema)); |
| 244 | + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*file_schema, &arrow_schema)); |
228 | 245 | PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<FormatStatsExtractor> stats_extractor, |
229 | 246 | format->CreateStatsExtractor(&arrow_schema)); |
230 | 247 | PAIMON_ASSIGN_OR_RAISE( |
231 | 248 | std::shared_ptr<DataFilePathFactory> data_file_path_factory, |
232 | 249 | file_store_path_factory_->CreateDataFilePathFactory(partition, bucket)); |
| 250 | + |
| 251 | + std::function<Status(ArrowArray*, ArrowArray*)> batch_converter; |
| 252 | + if (bundle.converter) { |
| 253 | + auto converter = bundle.converter; |
| 254 | + batch_converter = [converter](ArrowArray* input, ArrowArray* output) -> Status { |
| 255 | + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ArrowArray> physical, |
| 256 | + converter->Convert(input)); |
| 257 | + ArrowArrayMove(physical.get(), output); |
| 258 | + return Status::OK(); |
| 259 | + }; |
| 260 | + } |
| 261 | + |
233 | 262 | auto writer = std::make_unique<DataFileWriter>( |
234 | | - options_.GetFileCompression(), std::function<Status(ArrowArray*, ArrowArray*)>(), |
235 | | - table_schema_->Id(), |
| 263 | + options_.GetFileCompression(), batch_converter, table_schema_->Id(), |
236 | 264 | std::make_shared<LongCounter>(to_compact[0]->min_sequence_number), |
237 | 265 | FileSource::Compact(), stats_extractor, data_file_path_factory->IsExternalPath(), |
238 | 266 | write_cols, pool_); |
239 | 267 | PAIMON_RETURN_NOT_OK(writer->Init(options_.GetFileSystem(), |
240 | 268 | data_file_path_factory->NewPath(), writer_builder)); |
| 269 | + |
| 270 | + if (bundle.converter) { |
| 271 | + writer->SetMetadataFinalizer(MapSharedShreddingUtils::BuildMetadataFinalizer( |
| 272 | + bundle.converter, MapSharedShreddingDefine::kDefaultDictCompression, |
| 273 | + shredding_context, file_schema)); |
| 274 | + } |
241 | 275 | return writer; |
242 | 276 | }; |
243 | 277 | } |
|
0 commit comments