@@ -43,10 +43,10 @@ def struct_to_jsonb(table: pyarrow.Table) -> pyarrow.Table:
4343 # Check if the column is a STRUCT
4444 if pyarrow .types .is_struct (field .type ):
4545 # Convert each row in the STRUCT column to a JSON string
46- json_strings = [
47- orjson . dumps ( row . as_py ()) if row . is_valid else None for row in table .column (i )
48- ]
49- json_array = pyarrow . array ( json_strings , type = pyarrow . binary () )
46+ json_array = pyarrow . array (
47+ [ None if row is None else orjson . dumps ( row ) for row in table .column (i ). to_pylist ()],
48+ type = pyarrow . binary (),
49+ )
5050
5151 # Drop the original STRUCT column
5252 table = table .drop_columns (field .name )
@@ -62,13 +62,13 @@ def struct_to_jsonb(table: pyarrow.Table) -> pyarrow.Table:
6262
6363 # Convert each list element
6464 converted_data = []
65- for item in list_array :
65+ for item in list_array . to_pylist () :
6666 if item is None :
6767 converted_data .append (None )
6868 else :
6969 # Each item is a list of structs
7070 converted_list = []
71- for struct in item . as_py () :
71+ for struct in item :
7272 if struct is None :
7373 converted_list .append (None )
7474 else :
@@ -98,28 +98,28 @@ def normalize_morsel(schema: RelationSchema, morsel: pyarrow.Table) -> pyarrow.T
9898 # rename columns for internal use
9999 target_column_names = []
100100 # columns in the data but not in the schema, droppable
101- droppable_columns = []
101+ droppable_columns = set ()
102102
103103 # Find which columns to drop and which columns we already have
104104 for i , column in enumerate (morsel .column_names ):
105105 column_name = schema .find_column (column )
106106 if column_name is None :
107- droppable_columns .append (i )
107+ droppable_columns .add (i )
108108 else :
109109 target_column_names .append (str (column_name ))
110110
111111 # Remove from the end otherwise we'll remove the wrong columns after we've removed one
112- droppable_columns . reverse ()
113- for droppable in droppable_columns :
114- morsel = morsel .remove_column ( droppable )
112+ if droppable_columns :
113+ keep_indices = [ i for i in range ( len ( morsel . columns )) if i not in droppable_columns ]
114+ morsel = morsel .select ( keep_indices )
115115
116116 # remane columns to the internal names (identities)
117117 morsel = morsel .rename_columns (target_column_names )
118118
119119 # add columns we don't have, populate with nulls but try to get the correct type
120120 for column in schema .columns :
121121 if column .identity not in target_column_names :
122- null_column = pyarrow .array ([ None ] * morsel .num_rows , type = column .arrow_field .type )
122+ null_column = pyarrow .nulls ( morsel .num_rows , type = column .arrow_field .type )
123123 field = pyarrow .field (name = column .identity , type = column .arrow_field .type )
124124 morsel = morsel .append_column (field , null_column )
125125
0 commit comments