You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhengruifeng (via GitHub)" <gi...@apache.org> on 2026/01/05 09:42:16 UTC

[PR] [SPARK-54903][PYTHON] Make to_arrow_schema/to_arrow_type able to set timezone [spark]

zhengruifeng opened a new pull request, #53678:
URL: https://github.com/apache/spark/pull/53678

   ### What changes were proposed in this pull request?
   This PR make `to_arrow_schema/to_arrow_type` able to set timezone, and keep existing behaviors by setting `UTC` as the default timezone.
   So that we can check whether the timezone is reasonable later.
   
   ### Why are the changes needed?
   it is kind of problematical in pyspark timezone handling, one example is 
   `to_arrow_schema/to_arrow_type` is not able to set the timezone
   
   while timezone is supported in JVM side
   
   https://github.com/apache/spark/blob/d6f1e224df8fc620ab44817e4c1034f990f5d8ff/sql/api/src/main/scala/org/apache/spark/sql/util/ArrowUtils.scala#L40-L68
   
   
   ### Does this PR introduce _any_ user-facing change?
   No
   
   
   ### How was this patch tested?
   CI
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   No


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54903][PYTHON] Make to_arrow_schema/to_arrow_type able to set timezone [spark]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #53678:
URL: https://github.com/apache/spark/pull/53678#issuecomment-3709674928

   ## JIRA Issue Information
   
   === Improvement SPARK-54903 ===
   Summary: Make to_arrow_schema/to_arrow_type able to set timezone
   Assignee: None
   Status: Open
   Affected: ["4.2.0"]
   
   ---
   *This comment was automatically generated by GitHub Actions*


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54903][PYTHON] Make to_arrow_schema/to_arrow_type able to set timezone [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng closed pull request #53678: [SPARK-54903][PYTHON] Make to_arrow_schema/to_arrow_type able to set timezone
URL: https://github.com/apache/spark/pull/53678


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54903][PYTHON] Make to_arrow_schema/to_arrow_type able to set timezone [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #53678:
URL: https://github.com/apache/spark/pull/53678#discussion_r2660911125


##########
python/pyspark/sql/pandas/types.py:
##########
@@ -115,21 +111,15 @@ def to_arrow_type(
         arrow_type = pa.float64()
     elif type(dt) == DecimalType:
         arrow_type = pa.decimal128(dt.precision, dt.scale)
-    elif type(dt) == StringType and prefers_large_types:
-        arrow_type = pa.large_string()
     elif type(dt) == StringType:
-        arrow_type = pa.string()
-    elif type(dt) == BinaryType and prefers_large_types:
-        arrow_type = pa.large_binary()
+        arrow_type = pa.large_string() if prefers_large_types else pa.string()
     elif type(dt) == BinaryType:
-        arrow_type = pa.binary()
+        arrow_type = pa.large_binary() if prefers_large_types else pa.binary()
     elif type(dt) == DateType:
         arrow_type = pa.date32()
-    elif type(dt) == TimestampType and timestamp_utc:
-        # Timestamps should be in UTC, JVM Arrow timestamps require a timezone to be read

Review Comment:
   And the JVM side does send the timestamps with timezone to the Python side, see the test https://github.com/apache/spark/blob/b4c84c917aee673d26162a92bd95afc743ea7684/python/pyspark/sql/tests/arrow/test_arrow_udf.py#L103-L160 



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54903][PYTHON] Make to_arrow_schema/to_arrow_type able to set timezone [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #53678:
URL: https://github.com/apache/spark/pull/53678#discussion_r2660905321


##########
python/pyspark/sql/pandas/types.py:
##########
@@ -115,21 +111,15 @@ def to_arrow_type(
         arrow_type = pa.float64()
     elif type(dt) == DecimalType:
         arrow_type = pa.decimal128(dt.precision, dt.scale)
-    elif type(dt) == StringType and prefers_large_types:
-        arrow_type = pa.large_string()
     elif type(dt) == StringType:
-        arrow_type = pa.string()
-    elif type(dt) == BinaryType and prefers_large_types:
-        arrow_type = pa.large_binary()
+        arrow_type = pa.large_string() if prefers_large_types else pa.string()
     elif type(dt) == BinaryType:
-        arrow_type = pa.binary()
+        arrow_type = pa.large_binary() if prefers_large_types else pa.binary()
     elif type(dt) == DateType:
         arrow_type = pa.date32()
-    elif type(dt) == TimestampType and timestamp_utc:
-        # Timestamps should be in UTC, JVM Arrow timestamps require a timezone to be read

Review Comment:
   this comment seems not ture, the pyarrow timestamps always store UTC time as the underlying values, but it doesn't require the timezone to be UTC
   
   
   ```
   In [32]: ts1 = datetime.datetime(2026, 1, 5, 15, 0, 1, tzinfo=ZoneInfo('Asia/Singapore'))
   
   In [33]: ts2 = datetime.datetime(2026, 1, 5, 16, 0, 1, tzinfo=ZoneInfo('Asia/Tokyo'))
   
   In [34]: s1 = pa.scalar(ts1)
   
   In [35]: s2 = pa.scalar(ts2)
   
   In [36]: s1 == s2
   Out[36]: False
   
   In [37]: s1.value
   Out[37]: 1767596401000000
   
   In [38]: s2.value
   Out[38]: 1767596401000000
   
   In [39]: s3 = pa.compute.cast(s1, pa.timestamp('us', tz="UTC"))
   
   In [40]: s3.value
   Out[40]: 1767596401000000
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54903][PYTHON] Make to_arrow_schema/to_arrow_type able to set timezone [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #53678:
URL: https://github.com/apache/spark/pull/53678#issuecomment-3712795882

   thanks, merged to master


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-54903][PYTHON] Make to_arrow_schema/to_arrow_type able to set timezone [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #53678:
URL: https://github.com/apache/spark/pull/53678#discussion_r2660963806


##########
python/pyspark/sql/tests/arrow/test_arrow.py:
##########
@@ -1538,22 +1538,6 @@ def check_createDataFrame_pandas_nested_timestamp(self, arrow_enabled):
 
         self.assertEqual(df.first(), expected)
 
-    def test_createDataFrame_arrow_nested_timestamp(self):
-        from pyspark.sql.pandas.types import to_arrow_schema
-
-        schema = self.schema_nested_timestamp
-        data = self.data_nested_timestamp
-        pdf = pd.DataFrame.from_records(data, columns=schema.names)
-        arrow_schema = to_arrow_schema(schema, timestamp_utc=False)

Review Comment:
   this test converts timezone aware timestampe as a timestampeNTZ, seems not make sense



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org