yingcai-cy commented on code in PR #112: URL: https://github.com/apache/iceberg-cpp/pull/112#discussion_r2128307241
########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,125 @@ +/* + * 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() { + std::shared_ptr<Snapshot> snapshot; + if (snapshot_id_) { + ICEBERG_ASSIGN_OR_RAISE(snapshot, table_.snapshot(*snapshot_id_)); + } else { + snapshot = table_.current_snapshot(); Review Comment: Can current_snapshot() return nullptr? ########## src/iceberg/table_scan.h: ########## @@ -0,0 +1,116 @@ +/* + * 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 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. + /// Defaults to none which means select all columns + /// \param column_names A list of column names. + /// \return Reference to the builder. + TableScanBuilder& WithColumnNames(const std::vector<std::string>& column_names); Review Comment: ```suggestion TableScanBuilder& WithColumnNames(std::vector<std::string> column_names); ``` Pass by value and use move in implementation. ########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,125 @@ +/* + * 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() { + std::shared_ptr<Snapshot> snapshot; + if (snapshot_id_) { + ICEBERG_ASSIGN_OR_RAISE(snapshot, table_.snapshot(*snapshot_id_)); + } else { + snapshot = table_.current_snapshot(); + } + + std::shared_ptr<Schema> schema; + if (snapshot->schema_id) { + const auto& schemas = table_.schemas(); + if (auto it = schemas.find(*snapshot->schema_id); it != schemas.end()) { + schema = it->second; + } else { + return InvalidData("Schema {} in snapshot {} is not found", *snapshot->schema_id, Review Comment: nit:I think use InvalidArgument error code is more accurate. -- 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