Recursively asserts that the data content of two Arrays is identical.
(expected: &dyn Array, actual: &dyn Array, context: &str)
| 1815 | |
| 1816 | /// Recursively asserts that the data content of two Arrays is identical. |
| 1817 | fn assert_array_data_is_identical(expected: &dyn Array, actual: &dyn Array, context: &str) { |
| 1818 | assert_eq!( |
| 1819 | expected.nulls(), |
| 1820 | actual.nulls(), |
| 1821 | "{context}: null buffers must match" |
| 1822 | ); |
| 1823 | assert_eq!( |
| 1824 | expected.len(), |
| 1825 | actual.len(), |
| 1826 | "{context}: array lengths must match" |
| 1827 | ); |
| 1828 | |
| 1829 | match (expected.data_type(), actual.data_type()) { |
| 1830 | (DataType::Union(expected_fields, _), DataType::Union(..)) => { |
| 1831 | let expected_union = expected.as_any().downcast_ref::<UnionArray>().unwrap(); |
| 1832 | let actual_union = actual.as_any().downcast_ref::<UnionArray>().unwrap(); |
| 1833 | |
| 1834 | // Compare the type_ids buffer (always the first buffer). |
| 1835 | assert_eq!( |
| 1836 | &expected.to_data().buffers()[0], |
| 1837 | &actual.to_data().buffers()[0], |
| 1838 | "{context}: union type_ids buffer mismatch" |
| 1839 | ); |
| 1840 | |
| 1841 | // For dense unions, compare the value_offsets buffer (the second buffer). |
| 1842 | if expected.to_data().buffers().len() > 1 { |
| 1843 | assert_eq!( |
| 1844 | &expected.to_data().buffers()[1], |
| 1845 | &actual.to_data().buffers()[1], |
| 1846 | "{context}: union value_offsets buffer mismatch" |
| 1847 | ); |
| 1848 | } |
| 1849 | |
| 1850 | // Recursively compare children based on the fields in the DataType. |
| 1851 | for (type_id, _) in expected_fields.iter() { |
| 1852 | let child_context = format!("{context} -> child variant {type_id}"); |
| 1853 | assert_array_data_is_identical( |
| 1854 | expected_union.child(type_id), |
| 1855 | actual_union.child(type_id), |
| 1856 | &child_context, |
| 1857 | ); |
| 1858 | } |
| 1859 | } |
| 1860 | (DataType::Struct(_), DataType::Struct(_)) => { |
| 1861 | let expected_struct = expected.as_any().downcast_ref::<StructArray>().unwrap(); |
| 1862 | let actual_struct = actual.as_any().downcast_ref::<StructArray>().unwrap(); |
| 1863 | for i in 0..expected_struct.num_columns() { |
| 1864 | let child_context = format!("{context} -> struct child {i}"); |
| 1865 | assert_array_data_is_identical( |
| 1866 | expected_struct.column(i), |
| 1867 | actual_struct.column(i), |
| 1868 | &child_context, |
| 1869 | ); |
| 1870 | } |
| 1871 | } |
| 1872 | // Fallback for primitive types and other types where buffer comparison is sufficient. |
| 1873 | _ => { |
| 1874 | assert_eq!( |
no test coverage detected