| 2877 | |
| 2878 | |
| 2879 | def test_table_select(): |
| 2880 | a1 = pa.array([1, 2, 3, None, 5]) |
| 2881 | a2 = pa.array(['a', 'b', 'c', 'd', 'e']) |
| 2882 | a3 = pa.array([[1, 2], [3, 4], [5, 6], None, [9, 10]]) |
| 2883 | table = pa.table([a1, a2, a3], ['f1', 'f2', 'f3']) |
| 2884 | |
| 2885 | # selecting with string names |
| 2886 | result = table.select(['f1']) |
| 2887 | expected = pa.table([a1], ['f1']) |
| 2888 | assert result.equals(expected) |
| 2889 | |
| 2890 | result = table.select(['f3', 'f2']) |
| 2891 | expected = pa.table([a3, a2], ['f3', 'f2']) |
| 2892 | assert result.equals(expected) |
| 2893 | |
| 2894 | # selecting with integer indices |
| 2895 | result = table.select([0]) |
| 2896 | expected = pa.table([a1], ['f1']) |
| 2897 | assert result.equals(expected) |
| 2898 | |
| 2899 | result = table.select([2, 1]) |
| 2900 | expected = pa.table([a3, a2], ['f3', 'f2']) |
| 2901 | assert result.equals(expected) |
| 2902 | |
| 2903 | # preserve metadata |
| 2904 | table2 = table.replace_schema_metadata({"a": "test"}) |
| 2905 | result = table2.select(["f1", "f2"]) |
| 2906 | assert b"a" in result.schema.metadata |
| 2907 | |
| 2908 | # selecting non-existing column raises |
| 2909 | with pytest.raises(KeyError, match='Field "f5" does not exist'): |
| 2910 | table.select(['f5']) |
| 2911 | |
| 2912 | with pytest.raises(IndexError, match="index out of bounds"): |
| 2913 | table.select([5]) |
| 2914 | |
| 2915 | # duplicate selection gives duplicated names in resulting table |
| 2916 | result = table.select(['f2', 'f2']) |
| 2917 | expected = pa.table([a2, a2], ['f2', 'f2']) |
| 2918 | assert result.equals(expected) |
| 2919 | |
| 2920 | # selection duplicated column raises |
| 2921 | table = pa.table([a1, a2, a3], ['f1', 'f2', 'f1']) |
| 2922 | with pytest.raises(KeyError, match='Field "f1" exists 2 times'): |
| 2923 | table.select(['f1']) |
| 2924 | |
| 2925 | result = table.select(['f2']) |
| 2926 | expected = pa.table([a2], ['f2']) |
| 2927 | assert result.equals(expected) |
| 2928 | |
| 2929 | |
| 2930 | @pytest.mark.acero |