forked from alibaba/paimon-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_shared_shredding_batch_converter.h
More file actions
141 lines (120 loc) · 6.72 KB
/
Copy pathmap_shared_shredding_batch_converter.h
File metadata and controls
141 lines (120 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2026-present Alibaba Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cstdint>
#include <map>
#include <memory>
#include <unordered_map>
#include <vector>
#include "arrow/memory_pool.h"
#include "arrow/type_fwd.h"
#include "paimon/common/data/shredding/map_shared_shredding_column_allocator.h"
#include "paimon/common/data/shredding/map_shared_shredding_field_dict.h"
#include "paimon/common/data/shredding/map_shredding_defs.h"
#include "paimon/common/data/shredding/shredding_batch_converter.h"
#include "paimon/memory/memory_pool.h"
#include "paimon/result.h"
#include "paimon/status.h"
struct ArrowArray;
namespace paimon {
class CoreOptions;
class MapSharedShreddingContext;
/// Converts logical batches containing MAP<STRING, T> columns into physical batches
/// where each shared-shredding MAP column is replaced by
/// STRUCT<__field_mapping, __col_0..K-1, __overflow>.
///
/// Non-shared-shredding columns are passed through unchanged.
/// Each shared-shredding column has its own FieldDict and ColumnAllocator.
class MapSharedShreddingBatchConverter : public ShreddingBatchConverter {
public:
/// Creates a converter for one file write cycle.
/// Computes per-file K from context, builds physical schema, and constructs the converter.
/// @param logical_schema The original schema with MAP<STRING, T> columns.
/// @param context The cross-file shared context for K adaptation.
/// @param options CoreOptions used to read each column's placement policy.
/// @param pool Paimon memory pool for Arrow allocations.
/// @return The converter.
static Result<std::shared_ptr<MapSharedShreddingBatchConverter>> Create(
const std::shared_ptr<arrow::Schema>& logical_schema,
const std::shared_ptr<MapSharedShreddingContext>& context, const CoreOptions& options,
const std::shared_ptr<MemoryPool>& pool);
/// Returns the physical schema produced for this converter.
const std::shared_ptr<arrow::Schema>& GetPhysicalSchema() const override;
/// Converts a logical batch to a physical batch.
/// @param logical_batch Input ArrowArray (C ABI) with logical schema. Consumed on success.
/// @return Owned physical ArrowArray (C ABI) with physical schema.
Result<std::unique_ptr<ArrowArray>> Convert(ArrowArray* logical_batch) override;
/// Builds MapSharedShreddingFieldMeta for one shredding column (by field name).
/// Called at file close to serialize metadata.
Result<MapSharedShreddingFieldMeta> BuildFieldMeta(const std::string& field_name) const;
/// Returns all shredding column field names.
const std::vector<std::string>& GetShreddingColumnNames() const;
private:
/// Per-column context for one shared-shredding MAP column.
struct ColumnContext {
std::string field_name;
int32_t num_columns; // K
MapSharedShreddingFieldDict dict;
std::unique_ptr<MapSharedShreddingColumnAllocator> allocator;
ColumnContext(const std::string& field_name, int32_t num_columns,
std::unique_ptr<MapSharedShreddingColumnAllocator>&& allocator)
: field_name(field_name), num_columns(num_columns), allocator(std::move(allocator)) {}
};
/// Constructs a converter.
/// @param logical_schema The original schema with MAP<STRING, T> columns.
/// @param physical_schema The physical schema (MAP columns replaced with STRUCT).
/// @param contexts Per-shredding-column conversion contexts.
/// @param shredding_field_names Shared-shredding field names in schema order.
/// @param pool Paimon memory pool for Arrow allocations.
MapSharedShreddingBatchConverter(const std::shared_ptr<arrow::Schema>& logical_schema,
const std::shared_ptr<arrow::Schema>& physical_schema,
std::vector<ColumnContext>&& contexts,
std::vector<std::string>&& shredding_field_names,
const std::shared_ptr<MemoryPool>& pool);
/// Converts one MAP<STRING, T> column to physical STRUCT for all rows.
/// @param physical_struct_type The physical struct type from physical_schema for this column.
Result<std::shared_ptr<arrow::Array>> ConvertOneColumn(
const std::shared_ptr<arrow::Array>& map_column,
const std::shared_ptr<arrow::DataType>& physical_struct_type, ColumnContext* context) const;
/// Extracts field ids and builds field_id -> value_index map for one row.
void ExtractRowFields(const std::shared_ptr<arrow::StringArray>& keys_array, int64_t start,
int64_t length, MapSharedShreddingFieldDict* dict,
std::vector<int32_t>* field_ids_out,
std::unordered_map<int32_t, int64_t>* field_id_to_value_index_out) const;
/// Appends __field_mapping list for one row.
Status AppendFieldMapping(const RowAllocation& allocation, int32_t num_cols,
arrow::ListBuilder* list_builder,
arrow::Int32Builder* value_builder) const;
/// Appends __col_0..K-1 values for one row.
Status AppendColumnValues(const std::shared_ptr<arrow::Array>& values_array,
const RowAllocation& allocation,
const std::unordered_map<int32_t, int64_t>& field_id_to_value_index,
int32_t num_cols,
const std::vector<arrow::ArrayBuilder*>& col_builders) const;
/// Appends __overflow entries for one row.
Status AppendOverflow(const std::shared_ptr<arrow::Array>& values_array,
const RowAllocation& allocation,
const std::unordered_map<int32_t, int64_t>& field_id_to_value_index,
arrow::MapBuilder* overflow_builder,
arrow::Int32Builder* overflow_key_builder,
arrow::ArrayBuilder* overflow_value_builder) const;
std::shared_ptr<arrow::Schema> logical_schema_;
std::shared_ptr<arrow::Schema> physical_schema_;
std::vector<ColumnContext> contexts_;
std::vector<std::string> shredding_field_names_;
std::shared_ptr<arrow::MemoryPool> pool_;
};
} // namespace paimon