wgtmac commented on code in PR #432:
URL: https://github.com/apache/iceberg-cpp/pull/432#discussion_r2644804901
##########
src/iceberg/test/sort_order_test.cc:
##########
@@ -193,7 +193,7 @@ TEST_F(SortOrderTest, MakeInvalidSortOrderEmptyFields) {
auto sort_order = SortOrder::Make(*schema_, 1, std::vector<SortField>{});
EXPECT_THAT(sort_order, IsError(ErrorKind::kInvalidArgument));
EXPECT_THAT(sort_order,
- HasErrorMessage("Sort order must have at least one sort field"));
+ HasErrorMessage("Sorted order must have at least one sort
field"));
Review Comment:
Please revert this because `sort order` is a common norm.
##########
src/iceberg/update/update_sort_order.h:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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 <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/type_fwd.h"
+#include "iceberg/update/pending_update.h"
+
+/// \file iceberg/update/update_sort_order.h
+/// \brief Updates the table sort order.
+
+namespace iceberg {
+
+/// \brief Updating table sort order with a newly created order.
+class ICEBERG_EXPORT UpdateSortOrder : public PendingUpdate {
+ public:
+ static Result<std::shared_ptr<UpdateSortOrder>> Make(
+ std::shared_ptr<Transaction> transaction);
+
+ ~UpdateSortOrder() override;
+
+ struct ApplyResult {
+ std::shared_ptr<SortOrder> sort_order_;
+ };
+
+ /// \brief Add a sort field to the sort order.
+ ///
+ /// \param term A transform term referencing the field
+ /// \param direction The sort direction (ascending or descending)
+ /// \param null_order The null order (first or last)
+ /// \return Reference to this UpdateSortOrder for chaining
+ UpdateSortOrder& AddSortField(std::shared_ptr<Term> term, SortDirection
direction,
+ NullOrder null_order);
+
+ /// \brief Set case sensitivity of sort column name resolution.
+ ///
+ /// \param case_sensitive When true, column name resolution is case-sensitive
+ /// \return Reference to this UpdateSortOrder for chaining
+ UpdateSortOrder& CaseSensitive(bool case_sensitive);
+
+ Kind kind() const final { return Kind::kUpdateSortOrder; }
+
+ private:
+ Result<ApplyResult> Apply();
Review Comment:
ditto, make it public and add test cases for it.
##########
src/iceberg/update/update_sort_order.h:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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 <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/type_fwd.h"
+#include "iceberg/update/pending_update.h"
+
+/// \file iceberg/update/update_sort_order.h
+/// \brief Updates the table sort order.
+
+namespace iceberg {
+
+/// \brief Updating table sort order with a newly created order.
+class ICEBERG_EXPORT UpdateSortOrder : public PendingUpdate {
+ public:
+ static Result<std::shared_ptr<UpdateSortOrder>> Make(
+ std::shared_ptr<Transaction> transaction);
+
+ ~UpdateSortOrder() override;
+
+ struct ApplyResult {
+ std::shared_ptr<SortOrder> sort_order_;
+ };
+
+ /// \brief Add a sort field to the sort order.
+ ///
+ /// \param term A transform term referencing the field
+ /// \param direction The sort direction (ascending or descending)
+ /// \param null_order The null order (first or last)
+ /// \return Reference to this UpdateSortOrder for chaining
+ UpdateSortOrder& AddSortField(std::shared_ptr<Term> term, SortDirection
direction,
Review Comment:
I think it is still worth adding `AddSortField(std::string_view name,
SortDirection direction, NullOrder null_order)` and internally transfer it into
a named reference to call this one.
##########
src/iceberg/transaction.cc:
##########
@@ -60,9 +64,26 @@ Status Transaction::AddUpdate(const
std::shared_ptr<PendingUpdate>& update) {
return {};
}
-Status Transaction::Apply(std::vector<std::unique_ptr<TableUpdate>> updates) {
- for (const auto& update : updates) {
- update->ApplyTo(*metadata_builder_);
+Status Transaction::Apply(PendingUpdate& update) {
+ switch (update.kind()) {
+ case PendingUpdate::Kind::kUpdateProperties: {
+ auto& update_properties = static_cast<UpdateProperties&>(update);
+ ICEBERG_ASSIGN_OR_RAISE(UpdateProperties::ApplyResult result,
Review Comment:
```suggestion
ICEBERG_ASSIGN_OR_RAISE(auto result,
```
##########
src/iceberg/test/update_sort_order_test.cc:
##########
@@ -0,0 +1,272 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <memory>
+#include <string>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/schema.h"
+#include "iceberg/schema_field.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/test/update_test_base.h"
+#include "iceberg/transaction.h"
+#include "iceberg/transform.h"
+
+namespace iceberg {
+
+class UpdateSortOrderTest : public UpdateTestBase {};
+
+TEST_F(UpdateSortOrderTest, AddSingleSortFieldAscending) {
+ ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateSortOrder());
+ auto ref = NamedReference::Make("x").value();
Review Comment:
Let's use `Expressions::XX` to create these. Otherwise you cannot blindly
call `.value()` on a result.
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateSortOrder>> UpdateSortOrder::Make(
+ std::shared_ptr<Transaction> transaction) {
+ if (!transaction) [[unlikely]] {
+ return InvalidArgument("Cannot create UpdateSortOrder without a
transaction");
+ }
+ return std::shared_ptr<UpdateSortOrder>(new
UpdateSortOrder(std::move(transaction)));
+}
+
+UpdateSortOrder::UpdateSortOrder(std::shared_ptr<Transaction> transaction)
+ : PendingUpdate(std::move(transaction)) {}
+
+UpdateSortOrder::~UpdateSortOrder() = default;
+
+UpdateSortOrder& UpdateSortOrder::AddSortField(std::shared_ptr<Term> term,
+ SortDirection direction,
+ NullOrder null_order) {
+ if (!term) {
+ return AddError(ErrorKind::kInvalidArgument, "Term cannot be null");
+ }
+ if (term->kind() != Term::Kind::kTransform) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be a transform
term");
+ }
+ if (!term->is_unbound()) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be unbound");
+ }
Review Comment:
```suggestion
ICEBERG_BUILDER_CHECK(term != nullptr, "Term cannot be null");
ICEBERG_BUILDER_CHECK(term->kind() == Term::Kind::kTransform, "Term must
be a transform term");
ICEBERG_BUILDER_CHECK(term->is_unbound(), Term must be unbound");
```
##########
src/iceberg/update/update_properties.h:
##########
@@ -39,6 +43,12 @@ class ICEBERG_EXPORT UpdateProperties : public PendingUpdate
{
~UpdateProperties() override;
+ struct ApplyResult {
+ std::unordered_map<std::string, std::string> updates_;
+ std::unordered_set<std::string> removals_;
+ std::optional<int8_t> format_version_;
Review Comment:
```suggestion
std::unordered_map<std::string, std::string> updates;
std::unordered_set<std::string> removals;
std::optional<int8_t> format_version;
```
##########
src/iceberg/sort_order.cc:
##########
@@ -111,7 +111,9 @@ Result<std::unique_ptr<SortOrder>> SortOrder::Make(const
Schema& schema, int32_t
}
if (fields.empty() && sort_id != kUnsortedOrderId) [[unlikely]] {
- return InvalidArgument("Sort order must have at least one sort field");
+ return InvalidArgument(
Review Comment:
I still prefer the original concise message.
##########
src/iceberg/test/transaction_test.cc:
##########
@@ -35,14 +36,6 @@ TEST_F(TransactionTest, CreateTransaction) {
EXPECT_EQ(txn->table(), table_);
}
-TEST_F(TransactionTest, UpdatePropertiesInTransaction) {
Review Comment:
I'd suggest keeping this as a case for single action.
##########
src/iceberg/test/transaction_test.cc:
##########
@@ -67,24 +60,35 @@ TEST_F(TransactionTest,
CommitTransactionWithPropertyUpdate) {
TEST_F(TransactionTest, MultipleUpdatesInTransaction) {
ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction());
- // First update
+ // First update: set property
ICEBERG_UNWRAP_OR_FAIL(auto update1, txn->NewUpdateProperties());
- update1->Set("key1", "value1");
+ update1->Set("key1", "value1").Set("key2", "value2");
EXPECT_THAT(update1->Commit(), IsOk());
- // Second update
- ICEBERG_UNWRAP_OR_FAIL(auto update2, txn->NewUpdateProperties());
- update2->Set("key2", "value2");
+ // Second update: update sort order
+ ICEBERG_UNWRAP_OR_FAIL(auto update2, txn->NewUpdateSortOrder());
+ auto ref = NamedReference::Make("x").value();
Review Comment:
```suggestion
auto ref = Expressions::Ref("x");
```
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
Review Comment:
```suggestion
#include "iceberg/expression/term.h"
#include "iceberg/sort_order.h"
#include "iceberg/table_metadata.h"
#include "iceberg/transaction.h"
#include "iceberg/util/checked_cast.h"
#include "iceberg/util/macros.h"
```
##########
src/iceberg/update/update_properties.h:
##########
@@ -57,9 +67,11 @@ class ICEBERG_EXPORT UpdateProperties : public PendingUpdate
{
Kind kind() const final { return Kind::kUpdateProperties; }
- Result<ApplyResult> Apply() final;
-
private:
+ Result<ApplyResult> Apply();
Review Comment:
I think we still need to keep it public so we are able to test its result.
Please revert removals in the test cases due to making it private.
##########
src/iceberg/update/update_sort_order.h:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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 <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/type_fwd.h"
+#include "iceberg/update/pending_update.h"
+
+/// \file iceberg/update/update_sort_order.h
+/// \brief Updates the table sort order.
+
+namespace iceberg {
+
+/// \brief Updating table sort order with a newly created order.
+class ICEBERG_EXPORT UpdateSortOrder : public PendingUpdate {
+ public:
+ static Result<std::shared_ptr<UpdateSortOrder>> Make(
+ std::shared_ptr<Transaction> transaction);
+
+ ~UpdateSortOrder() override;
+
+ struct ApplyResult {
+ std::shared_ptr<SortOrder> sort_order_;
Review Comment:
```suggestion
std::shared_ptr<SortOrder> sort_order;
```
##########
src/iceberg/update/update_sort_order.h:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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 <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/type_fwd.h"
+#include "iceberg/update/pending_update.h"
+
+/// \file iceberg/update/update_sort_order.h
+/// \brief Updates the table sort order.
+
+namespace iceberg {
+
+/// \brief Updating table sort order with a newly created order.
+class ICEBERG_EXPORT UpdateSortOrder : public PendingUpdate {
+ public:
+ static Result<std::shared_ptr<UpdateSortOrder>> Make(
+ std::shared_ptr<Transaction> transaction);
+
+ ~UpdateSortOrder() override;
+
+ struct ApplyResult {
+ std::shared_ptr<SortOrder> sort_order_;
+ };
+
+ /// \brief Add a sort field to the sort order.
+ ///
+ /// \param term A transform term referencing the field
+ /// \param direction The sort direction (ascending or descending)
+ /// \param null_order The null order (first or last)
+ /// \return Reference to this UpdateSortOrder for chaining
+ UpdateSortOrder& AddSortField(std::shared_ptr<Term> term, SortDirection
direction,
+ NullOrder null_order);
+
+ /// \brief Set case sensitivity of sort column name resolution.
+ ///
+ /// \param case_sensitive When true, column name resolution is case-sensitive
+ /// \return Reference to this UpdateSortOrder for chaining
+ UpdateSortOrder& CaseSensitive(bool case_sensitive);
+
+ Kind kind() const final { return Kind::kUpdateSortOrder; }
+
+ private:
+ Result<ApplyResult> Apply();
+
+ friend class Transaction;
+
+ explicit UpdateSortOrder(std::shared_ptr<Transaction> transaction);
+
+ std::vector<SortField> sort_fields_;
+ bool case_sensitive_ = true;
+ std::shared_ptr<SortOrder> sort_order_;
Review Comment:
```suggestion
```
We should not cache it.
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateSortOrder>> UpdateSortOrder::Make(
+ std::shared_ptr<Transaction> transaction) {
+ if (!transaction) [[unlikely]] {
+ return InvalidArgument("Cannot create UpdateSortOrder without a
transaction");
+ }
Review Comment:
```suggestion
ICEBERG_PRECHECK(transaction != nullptr, "Cannot create UpdateSortOrder
without a transaction");
```
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateSortOrder>> UpdateSortOrder::Make(
+ std::shared_ptr<Transaction> transaction) {
+ if (!transaction) [[unlikely]] {
+ return InvalidArgument("Cannot create UpdateSortOrder without a
transaction");
+ }
+ return std::shared_ptr<UpdateSortOrder>(new
UpdateSortOrder(std::move(transaction)));
+}
+
+UpdateSortOrder::UpdateSortOrder(std::shared_ptr<Transaction> transaction)
+ : PendingUpdate(std::move(transaction)) {}
+
+UpdateSortOrder::~UpdateSortOrder() = default;
+
+UpdateSortOrder& UpdateSortOrder::AddSortField(std::shared_ptr<Term> term,
+ SortDirection direction,
+ NullOrder null_order) {
+ if (!term) {
+ return AddError(ErrorKind::kInvalidArgument, "Term cannot be null");
+ }
+ if (term->kind() != Term::Kind::kTransform) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be a transform
term");
+ }
+ if (!term->is_unbound()) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be unbound");
+ }
+ // use checked-cast to get UnboundTransform
Review Comment:
```suggestion
```
Do not add obvious comment
##########
src/iceberg/update/update_properties.cc:
##########
@@ -111,27 +110,9 @@ Result<PendingUpdate::ApplyResult>
UpdateProperties::Apply() {
ICEBERG_RETURN_UNEXPECTED(
MetricsConfig::VerifyReferencedColumns(new_properties,
*schema.value()));
}
-
- ApplyResult result;
- if (!updates_.empty()) {
-
result.updates.emplace_back(std::make_unique<table::SetProperties>(updates_));
- }
- if (!removals_.empty()) {
- for (const auto& key : removals_) {
- if (current_props.contains(key)) {
- removals.push_back(key);
- }
- }
- if (!removals.empty()) {
-
result.updates.emplace_back(std::make_unique<table::RemoveProperties>(removals));
- }
- }
- if (format_version_.has_value()) {
- result.updates.emplace_back(
-
std::make_unique<table::UpgradeFormatVersion>(format_version_.value()));
- };
-
- return result;
+ return ApplyResult{.updates_ = std::move(new_properties),
Review Comment:
```suggestion
return ApplyResult{.updates_ = updates_,
```
This is wrong. You've carried over properties that do not belong to this
update.
##########
src/iceberg/test/transaction_test.cc:
##########
@@ -67,24 +60,35 @@ TEST_F(TransactionTest,
CommitTransactionWithPropertyUpdate) {
TEST_F(TransactionTest, MultipleUpdatesInTransaction) {
ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction());
- // First update
+ // First update: set property
ICEBERG_UNWRAP_OR_FAIL(auto update1, txn->NewUpdateProperties());
- update1->Set("key1", "value1");
+ update1->Set("key1", "value1").Set("key2", "value2");
EXPECT_THAT(update1->Commit(), IsOk());
- // Second update
- ICEBERG_UNWRAP_OR_FAIL(auto update2, txn->NewUpdateProperties());
- update2->Set("key2", "value2");
+ // Second update: update sort order
+ ICEBERG_UNWRAP_OR_FAIL(auto update2, txn->NewUpdateSortOrder());
+ auto ref = NamedReference::Make("x").value();
+ auto term = UnboundTransform::Make(std::move(ref),
Transform::Identity()).value();
+ update2->AddSortField(std::move(term), SortDirection::kAscending,
NullOrder::kFirst);
EXPECT_THAT(update2->Commit(), IsOk());
// Commit transaction
ICEBERG_UNWRAP_OR_FAIL(auto updated_table, txn->Commit());
- // Verify both properties were set
+ // Verify properties were set
ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_));
const auto& props = reloaded->properties().configs();
EXPECT_EQ(props.at("key1"), "value1");
EXPECT_EQ(props.at("key2"), "value2");
+
+ // Verify sort order was updated
+ ICEBERG_UNWRAP_OR_FAIL(auto sort_order, reloaded->sort_order());
+ EXPECT_FALSE(sort_order->is_unsorted());
+ const auto& fields = sort_order->fields();
+ ASSERT_EQ(fields.size(), 1);
Review Comment:
We have `bool operator==(const SortOrder& lhs, const SortOrder& rhs)`. Can
you create a expected sort order and directly call `EXPECT_EQ`?
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateSortOrder>> UpdateSortOrder::Make(
+ std::shared_ptr<Transaction> transaction) {
+ if (!transaction) [[unlikely]] {
+ return InvalidArgument("Cannot create UpdateSortOrder without a
transaction");
+ }
+ return std::shared_ptr<UpdateSortOrder>(new
UpdateSortOrder(std::move(transaction)));
+}
+
+UpdateSortOrder::UpdateSortOrder(std::shared_ptr<Transaction> transaction)
+ : PendingUpdate(std::move(transaction)) {}
+
+UpdateSortOrder::~UpdateSortOrder() = default;
+
+UpdateSortOrder& UpdateSortOrder::AddSortField(std::shared_ptr<Term> term,
+ SortDirection direction,
+ NullOrder null_order) {
+ if (!term) {
+ return AddError(ErrorKind::kInvalidArgument, "Term cannot be null");
+ }
+ if (term->kind() != Term::Kind::kTransform) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be a transform
term");
+ }
+ if (!term->is_unbound()) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be unbound");
+ }
+ // use checked-cast to get UnboundTransform
+ auto unbound_transform =
internal::checked_pointer_cast<UnboundTransform>(term);
+ ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto schema,
transaction_->current().Schema());
+ ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto bound_term,
+ unbound_transform->Bind(*schema,
case_sensitive_));
+
+ int32_t source_id = bound_term->reference()->field_id();
+ sort_fields_.emplace_back(source_id, unbound_transform->transform(),
direction,
+ null_order);
+ return *this;
+}
+
+UpdateSortOrder& UpdateSortOrder::CaseSensitive(bool case_sensitive) {
+ case_sensitive_ = case_sensitive;
+ return *this;
+}
+
+Result<UpdateSortOrder::ApplyResult> UpdateSortOrder::Apply() {
+ ICEBERG_RETURN_UNEXPECTED(CheckErrors());
+
+ // If no sort fields are specified, return an unsorted order (ID = 0).
+ std::shared_ptr<SortOrder> order;
+ if (sort_fields_.empty()) {
+ order = SortOrder::Unsorted();
+ } else {
+ // Use kInitialSortOrderId (1) as a placeholder for non-empty sort orders.
+ // The actual sort order ID will be assigned by TableMetadataBuilder when
+ // the AddSortOrder update is applied.
+ ICEBERG_ASSIGN_OR_RAISE(
+ order, SortOrder::Make(SortOrder::kInitialSortOrderId, sort_fields_));
Review Comment:
```suggestion
order, SortOrder::Make(/*sort_id=*/-1, sort_fields_));
```
I would recommend using an invalid id by purpose.
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateSortOrder>> UpdateSortOrder::Make(
+ std::shared_ptr<Transaction> transaction) {
+ if (!transaction) [[unlikely]] {
+ return InvalidArgument("Cannot create UpdateSortOrder without a
transaction");
+ }
+ return std::shared_ptr<UpdateSortOrder>(new
UpdateSortOrder(std::move(transaction)));
+}
+
+UpdateSortOrder::UpdateSortOrder(std::shared_ptr<Transaction> transaction)
+ : PendingUpdate(std::move(transaction)) {}
+
+UpdateSortOrder::~UpdateSortOrder() = default;
+
+UpdateSortOrder& UpdateSortOrder::AddSortField(std::shared_ptr<Term> term,
+ SortDirection direction,
+ NullOrder null_order) {
+ if (!term) {
+ return AddError(ErrorKind::kInvalidArgument, "Term cannot be null");
+ }
+ if (term->kind() != Term::Kind::kTransform) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be a transform
term");
+ }
+ if (!term->is_unbound()) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be unbound");
+ }
+ // use checked-cast to get UnboundTransform
+ auto unbound_transform =
internal::checked_pointer_cast<UnboundTransform>(term);
Review Comment:
You just need to cast to NamedReference or UnboundTransform depending on the
term->kind()
##########
src/iceberg/transaction.cc:
##########
@@ -60,9 +64,26 @@ Status Transaction::AddUpdate(const
std::shared_ptr<PendingUpdate>& update) {
return {};
}
-Status Transaction::Apply(std::vector<std::unique_ptr<TableUpdate>> updates) {
- for (const auto& update : updates) {
- update->ApplyTo(*metadata_builder_);
+Status Transaction::Apply(PendingUpdate& update) {
+ switch (update.kind()) {
+ case PendingUpdate::Kind::kUpdateProperties: {
+ auto& update_properties = static_cast<UpdateProperties&>(update);
+ ICEBERG_ASSIGN_OR_RAISE(UpdateProperties::ApplyResult result,
+ update_properties.Apply());
+ metadata_builder_->SetProperties(std::move(result.updates_));
+ metadata_builder_->RemoveProperties(std::move(result.removals_));
+ if (result.format_version_.has_value()) {
+
metadata_builder_->UpgradeFormatVersion(result.format_version_.value());
+ }
+ } break;
+ case PendingUpdate::Kind::kUpdateSortOrder: {
+ auto& update_sort_order = static_cast<UpdateSortOrder&>(update);
Review Comment:
```suggestion
auto& update_sort_order =
internal::checked_cast<UpdateSortOrder&>(update);
```
##########
src/iceberg/transaction.cc:
##########
@@ -60,9 +64,26 @@ Status Transaction::AddUpdate(const
std::shared_ptr<PendingUpdate>& update) {
return {};
}
-Status Transaction::Apply(std::vector<std::unique_ptr<TableUpdate>> updates) {
- for (const auto& update : updates) {
- update->ApplyTo(*metadata_builder_);
+Status Transaction::Apply(PendingUpdate& update) {
+ switch (update.kind()) {
+ case PendingUpdate::Kind::kUpdateProperties: {
+ auto& update_properties = static_cast<UpdateProperties&>(update);
+ ICEBERG_ASSIGN_OR_RAISE(UpdateProperties::ApplyResult result,
+ update_properties.Apply());
+ metadata_builder_->SetProperties(std::move(result.updates_));
+ metadata_builder_->RemoveProperties(std::move(result.removals_));
+ if (result.format_version_.has_value()) {
+
metadata_builder_->UpgradeFormatVersion(result.format_version_.value());
+ }
+ } break;
+ case PendingUpdate::Kind::kUpdateSortOrder: {
+ auto& update_sort_order = static_cast<UpdateSortOrder&>(update);
Review Comment:
ditto
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateSortOrder>> UpdateSortOrder::Make(
+ std::shared_ptr<Transaction> transaction) {
+ if (!transaction) [[unlikely]] {
+ return InvalidArgument("Cannot create UpdateSortOrder without a
transaction");
+ }
+ return std::shared_ptr<UpdateSortOrder>(new
UpdateSortOrder(std::move(transaction)));
+}
+
+UpdateSortOrder::UpdateSortOrder(std::shared_ptr<Transaction> transaction)
+ : PendingUpdate(std::move(transaction)) {}
+
+UpdateSortOrder::~UpdateSortOrder() = default;
+
+UpdateSortOrder& UpdateSortOrder::AddSortField(std::shared_ptr<Term> term,
+ SortDirection direction,
+ NullOrder null_order) {
+ if (!term) {
+ return AddError(ErrorKind::kInvalidArgument, "Term cannot be null");
+ }
+ if (term->kind() != Term::Kind::kTransform) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be a transform
term");
+ }
+ if (!term->is_unbound()) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be unbound");
+ }
+ // use checked-cast to get UnboundTransform
+ auto unbound_transform =
internal::checked_pointer_cast<UnboundTransform>(term);
+ ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto schema,
transaction_->current().Schema());
+ ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto bound_term,
+ unbound_transform->Bind(*schema,
case_sensitive_));
+
+ int32_t source_id = bound_term->reference()->field_id();
+ sort_fields_.emplace_back(source_id, unbound_transform->transform(),
direction,
+ null_order);
+ return *this;
+}
+
+UpdateSortOrder& UpdateSortOrder::CaseSensitive(bool case_sensitive) {
+ case_sensitive_ = case_sensitive;
+ return *this;
+}
+
+Result<UpdateSortOrder::ApplyResult> UpdateSortOrder::Apply() {
+ ICEBERG_RETURN_UNEXPECTED(CheckErrors());
+
+ // If no sort fields are specified, return an unsorted order (ID = 0).
+ std::shared_ptr<SortOrder> order;
+ if (sort_fields_.empty()) {
+ order = SortOrder::Unsorted();
+ } else {
+ // Use kInitialSortOrderId (1) as a placeholder for non-empty sort orders.
+ // The actual sort order ID will be assigned by TableMetadataBuilder when
+ // the AddSortOrder update is applied.
+ ICEBERG_ASSIGN_OR_RAISE(
+ order, SortOrder::Make(SortOrder::kInitialSortOrderId, sort_fields_));
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(auto schema, transaction_->current().Schema());
Review Comment:
We don't need to get schema and validate if this is an unsorted order. Move
it to the else branch above.
##########
src/iceberg/update/update_sort_order.h:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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 <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/type_fwd.h"
+#include "iceberg/update/pending_update.h"
Review Comment:
```suggestion
#include "iceberg/iceberg_export.h"
#include "iceberg/sort_field.h"
#include "iceberg/type_fwd.h"
#include "iceberg/update/pending_update.h"
```
##########
src/iceberg/update/update_properties.cc:
##########
@@ -111,27 +110,9 @@ Result<PendingUpdate::ApplyResult>
UpdateProperties::Apply() {
ICEBERG_RETURN_UNEXPECTED(
MetricsConfig::VerifyReferencedColumns(new_properties,
*schema.value()));
}
-
- ApplyResult result;
- if (!updates_.empty()) {
-
result.updates.emplace_back(std::make_unique<table::SetProperties>(updates_));
- }
- if (!removals_.empty()) {
- for (const auto& key : removals_) {
- if (current_props.contains(key)) {
- removals.push_back(key);
- }
- }
- if (!removals.empty()) {
-
result.updates.emplace_back(std::make_unique<table::RemoveProperties>(removals));
- }
- }
- if (format_version_.has_value()) {
- result.updates.emplace_back(
-
std::make_unique<table::UpgradeFormatVersion>(format_version_.value()));
- };
-
- return result;
+ return ApplyResult{.updates_ = std::move(new_properties),
+ .removals_ = std::move(removals_),
Review Comment:
```suggestion
.removals_ = removals_,
```
Do not move away internal state, otherwise we cannot retry in the future.
##########
src/iceberg/test/update_sort_order_test.cc:
##########
@@ -0,0 +1,272 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <memory>
+#include <string>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/schema.h"
+#include "iceberg/schema_field.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/test/update_test_base.h"
+#include "iceberg/transaction.h"
+#include "iceberg/transform.h"
+
+namespace iceberg {
+
+class UpdateSortOrderTest : public UpdateTestBase {};
+
+TEST_F(UpdateSortOrderTest, AddSingleSortFieldAscending) {
+ ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateSortOrder());
+ auto ref = NamedReference::Make("x").value();
+ auto term = UnboundTransform::Make(std::move(ref),
Transform::Identity()).value();
+
+ update->AddSortField(std::move(term), SortDirection::kAscending,
NullOrder::kFirst);
+ EXPECT_THAT(update->Commit(), IsOk());
+
+ // Verify the sort order was set
+ ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_));
+ ICEBERG_UNWRAP_OR_FAIL(auto sort_order, reloaded->metadata()->SortOrder());
+ ASSERT_NE(sort_order, nullptr);
+ EXPECT_EQ(sort_order->fields().size(), 1);
+
+ const auto& field = sort_order->fields()[0];
+ EXPECT_EQ(field.source_id(), 1);
Review Comment:
You may store the result returned by `Apply` and then call `EXPECT_TQ` on
them.
##########
src/iceberg/test/update_sort_order_test.cc:
##########
@@ -0,0 +1,272 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <memory>
+#include <string>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/schema.h"
+#include "iceberg/schema_field.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/test/update_test_base.h"
+#include "iceberg/transaction.h"
+#include "iceberg/transform.h"
+
+namespace iceberg {
+
+class UpdateSortOrderTest : public UpdateTestBase {};
+
+TEST_F(UpdateSortOrderTest, AddSingleSortFieldAscending) {
+ ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateSortOrder());
+ auto ref = NamedReference::Make("x").value();
+ auto term = UnboundTransform::Make(std::move(ref),
Transform::Identity()).value();
+
+ update->AddSortField(std::move(term), SortDirection::kAscending,
NullOrder::kFirst);
+ EXPECT_THAT(update->Commit(), IsOk());
+
+ // Verify the sort order was set
+ ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_));
+ ICEBERG_UNWRAP_OR_FAIL(auto sort_order, reloaded->metadata()->SortOrder());
+ ASSERT_NE(sort_order, nullptr);
+ EXPECT_EQ(sort_order->fields().size(), 1);
+
+ const auto& field = sort_order->fields()[0];
+ EXPECT_EQ(field.source_id(), 1);
+ EXPECT_EQ(field.direction(), SortDirection::kAscending);
+ EXPECT_EQ(field.null_order(), NullOrder::kFirst);
+}
+
+TEST_F(UpdateSortOrderTest, AddSingleSortFieldDescending) {
+ ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateSortOrder());
+ auto ref = NamedReference::Make("y").value();
+ auto term = UnboundTransform::Make(std::move(ref),
Transform::Identity()).value();
+
+ update->AddSortField(std::move(term), SortDirection::kDescending,
NullOrder::kLast);
+ EXPECT_THAT(update->Commit(), IsOk());
+
+ // Verify the sort order was set
+ ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_));
+ ICEBERG_UNWRAP_OR_FAIL(auto sort_order, reloaded->metadata()->SortOrder());
+ ASSERT_NE(sort_order, nullptr);
+ EXPECT_EQ(sort_order->fields().size(), 1);
+
+ const auto& field = sort_order->fields()[0];
+ EXPECT_EQ(field.source_id(), 2);
+ EXPECT_EQ(field.direction(), SortDirection::kDescending);
+ EXPECT_EQ(field.null_order(), NullOrder::kLast);
+}
+
+TEST_F(UpdateSortOrderTest, AddMultipleSortFields) {
+ ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateSortOrder());
+ auto ref1 = NamedReference::Make("y").value();
+ auto term1 = UnboundTransform::Make(std::move(ref1),
Transform::Identity()).value();
+
+ auto ref2 = NamedReference::Make("x").value();
+ auto term2 = UnboundTransform::Make(std::move(ref2),
Transform::Identity()).value();
+
+ update->AddSortField(std::move(term1), SortDirection::kAscending,
NullOrder::kFirst)
+ .AddSortField(std::move(term2), SortDirection::kDescending,
NullOrder::kLast);
+
+ EXPECT_THAT(update->Commit(), IsOk());
+
+ // Verify the sort order was set with multiple fields
+ ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_));
+ ICEBERG_UNWRAP_OR_FAIL(auto sort_order, reloaded->metadata()->SortOrder());
+ ASSERT_NE(sort_order, nullptr);
+ EXPECT_EQ(sort_order->fields().size(), 2);
+
+ // Check first field (y field)
+ const auto& field1 = sort_order->fields()[0];
+ EXPECT_EQ(field1.source_id(), 2);
+ EXPECT_EQ(field1.direction(), SortDirection::kAscending);
+ EXPECT_EQ(field1.null_order(), NullOrder::kFirst);
+
+ // Check second field (x field)
+ const auto& field2 = sort_order->fields()[1];
+ EXPECT_EQ(field2.source_id(), 1);
+ EXPECT_EQ(field2.direction(), SortDirection::kDescending);
+ EXPECT_EQ(field2.null_order(), NullOrder::kLast);
+}
+
+TEST_F(UpdateSortOrderTest, AddSortFieldWithTruncateTransform) {
Review Comment:
These cases look so similar. Perhaps use a common function for different
inputs?
##########
src/iceberg/transaction.cc:
##########
@@ -60,9 +64,26 @@ Status Transaction::AddUpdate(const
std::shared_ptr<PendingUpdate>& update) {
return {};
}
-Status Transaction::Apply(std::vector<std::unique_ptr<TableUpdate>> updates) {
- for (const auto& update : updates) {
- update->ApplyTo(*metadata_builder_);
+Status Transaction::Apply(PendingUpdate& update) {
+ switch (update.kind()) {
+ case PendingUpdate::Kind::kUpdateProperties: {
+ auto& update_properties = static_cast<UpdateProperties&>(update);
+ ICEBERG_ASSIGN_OR_RAISE(UpdateProperties::ApplyResult result,
+ update_properties.Apply());
+ metadata_builder_->SetProperties(std::move(result.updates_));
+ metadata_builder_->RemoveProperties(std::move(result.removals_));
+ if (result.format_version_.has_value()) {
+
metadata_builder_->UpgradeFormatVersion(result.format_version_.value());
+ }
+ } break;
+ case PendingUpdate::Kind::kUpdateSortOrder: {
+ auto& update_sort_order = static_cast<UpdateSortOrder&>(update);
+ ICEBERG_ASSIGN_OR_RAISE(UpdateSortOrder::ApplyResult result,
Review Comment:
```suggestion
ICEBERG_ASSIGN_OR_RAISE(auto result,
```
##########
src/iceberg/transaction.cc:
##########
@@ -60,9 +64,26 @@ Status Transaction::AddUpdate(const
std::shared_ptr<PendingUpdate>& update) {
return {};
}
-Status Transaction::Apply(std::vector<std::unique_ptr<TableUpdate>> updates) {
- for (const auto& update : updates) {
- update->ApplyTo(*metadata_builder_);
+Status Transaction::Apply(PendingUpdate& update) {
+ switch (update.kind()) {
+ case PendingUpdate::Kind::kUpdateProperties: {
+ auto& update_properties = static_cast<UpdateProperties&>(update);
Review Comment:
```suggestion
auto& update_properties =
internal::checked_cast<UpdateProperties&>(update);
```
##########
src/iceberg/transaction.cc:
##########
@@ -60,9 +64,26 @@ Status Transaction::AddUpdate(const
std::shared_ptr<PendingUpdate>& update) {
return {};
}
-Status Transaction::Apply(std::vector<std::unique_ptr<TableUpdate>> updates) {
- for (const auto& update : updates) {
- update->ApplyTo(*metadata_builder_);
+Status Transaction::Apply(PendingUpdate& update) {
+ switch (update.kind()) {
+ case PendingUpdate::Kind::kUpdateProperties: {
+ auto& update_properties = static_cast<UpdateProperties&>(update);
+ ICEBERG_ASSIGN_OR_RAISE(UpdateProperties::ApplyResult result,
+ update_properties.Apply());
+ metadata_builder_->SetProperties(std::move(result.updates_));
Review Comment:
Check empty before applying updates and removals.
##########
src/iceberg/transaction.cc:
##########
@@ -60,9 +64,26 @@ Status Transaction::AddUpdate(const
std::shared_ptr<PendingUpdate>& update) {
return {};
}
-Status Transaction::Apply(std::vector<std::unique_ptr<TableUpdate>> updates) {
- for (const auto& update : updates) {
- update->ApplyTo(*metadata_builder_);
+Status Transaction::Apply(PendingUpdate& update) {
+ switch (update.kind()) {
+ case PendingUpdate::Kind::kUpdateProperties: {
+ auto& update_properties = static_cast<UpdateProperties&>(update);
+ ICEBERG_ASSIGN_OR_RAISE(UpdateProperties::ApplyResult result,
+ update_properties.Apply());
+ metadata_builder_->SetProperties(std::move(result.updates_));
+ metadata_builder_->RemoveProperties(std::move(result.removals_));
+ if (result.format_version_.has_value()) {
+
metadata_builder_->UpgradeFormatVersion(result.format_version_.value());
+ }
+ } break;
+ case PendingUpdate::Kind::kUpdateSortOrder: {
+ auto& update_sort_order = static_cast<UpdateSortOrder&>(update);
+ ICEBERG_ASSIGN_OR_RAISE(UpdateSortOrder::ApplyResult result,
+ update_sort_order.Apply());
+ metadata_builder_->SetDefaultSortOrder(result.sort_order_);
+ } break;
+ default:
+ return InvalidArgument("Unsupported pending update kind");
Review Comment:
```suggestion
return NotSupported("Unsupported pending update: {}",
static_cast<int>(update.kind()));
```
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateSortOrder>> UpdateSortOrder::Make(
+ std::shared_ptr<Transaction> transaction) {
+ if (!transaction) [[unlikely]] {
+ return InvalidArgument("Cannot create UpdateSortOrder without a
transaction");
+ }
+ return std::shared_ptr<UpdateSortOrder>(new
UpdateSortOrder(std::move(transaction)));
+}
+
+UpdateSortOrder::UpdateSortOrder(std::shared_ptr<Transaction> transaction)
+ : PendingUpdate(std::move(transaction)) {}
+
+UpdateSortOrder::~UpdateSortOrder() = default;
+
+UpdateSortOrder& UpdateSortOrder::AddSortField(std::shared_ptr<Term> term,
+ SortDirection direction,
+ NullOrder null_order) {
+ if (!term) {
+ return AddError(ErrorKind::kInvalidArgument, "Term cannot be null");
+ }
+ if (term->kind() != Term::Kind::kTransform) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be a transform
term");
+ }
+ if (!term->is_unbound()) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be unbound");
+ }
+ // use checked-cast to get UnboundTransform
+ auto unbound_transform =
internal::checked_pointer_cast<UnboundTransform>(term);
+ ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto schema,
transaction_->current().Schema());
+ ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto bound_term,
+ unbound_transform->Bind(*schema,
case_sensitive_));
+
+ int32_t source_id = bound_term->reference()->field_id();
+ sort_fields_.emplace_back(source_id, unbound_transform->transform(),
direction,
+ null_order);
+ return *this;
+}
+
+UpdateSortOrder& UpdateSortOrder::CaseSensitive(bool case_sensitive) {
+ case_sensitive_ = case_sensitive;
+ return *this;
+}
+
+Result<UpdateSortOrder::ApplyResult> UpdateSortOrder::Apply() {
+ ICEBERG_RETURN_UNEXPECTED(CheckErrors());
+
+ // If no sort fields are specified, return an unsorted order (ID = 0).
+ std::shared_ptr<SortOrder> order;
+ if (sort_fields_.empty()) {
+ order = SortOrder::Unsorted();
+ } else {
+ // Use kInitialSortOrderId (1) as a placeholder for non-empty sort orders.
+ // The actual sort order ID will be assigned by TableMetadataBuilder when
+ // the AddSortOrder update is applied.
+ ICEBERG_ASSIGN_OR_RAISE(
+ order, SortOrder::Make(SortOrder::kInitialSortOrderId, sort_fields_));
+ }
+
+ ICEBERG_ASSIGN_OR_RAISE(auto schema, transaction_->current().Schema());
+ ICEBERG_RETURN_UNEXPECTED(order->Validate(*schema));
+ return ApplyResult{std::move(order)};
Review Comment:
```suggestion
return {std::move(order)};
```
##########
src/iceberg/update/update_sort_order.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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/update/update_sort_order.h"
+
+#include <cstdint>
+#include <memory>
+#include <vector>
+
+#include "iceberg/expression/term.h"
+#include "iceberg/result.h"
+#include "iceberg/sort_field.h"
+#include "iceberg/sort_order.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/checked_cast.h"
+#include "iceberg/util/error_collector.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateSortOrder>> UpdateSortOrder::Make(
+ std::shared_ptr<Transaction> transaction) {
+ if (!transaction) [[unlikely]] {
+ return InvalidArgument("Cannot create UpdateSortOrder without a
transaction");
+ }
+ return std::shared_ptr<UpdateSortOrder>(new
UpdateSortOrder(std::move(transaction)));
+}
+
+UpdateSortOrder::UpdateSortOrder(std::shared_ptr<Transaction> transaction)
+ : PendingUpdate(std::move(transaction)) {}
+
+UpdateSortOrder::~UpdateSortOrder() = default;
+
+UpdateSortOrder& UpdateSortOrder::AddSortField(std::shared_ptr<Term> term,
+ SortDirection direction,
+ NullOrder null_order) {
+ if (!term) {
+ return AddError(ErrorKind::kInvalidArgument, "Term cannot be null");
+ }
+ if (term->kind() != Term::Kind::kTransform) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be a transform
term");
+ }
+ if (!term->is_unbound()) {
+ return AddError(ErrorKind::kInvalidArgument, "Term must be unbound");
+ }
Review Comment:
BTW, I think `term->kind()` can also be `kReference` which is regarded as
identity transform.
--
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]