Repository: apache/arrow
File: python/pyarrow/tests/test_compute.py (Python)

Code before:
    a = pa.array([True, False, False, None])
    b = pa.array([True, True, False, True])

    assert pc.and_(a, b) == pa.array([True, False, False, None])
    assert pc.and_kleene(a, b) == pa.array([True, False, False, None])

    assert pc.or_(a, b) == pa.array([True, True, False, None])
    assert pc.or_kleene(a, b) == pa.array([True, True, False, True])

    assert pc.xor(a, b) == pa.array([False, True, False, None])

    assert pc.invert(a) == pa.array([False, True, True, None])


def test_dictionary_decode():
    array = pa.array(["a", "a", "b", "c", "b"])
    dictionary_array = array.dictionary_encode()
    dictionary_array_decode = pc.dictionary_decode(dictionary_array)

    assert array != dictionary_array

    assert array == dictionary_array_decode
    assert array == pc.dictionary_decode(array)
    assert pc.dictionary_encode(dictionary_array) == dictionary_array


def test_cast():
    arr = pa.array([1, 2, 3, 4], type='int64')
    options = pc.CastOptions(pa.int8())

    with pytest.raises(TypeError):
        pc.cast(arr, target_type=None)

    with pytest.raises(ValueError):
        pc.cast(arr, 'int32', options=options)

    with pytest.raises(ValueError):
        pc.cast(arr, safe=True, options=options)

    assert pc.cast(arr, options=options) == pa.array(
        [1, 2, 3, 4], type='int8')

    arr = pa.array([2 ** 63 - 1], type='int64')
    allow_overflow_options = pc.CastOptions(
        pa.int32(), allow_int_overflow=True)

    with pytest.raises(pa.ArrowInvalid):
        pc.cast(arr, 'int32')

    assert pc.cast(arr, 'int32', safe=False) == pa.array([-1], type='int32')


Review comment (suggestion):
```suggestion
    arr = pa.array([1, 2, 3, 4, 5, None], type=ty)
    s = pa.scalar(42, type=ty)

    result = pa.compute.call_function("coalesce", [arr, s])
    expected = pa.array([1, 2, 3, 4, 5, 42], type=ty)
```

Code after:
    # Regression test for GH-47234, which was failing due to a MSVC compiler bug
    # (possibly https://developercommunity.visualstudio.com/t/10912292
    # or https://developercommunity.visualstudio.com/t/10945478)
    arr = pa.array([True, False, False, False, False, None])
    s = pa.scalar(True, type=pa.bool_())

    result = pa.compute.call_function("coalesce", [arr, s])
    result.validate(full=True)
    expected = pa.array([True, False, False, False, False, True])
    assert result.equals(expected)

    for ty in [pa.int8(), pa.int16(), pa.int32(), pa.int64()]:
        arr = pa.array([1, 2, 3, 4, 5, None], type=ty)
        s = pa.scalar(42, type=ty)

        result = pa.compute.call_function("coalesce", [arr, s])
        result.validate(full=True)
        expected = pa.array([1, 2, 3, 4, 5, 42], type=ty)
        assert result.equals(expected)


def test_logical():
    a = pa.array([True, False, False, None])
    b = pa.array([True, True, False, True])

    assert pc.and_(a, b) == pa.array([True, False, False, None])
    assert pc.and_kleene(a, b) == pa.array([True, False, False, None])

    assert pc.or_(a, b) == pa.array([True, True, False, None])
    assert pc.or_kleene(a, b) == pa.array([True, True, False, True])

    assert pc.xor(a, b) == pa.array([False, True, False, None])

    assert pc.invert(a) == pa.array([False, True, True, None])


def test_dictionary_decode():
    array = pa.array(["a", "a", "b", "c", "b"])
    dictionary_array = array.dictionary_encode()
    dictionary_array_decode = pc.dictionary_decode(dictionary_array)

    assert array != dictionary_array

    assert array == dictionary_array_decode
    assert array == pc.dictionary_decode(array)
    assert pc.dictionary_encode(dictionary_array) == dictionary_array


def test_cast():
    arr = pa.array([1, 2, 3, 4], type='int64')
    options = pc.CastOptions(pa.int8())