zhjwpku commented on code in PR #1066:
URL: https://github.com/apache/iceberg-cpp/pull/1066#discussion_r4390863856


##########
src/iceberg/inspect/metadata_scan_util_internal.h:
##########
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+
+/// \file iceberg/inspect/metadata_scan_util_internal.h
+/// \brief Read in-memory metadata as Arrow batches produced on demand.
+
+#include <algorithm>
+#include <cstdint>
+#include <memory>
+#include <optional>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "iceberg/arrow_c_data.h"
+#include "iceberg/arrow_c_data_util_internal.h"
+#include "iceberg/arrow_row_builder_internal.h"
+#include "iceberg/result.h"
+#include "iceberg/schema.h"
+#include "iceberg/schema_internal.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg::internal {
+
+/// \brief Build one Arrow batch per read, retaining the row producer's owners.
+template <typename AppendValue>
+class MetadataBatchSource {
+ public:
+  MetadataBatchSource(std::shared_ptr<iceberg::Schema> schema,
+                      std::vector<int32_t> field_ids, size_t num_rows, int64_t 
batch_size,
+                      AppendValue append_value)
+      : schema_(std::move(schema)),
+        field_ids_(std::move(field_ids)),
+        num_rows_(num_rows),
+        batch_size_(static_cast<size_t>(batch_size)),
+        append_value_(std::move(append_value)) {}
+
+  Status Close() { return {}; }
+
+  Result<ArrowSchema> Schema() {
+    ArrowSchema schema{};
+    ICEBERG_RETURN_UNEXPECTED(ToArrowSchema(*schema_, &schema));
+    return schema;
+  }
+
+  Result<std::optional<ArrowArray>> Next() {
+    if (error_) {
+      return unexpected(*error_);
+    }
+    auto result = NextBatch();
+    if (!result) {
+      error_ = result.error();
+    }
+    return result;
+  }
+
+ private:
+  Result<std::optional<ArrowArray>> NextBatch() {
+    if (index_ == num_rows_) {
+      return std::nullopt;
+    }
+    const size_t end = index_ + std::min(batch_size_, num_rows_ - index_);
+    ICEBERG_ASSIGN_OR_RAISE(auto builder, ArrowRowBuilder::Make(*schema_));
+    while (index_ < end) {
+      for (size_t i = 0; i < field_ids_.size(); ++i) {
+        
ICEBERG_RETURN_UNEXPECTED(append_value_(builder.column(static_cast<int64_t>(i)),
+                                                field_ids_[i], index_));
+      }
+      ICEBERG_RETURN_UNEXPECTED(builder.FinishRow());
+      ++index_;
+    }
+    ICEBERG_ASSIGN_OR_RAISE(auto batch, std::move(builder).Finish());
+    return batch;
+  }
+
+  const std::shared_ptr<iceberg::Schema> schema_;
+  const std::vector<int32_t> field_ids_;
+  const size_t num_rows_;
+  const size_t batch_size_;
+  AppendValue append_value_;
+  size_t index_ = 0;
+  std::optional<Error> error_;
+};
+
+/// \brief Create a stream that builds at most batch_size metadata rows per 
read.
+///
+/// Each output column selects a whole canonical column by field ID, without
+/// tightening its nullability. The row producer must capture shared ownership
+/// of everything it uses; it is invoked only when the stream is read.
+template <typename AppendValue>
+Result<ArrowArrayStream> MakeMetadataBatchStream(
+    const std::shared_ptr<Schema>& canonical_schema,
+    const std::shared_ptr<Schema>& projected_schema, size_t num_rows, int64_t 
batch_size,
+    AppendValue&& append_value) {
+  ICEBERG_PRECHECK(batch_size > 0, "Batch size must be positive");
+  const auto& output_schema = projected_schema ? projected_schema : 
canonical_schema;
+  std::vector<int32_t> field_ids;
+  field_ids.reserve(output_schema->fields().size());
+  for (const auto& field : output_schema->fields()) {
+    ICEBERG_ASSIGN_OR_RAISE(auto canonical_field,
+                            canonical_schema->FindFieldById(field.field_id()));

Review Comment:
   Should we use GetFieldById() here to check only top-level fields? 
FindFieldById() also finds nested fields, so a projection with summary.key 
(field ID 7) as a top-level field passes validation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to