gty404 commented on code in PR #112:
URL: https://github.com/apache/iceberg-cpp/pull/112#discussion_r2177839683


##########
src/iceberg/table_scan.h:
##########
@@ -0,0 +1,210 @@
+/*
+ * 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
+
+#include <string>
+#include <vector>
+
+#include "iceberg/manifest_entry.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Represents a task to scan a table or a portion of it.
+class ICEBERG_EXPORT ScanTask {
+ public:
+  virtual ~ScanTask() = default;
+
+  /// \brief The number of bytes that should be read by this scan task.
+  virtual int64_t size_bytes() const = 0;
+
+  /// \brief The number of files that should be read by this scan task.
+  virtual int32_t files_count() const = 0;
+
+  /// \brief The number of rows that should be read by this scan task.
+  virtual int64_t estimated_row_count() const = 0;
+};
+
+/// \brief Represents a task to scan a portion of a data file.
+class ICEBERG_EXPORT FileScanTask : public ScanTask {

Review Comment:
   1. I initially expected it to just be a struct, but since the previous 
comments suggested doing an abstraction, I referred to the design in 
iceberg-java/iceberg-python.
   3. Partition spec and value can be obtained from DataFile and Snapshot, and 
we can add these interfaces when needed for subsequent PR
   4. Sure, I will modify it to optional, thanks.



##########
src/iceberg/table_scan.cc:
##########
@@ -0,0 +1,286 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/table_scan.h"
+
+#include <algorithm>
+#include <ranges>
+
+#include "iceberg/manifest_entry.h"
+#include "iceberg/manifest_list.h"
+#include "iceberg/manifest_reader.h"
+#include "iceberg/schema.h"
+#include "iceberg/schema_field.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/table.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+/// \brief Use indexed data structures for efficient lookups
+struct DeleteFileIndex {
+  /// \brief Index by sequence number for quick filtering
+  std::multimap<int64_t, ManifestEntry*> sequence_index;
+
+  /// \brief Build the index from a list of manifest entries.
+  void BuildIndex(const std::vector<std::unique_ptr<ManifestEntry>>& entries) {
+    sequence_index.clear();
+
+    for (const auto& entry : entries) {
+      const int64_t seq_num =
+          
entry->sequence_number.value_or(TableMetadata::kInitialSequenceNumber);
+      sequence_index.emplace(seq_num, entry.get());
+    }
+  }
+
+  /// \brief Find delete files that match the sequence number of a data entry.
+  std::vector<ManifestEntry*> FindRelevantEntries(const ManifestEntry& 
data_entry) const {
+    std::vector<ManifestEntry*> relevant_deletes;
+
+    // Use lower_bound for efficient range search
+    auto data_sequence_number =
+        
data_entry.sequence_number.value_or(TableMetadata::kInitialSequenceNumber);
+    for (auto it = sequence_index.lower_bound(data_sequence_number);
+         it != sequence_index.end(); ++it) {
+      // Additional filtering logic here

Review Comment:
   DataFiles only need to retain DeleteFiles with a sequence greater than their 
own?



##########
src/iceberg/table_scan.h:
##########
@@ -0,0 +1,210 @@
+/*
+ * 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
+
+#include <string>
+#include <vector>
+
+#include "iceberg/manifest_entry.h"
+#include "iceberg/type_fwd.h"
+
+namespace iceberg {
+
+/// \brief Represents a task to scan a table or a portion of it.
+class ICEBERG_EXPORT ScanTask {
+ public:
+  virtual ~ScanTask() = default;
+
+  /// \brief The number of bytes that should be read by this scan task.
+  virtual int64_t size_bytes() const = 0;
+
+  /// \brief The number of files that should be read by this scan task.
+  virtual int32_t files_count() const = 0;
+
+  /// \brief The number of rows that should be read by this scan task.
+  virtual int64_t estimated_row_count() const = 0;
+};
+
+/// \brief Represents a task to scan a portion of a data file.
+class ICEBERG_EXPORT FileScanTask : public ScanTask {
+ public:
+  FileScanTask(std::shared_ptr<DataFile> file,
+               std::vector<std::shared_ptr<DataFile>> delete_files, int64_t 
start,
+               int64_t length, std::shared_ptr<Expression> residual);
+
+  /// \brief The data file that should be read by this scan task.
+  const std::shared_ptr<DataFile>& data_file() const;
+
+  /// \brief The delete files that should be read by this scan task.
+  const std::vector<std::shared_ptr<DataFile>>& delete_files() const;
+
+  /// \brief The byte offset in the data file where the scan should start.
+  int64_t start() const;
+
+  /// \brief The length in bytes to scan from the start offset.
+  int64_t length() const;
+
+  /// \brief The residual expression to apply after scanning the data file.
+  const std::shared_ptr<Expression>& residual() const;
+
+  int64_t size_bytes() const override;
+  int32_t files_count() const override;
+  int64_t estimated_row_count() const override;
+
+ private:
+  /// \brief Data file metadata.
+  std::shared_ptr<DataFile> data_file_;
+  /// \brief Delete files metadata.
+  std::vector<std::shared_ptr<DataFile>> delete_files_;
+  /// \brief Start byte offset.
+  int64_t start_;
+  /// \brief Length in bytes to scan.
+  int64_t length_;
+  /// \brief Residual expression to apply.
+  std::shared_ptr<Expression> residual_;
+};
+
+/// \brief Scan context holding snapshot and scan-specific metadata.
+struct TableScanContext {
+  /// \brief Table metadata.
+  std::shared_ptr<TableMetadata> table_metadata;
+  /// \brief Snapshot to scan.
+  std::shared_ptr<Snapshot> snapshot;
+  /// \brief Projected schema.
+  std::shared_ptr<Schema> projected_schema;
+  /// \brief Filter expression to apply.
+  std::shared_ptr<Expression> filter;
+  /// \brief Whether the scan is case-sensitive.
+  bool case_sensitive = false;
+  /// \brief Additional options for the scan.
+  std::unordered_map<std::string, std::string> options;
+  /// \brief Optional limit on the number of rows to scan.
+  std::optional<int64_t> limit;
+};
+
+/// \brief Builder class for creating TableScan instances.
+class ICEBERG_EXPORT TableScanBuilder {
+ public:
+  /// \brief Constructs a TableScanBuilder for the given table.
+  /// \param table The table to scan.
+  /// \param table_metadata The metadata of the table to scan.
+  explicit TableScanBuilder(const Table& table,
+                            std::shared_ptr<TableMetadata> table_metadata);
+
+  /// \brief Sets the snapshot ID to scan.
+  /// \param snapshot_id The ID of the snapshot.
+  /// \return Reference to the builder.
+  TableScanBuilder& WithSnapshotId(int64_t snapshot_id);
+
+  /// \brief Selects columns to include in the scan.
+  /// \param column_names A list of column names. If empty, all columns will 
be selected.
+  /// \return Reference to the builder.
+  TableScanBuilder& WithColumnNames(std::vector<std::string> column_names);
+
+  /// \brief Sets the schema to use for the scan.
+  /// \param schema The schema to use.
+  /// \return Reference to the builder.
+  TableScanBuilder& WithSchema(std::shared_ptr<Schema> schema);
+
+  /// \brief Applies a filter expression to the scan.
+  /// \param filter Filter expression to use.
+  /// \return Reference to the builder.
+  TableScanBuilder& WithFilter(std::shared_ptr<Expression> filter);
+
+  /// \brief Sets whether the scan should be case-sensitive.
+  /// \param case_sensitive Whether the scan is case-sensitive.
+  /// /return Reference to the builder.
+  TableScanBuilder& WithCaseSensitive(bool case_sensitive);
+
+  /// \brief Sets an option for the scan.
+  /// \param property The name of the option.
+  /// \param value The value of the option.
+  /// \return Reference to the builder.
+  TableScanBuilder& WithOption(std::string property, std::string value);
+
+  /// \brief Sets an optional limit on the number of rows to scan.
+  /// \param limit Optional limit on the number of rows.
+  /// \return Reference to the builder.
+  TableScanBuilder& WithLimit(std::optional<int64_t> limit);
+
+  /// \brief Builds and returns a TableScan instance.
+  /// \return A Result containing the TableScan or an error.
+  Result<std::unique_ptr<TableScan>> Build();
+
+ private:
+  /// \brief Reference to the table to scan.
+  const Table& table_;
+  /// \brief column names to project in the scan.
+  std::vector<std::string> column_names_;
+  /// \brief snapshot ID to scan, if specified.
+  std::optional<int64_t> snapshot_id_;
+  /// \brief Context for the scan, including snapshot, schema, and filter.
+  TableScanContext context_;

Review Comment:
   I originally expected that TableScanContext would be context information 
retained after converting various input parameters, and that what was no longer 
needed in the subsequent file scanning process would be removed.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to