tanmayrauth commented on code in PR #1328: URL: https://github.com/apache/iceberg-go/pull/1328#discussion_r3486515808
########## table/transaction_internal_test.go: ########## @@ -0,0 +1,111 @@ +// 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. + +package table + +import ( + "testing" + "time" + + "github.com/apache/iceberg-go" + "github.com/stretchr/testify/require" +) + +func TestTransactionApplyKeepsDistinctRequirementsOfSameType(t *testing.T) { + txn := newTransactionWithSnapshotRefs(t) + + mainSnapshotID := int64(10) + featureSnapshotID := int64(20) + + err := txn.apply(nil, []Requirement{ + AssertRefSnapshotID(MainBranch, &mainSnapshotID), + AssertRefSnapshotID("feature", &featureSnapshotID), + }) + require.NoError(t, err) + + require.Len(t, txn.reqs, 2) + requireContainsRequirement(t, txn.reqs, AssertRefSnapshotID(MainBranch, &mainSnapshotID)) + requireContainsRequirement(t, txn.reqs, AssertRefSnapshotID("feature", &featureSnapshotID)) +} + +func TestTransactionApplyDedupesEquivalentRequirementsWithinAndAcrossCalls(t *testing.T) { + txn := newTransactionWithSnapshotRefs(t) + + mainSnapshotID := int64(10) + first := AssertRefSnapshotID(MainBranch, &mainSnapshotID) + second := AssertRefSnapshotID(MainBranch, &mainSnapshotID) + + err := txn.apply(nil, []Requirement{first, second}) + require.NoError(t, err) + require.Len(t, txn.reqs, 1) + requireContainsRequirement(t, txn.reqs, first) + + err = txn.apply(nil, []Requirement{AssertRefSnapshotID(MainBranch, &mainSnapshotID)}) + require.NoError(t, err) + require.Len(t, txn.reqs, 1) + requireContainsRequirement(t, txn.reqs, first) +} + +func newTransactionWithSnapshotRefs(t *testing.T) *Transaction { + t.Helper() + + txn, _ := createTestTransactionWithMemIO(t, *iceberg.UnpartitionedSpec) + now := time.Now().UnixMilli() + + require.NoError(t, txn.meta.AddSnapshot(&Snapshot{ + SnapshotID: 10, + SequenceNumber: 1, + ManifestList: "mem://default/table-location/metadata/manifest-10.avro", + Summary: &Summary{Operation: OpAppend}, + TimestampMs: now, + })) + + require.NoError(t, txn.meta.AddSnapshot(&Snapshot{ + SnapshotID: 20, + ParentSnapshotID: transactionTestPtr(int64(10)), + SequenceNumber: 2, + ManifestList: "mem://default/table-location/metadata/manifest-20.avro", + Summary: &Summary{Operation: OpAppend}, + TimestampMs: now + 1, + })) + + require.NoError(t, txn.meta.SetSnapshotRef(MainBranch, 10, BranchRef)) + require.NoError(t, txn.meta.SetSnapshotRef("feature", 20, BranchRef)) + + return txn +} + +func requireContainsRequirement(t *testing.T, requirements []Requirement, expected Requirement) { Review Comment: requireContainsRequirement asserts membership via requirementSemanticKey — the same function the production change uses. If that function had a bug, both sides would agree and the test would still pass. Comparing on the concrete fields (ref + snapshot id) instead would make the assertion independent of the code under test. ########## table/transaction.go: ########## @@ -142,6 +153,15 @@ func (t *Transaction) apply(updates []Update, reqs []Requirement) error { return nil } +func requirementSemanticKey(r Requirement) (string, error) { + data, err := json.Marshal(r) Review Comment: The dedupe key relies on encoding/json being deterministic. That holds for all current requirement types, fields serialize in declaration order and none contain maps — but a future requirement that adds a map field would silently break dedupe (Go sorts map keys, so it'd still be stable... actually that's fine — the real risk is any field whose marshaling isn't canonical). Could you add a one-line doc comment on requirementSemanticKey noting it assumes canonical/deterministic marshaling, so the constraint is visible to whoever adds the next Requirement type? -- 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]
