()
| 1557 | |
| 1558 | |
| 1559 | def test_union_from_sparse(): |
| 1560 | binary = pa.array([b'a', b' ', b'b', b'c', b' ', b' ', b'd'], |
| 1561 | type='binary') |
| 1562 | int64 = pa.array([0, 1, 0, 0, 2, 3, 0], type='int64') |
| 1563 | types = pa.array([0, 1, 0, 0, 1, 1, 0], type='int8') |
| 1564 | logical_types = pa.array([11, 13, 11, 11, 13, 13, 11], type='int8') |
| 1565 | py_value = [b'a', 1, b'b', b'c', 2, 3, b'd'] |
| 1566 | |
| 1567 | def check_result(result, expected_field_names, expected_type_codes, |
| 1568 | expected_type_code_values): |
| 1569 | result.validate(full=True) |
| 1570 | assert result.to_pylist() == py_value |
| 1571 | actual_field_names = [result.type[i].name |
| 1572 | for i in range(result.type.num_fields)] |
| 1573 | assert actual_field_names == expected_field_names |
| 1574 | assert result.type.mode == "sparse" |
| 1575 | assert result.type.type_codes == expected_type_codes |
| 1576 | assert expected_type_code_values.equals(result.type_codes) |
| 1577 | assert result.field(0).equals(binary) |
| 1578 | assert result.field(1).equals(int64) |
| 1579 | with pytest.raises(pa.ArrowTypeError): |
| 1580 | result.offsets |
| 1581 | with pytest.raises(KeyError): |
| 1582 | result.field(-1) |
| 1583 | with pytest.raises(KeyError): |
| 1584 | result.field(2) |
| 1585 | |
| 1586 | # without field names and type codes |
| 1587 | check_result(pa.UnionArray.from_sparse(types, [binary, int64]), |
| 1588 | expected_field_names=['0', '1'], |
| 1589 | expected_type_codes=[0, 1], |
| 1590 | expected_type_code_values=types) |
| 1591 | |
| 1592 | # with field names |
| 1593 | check_result(pa.UnionArray.from_sparse(types, [binary, int64], |
| 1594 | ['bin', 'int']), |
| 1595 | expected_field_names=['bin', 'int'], |
| 1596 | expected_type_codes=[0, 1], |
| 1597 | expected_type_code_values=types) |
| 1598 | |
| 1599 | # with type codes |
| 1600 | check_result(pa.UnionArray.from_sparse(logical_types, [binary, int64], |
| 1601 | type_codes=[11, 13]), |
| 1602 | expected_field_names=['0', '1'], |
| 1603 | expected_type_codes=[11, 13], |
| 1604 | expected_type_code_values=logical_types) |
| 1605 | |
| 1606 | # with field names and type codes |
| 1607 | check_result(pa.UnionArray.from_sparse(logical_types, [binary, int64], |
| 1608 | ['bin', 'int'], |
| 1609 | [11, 13]), |
| 1610 | expected_field_names=['bin', 'int'], |
| 1611 | expected_type_codes=[11, 13], |
| 1612 | expected_type_code_values=logical_types) |
| 1613 | |
| 1614 | # Bad type ids |
| 1615 | arr = pa.UnionArray.from_sparse(logical_types, [binary, int64]) |
| 1616 | with pytest.raises(pa.ArrowInvalid): |
nothing calls this directly
no test coverage detected