badalprasadsingh commented on code in PR #2774:
URL: https://github.com/apache/iceberg-go/pull/2774#discussion_r4751626309


##########
table/update_schema.go:
##########
@@ -1098,6 +1114,40 @@ func nativeDefaultLiteral(v any) (iceberg.Literal, bool) 
{
        return nil, false
 }
 
+// isPromotion reports whether changing a column from one type to another is
+// a type promotion (int to long, float to double, or widening a decimal).
+func isPromotion(from, to iceberg.Type) bool {
+       f, ok := from.(iceberg.PrimitiveType)
+       if !ok {
+               return false
+       }
+       t, ok := to.(iceberg.PrimitiveType)
+       if !ok {
+               return false
+       }
+
+       return !f.Equals(t) && iceberg.IsPromotionAllowed(f, t)
+}
+
+// convertDefault converts a stored default to the type of a promotion, as
+// Java's castDefault does. Decimal defaults are returned unchanged, because
+// widening the precision doesn't change their value.
+func convertDefault(v any, typ iceberg.Type) (any, error) {

Review Comment:
   int and float defaults are held as float64 in loaded tables, which 
"nativeDefaultLiteral" skips, so promoting one converts nothing
   
   a float 1.1 stays 1.1, and rows reading the default differ from rows that 
stored it, better: convert by old type replacing both the helpers



##########
table/update_schema_test.go:
##########
@@ -665,6 +666,181 @@ func TestUpdateColumnAddedInSameUpdate(t *testing.T) {
        })
 }
 
+func TestUpdateColumnPromotionConvertsDefaults(t *testing.T) {
+       // A v3 table whose columns have defaults, so a promotion has defaults 
to
+       // convert, as Java's castDefault does when a field's type is promoted.
+       dec := iceberg.Decimal{Val: decimal128.FromI64(12345), Scale: 2}
+       newTxn := func(t *testing.T) *Transaction {
+               t.Helper()
+               md, err := NewMetadata(iceberg.NewSchema(0,
+                       iceberg.NestedField{ID: 1, Name: "i", Type: 
iceberg.PrimitiveTypes.Int32, InitialDefault: int32(1), WriteDefault: int32(2)},
+                       iceberg.NestedField{ID: 2, Name: "f", Type: 
iceberg.PrimitiveTypes.Float32, InitialDefault: float32(1.1), WriteDefault: 
float32(2.5)},

Review Comment:
   the promotion cases start from int32 and float32 defaults, but a loaded 
table holds float64 for both
   
   please add a case that parses the metadata.json: 2.5 is also the same number 
in float and double, so 2.2 would check the write default better



##########
table/update_schema.go:
##########
@@ -616,7 +616,23 @@ func (u *UpdateSchema) updateColumn(path []string, update 
ColumnUpdate) error {
                updatedField.Name = update.Name.Val
        }
        if update.FieldType.Valid {
+               oldType := updatedField.Type
                updatedField.Type = update.FieldType.Val
+               // As in Java's castDefault, a promotion converts the defaults 
to the
+               // new type. Other type changes, allowed by 
allowIncompatibleChanges,
+               // leave the defaults as they are. A write default set by this 
update
+               // replaces the converted one below.
+               if isPromotion(oldType, updatedField.Type) {

Review Comment:
   the checks above use `field.Type`, the original type; this uses the pending 
one
   so, int -> long -> int (rare but possible) in one update now leaves int64 
defaults on the int col
   
   better, check the pending type



##########
table/update_schema.go:
##########


Review Comment:
   nned to update doc



##########
table/update_schema.go:
##########
@@ -616,7 +616,23 @@ func (u *UpdateSchema) updateColumn(path []string, update 
ColumnUpdate) error {
                updatedField.Name = update.Name.Val
        }
        if update.FieldType.Valid {
+               oldType := updatedField.Type
                updatedField.Type = update.FieldType.Val
+               // As in Java's castDefault, a promotion converts the defaults 
to the
+               // new type. Other type changes, allowed by 
allowIncompatibleChanges,
+               // leave the defaults as they are. A write default set by this 
update
+               // replaces the converted one below.
+               if isPromotion(oldType, updatedField.Type) {
+                       initial, err := 
convertDefault(updatedField.InitialDefault, updatedField.Type)
+                       if err != nil {
+                               return fmt.Errorf("cannot convert 
initial-default of %s to %s: %w", fullName, updatedField.Type, err)
+                       }
+                       write, err := convertDefault(updatedField.WriteDefault, 
updatedField.Type)
+                       if err != nil {
+                               return fmt.Errorf("cannot convert write-default 
of %s to %s: %w", fullName, updatedField.Type, err)
+                       }
+                       updatedField.InitialDefault, updatedField.WriteDefault 
= initial, write

Review Comment:
   first convert
   
   `unionUpdateColumn` comapres write defaults of diff types. after a 
promotion, a union of an older schema now fails, where earlier we did nothing 
until a reload



##########
table/update_schema.go:
##########
@@ -1457,18 +1507,24 @@ func (a *applyChanges) Struct(structType 
iceberg.StructType, fieldResults []iceb
                name := field.Name
                doc := field.Doc
                required := field.Required
+               initialDefault := field.InitialDefault
                writeDefault := field.WriteDefault
 
                if update, ok := a.updates[field.ID]; ok {
                        name = update.Name
                        doc = update.Doc
                        required = update.Required
+                       // updateColumn converts the defaults when it promotes 
the type.
+                       initialDefault = update.InitialDefault
                        writeDefault = update.WriteDefault
                }
                if field.Name == name &&
                        field.Type.Equals(resultType) &&
                        field.Required == required &&
                        field.Doc == doc &&
+                       // Defensive: defaults only change with the type today, 
which the
+                       // Type check above already catches.
+                       reflect.DeepEqual(field.InitialDefault, initialDefault) 
&&
                        reflect.DeepEqual(field.WriteDefault, writeDefault) {

Review Comment:
   the comment is not correct based on the line below it: a write default can 
change without the type



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