Fokko commented on code in PR #1770:
URL: https://github.com/apache/iceberg-python/pull/1770#discussion_r1989938216


##########
pyiceberg/table/update/schema.py:
##########
@@ -212,13 +215,34 @@ def add_column(
 
         # assign new IDs in order
         new_id = self.assign_new_column_id()
+        new_type = assign_fresh_schema_ids(field_type, 
self.assign_new_column_id)
+
+        if default_value is not None:
+            try:
+                # To make sure that the value is valid for the type
+                initial_default = literal(default_value).to(new_type).value
+            except ValueError as e:
+                raise ValueError(f"Invalid default value: {e}") from e
+        else:
+            initial_default = default_value
+
+        if (required and initial_default is None) and not 
self._allow_incompatible_changes:
+            # Table format version 1 and 2 cannot add required column because 
there is no initial value
+            raise ValueError(f"Incompatible change: cannot add required 
column: {'.'.join(path)}")
 
         # update tracking for moves
         self._added_name_to_id[full_name] = new_id
         self._id_to_parent[new_id] = parent_full_path
 
-        new_type = assign_fresh_schema_ids(field_type, 
self.assign_new_column_id)
-        field = NestedField(field_id=new_id, name=name, field_type=new_type, 
required=required, doc=doc)
+        field = NestedField(
+            field_id=new_id,
+            name=name,
+            field_type=new_type,
+            required=required,
+            doc=doc,
+            initial_default=initial_default,
+            write_default=initial_default,

Review Comment:
   This follows [this part of the 
spec](https://iceberg.apache.org/spec/#default-values): 
   <img width="762" alt="image" 
src="https://github.com/user-attachments/assets/191f0505-05a9-4cba-a944-ef79035c1fb5";
 />



##########
pyiceberg/table/update/schema.py:
##########
@@ -330,6 +354,7 @@ def _set_column_requirement(self, path: Union[str, 
Tuple[str, ...]], required: b
                 field_type=updated.field_type,
                 doc=updated.doc,
                 required=required,
+                initial_default=updated.initial_default,

Review Comment:
   Yes, you're right :)



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to