MCPcopy Create free account
hub / github.com/apache/arrow-rs / test_union_sparse_array

Function test_union_sparse_array

arrow-array/src/ffi.rs:1261–1319  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1259
1260 #[test]
1261 fn test_union_sparse_array() -> Result<()> {
1262 let mut builder = UnionBuilder::new_sparse();
1263 builder.append::<Int32Type>("a", 1).unwrap();
1264 builder.append_null::<Int32Type>("a").unwrap();
1265 builder.append::<Float64Type>("c", 3.0).unwrap();
1266 builder.append::<Int32Type>("a", 4).unwrap();
1267 let union = builder.build().unwrap();
1268
1269 // export it
1270 let (array, schema) = to_ffi(&union.to_data())?;
1271
1272 // (simulate consumer) import it
1273 let data = unsafe { from_ffi(array, &schema) }?;
1274 let array = make_array(data);
1275
1276 let array = array.as_any().downcast_ref::<UnionArray>().unwrap();
1277
1278 let expected_type_ids = vec![0_i8, 0, 1, 0];
1279
1280 // Check type ids
1281 assert_eq!(*array.type_ids(), expected_type_ids);
1282 for (i, id) in expected_type_ids.iter().enumerate() {
1283 assert_eq!(id, &array.type_id(i));
1284 }
1285
1286 // Check offsets, sparse union should only have a single buffer, i.e. no offsets
1287 assert!(array.offsets().is_none());
1288
1289 for i in 0..array.len() {
1290 let slot = array.value(i);
1291 match i {
1292 0 => {
1293 let slot = slot.as_primitive::<Int32Type>();
1294 assert!(!slot.is_null(0));
1295 assert_eq!(slot.len(), 1);
1296 let value = slot.value(0);
1297 assert_eq!(1_i32, value);
1298 }
1299 1 => assert!(slot.is_null(0)),
1300 2 => {
1301 let slot = slot.as_primitive::<Float64Type>();
1302 assert!(!slot.is_null(0));
1303 assert_eq!(slot.len(), 1);
1304 let value = slot.value(0);
1305 assert_eq!(value, 3_f64);
1306 }
1307 3 => {
1308 let slot = slot.as_primitive::<Int32Type>();
1309 assert!(!slot.is_null(0));
1310 assert_eq!(slot.len(), 1);
1311 let value = slot.value(0);
1312 assert_eq!(4_i32, value);
1313 }
1314 _ => unreachable!(),
1315 }
1316 }
1317
1318 Ok(())

Callers

nothing calls this directly

Calls 9

to_ffiFunction · 0.85
from_ffiFunction · 0.85
make_arrayFunction · 0.50
buildMethod · 0.45
to_dataMethod · 0.45
as_anyMethod · 0.45
iterMethod · 0.45
lenMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected