This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/terraform-provider-iceberg.git
The following commit(s) were added to refs/heads/main by this push:
new fb6181b Reject Field ID Reuse (#135)
fb6181b is described below
commit fb6181b549a16c3e35dc0d49bb4414d336bf3f3f
Author: Alex Stephen <[email protected]>
AuthorDate: Wed Nov 18 13:19:12 2026 -0800
Reject Field ID Reuse (#135)
reject field id reuse
---
internal/provider/resource_table.go | 5 ++
internal/provider/resource_table_test.go | 79 +++++++++++++++++++++++++++++
internal/provider/table_schema.go | 40 +++++++++++++++
internal/provider/table_schema_test.go | 85 ++++++++++++++++++++++++++++++++
4 files changed, 209 insertions(+)
diff --git a/internal/provider/resource_table.go
b/internal/provider/resource_table.go
index e0b0ab9..b88121c 100644
--- a/internal/provider/resource_table.go
+++ b/internal/provider/resource_table.go
@@ -493,6 +493,11 @@ func (r *icebergTableResource) calculateSchemaUpdates(ctx
context.Context, plan,
return nil
}
+ if err := validateNewFieldIDs(tbl.Metadata(), planIceberg); err != nil {
+ diags.AddError("invalid field id", err.Error())
+
+ return nil
+ }
// Find the highest existing schema ID to ensure the new one is unique.
maxSchemaID := int64(0)
diff --git a/internal/provider/resource_table_test.go
b/internal/provider/resource_table_test.go
index 20b9457..30cd017 100644
--- a/internal/provider/resource_table_test.go
+++ b/internal/provider/resource_table_test.go
@@ -478,6 +478,85 @@ func TestAccIcebergTableReservedFieldIDs(t *testing.T) {
})
}
+func testAccIcebergTableSecondFieldConfig(providerCfg, tableName, fieldName,
fieldID string) string {
+ second := ""
+ if fieldName != "" {
+ id := ""
+ if fieldID != "" {
+ id = "id = " + fieldID
+ }
+ second = fmt.Sprintf(`,
+ {
+ %s
+ name = "%s"
+ type = "string"
+ required = false
+ }`, id, fieldName)
+ }
+
+ return providerCfg + fmt.Sprintf(`
+resource "iceberg_namespace" "db_dropped" {
+ name = ["db_dropped"]
+}
+
+resource "iceberg_table" "test" {
+ namespace = iceberg_namespace.db_dropped.name
+ name = "%s"
+ schema = {
+ fields = [
+ {
+ id = 1
+ name = "id"
+ type = "long"
+ required = true
+ }%s
+ ]
+ }
+}
+`, tableName, second)
+}
+
+func TestAccIcebergTableDroppedFieldIDReuse(t *testing.T) {
+ catalogURI := os.Getenv("ICEBERG_CATALOG_URI")
+ if catalogURI == "" {
+ catalogURI = "http://localhost:8181"
+ }
+
+ providerCfg := fmt.Sprintf(providerConfig, catalogURI)
+ tableName := "dropped_ids_table"
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
+ Steps: []resource.TestStep{
+ {
+ Config:
testAccIcebergTableSecondFieldConfig(providerCfg, tableName, "dropped", "2"),
+ Check:
resource.TestCheckResourceAttr("iceberg_table.test", "schema.fields.1.id", "2"),
+ },
+ {
+ Config:
testAccIcebergTableSecondFieldConfig(providerCfg, tableName, "", ""),
+ Check:
resource.TestCheckResourceAttr("iceberg_table.test", "schema.fields.#", "1"),
+ },
+ {
+ Config:
testAccIcebergTableSecondFieldConfig(providerCfg, tableName, "replacement",
"2"),
+ ExpectError: regexp.MustCompile(`(?s)invalid
field id.*belonged to dropped field "dropped"`),
+ },
+ {
+ // Re-adding the dropped field by name doesn't
restore its id either.
+ Config:
testAccIcebergTableSecondFieldConfig(providerCfg, tableName, "dropped", "2"),
+ ExpectError: regexp.MustCompile(`(?s)invalid
field id.*belonged to dropped field "dropped"`),
+ },
+ {
+ Config:
testAccIcebergTableSecondFieldConfig(providerCfg, tableName, "replacement", ""),
+ Check: resource.ComposeTestCheckFunc(
+
resource.TestCheckResourceAttr("iceberg_table.test", "schema.fields.1.name",
"replacement"),
+
resource.TestCheckResourceAttr("iceberg_table.test", "schema.fields.1.id", "3"),
+ ),
+ },
+ },
+ })
+}
+
func TestAccIcebergTableInvalidEvolution(t *testing.T) {
catalogURI := os.Getenv("ICEBERG_CATALOG_URI")
if catalogURI == "" {
diff --git a/internal/provider/table_schema.go
b/internal/provider/table_schema.go
index c9c02e7..939e78e 100644
--- a/internal/provider/table_schema.go
+++ b/internal/provider/table_schema.go
@@ -18,6 +18,8 @@ package provider
import (
"encoding/json"
"fmt"
+ "maps"
+ "slices"
"strings"
"github.com/apache/iceberg-go"
@@ -321,6 +323,44 @@ func validateSchemaEvolution(current, planned
*iceberg.Schema) error {
return nil
}
+// validateNewFieldIDs rejects a planned field whose id is not in the current
+// schema but was already assigned, by a past schema or up to last-column-id.
+func validateNewFieldIDs(metadata table.Metadata, planned *iceberg.Schema)
error {
+ currentByID, err := iceberg.IndexByID(metadata.CurrentSchema())
+ if err != nil {
+ return err
+ }
+ plannedNames, err := iceberg.IndexNameByID(planned)
+ if err != nil {
+ return err
+ }
+
+ pastNames := map[int]string{}
+ for _, s := range metadata.Schemas() {
+ names, err := iceberg.IndexNameByID(s)
+ if err != nil {
+ return err
+ }
+ maps.Copy(pastNames, names)
+ }
+
+ lastColumnID := metadata.LastColumnID()
+ for _, id := range slices.Sorted(maps.Keys(plannedNames)) {
+ if _, ok := currentByID[id]; ok {
+ continue
+ }
+ name := plannedNames[id]
+ if past, ok := pastNames[id]; ok {
+ return fmt.Errorf("field %q has id %d, which belonged
to dropped field %q; ids of dropped fields cannot be reused", name, id, past)
+ }
+ if id <= lastColumnID {
+ return fmt.Errorf("field %q has id %d, which the table
has already assigned; new fields need ids above the last-column-id %d", name,
id, lastColumnID)
+ }
+ }
+
+ return nil
+}
+
func validateFieldEvolution(cur, plan iceberg.NestedField) error {
if !cur.Required && plan.Required {
return fmt.Errorf("field %q (id %d): changing a field from
optional to required is not an allowed schema evolution", cur.Name, cur.ID)
diff --git a/internal/provider/table_schema_test.go
b/internal/provider/table_schema_test.go
index 4d71d73..1f97173 100644
--- a/internal/provider/table_schema_test.go
+++ b/internal/provider/table_schema_test.go
@@ -22,6 +22,7 @@ import (
"testing"
"github.com/apache/iceberg-go"
+ "github.com/apache/iceberg-go/table"
"github.com/hashicorp/terraform-plugin-framework/types"
)
@@ -287,3 +288,87 @@ func TestPartitionSpecSourceIDsRoundTrip(t *testing.T) {
})
}
}
+
+func TestValidateNewFieldIDs(t *testing.T) {
+ // last-column-id 5 is above any id a schema used.
+ metadata, err := table.ParseMetadataString(`{
+ "format-version": 2,
+ "table-uuid": "9c12d441-03fe-4693-9a96-a0705ddf69c1",
+ "location": "s3://warehouse/table",
+ "last-sequence-number": 0,
+ "last-updated-ms": 1700000000000,
+ "last-column-id": 5,
+ "current-schema-id": 1,
+ "schemas": [
+ {"type": "struct", "schema-id": 0, "fields": [
+ {"id": 1, "name": "id", "required": true,
"type": "long"},
+ {"id": 2, "name": "dropped", "required": false,
"type": "string"},
+ {"id": 3, "name": "tags", "required": false,
"type": {
+ "type": "list", "element-id": 4,
"element": "string", "element-required": false}}
+ ]},
+ {"type": "struct", "schema-id": 1, "fields": [
+ {"id": 1, "name": "id", "required": true,
"type": "long"}
+ ]}
+ ],
+ "default-spec-id": 0,
+ "partition-specs": [{"spec-id": 0, "fields": []}],
+ "last-partition-id": 999,
+ "default-sort-order-id": 0,
+ "sort-orders": [{"order-id": 0, "fields": []}]
+ }`)
+ if err != nil {
+ t.Fatalf("parse metadata: %v", err)
+ }
+
+ id := iceberg.NestedField{ID: 1, Name: "id", Type:
iceberg.PrimitiveTypes.Int64, Required: true}
+ withField := func(f iceberg.NestedField) *iceberg.Schema {
+ return iceberg.NewSchema(2, id, f)
+ }
+
+ tests := []struct {
+ name string
+ planned *iceberg.Schema
+ wantErr string
+ }{
+ {name: "unchanged", planned: iceberg.NewSchema(2, id)},
+ {
+ name: "new id above last-column-id",
+ planned: withField(iceberg.NestedField{ID: 6, Name:
"added", Type: iceberg.PrimitiveTypes.String}),
+ },
+ {
+ name: "dropped field id",
+ planned: withField(iceberg.NestedField{ID: 2, Name:
"replacement", Type: iceberg.PrimitiveTypes.String}),
+ wantErr: `field "replacement" has id 2, which belonged
to dropped field "dropped"`,
+ },
+ {
+ name: "dropped list element id",
+ planned: withField(iceberg.NestedField{ID: 4, Name:
"replacement", Type: iceberg.PrimitiveTypes.String}),
+ wantErr: `which belonged to dropped field
"tags.element"`,
+ },
+ {
+ name: "assigned id never in a schema",
+ planned: withField(iceberg.NestedField{ID: 5, Name:
"replacement", Type: iceberg.PrimitiveTypes.String}),
+ wantErr: "new fields need ids above the last-column-id
5",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := validateNewFieldIDs(metadata, tt.planned)
+ if tt.wantErr == "" {
+ if err != nil {
+ t.Fatalf("expected no error, got %v",
err)
+ }
+
+ return
+ }
+
+ if err == nil {
+ t.Fatalf("expected error containing %q, got
nil", tt.wantErr)
+ }
+ if !strings.Contains(err.Error(), tt.wantErr) {
+ t.Errorf("expected error containing %q, got
%v", tt.wantErr, err)
+ }
+ })
+ }
+}