zeroshade commented on code in PR #23:
URL: https://github.com/apache/iceberg-terraform/pull/23#discussion_r2961737476


##########
internal/provider/resource_table.go:
##########
@@ -507,6 +602,95 @@ func (r *icebergTableResource) calculateSchemaUpdates(ctx 
context.Context, plan,
        return nil
 }
 
+func (r *icebergTableResource) calculatePartitionUpdates(ctx context.Context, 
plan *icebergTableResourceModel, tbl *table.Table, diags *diag.Diagnostics) 
[]table.Update {
+       spec := tbl.Spec()
+       if plan.PartitionSpec.IsUnknown() {
+               if spec.NumFields() > 0 {
+                       // Create a new unpartitioned spec and set it as default
+                       unpartitionedSpec := iceberg.NewPartitionSpec()
+                       return []table.Update{
+                               
table.NewAddPartitionSpecUpdate(&unpartitionedSpec, false),
+                               table.NewSetDefaultSpecUpdate(-1),
+                       }
+               }
+               return nil
+       }
+
+       if plan.PartitionSpec.IsNull() {
+               return nil
+       }
+
+       var planSpec icebergTablePartitionSpec
+       d := plan.PartitionSpec.As(ctx, &planSpec, basetypes.ObjectAsOptions{})
+       diags.Append(d...)
+
+       if diags.HasError() {
+               return nil
+       }
+
+       newIcebergSpec, err := planSpec.ToIceberg()
+       if err != nil {
+               diags.AddError("failed to convert partition spec", err.Error())
+               return nil
+       }
+
+       // Compare with current spec
+       if !spec.CompatibleWith(newIcebergSpec) {
+               return []table.Update{
+                       table.NewAddPartitionSpecUpdate(newIcebergSpec, false),
+                       table.NewSetDefaultSpecUpdate(-1),
+               }
+       }
+
+       return nil
+}
+
+func (r *icebergTableResource) calculateSortOrderUpdates(ctx context.Context, 
plan *icebergTableResourceModel, tbl *table.Table, diags *diag.Diagnostics) 
[]table.Update {
+       if plan.SortOrder.IsUnknown() {
+               if tbl.SortOrder().OrderID() != 0 {
+                       // Create a new unsorted order and set it as default
+                       unsortedOrder, err := table.NewSortOrder(0, 
[]table.SortField{})
+                       if err != nil {
+                               diags.AddError("failed to create unsorted 
order", err.Error())
+                               return nil
+                       }

Review Comment:
   just use `table.UnsortedSortOrder` for this



##########
internal/provider/table_schema.go:
##########
@@ -95,6 +96,105 @@ func (s *icebergTableSchema) FromIceberg(icebergSchema 
*iceberg.Schema) error {
        return json.Unmarshal(b, s)
 }
 
+type icebergTablePartitionSpec struct {
+       Fields []icebergTablePartitionField `tfsdk:"fields" json:"fields"`
+}
+
+func (s icebergTablePartitionSpec) AttrTypes() map[string]attr.Type {
+       return map[string]attr.Type{
+               "fields": types.ListType{
+                       ElemType: types.ObjectType{
+                               AttrTypes: 
icebergTablePartitionField{}.AttrTypes(),
+                       },
+               },
+       }
+}
+
+func (s *icebergTablePartitionSpec) ToIceberg() (*iceberg.PartitionSpec, 
error) {
+       b, err := json.Marshal(s)
+       if err != nil {
+               return nil, err
+       }
+       var icebergSpec iceberg.PartitionSpec
+       if err := json.Unmarshal(b, &icebergSpec); err != nil {
+               return nil, err
+       }
+       return &icebergSpec, nil
+}
+
+func (s *icebergTablePartitionSpec) FromIceberg(icebergSpec 
iceberg.PartitionSpec) error {
+       b, err := json.Marshal(icebergSpec)
+       if err != nil {
+               return err
+       }
+       return json.Unmarshal(b, s)
+}
+
+type icebergTablePartitionField struct {
+       SourceID  int64  `tfsdk:"source_id" json:"source-id"`
+       FieldID   int64  `tfsdk:"-" json:"field-id"`

Review Comment:
   why is this not part of the tfsdk?



##########
internal/provider/resource_table.go:
##########
@@ -96,6 +98,64 @@ func (r *icebergTableResource) Schema(_ context.Context, _ 
resource.SchemaReques
                                        },
                                },
                        },
+                       "partition_spec": rscschema.SingleNestedAttribute{
+                               Description: "The partition spec of the table.",
+                               Optional:    true,
+                               Computed:    true,
+                               Attributes: map[string]rscschema.Attribute{
+                                       "fields": rscschema.ListNestedAttribute{
+                                               Description: "The fields of the 
partition spec.",
+                                               Required:    true,
+                                               NestedObject: 
rscschema.NestedAttributeObject{
+                                                       Attributes: 
map[string]rscschema.Attribute{

Review Comment:
   you're missing the partition field id



##########
internal/provider/resource_table.go:
##########
@@ -96,6 +98,64 @@ func (r *icebergTableResource) Schema(_ context.Context, _ 
resource.SchemaReques
                                        },
                                },
                        },
+                       "partition_spec": rscschema.SingleNestedAttribute{
+                               Description: "The partition spec of the table.",
+                               Optional:    true,
+                               Computed:    true,
+                               Attributes: map[string]rscschema.Attribute{
+                                       "fields": rscschema.ListNestedAttribute{
+                                               Description: "The fields of the 
partition spec.",
+                                               Required:    true,
+                                               NestedObject: 
rscschema.NestedAttributeObject{
+                                                       Attributes: 
map[string]rscschema.Attribute{
+                                                               "source_id": 
rscschema.Int64Attribute{
+                                                                       
Description: "The source field ID.",
+                                                                       
Required:    true,
+                                                               },
+                                                               "name": 
rscschema.StringAttribute{
+                                                                       
Description: "The partition field name.",
+                                                                       
Required:    true,
+                                                               },
+                                                               "transform": 
rscschema.StringAttribute{
+                                                                       
Description: "The partition transform.",
+                                                                       
Required:    true,
+                                                               },

Review Comment:
   are we validating this anywhere? Can we add a test with invalid transform 
choices?



##########
internal/provider/resource_table.go:
##########
@@ -96,6 +98,64 @@ func (r *icebergTableResource) Schema(_ context.Context, _ 
resource.SchemaReques
                                        },
                                },
                        },
+                       "partition_spec": rscschema.SingleNestedAttribute{
+                               Description: "The partition spec of the table.",
+                               Optional:    true,
+                               Computed:    true,
+                               Attributes: map[string]rscschema.Attribute{
+                                       "fields": rscschema.ListNestedAttribute{
+                                               Description: "The fields of the 
partition spec.",
+                                               Required:    true,
+                                               NestedObject: 
rscschema.NestedAttributeObject{
+                                                       Attributes: 
map[string]rscschema.Attribute{
+                                                               "source_id": 
rscschema.Int64Attribute{
+                                                                       
Description: "The source field ID.",
+                                                                       
Required:    true,
+                                                               },
+                                                               "name": 
rscschema.StringAttribute{
+                                                                       
Description: "The partition field name.",
+                                                                       
Required:    true,
+                                                               },
+                                                               "transform": 
rscschema.StringAttribute{
+                                                                       
Description: "The partition transform.",
+                                                                       
Required:    true,
+                                                               },
+                                                       },
+                                               },
+                                       },
+                               },
+                       },
+                       "sort_order": rscschema.SingleNestedAttribute{
+                               Description: "The sort order of the table.",
+                               Optional:    true,
+                               Computed:    true,
+                               Attributes: map[string]rscschema.Attribute{
+                                       "fields": rscschema.ListNestedAttribute{
+                                               Description: "The fields of the 
sort order.",
+                                               Required:    true,
+                                               NestedObject: 
rscschema.NestedAttributeObject{
+                                                       Attributes: 
map[string]rscschema.Attribute{
+                                                               "source_id": 
rscschema.Int64Attribute{
+                                                                       
Description: "The source field ID.",
+                                                                       
Required:    true,
+                                                               },
+                                                               "transform": 
rscschema.StringAttribute{
+                                                                       
Description: "The sort transform.",
+                                                                       
Required:    true,
+                                                               },
+                                                               "direction": 
rscschema.StringAttribute{
+                                                                       
Description: "The sort direction (asc or desc).",
+                                                                       
Required:    true,
+                                                               },
+                                                               "null_order": 
rscschema.StringAttribute{
+                                                                       
Description: "The null order (nulls-first or nulls-last).",
+                                                                       
Required:    true,
+                                                               },

Review Comment:
   are we validating these anywhere to ensure the inputs are valid? 
   
   Can we add tests with invalid values for these?



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