|
22 | 22 | #include <limits> |
23 | 23 | #include <utility> |
24 | 24 |
|
| 25 | +#include "arrow/array/array_primitive.h" |
25 | 26 | #include "arrow/c/abi.h" |
26 | 27 | #include "arrow/c/bridge.h" |
27 | 28 | #include "paimon/common/data/columnar/columnar_row.h" |
28 | 29 | #include "paimon/common/utils/arrow/status_utils.h" |
| 30 | +#include "paimon/common/utils/checked_cast.h" |
29 | 31 | #include "paimon/core/io/rolling_file_writer.h" |
30 | 32 | #include "paimon/core/manifest/manifest_entry.h" |
31 | 33 | #include "paimon/core/manifest/manifest_entry_serializer.h" |
@@ -89,26 +91,168 @@ Result<std::unique_ptr<ManifestFile>> ManifestFile::Create( |
89 | 91 | manifest_file_factory, target_file_size, pool, options, partition_type)); |
90 | 92 | } |
91 | 93 |
|
92 | | -Status ManifestFile::ReadBucketEntries(const std::string& file_name, int32_t bucket, |
93 | | - std::vector<ManifestEntry>* entries) const { |
| 94 | +Status ManifestFile::ReadBucketEntries( |
| 95 | + const std::string& file_name, int32_t bucket, std::vector<ManifestEntry>* entries, |
| 96 | + const std::optional<InferredBucketLayout>& inferred_layout) const { |
| 97 | + // Readers without an in-memory selective probe still filter aligned Arrow columns |
| 98 | + // before constructing ManifestEntry and DataFileMeta objects. |
94 | 99 | return ReadArrowBatches( |
95 | 100 | file_name, |
96 | | - [this, bucket, entries](const std::shared_ptr<arrow::StructArray>& batch) -> Status { |
97 | | - const arrow::ArrayVector& fields = batch->fields(); |
98 | | - ColumnarRow row(fields, pool_, /*row_id=*/0); |
99 | | - for (int64_t i = 0; i < batch->length(); i++) { |
| 101 | + [this, bucket, inferred_layout, |
| 102 | + entries](const std::shared_ptr<arrow::StructArray>& batch) -> Status { |
| 103 | + std::shared_ptr<arrow::StructArray> files; |
| 104 | + std::shared_ptr<arrow::Int64Array> schema_ids; |
| 105 | + if (inferred_layout) { |
| 106 | + auto file_array = batch->GetFieldByName("_FILE"); |
| 107 | + if (!file_array || file_array->type_id() != arrow::Type::STRUCT) { |
| 108 | + return Status::Invalid("Manifest file metadata must be a struct array"); |
| 109 | + } |
| 110 | + files = checked_pointer_cast<arrow::StructArray>(file_array); |
| 111 | + auto schema_array = files->GetFieldByName("_SCHEMA_ID"); |
| 112 | + if (schema_array && schema_array->type_id() == arrow::Type::INT64) { |
| 113 | + schema_ids = checked_pointer_cast<arrow::Int64Array>(schema_array); |
| 114 | + } |
| 115 | + } |
| 116 | + ColumnarRow row(batch->fields(), pool_, /*row_id=*/0); |
| 117 | + for (int64_t i = 0; i < batch->length(); ++i) { |
100 | 118 | row.SetRowId(i); |
101 | 119 | PAIMON_RETURN_NOT_OK(ManifestEntrySerializer::ValidateVersion(row.GetInt(0))); |
102 | | - if (ManifestEntrySerializer::GetBucket(row) != bucket) { |
| 120 | + // Unknown or historical layouts must reach the existing compatibility checks. |
| 121 | + const bool historical_layout = |
| 122 | + inferred_layout && (row.IsNullAt(3) || row.IsNullAt(4) || |
| 123 | + row.GetInt(4) != inferred_layout->total_buckets || |
| 124 | + files->IsNull(i) || !schema_ids || schema_ids->IsNull(i) || |
| 125 | + schema_ids->Value(i) != inferred_layout->schema_id); |
| 126 | + if (!historical_layout && ManifestEntrySerializer::GetBucket(row) != bucket) { |
103 | 127 | continue; |
104 | 128 | } |
105 | 129 | PAIMON_ASSIGN_OR_RAISE(ManifestEntry entry, serializer_->FromRow(row)); |
106 | 130 | entries->push_back(std::move(entry)); |
107 | 131 | } |
108 | 132 | return Status::OK(); |
| 133 | + }, |
| 134 | + [this, bucket, inferred_layout](FileBatchReader* reader) { |
| 135 | + return PrepareBucketRead(reader, bucket, inferred_layout); |
109 | 136 | }); |
110 | 137 | } |
111 | 138 |
|
| 139 | +Status ManifestFile::PrepareBucketRead( |
| 140 | + FileBatchReader* reader, int32_t bucket, |
| 141 | + const std::optional<InferredBucketLayout>& inferred_layout) const { |
| 142 | + if (!reader->SupportPreciseBitmapSelection()) { |
| 143 | + return Status::OK(); |
| 144 | + } |
| 145 | + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ArrowSchema> c_schema, reader->GetFileSchema()); |
| 146 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Schema> file_schema, |
| 147 | + arrow::ImportSchema(c_schema.get())); |
| 148 | + const auto& target_type = serializer_->GetDataType(); |
| 149 | + const std::string& version_name = target_type->field(0)->name(); |
| 150 | + const std::string& bucket_name = target_type->field(3)->name(); |
| 151 | + auto version_field = file_schema->GetFieldByName(version_name); |
| 152 | + auto bucket_field = file_schema->GetFieldByName(bucket_name); |
| 153 | + if (!version_field || !bucket_field || version_field->type()->id() != arrow::Type::INT32 || |
| 154 | + bucket_field->type()->id() != arrow::Type::INT32) { |
| 155 | + return Status::OK(); |
| 156 | + } |
| 157 | + std::shared_ptr<arrow::Field> projected_file; |
| 158 | + if (inferred_layout) { |
| 159 | + auto total_field = file_schema->GetFieldByName("_TOTAL_BUCKETS"); |
| 160 | + auto file_field = file_schema->GetFieldByName("_FILE"); |
| 161 | + if (!total_field || total_field->type()->id() != arrow::Type::INT32 || !file_field || |
| 162 | + file_field->type()->id() != arrow::Type::STRUCT) { |
| 163 | + return Status::OK(); |
| 164 | + } |
| 165 | + auto file_type = checked_pointer_cast<arrow::StructType>(file_field->type()); |
| 166 | + auto schema_field = file_type->GetFieldByName("_SCHEMA_ID"); |
| 167 | + if (!schema_field || schema_field->type()->id() != arrow::Type::INT64) { |
| 168 | + return Status::OK(); |
| 169 | + } |
| 170 | + projected_file = file_field->WithType(arrow::struct_({schema_field})); |
| 171 | + } |
| 172 | + arrow::FieldVector probe_fields; |
| 173 | + for (const auto& field : file_schema->fields()) { |
| 174 | + if (field->name() == version_name || field->name() == bucket_name) { |
| 175 | + probe_fields.push_back(field); |
| 176 | + } else if (inferred_layout && field->name() == "_TOTAL_BUCKETS") { |
| 177 | + probe_fields.push_back(field); |
| 178 | + } else if (inferred_layout && field->name() == "_FILE") { |
| 179 | + probe_fields.push_back(projected_file); |
| 180 | + } |
| 181 | + } |
| 182 | + auto probe_schema = arrow::schema(probe_fields); |
| 183 | + ArrowSchema probe_c_schema; |
| 184 | + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*probe_schema, &probe_c_schema)); |
| 185 | + PAIMON_RETURN_NOT_OK( |
| 186 | + reader->SetReadSchema(&probe_c_schema, /*predicate=*/nullptr, std::nullopt)); |
| 187 | + RoaringBitmap32 selected; |
| 188 | + bool row_ids_fit = true; |
| 189 | + while (true) { |
| 190 | + PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatch batch, reader->NextBatch()); |
| 191 | + if (BatchReader::IsEofBatch(batch)) { |
| 192 | + break; |
| 193 | + } |
| 194 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW( |
| 195 | + std::shared_ptr<arrow::Array> array, |
| 196 | + arrow::ImportArray(batch.first.get(), batch.second.get())); |
| 197 | + if (!array || array->type_id() != arrow::Type::STRUCT) { |
| 198 | + return Status::Invalid("Manifest bucket probe must return a struct array"); |
| 199 | + } |
| 200 | + auto rows = checked_pointer_cast<arrow::StructArray>(array); |
| 201 | + auto version_array = rows->GetFieldByName(version_name); |
| 202 | + auto bucket_array = rows->GetFieldByName(bucket_name); |
| 203 | + if (!version_array || !bucket_array || version_array->type_id() != arrow::Type::INT32 || |
| 204 | + bucket_array->type_id() != arrow::Type::INT32) { |
| 205 | + return Status::Invalid("Manifest bucket probe must return int32 version and bucket"); |
| 206 | + } |
| 207 | + auto versions = checked_pointer_cast<arrow::Int32Array>(version_array); |
| 208 | + auto buckets = checked_pointer_cast<arrow::Int32Array>(bucket_array); |
| 209 | + std::shared_ptr<arrow::Int32Array> totals; |
| 210 | + std::shared_ptr<arrow::StructArray> files; |
| 211 | + std::shared_ptr<arrow::Int64Array> schema_ids; |
| 212 | + if (inferred_layout) { |
| 213 | + auto total_array = rows->GetFieldByName("_TOTAL_BUCKETS"); |
| 214 | + auto file_array = rows->GetFieldByName("_FILE"); |
| 215 | + if (!total_array || total_array->type_id() != arrow::Type::INT32 || !file_array || |
| 216 | + file_array->type_id() != arrow::Type::STRUCT) { |
| 217 | + return Status::Invalid("Manifest layout probe must return total buckets and file"); |
| 218 | + } |
| 219 | + totals = checked_pointer_cast<arrow::Int32Array>(total_array); |
| 220 | + files = checked_pointer_cast<arrow::StructArray>(file_array); |
| 221 | + auto schema_array = files->GetFieldByName("_SCHEMA_ID"); |
| 222 | + if (!schema_array || schema_array->type_id() != arrow::Type::INT64) { |
| 223 | + return Status::Invalid("Manifest layout probe must return int64 schema ID"); |
| 224 | + } |
| 225 | + schema_ids = checked_pointer_cast<arrow::Int64Array>(schema_array); |
| 226 | + } |
| 227 | + for (int64_t i = 0; i < rows->length(); ++i) { |
| 228 | + if (rows->IsNull(i) || versions->IsNull(i) || buckets->IsNull(i)) { |
| 229 | + return Status::Invalid("Manifest version and bucket must not be null"); |
| 230 | + } |
| 231 | + // Validate every entry, including buckets not selected by this scan. |
| 232 | + PAIMON_RETURN_NOT_OK(ManifestEntrySerializer::ValidateVersion(versions->Value(i))); |
| 233 | + const bool historical_layout = |
| 234 | + inferred_layout && |
| 235 | + (totals->IsNull(i) || totals->Value(i) != inferred_layout->total_buckets || |
| 236 | + files->IsNull(i) || schema_ids->IsNull(i) || |
| 237 | + schema_ids->Value(i) != inferred_layout->schema_id); |
| 238 | + if (buckets->Value(i) == bucket || historical_layout) { |
| 239 | + PAIMON_ASSIGN_OR_RAISE(uint64_t file_row, reader->GetPreviousBatchFileRowId(i)); |
| 240 | + if (file_row > std::numeric_limits<uint32_t>::max()) { |
| 241 | + row_ids_fit = false; |
| 242 | + } else { |
| 243 | + selected.Add(static_cast<uint32_t>(file_row)); |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + // Keep the on-disk schema; ManifestMetaReader still performs schema evolution afterwards. |
| 249 | + ArrowSchema full_c_schema; |
| 250 | + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*file_schema, &full_c_schema)); |
| 251 | + return reader->SetReadSchema( |
| 252 | + &full_c_schema, /*predicate=*/nullptr, |
| 253 | + row_ids_fit ? std::optional<RoaringBitmap32>(std::move(selected)) : std::nullopt); |
| 254 | +} |
| 255 | + |
112 | 256 | Result<std::vector<ManifestFileMeta>> ManifestFile::Write( |
113 | 257 | const std::vector<ManifestEntry>& entries) { |
114 | 258 | if (entries.empty()) { |
|
0 commit comments