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


##########
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:
   This replacement boundary is incomplete for Paimon key columns. 
`SchemaManager` rejects `UpdateColumnType` for both partition-key and 
primary-key fields, but a primitive type change on one of those fields does not 
satisfy `compositeFieldTypesRequireReplace`, and the key lists themselves are 
unchanged. Terraform therefore plans an in-place update that the REST Catalog 
will always reject. Please require replacement (or report a plan-time 
diagnostic) when a retained key field changes type.



##########
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:
   A newly added field with `nullable = false` reaches this payload as a `NOT 
NULL` data type. Paimon's `SchemaManager` rejects every `AddColumn` whose data 
type is non-nullable, so this currently produces an update plan that always 
fails at apply time. Please make this case require replacement / fail during 
planning, or generate a server-supported evolution sequence.



##########
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:
   This order model assumes every `AddColumn` appends. With Paimon's 
`add-column-before-partition = true`, an add without an explicit move is 
inserted before the first partition column instead. In that case `currentOrder` 
can already equal the desired order here, no corrective move is emitted, and 
post-mutation reconciliation fails because the server order differs. Please 
attach deterministic moves to added columns or account for the table option 
when modeling the intermediate order.



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