wgtmac commented on code in PR #112: URL: https://github.com/apache/iceberg-cpp/pull/112#discussion_r2203423647
########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,192 @@ +/* + * 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_metadata.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +// implement FileScanTask +FileScanTask::FileScanTask(std::shared_ptr<DataFile> file) + : data_file_(std::move(file)) {} + +const std::shared_ptr<DataFile>& FileScanTask::data_file() const { return data_file_; } + +int64_t FileScanTask::size_bytes() const { return data_file_->file_size_in_bytes; } + +int32_t FileScanTask::files_count() const { return 1; } + +int64_t FileScanTask::estimated_row_count() const { return data_file_->record_count; } + +TableScanBuilder::TableScanBuilder(std::shared_ptr<TableMetadata> table_metadata, + std::shared_ptr<FileIO> file_io) + : file_io_(std::move(file_io)) { + context_.table_metadata = std::move(table_metadata); +} + +TableScanBuilder& TableScanBuilder::WithColumnNames( + std::vector<std::string> column_names) { + column_names_ = std::move(column_names); Review Comment: nit: make sure `context_.projected_schema` is not set. ########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,192 @@ +/* + * 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_metadata.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +// implement FileScanTask +FileScanTask::FileScanTask(std::shared_ptr<DataFile> file) + : data_file_(std::move(file)) {} Review Comment: ```suggestion FileScanTask::FileScanTask(std::shared_ptr<DataFile> data_file) : data_file_(std::move(data_file)) {} ``` nit: I think eventually we need to rename it to `data_file` once we will add delete files. ########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,192 @@ +/* + * 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_metadata.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +// implement FileScanTask +FileScanTask::FileScanTask(std::shared_ptr<DataFile> file) + : data_file_(std::move(file)) {} + +const std::shared_ptr<DataFile>& FileScanTask::data_file() const { return data_file_; } + +int64_t FileScanTask::size_bytes() const { return data_file_->file_size_in_bytes; } + +int32_t FileScanTask::files_count() const { return 1; } + +int64_t FileScanTask::estimated_row_count() const { return data_file_->record_count; } + +TableScanBuilder::TableScanBuilder(std::shared_ptr<TableMetadata> table_metadata, + std::shared_ptr<FileIO> file_io) + : file_io_(std::move(file_io)) { + context_.table_metadata = std::move(table_metadata); +} + +TableScanBuilder& TableScanBuilder::WithColumnNames( + std::vector<std::string> column_names) { + column_names_ = std::move(column_names); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithProjectedSchema(std::shared_ptr<Schema> schema) { + context_.projected_schema = std::move(schema); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithSnapshotId(int64_t snapshot_id) { + snapshot_id_ = snapshot_id; + return *this; +} + +TableScanBuilder& TableScanBuilder::WithFilter(std::shared_ptr<Expression> filter) { + context_.filter = std::move(filter); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithCaseSensitive(bool case_sensitive) { + context_.case_sensitive = case_sensitive; + return *this; +} + +TableScanBuilder& TableScanBuilder::WithOption(std::string property, std::string value) { + context_.options[std::move(property)] = std::move(value); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithLimit(std::optional<int64_t> limit) { + context_.limit = limit; + return *this; +} + +Result<std::unique_ptr<TableScan>> TableScanBuilder::Build() { + const auto& table_metadata = context_.table_metadata; + auto snapshot_id = snapshot_id_ ? snapshot_id_ : table_metadata->current_snapshot_id; + if (!snapshot_id) { + return InvalidArgument("No snapshot ID specified for table {}", + table_metadata->table_uuid); + } + auto iter = std::ranges::find_if( Review Comment: nit: add `Result<std::shared_ptr<Snapshot>> TableMetadata::Snapshot(int64_t snapshot_id) const` and move the logic below to it. ########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,192 @@ +/* + * 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_metadata.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +// implement FileScanTask +FileScanTask::FileScanTask(std::shared_ptr<DataFile> file) + : data_file_(std::move(file)) {} + +const std::shared_ptr<DataFile>& FileScanTask::data_file() const { return data_file_; } + +int64_t FileScanTask::size_bytes() const { return data_file_->file_size_in_bytes; } + +int32_t FileScanTask::files_count() const { return 1; } + +int64_t FileScanTask::estimated_row_count() const { return data_file_->record_count; } + +TableScanBuilder::TableScanBuilder(std::shared_ptr<TableMetadata> table_metadata, + std::shared_ptr<FileIO> file_io) + : file_io_(std::move(file_io)) { + context_.table_metadata = std::move(table_metadata); +} + +TableScanBuilder& TableScanBuilder::WithColumnNames( + std::vector<std::string> column_names) { + column_names_ = std::move(column_names); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithProjectedSchema(std::shared_ptr<Schema> schema) { + context_.projected_schema = std::move(schema); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithSnapshotId(int64_t snapshot_id) { + snapshot_id_ = snapshot_id; + return *this; +} + +TableScanBuilder& TableScanBuilder::WithFilter(std::shared_ptr<Expression> filter) { + context_.filter = std::move(filter); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithCaseSensitive(bool case_sensitive) { + context_.case_sensitive = case_sensitive; + return *this; +} + +TableScanBuilder& TableScanBuilder::WithOption(std::string property, std::string value) { + context_.options[std::move(property)] = std::move(value); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithLimit(std::optional<int64_t> limit) { + context_.limit = limit; + return *this; +} + +Result<std::unique_ptr<TableScan>> TableScanBuilder::Build() { + const auto& table_metadata = context_.table_metadata; + auto snapshot_id = snapshot_id_ ? snapshot_id_ : table_metadata->current_snapshot_id; + if (!snapshot_id) { + return InvalidArgument("No snapshot ID specified for table {}", + table_metadata->table_uuid); + } + auto iter = std::ranges::find_if( + table_metadata->snapshots, + [id = *snapshot_id](const auto& snapshot) { return snapshot->snapshot_id == id; }); + if (iter == table_metadata->snapshots.end()) { + return NotFound("Snapshot with ID {} is not found", *snapshot_id); + } + context_.snapshot = *iter; + + if (!context_.projected_schema) { + const auto& snapshot = context_.snapshot; + auto schema_id = + snapshot->schema_id ? snapshot->schema_id : table_metadata->current_schema_id; + if (!schema_id) { + return InvalidArgument("No schema ID found in snapshot {} for table {}", + snapshot->snapshot_id, table_metadata->table_uuid); + } + + const auto& schemas = table_metadata->schemas; + const auto it = std::ranges::find_if(schemas, [id = *schema_id](const auto& schema) { Review Comment: ditto, we can add `TableMetadata::Schema(int64_t schema_id)` for this. ########## src/iceberg/table_scan.cc: ########## @@ -0,0 +1,192 @@ +/* + * 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_metadata.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +// implement FileScanTask +FileScanTask::FileScanTask(std::shared_ptr<DataFile> file) + : data_file_(std::move(file)) {} + +const std::shared_ptr<DataFile>& FileScanTask::data_file() const { return data_file_; } + +int64_t FileScanTask::size_bytes() const { return data_file_->file_size_in_bytes; } + +int32_t FileScanTask::files_count() const { return 1; } + +int64_t FileScanTask::estimated_row_count() const { return data_file_->record_count; } + +TableScanBuilder::TableScanBuilder(std::shared_ptr<TableMetadata> table_metadata, + std::shared_ptr<FileIO> file_io) + : file_io_(std::move(file_io)) { + context_.table_metadata = std::move(table_metadata); +} + +TableScanBuilder& TableScanBuilder::WithColumnNames( + std::vector<std::string> column_names) { + column_names_ = std::move(column_names); + return *this; +} + +TableScanBuilder& TableScanBuilder::WithProjectedSchema(std::shared_ptr<Schema> schema) { + context_.projected_schema = std::move(schema); Review Comment: nit: make sure `column_names_` is not set. -- 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