JingsongLi commented on code in PR #14:
URL: 
https://github.com/apache/terraform-provider-paimon/pull/14#discussion_r3893322440


##########
internal/provider/resource_table.go:
##########
@@ -80,6 +84,73 @@ func (r *tableResource) Configure(_ context.Context, req 
resource.ConfigureReque
        clientFromProviderData(req.ProviderData, &r.client, &resp.Diagnostics, 
"paimon_table resource")
 }
 
+func (r *tableResource) ModifyPlan(ctx context.Context, req 
resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
+       if req.State.Raw.IsNull() || req.Plan.Raw.IsNull() {
+               return
+       }
+
+       var config, state, plan tableResourceModel
+       resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
+       resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
+       resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
+       if resp.Diagnostics.HasError() {
+               return
+       }
+
+       var configuredFields, stateFields, plannedFields []tableFieldModel
+       resp.Diagnostics.Append(config.Fields.ElementsAs(ctx, 
&configuredFields, false)...)
+       resp.Diagnostics.Append(state.Fields.ElementsAs(ctx, &stateFields, 
false)...)
+       resp.Diagnostics.Append(plan.Fields.ElementsAs(ctx, &plannedFields, 
false)...)
+       if resp.Diagnostics.HasError() {
+               return
+       }
+       if len(configuredFields) != len(plannedFields) {
+               resp.Diagnostics.AddError("Unable to stabilize Paimon field 
identities", "The configured and planned field lists have different lengths. 
Please report this issue to the provider developers.")
+
+               return
+       }
+
+       stabilizePlannedFieldIdentities(configuredFields, stateFields, 
plannedFields)
+       plan.Fields = fieldsValueFromModels(ctx, plannedFields, 
&resp.Diagnostics)
+       if resp.Diagnostics.HasError() {
+               return
+       }
+       resp.Diagnostics.Append(resp.Plan.Set(ctx, &plan)...)
+       if compositeFieldTypesRequireReplace(stateFields, plannedFields) {

Review Comment:
   Fixed in 3966f9c. ModifyPlan now requires replacement when a retained 
primary-key or partition-key field changes type; helper and Terraform 
acceptance tests cover the boundary.



##########
internal/provider/table_schema_changes.go:
##########
@@ -0,0 +1,267 @@
+// 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 provider
+
+import (
+       "errors"
+       "fmt"
+       "slices"
+       "strings"
+
+       "github.com/apache/terraform-provider-paimon/internal/client"
+)
+
+func assignTemporaryIDsToNewFields(before []client.Field, planned 
[]tableFieldModel, after []client.Field) error {
+       if len(planned) != len(after) {
+               return errors.New("the planned field models and converted 
schema have different lengths")
+       }
+
+       used := make(map[int]struct{})
+       next := 0
+       reserve := func(id int) {
+               used[id] = struct{}{}
+               if id >= next {
+                       next = id + 1
+               }
+       }
+       for _, field := range before {
+               reserve(field.ID)
+               for _, nestedID := range field.NestedFieldIDs {
+                       reserve(nestedID)
+               }
+       }
+       for index, field := range planned {
+               if !field.ID.IsNull() && !field.ID.IsUnknown() {
+                       reserve(int(field.ID.ValueInt64()))
+               }
+               for _, nestedID := range after[index].NestedFieldIDs {
+                       reserve(nestedID)
+               }
+       }
+
+       for index, field := range planned {
+               if !field.ID.IsNull() && !field.ID.IsUnknown() {
+                       continue
+               }
+               for {
+                       if _, exists := used[next]; !exists {
+                               break
+                       }
+                       next++
+               }
+               if next > maxPaimonFieldID {
+                       return errors.New("no Paimon field IDs remain for a 
newly added field")
+               }
+               after[index].ID = next
+               reserve(next)
+       }
+
+       return nil
+}
+
+func tableFieldSchemaChanges(before, after []client.Field) 
([]client.SchemaChange, error) {
+       beforeByID, err := fieldsByID(before)
+       if err != nil {
+               return nil, err
+       }
+       afterByID, err := fieldsByID(after)
+       if err != nil {
+               return nil, err
+       }
+
+       changes := make([]client.SchemaChange, 0)
+       for _, previous := range before {
+               planned, exists := afterByID[previous.ID]
+               if !exists {
+                       continue
+               }
+               previousType, previousNullable := splitFieldType(previous.Type)
+               plannedType, plannedNullable := splitFieldType(planned.Type)
+               path := []string{previous.Name}
+               if !client.EquivalentDataTypes(previousType, plannedType) {
+                       plannedTypeField := planned
+                       plannedTypeField.Type = plannedType
+                       changes = append(changes, client.SchemaChange{
+                               "action":          "updateColumnType",
+                               "fieldNames":      path,
+                               "newDataType":     
schemaChangeDataType(plannedTypeField, before, previous.ID),
+                               "keepNullability": true,
+                       })
+               }
+               if previousNullable != plannedNullable {
+                       changes = append(changes, client.SchemaChange{
+                               "action":         "updateColumnNullability",
+                               "fieldNames":     path,
+                               "newNullability": plannedNullable,
+                       })
+               }
+               if !stringPointersEqual(previous.Description, 
planned.Description) {
+                       changes = append(changes, client.SchemaChange{
+                               "action":     "updateColumnComment",
+                               "fieldNames": path,
+                               "newComment": planned.Description,
+                       })
+               }
+               if !stringPointersEqual(previous.DefaultValue, 
planned.DefaultValue) {
+                       changes = append(changes, client.SchemaChange{
+                               "action":          "updateColumnDefaultValue",
+                               "fieldNames":      path,
+                               "newDefaultValue": planned.DefaultValue,
+                       })
+               }
+       }
+
+       for _, previous := range before {
+               if _, exists := afterByID[previous.ID]; !exists {
+                       changes = append(changes, client.SchemaChange{"action": 
"dropColumn", "fieldNames": []string{previous.Name}})
+               }
+       }
+
+       retainedNames := make(map[string]int)
+       pendingRenames := make(map[string]client.Field)
+       for _, previous := range before {
+               if planned, exists := afterByID[previous.ID]; exists {
+                       retainedNames[previous.Name] = planned.ID
+                       if previous.Name != planned.Name {
+                               pendingRenames[previous.Name] = planned
+                       }
+               }
+       }
+       for len(pendingRenames) > 0 {
+               progress := false
+               for _, previous := range before {
+                       planned, pending := pendingRenames[previous.Name]
+                       if !pending {
+                               continue
+                       }
+                       if conflictingID, conflict := 
retainedNames[planned.Name]; conflict && conflictingID != planned.ID {
+                               continue
+                       }
+                       changes = append(changes, client.SchemaChange{
+                               "action":     "renameColumn",
+                               "fieldNames": []string{previous.Name},
+                               "newName":    planned.Name,
+                       })
+                       delete(retainedNames, previous.Name)
+                       retainedNames[planned.Name] = planned.ID
+                       delete(pendingRenames, previous.Name)
+                       progress = true
+               }
+               if !progress {
+                       return nil, errors.New("cannot apply a cycle of table 
field renames in one apply; use an intermediate name")
+               }
+       }
+
+       for _, planned := range after {
+               if _, exists := beforeByID[planned.ID]; exists {
+                       continue
+               }
+               changes = append(changes, client.SchemaChange{
+                       "action":     "addColumn",
+                       "fieldNames": []string{planned.Name},
+                       "dataType":   schemaChangeDataType(planned, before, -1),

Review Comment:
   Fixed in 3966f9c. Newly added fields configured as nullable=false or with a 
NOT NULL type now require table replacement, with unit and Terraform acceptance 
coverage.



##########
internal/provider/table_schema_changes.go:
##########
@@ -0,0 +1,267 @@
+// 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 provider
+
+import (
+       "errors"
+       "fmt"
+       "slices"
+       "strings"
+
+       "github.com/apache/terraform-provider-paimon/internal/client"
+)
+
+func assignTemporaryIDsToNewFields(before []client.Field, planned 
[]tableFieldModel, after []client.Field) error {
+       if len(planned) != len(after) {
+               return errors.New("the planned field models and converted 
schema have different lengths")
+       }
+
+       used := make(map[int]struct{})
+       next := 0
+       reserve := func(id int) {
+               used[id] = struct{}{}
+               if id >= next {
+                       next = id + 1
+               }
+       }
+       for _, field := range before {
+               reserve(field.ID)
+               for _, nestedID := range field.NestedFieldIDs {
+                       reserve(nestedID)
+               }
+       }
+       for index, field := range planned {
+               if !field.ID.IsNull() && !field.ID.IsUnknown() {
+                       reserve(int(field.ID.ValueInt64()))
+               }
+               for _, nestedID := range after[index].NestedFieldIDs {
+                       reserve(nestedID)
+               }
+       }
+
+       for index, field := range planned {
+               if !field.ID.IsNull() && !field.ID.IsUnknown() {
+                       continue
+               }
+               for {
+                       if _, exists := used[next]; !exists {
+                               break
+                       }
+                       next++
+               }
+               if next > maxPaimonFieldID {
+                       return errors.New("no Paimon field IDs remain for a 
newly added field")
+               }
+               after[index].ID = next
+               reserve(next)
+       }
+
+       return nil
+}
+
+func tableFieldSchemaChanges(before, after []client.Field) 
([]client.SchemaChange, error) {
+       beforeByID, err := fieldsByID(before)
+       if err != nil {
+               return nil, err
+       }
+       afterByID, err := fieldsByID(after)
+       if err != nil {
+               return nil, err
+       }
+
+       changes := make([]client.SchemaChange, 0)
+       for _, previous := range before {
+               planned, exists := afterByID[previous.ID]
+               if !exists {
+                       continue
+               }
+               previousType, previousNullable := splitFieldType(previous.Type)
+               plannedType, plannedNullable := splitFieldType(planned.Type)
+               path := []string{previous.Name}
+               if !client.EquivalentDataTypes(previousType, plannedType) {
+                       plannedTypeField := planned
+                       plannedTypeField.Type = plannedType
+                       changes = append(changes, client.SchemaChange{
+                               "action":          "updateColumnType",
+                               "fieldNames":      path,
+                               "newDataType":     
schemaChangeDataType(plannedTypeField, before, previous.ID),
+                               "keepNullability": true,
+                       })
+               }
+               if previousNullable != plannedNullable {
+                       changes = append(changes, client.SchemaChange{
+                               "action":         "updateColumnNullability",
+                               "fieldNames":     path,
+                               "newNullability": plannedNullable,
+                       })
+               }
+               if !stringPointersEqual(previous.Description, 
planned.Description) {
+                       changes = append(changes, client.SchemaChange{
+                               "action":     "updateColumnComment",
+                               "fieldNames": path,
+                               "newComment": planned.Description,
+                       })
+               }
+               if !stringPointersEqual(previous.DefaultValue, 
planned.DefaultValue) {
+                       changes = append(changes, client.SchemaChange{
+                               "action":          "updateColumnDefaultValue",
+                               "fieldNames":      path,
+                               "newDefaultValue": planned.DefaultValue,
+                       })
+               }
+       }
+
+       for _, previous := range before {
+               if _, exists := afterByID[previous.ID]; !exists {
+                       changes = append(changes, client.SchemaChange{"action": 
"dropColumn", "fieldNames": []string{previous.Name}})
+               }
+       }
+
+       retainedNames := make(map[string]int)
+       pendingRenames := make(map[string]client.Field)
+       for _, previous := range before {
+               if planned, exists := afterByID[previous.ID]; exists {
+                       retainedNames[previous.Name] = planned.ID
+                       if previous.Name != planned.Name {
+                               pendingRenames[previous.Name] = planned
+                       }
+               }
+       }
+       for len(pendingRenames) > 0 {
+               progress := false
+               for _, previous := range before {
+                       planned, pending := pendingRenames[previous.Name]
+                       if !pending {
+                               continue
+                       }
+                       if conflictingID, conflict := 
retainedNames[planned.Name]; conflict && conflictingID != planned.ID {
+                               continue
+                       }
+                       changes = append(changes, client.SchemaChange{
+                               "action":     "renameColumn",
+                               "fieldNames": []string{previous.Name},
+                               "newName":    planned.Name,
+                       })
+                       delete(retainedNames, previous.Name)
+                       retainedNames[planned.Name] = planned.ID
+                       delete(pendingRenames, previous.Name)
+                       progress = true
+               }
+               if !progress {
+                       return nil, errors.New("cannot apply a cycle of table 
field renames in one apply; use an intermediate name")
+               }
+       }
+
+       for _, planned := range after {
+               if _, exists := beforeByID[planned.ID]; exists {
+                       continue
+               }
+               changes = append(changes, client.SchemaChange{
+                       "action":     "addColumn",
+                       "fieldNames": []string{planned.Name},
+                       "dataType":   schemaChangeDataType(planned, before, -1),
+                       "comment":    planned.Description,
+               })
+               if planned.DefaultValue != nil {
+                       changes = append(changes, client.SchemaChange{
+                               "action":          "updateColumnDefaultValue",
+                               "fieldNames":      []string{planned.Name},
+                               "newDefaultValue": planned.DefaultValue,
+                       })
+               }
+       }
+
+       currentOrder := make([]string, 0, len(after))
+       for _, previous := range before {
+               if planned, exists := afterByID[previous.ID]; exists {
+                       currentOrder = append(currentOrder, planned.Name)
+               }
+       }
+       for _, planned := range after {

Review Comment:
   Fixed in 3966f9c. Update now reads the server-reported 
add-column-before-partition option, models the actual insertion point, and 
emits the required move for the added field; a regression test covers the 
resulting change sequence.



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

Reply via email to