spark-test commented on code in PR #60578:
URL: https://github.com/apache/spark/pull/60578#discussion_r4594226015


##########
python/pyspark/sql/tests/test_observation.py:
##########
@@ -257,6 +259,36 @@ def test_observe_with_map_type(self):
 
         self.assertEqual(observation.get, {"map": {"count": 10}})
 
+    def test_observe_with_various_types(self):
+        observation = Observation("types")
+
+        df = self.spark.range(10).observe(
+            observation,
+            F.max(F.lit(None).cast("int")).alias("null"),
+            F.max(F.col("id").cast("byte")).alias("byte"),
+            F.max(F.col("id").cast("short")).alias("short"),
+            F.max(F.col("id").cast("float")).alias("float"),
+            F.max(F.lit("2024-01-02 
03:04:05").cast("timestamp_ntz")).alias("ntz"),
+            F.struct(
+                F.struct(F.max("id").alias("maxid")).alias("inner"),
+                F.create_map(F.lit("count"), F.count(F.lit(1))).alias("map"),
+            ).alias("nested"),
+        )
+
+        assertDataFrameEqual(df, [Row(id=id) for id in range(10)])
+
+        self.assertEqual(
+            observation.get,
+            {
+                "null": None,
+                "byte": 9,
+                "short": 9,
+                "float": 9.0,
+                "ntz": datetime.datetime(2024, 1, 2, 3, 4, 5),

Review Comment:
   **Nit (P3):** This expectation holds whether the Spark Connect client 
decodes the metric as a timestamp_ntz value or as a local-time timestamp, as 
long as the test process runs in UTC, which is likely what CI uses: the PySpark 
CI jobs, `ReusedSQLTestCase`, and `ReusedConnectTestCase` set no zone. 
`TimestampNTZType.fromInternal` (`types.py:521-525`) converts the micros in 
UTC, while `TimestampType.fromInternal` (`types.py:500-503`) uses the 
process-local zone, so under `TZ=UTC` both turn this metric into `2024-01-02 
03:04:05`. Under `America/Los_Angeles` the local-time decode would give 
`2024-01-01 19:04:05`. If the timestamp_ntz branch at `expressions.py:427` were 
changed to the adjacent timestamp branch's `TimestampType().fromInternal(...)`, 
or the server sent this metric as a `timestamp` literal, CI would still pass 
while Connect users outside UTC got shifted values. Classic's shared NTZ 
decoder is already checked in a pinned America/Los_Angeles zone by 
`check_create_data_frame_
 to_pandas_timestamp_ntz` in the Arrow tests, so the gap is the Connect branch, 
which only this test runs.
   
   With the zones pinned, this check can fail on a zone-shifted value. Set the 
process zone to a non-UTC zone and `spark.sql.session.timeZone` to a different 
one before the `assertDataFrameEqual` call, because the Connect client decodes 
the metrics while that action runs. Then restore both afterwards, as 
`test_create_dataframe_from_pandas_with_dst` does. This needs `import os` and 
`import time`:
   
   ```python
   orig_tz = os.environ.get("TZ")
   try:
       # Two different non-UTC zones, so a zone-shifted NTZ value fails the 
check.
       os.environ["TZ"] = "America/Los_Angeles"
       time.tzset()
       with self.sql_conf({"spark.sql.session.timeZone": "Asia/Tokyo"}):
           assertDataFrameEqual(df, [Row(id=id) for id in range(10)])
           self.assertEqual(observation.get, {...})  # the current expected 
dict, unchanged
   finally:
       if orig_tz is None:
           os.environ.pop("TZ", None)
       else:
           os.environ["TZ"] = orig_tz
       time.tzset()
   ```
   
   **Verification:**
   - **Behavior:** On both classic PySpark and Spark Connect, the observation 
test passes with the zones pinned: the timestamp_ntz metric comes back as the 
naive datetime 2024-01-02 03:04:05, and every other metric keeps its expected 
value.
   - **Regression:** With timestamp_ntz decoding temporarily switched to the 
local-time-zone decoder of the adjacent timestamp branch, the matching run of 
the test fails on its timestamp_ntz value even when the ambient process zone is 
UTC. Check the Spark Connect client's timestamp_ntz literal branch for the 
Connect run and the shared NTZ decoder for the classic run; the test passes 
again once the decoder is restored.
   - **Inspection:** The process zone and the session zone are pinned before 
the action that runs the observed query, are both non-UTC and differ from each 
other, and are restored, with `time.tzset()` called again after the process 
zone is reset, whether or not the test body raises, so later tests in the same 
process run in the original zones.



-- 
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]

Reply via email to