This is an automated email from the ASF dual-hosted git repository.

alenka pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new b171b2634c GH-40023: [Python] Use Cast() instead of CastTo (#40116)
b171b2634c is described below

commit b171b2634cd02cb7b248c45ba265495270183eec
Author: Hyunseok Seo <[email protected]>
AuthorDate: Mon Mar 11 17:49:10 2024 +0900

    GH-40023: [Python] Use Cast() instead of CastTo (#40116)
    
    
    
    ### Rationale for this change
    
    Remove legacy code
    
    ### What changes are included in this PR?
    
    * Replace the legacy scalar CastTo implementation for Python.
    
    ### Are these changes tested?
    
    Yes.
    
    ### Are there any user-facing changes?
    
    No.
    
    * Closes: #40023
    
    Authored-by: Hyunseok Seo <[email protected]>
    Signed-off-by: AlenkaF <[email protected]>
---
 python/pyarrow/includes/libarrow.pxd            |  4 +++-
 python/pyarrow/tests/pyarrow_cython_example.pyx | 14 ++++++++++----
 python/pyarrow/tests/test_cython.py             |  5 ++---
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/python/pyarrow/includes/libarrow.pxd 
b/python/pyarrow/includes/libarrow.pxd
index 5ae0f2e0b5..e44c699fc5 100644
--- a/python/pyarrow/includes/libarrow.pxd
+++ b/python/pyarrow/includes/libarrow.pxd
@@ -1161,7 +1161,6 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
         c_bool Equals(const CScalar& other) const
         CStatus Validate() const
         CStatus ValidateFull() const
-        CResult[shared_ptr[CScalar]] CastTo(shared_ptr[CDataType] to) const
 
     cdef cppclass CScalarHash" arrow::Scalar::Hash":
         size_t operator()(const shared_ptr[CScalar]& scalar) const
@@ -3010,3 +3009,6 @@ cdef extern from "arrow/python/udf.h" namespace 
"arrow::py" nogil:
 
     CResult[shared_ptr[CRecordBatchReader]] CallTabularFunction(
         const c_string& func_name, const vector[CDatum]& args, 
CFunctionRegistry* registry)
+
+cdef extern from "arrow/compute/cast.h" namespace "arrow::compute":
+    CResult[CDatum] Cast(const CDatum& value, const CCastOptions& options)
diff --git a/python/pyarrow/tests/pyarrow_cython_example.pyx 
b/python/pyarrow/tests/pyarrow_cython_example.pyx
index 08f5e17a98..9ae59efb8b 100644
--- a/python/pyarrow/tests/pyarrow_cython_example.pyx
+++ b/python/pyarrow/tests/pyarrow_cython_example.pyx
@@ -42,7 +42,9 @@ def cast_scalar(scalar, to_type):
     cdef:
         shared_ptr[CScalar] c_scalar
         shared_ptr[CDataType] c_type
-        CResult[shared_ptr[CScalar]] c_result
+        CCastOptions cast_options
+        CDatum c_datum
+        CResult[CDatum] c_cast_result
 
     c_scalar = pyarrow_unwrap_scalar(scalar)
     if c_scalar.get() == NULL:
@@ -50,6 +52,10 @@ def cast_scalar(scalar, to_type):
     c_type = pyarrow_unwrap_data_type(to_type)
     if c_type.get() == NULL:
         raise TypeError("not a type")
-    c_result = c_scalar.get().CastTo(c_type)
-    c_scalar = GetResultValue(c_result)
-    return pyarrow_wrap_scalar(c_scalar)
+
+    c_datum = CDatum(c_scalar)
+    cast_options = CCastOptions()
+    cast_options.to_type = c_type
+    c_cast_result = Cast(c_datum, cast_options)
+    c_datum = GetResultValue(c_cast_result)
+    return pyarrow_wrap_scalar(c_datum.scalar())
diff --git a/python/pyarrow/tests/test_cython.py 
b/python/pyarrow/tests/test_cython.py
index 59875e7b01..0eeae5d65f 100644
--- a/python/pyarrow/tests/test_cython.py
+++ b/python/pyarrow/tests/test_cython.py
@@ -25,7 +25,6 @@ import pytest
 import pyarrow as pa
 import pyarrow.tests.util as test_util
 
-
 here = os.path.dirname(os.path.abspath(__file__))
 test_ld_path = os.environ.get('PYARROW_TEST_LD_PATH', '')
 if os.name == 'posix':
@@ -35,7 +34,6 @@ elif os.name == 'nt':
 else:
     compiler_opts = []
 
-
 setup_template = """if 1:
     from setuptools import setup
     from Cython.Build import cythonize
@@ -77,7 +75,8 @@ def check_cython_example_module(mod):
     cast_scal = mod.cast_scalar(scal, pa.utf8())
     assert cast_scal == pa.scalar("123")
     with pytest.raises(NotImplementedError,
-                       match="casting scalars of type int64 to type list"):
+                       match="Unsupported cast from int64 to list using 
function "
+                             "cast_list"):
         mod.cast_scalar(scal, pa.list_(pa.int64()))
 
 

Reply via email to