zeroshade commented on code in PR #2698:
URL: https://github.com/apache/iceberg-go/pull/2698#discussion_r4721967793


##########
catalog/catalogtest/create_table_opts.go:
##########
@@ -0,0 +1,104 @@
+// 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 catalogtest
+
+import (
+       "context"
+       "testing"
+
+       "github.com/apache/iceberg-go"
+       "github.com/apache/iceberg-go/catalog"
+       "github.com/apache/iceberg-go/table"
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+// sourceName returns the name of the column a partition or sort field reads
+// from. Catalogs assign fresh field IDs on create, so fields are matched to
+// what was passed by column name rather than by ID.
+func sourceName(t *testing.T, schema *iceberg.Schema, sourceIDs []int) string {
+       t.Helper()
+
+       require.Len(t, sourceIDs, 1)
+       name, ok := schema.FindColumnName(sourceIDs[0])
+       require.Truef(t, ok, "source ID %d should be in the schema", 
sourceIDs[0])
+
+       return name
+}
+
+// testCreateTableWithPartitionSpec asserts that the partition spec passed at
+// create time is the table's spec on a fresh load, allowing for reassigned
+// spec, field and source IDs.
+func testCreateTableWithPartitionSpec(t *testing.T, cfg Config) {
+       ctx := context.Background()
+       cat := cfg.NewCatalog(t)
+
+       spec, err := iceberg.NewPartitionSpecOpts(
+               iceberg.AddPartitionFieldByName("id", "id_bucket", 
iceberg.BucketTransform{NumBuckets: 16}, Schema, nil),
+               iceberg.AddPartitionFieldByName("data", "data", 
iceberg.IdentityTransform{}, Schema, nil),
+       )
+       require.NoError(t, err)
+
+       ident, _ := createTable(t, cat, catalog.WithPartitionSpec(&spec))
+
+       tbl, err := cat.LoadTable(ctx, ident)
+       require.NoError(t, err)
+
+       loaded := tbl.Spec()

Review Comment:
   nit: `NewPartitionSpecOpts` already produces spec ID 0 and field IDs 
1000/1001, so the fresh-ID assignment isn't exercised here. Consider asserting 
`loaded.ID() == iceberg.InitialPartitionSpecID` and that partition `FieldID`s 
start at `iceberg.PartitionDataIDStart` (ideally with a non-fresh spec ID 
passed in).



##########
catalog/catalogtest/create_table_opts.go:
##########
@@ -0,0 +1,104 @@
+// 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 catalogtest
+
+import (
+       "context"
+       "testing"
+
+       "github.com/apache/iceberg-go"
+       "github.com/apache/iceberg-go/catalog"
+       "github.com/apache/iceberg-go/table"
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+// sourceName returns the name of the column a partition or sort field reads
+// from. Catalogs assign fresh field IDs on create, so fields are matched to
+// what was passed by column name rather than by ID.
+func sourceName(t *testing.T, schema *iceberg.Schema, sourceIDs []int) string {
+       t.Helper()
+
+       require.Len(t, sourceIDs, 1)
+       name, ok := schema.FindColumnName(sourceIDs[0])
+       require.Truef(t, ok, "source ID %d should be in the schema", 
sourceIDs[0])
+
+       return name
+}
+
+// testCreateTableWithPartitionSpec asserts that the partition spec passed at
+// create time is the table's spec on a fresh load, allowing for reassigned
+// spec, field and source IDs.
+func testCreateTableWithPartitionSpec(t *testing.T, cfg Config) {
+       ctx := context.Background()
+       cat := cfg.NewCatalog(t)
+
+       spec, err := iceberg.NewPartitionSpecOpts(
+               iceberg.AddPartitionFieldByName("id", "id_bucket", 
iceberg.BucketTransform{NumBuckets: 16}, Schema, nil),
+               iceberg.AddPartitionFieldByName("data", "data", 
iceberg.IdentityTransform{}, Schema, nil),
+       )
+       require.NoError(t, err)
+
+       ident, _ := createTable(t, cat, catalog.WithPartitionSpec(&spec))
+
+       tbl, err := cat.LoadTable(ctx, ident)
+       require.NoError(t, err)
+
+       loaded := tbl.Spec()
+       require.Equal(t, spec.NumFields(), loaded.NumFields(), "loaded spec 
should have the fields it was created with")
+       for i := range spec.NumFields() {
+               want, got := spec.Field(i), loaded.Field(i)
+               assert.Equal(t, want.Name, got.Name)
+               assert.Equal(t, want.Transform, got.Transform)
+               assert.Equal(t, sourceName(t, Schema, want.SourceIDs), 
sourceName(t, tbl.Schema(), got.SourceIDs))
+       }
+}
+
+// testCreateTableWithSortOrder asserts that the sort order passed at create
+// time is the table's sort order on a fresh load, allowing for reassigned
+// order and source IDs.
+func testCreateTableWithSortOrder(t *testing.T, cfg Config) {
+       ctx := context.Background()
+       cat := cfg.NewCatalog(t)
+
+       order, err := table.NewSortOrder(1, []table.SortField{

Review Comment:
   nit: order ID 1 is already `table.InitialSortOrderID`, so a catalog that 
keeps the caller's order ID would still pass. Passing e.g. `5` and asserting 
`loaded.OrderID() == table.InitialSortOrderID` would cover reassignment.



##########
catalog/catalogtest/create_table_opts.go:
##########
@@ -0,0 +1,104 @@
+// 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 catalogtest
+
+import (
+       "context"
+       "testing"
+
+       "github.com/apache/iceberg-go"
+       "github.com/apache/iceberg-go/catalog"
+       "github.com/apache/iceberg-go/table"
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+// sourceName returns the name of the column a partition or sort field reads
+// from. Catalogs assign fresh field IDs on create, so fields are matched to
+// what was passed by column name rather than by ID.
+func sourceName(t *testing.T, schema *iceberg.Schema, sourceIDs []int) string {
+       t.Helper()
+
+       require.Len(t, sourceIDs, 1)
+       name, ok := schema.FindColumnName(sourceIDs[0])
+       require.Truef(t, ok, "source ID %d should be in the schema", 
sourceIDs[0])
+
+       return name
+}
+
+// testCreateTableWithPartitionSpec asserts that the partition spec passed at
+// create time is the table's spec on a fresh load, allowing for reassigned
+// spec, field and source IDs.
+func testCreateTableWithPartitionSpec(t *testing.T, cfg Config) {

Review Comment:
   nit: `testCreateTableWithProperties` lives in `commits.go`; might be worth 
moving it here so the create-option tests sit together.



##########
catalog/catalogtest/create_table_opts.go:
##########
@@ -0,0 +1,104 @@
+// 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 catalogtest
+
+import (
+       "context"
+       "testing"
+
+       "github.com/apache/iceberg-go"
+       "github.com/apache/iceberg-go/catalog"
+       "github.com/apache/iceberg-go/table"
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+// sourceName returns the name of the column a partition or sort field reads
+// from. Catalogs assign fresh field IDs on create, so fields are matched to
+// what was passed by column name rather than by ID.
+func sourceName(t *testing.T, schema *iceberg.Schema, sourceIDs []int) string {
+       t.Helper()
+
+       require.Len(t, sourceIDs, 1)
+       name, ok := schema.FindColumnName(sourceIDs[0])
+       require.Truef(t, ok, "source ID %d should be in the schema", 
sourceIDs[0])
+
+       return name
+}
+
+// testCreateTableWithPartitionSpec asserts that the partition spec passed at
+// create time is the table's spec on a fresh load, allowing for reassigned
+// spec, field and source IDs.
+func testCreateTableWithPartitionSpec(t *testing.T, cfg Config) {
+       ctx := context.Background()
+       cat := cfg.NewCatalog(t)
+
+       spec, err := iceberg.NewPartitionSpecOpts(
+               iceberg.AddPartitionFieldByName("id", "id_bucket", 
iceberg.BucketTransform{NumBuckets: 16}, Schema, nil),
+               iceberg.AddPartitionFieldByName("data", "data", 
iceberg.IdentityTransform{}, Schema, nil),
+       )
+       require.NoError(t, err)
+
+       ident, _ := createTable(t, cat, catalog.WithPartitionSpec(&spec))
+
+       tbl, err := cat.LoadTable(ctx, ident)
+       require.NoError(t, err)
+
+       loaded := tbl.Spec()
+       require.Equal(t, spec.NumFields(), loaded.NumFields(), "loaded spec 
should have the fields it was created with")
+       for i := range spec.NumFields() {
+               want, got := spec.Field(i), loaded.Field(i)
+               assert.Equal(t, want.Name, got.Name)
+               assert.Equal(t, want.Transform, got.Transform)
+               assert.Equal(t, sourceName(t, Schema, want.SourceIDs), 
sourceName(t, tbl.Schema(), got.SourceIDs))
+       }
+}
+
+// testCreateTableWithSortOrder asserts that the sort order passed at create
+// time is the table's sort order on a fresh load, allowing for reassigned
+// order and source IDs.
+func testCreateTableWithSortOrder(t *testing.T, cfg Config) {
+       ctx := context.Background()
+       cat := cfg.NewCatalog(t)
+
+       order, err := table.NewSortOrder(1, []table.SortField{
+               {SourceIDs: []int{4}, Transform: iceberg.IdentityTransform{}, 
Direction: table.SortASC, NullOrder: table.NullsFirst},
+               {SourceIDs: []int{3}, Transform: 
iceberg.BucketTransform{NumBuckets: 16}, Direction: table.SortDESC, NullOrder: 
table.NullsLast},
+       })
+       require.NoError(t, err)
+
+       ident, _ := createTable(t, cat, catalog.WithSortOrder(order))
+
+       tbl, err := cat.LoadTable(ctx, ident)
+       require.NoError(t, err)
+
+       loaded := tbl.SortOrder()
+       require.False(t, loaded.IsUnsorted(), "loaded table should be sorted")
+       require.Equal(t, order.Len(), loaded.Len(), "loaded sort order should 
have the fields it was created with")
+
+       want := make([]table.SortField, 0, order.Len())

Review Comment:
   nit: the `want` slice isn't needed, `SortOrder.Field(i)` exists:
   ```go
   for i, got := range loaded.Fields() {
        want := order.Field(i)
        ...
   }
   ```



-- 
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]

Reply via email to