zhengruifeng commented on code in PR #53762: URL: https://github.com/apache/spark/pull/53762#discussion_r2710851062
########## python/pyspark/tests/upstream/pyarrow/test_pyarrow_scalar_type_coercion.py: ########## @@ -0,0 +1,240 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Test pa.scalar type coercion behavior when creating scalars with explicit type parameter. + +This test monitors the behavior of PyArrow's type coercion to ensure PySpark's assumptions +about PyArrow behavior remain valid across versions. + +Test categories: +1. Null coercion - None to numeric types +2. Numeric types - int, float, decimal coercion and boundaries + +The helper method pattern is adapted from PR #53721 (pa.array type coercion tests). +""" + +import math +import unittest +from decimal import Decimal + +from pyspark.testing.utils import ( + have_pyarrow, + pyarrow_requirement_message, +) + + [email protected](not have_pyarrow, pyarrow_requirement_message) +class PyArrowScalarTypeCoercionTests(unittest.TestCase): + """Test PyArrow's type coercion behavior for pa.scalar with explicit type parameter.""" + + # ========================================================================= + # Helper methods + # ========================================================================= + + def _run_coercion_tests(self, cases): + """Run coercion tests: (value, target_type).""" + import pyarrow as pa + + for value, target_type in cases: + scalar = pa.scalar(value, type=target_type) + self.assertEqual(scalar.type, target_type) + + def _run_coercion_tests_with_values(self, cases): + """Run coercion tests with value verification: (value, target_type, expected).""" + import pyarrow as pa + + for value, target_type, expected in cases: + scalar = pa.scalar(value, type=target_type) + self.assertEqual(scalar.type, target_type) + self.assertEqual(scalar.as_py(), expected) + + def _run_error_tests(self, cases, error_type): + """Run tests expecting errors: (value, target_type).""" + import pyarrow as pa + + for value, target_type in cases: + with self.assertRaises(error_type): + pa.scalar(value, type=target_type) + + # ========================================================================= + # SECTION 1: Null Coercion + # ========================================================================= + + def test_null_coercion(self): + """Test that None can be coerced to numeric types as a null scalar.""" + import pyarrow as pa + + target_types = [ + pa.int8(), + pa.int16(), + pa.int32(), + pa.int64(), + pa.uint8(), + pa.uint16(), + pa.uint32(), + pa.uint64(), + pa.float32(), + pa.float64(), + pa.decimal128(20, 2), Review Comment: we can add it in followups -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
