|
16 | 16 | // under the License. |
17 | 17 |
|
18 | 18 | use arrow::array::{make_array, Array, GenericListArray, OffsetSizeTrait, StructArray}; |
| 19 | +use arrow::buffer::NullBuffer; |
19 | 20 | use arrow::datatypes::{DataType, FieldRef, Schema}; |
20 | 21 | use arrow::record_batch::RecordBatch; |
21 | 22 | use datafusion::common::{ |
@@ -148,27 +149,28 @@ fn get_array_struct_fields<O: OffsetSizeTrait>( |
148 | 149 | .values() |
149 | 150 | .as_any() |
150 | 151 | .downcast_ref::<StructArray>() |
151 | | - .expect("A struct is expected"); |
| 152 | + .expect("A StructType is expected"); |
152 | 153 |
|
153 | 154 | let field = Arc::clone(&values.fields()[ordinal]); |
154 | 155 | // Get struct column by ordinal |
155 | | - let column = values.column(ordinal); |
| 156 | + let extracted_column = values.column(ordinal); |
156 | 157 |
|
157 | | - let data = if values.null_count() > column.null_count() { |
| 158 | + let data = if values.null_count() == extracted_column.null_count() { |
| 159 | + Arc::clone(extracted_column) |
| 160 | + } else { |
158 | 161 | // In some cases the column obtained from struct by ordinal doesn't |
159 | | - // represent all nulls which imposed by parent values. |
| 162 | + // represent all nulls that imposed by parent values. |
160 | 163 | // This maybe caused by a low level reader bug and needs more investigation. |
161 | | - // For this specific case we patch the null buffer for column taking them |
162 | | - // from parent values |
| 164 | + // For this specific case we patch the null buffer for the column by merging nulls buffers |
| 165 | + // from parent and column |
| 166 | + let merged_nulls = NullBuffer::union(values.nulls(), extracted_column.nulls()); |
163 | 167 | make_array( |
164 | | - column |
| 168 | + extracted_column |
165 | 169 | .into_data() |
166 | 170 | .into_builder() |
167 | | - .nulls(values.nulls().cloned()) |
| 171 | + .nulls(merged_nulls) |
168 | 172 | .build()?, |
169 | 173 | ) |
170 | | - } else { |
171 | | - Arc::clone(column) |
172 | 174 | }; |
173 | 175 |
|
174 | 176 | let array = GenericListArray::new( |
|
0 commit comments