lidavidm commented on code in PR #112: URL: https://github.com/apache/iceberg-cpp/pull/112#discussion_r2110599021
########## src/iceberg/table_scan.h: ########## @@ -0,0 +1,117 @@ +/* + * 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/expression/expression.h" +#include "iceberg/file_io.h" +#include "iceberg/manifest_entry.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/// \brief Builder class for creating TableScan instances. +class ICEBERG_EXPORT TableScanBuilder { + public: + /// \brief Constructs a TableScanBuilder for the given table. + /// \param table Reference to the table to scan. + explicit TableScanBuilder(const Table& table); + + /// \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. + /// \return Reference to the builder. + TableScanBuilder& WithColumnNames(const std::vector<std::string>& column_names); + + /// \brief Applies a filter expression to the scan. + /// \param filter Filter expression to use. + /// \return Reference to the builder. + TableScanBuilder& WithFilter(const std::shared_ptr<Expression>& filter); + + /// \brief Builds and returns a TableScan instance. + /// \return A Result containing the TableScan or an error. + Result<std::unique_ptr<TableScan>> Build(); + + private: + const Table& table_; + std::vector<std::string> column_names_; + std::optional<int64_t> snapshot_id_; + std::shared_ptr<Expression> filter_; +}; + +/// \brief Represents a configured scan operation on a table. +class ICEBERG_EXPORT TableScan { + public: + /// \brief Scan context holding snapshot and scan-specific metadata. + struct ScanContext { + std::shared_ptr<Snapshot> snapshot_; ///< Snapshot to scan. Review Comment: Same here: these are public fields ########## src/iceberg/table_scan.h: ########## @@ -0,0 +1,117 @@ +/* + * 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/expression/expression.h" +#include "iceberg/file_io.h" +#include "iceberg/manifest_entry.h" +#include "iceberg/type_fwd.h" + +namespace iceberg { + +/// \brief Builder class for creating TableScan instances. +class ICEBERG_EXPORT TableScanBuilder { + public: + /// \brief Constructs a TableScanBuilder for the given table. + /// \param table Reference to the table to scan. + explicit TableScanBuilder(const Table& table); + + /// \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. + /// \return Reference to the builder. + TableScanBuilder& WithColumnNames(const std::vector<std::string>& column_names); + + /// \brief Applies a filter expression to the scan. + /// \param filter Filter expression to use. + /// \return Reference to the builder. + TableScanBuilder& WithFilter(const std::shared_ptr<Expression>& filter); + + /// \brief Builds and returns a TableScan instance. + /// \return A Result containing the TableScan or an error. + Result<std::unique_ptr<TableScan>> Build(); + + private: + const Table& table_; + std::vector<std::string> column_names_; + std::optional<int64_t> snapshot_id_; + std::shared_ptr<Expression> filter_; +}; + +/// \brief Represents a configured scan operation on a table. +class ICEBERG_EXPORT TableScan { + public: + /// \brief Scan context holding snapshot and scan-specific metadata. + struct ScanContext { + std::shared_ptr<Snapshot> snapshot_; ///< Snapshot to scan. + std::shared_ptr<Schema> schema_; ///< Projected schema. + std::vector<int32_t> field_ids_; ///< Field IDs of selected columns. + std::shared_ptr<Expression> filter_; ///< Filter expression to apply. + }; + + /// \brief Constructs a TableScan with the given context and file I/O. + /// \param context Scan context including snapshot, schema, and filter. + /// \param file_io File I/O instance for reading manifests and data files. + TableScan(std::unique_ptr<ScanContext> context, std::shared_ptr<FileIO> file_io); Review Comment: Is there any reason to have unique_ptr vs just the struct itself? ########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,122 @@ +/* + * 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 "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/util/macros.h" + +namespace iceberg { + +TableScanBuilder::TableScanBuilder(const Table& table) : table_(table) {} + +TableScanBuilder& TableScanBuilder::WithColumnNames( + const std::vector<std::string>& column_names) { + column_names_ = column_names; + return *this; +} + +TableScanBuilder& TableScanBuilder::WithSnapshotId(int64_t snapshot_id) { + snapshot_id_ = snapshot_id; + return *this; +} + +TableScanBuilder& TableScanBuilder::WithFilter( + const std::shared_ptr<Expression>& filter) { + filter_ = filter; + return *this; +} + +Result<std::unique_ptr<TableScan>> TableScanBuilder::Build() { + ICEBERG_ASSIGN_OR_RAISE(auto snapshot, snapshot_id_ ? table_.snapshot(*snapshot_id_) + : Result<std::shared_ptr<Snapshot>>( + table_.current_snapshot())); + + auto ResolveSchema = [&]() -> Result<std::shared_ptr<Schema>> { + if (snapshot->schema_id) { + const auto& schemas = table_.schemas(); + if (auto it = schemas.find(*snapshot->schema_id); it != schemas.end()) { + return it->second; + } + return InvalidData("Schema {} in snapshot {} is not found", *snapshot->schema_id, + snapshot->snapshot_id); + } + return table_.schema(); + }; + + ICEBERG_ASSIGN_OR_RAISE(auto schema, ResolveSchema()); Review Comment: Why are we indirecting through a lambda? ########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,122 @@ +/* + * 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 "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/util/macros.h" + +namespace iceberg { + +TableScanBuilder::TableScanBuilder(const Table& table) : table_(table) {} + +TableScanBuilder& TableScanBuilder::WithColumnNames( + const std::vector<std::string>& column_names) { + column_names_ = column_names; + return *this; +} + +TableScanBuilder& TableScanBuilder::WithSnapshotId(int64_t snapshot_id) { + snapshot_id_ = snapshot_id; + return *this; +} + +TableScanBuilder& TableScanBuilder::WithFilter( + const std::shared_ptr<Expression>& filter) { + filter_ = filter; + return *this; +} + +Result<std::unique_ptr<TableScan>> TableScanBuilder::Build() { + ICEBERG_ASSIGN_OR_RAISE(auto snapshot, snapshot_id_ ? table_.snapshot(*snapshot_id_) + : Result<std::shared_ptr<Snapshot>>( Review Comment: Might as well write out the if/else at this point -- 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