You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by su...@apache.org on 2026/08/31 16:45:38 UTC
(datafusion-comet) branch main updated: fix: accept UTC timezone aliases in Python Arrow input (#5556)
This is an automated email from the ASF dual-hosted git repository.
sunchao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-comet.git
The following commit(s) were added to refs/heads/main by this push:
new dee94025ba fix: accept UTC timezone aliases in Python Arrow input (#5556)
dee94025ba is described below
commit dee94025ba101a0b148dd13e2039a2d6a24efe5f
Author: Chao Sun <su...@apache.org>
AuthorDate: Mon Aug 31 09:45:34 2026 -0700
fix: accept UTC timezone aliases in Python Arrow input (#5556)
* fix: accept UTC timezone aliases in Python Arrow input
* refactor: clarify Arrow type compatibility helper name
---
.../python/CometArrowPythonRunnerBase.scala | 18 +++++-
.../src/test/resources/pyspark/test_pyarrow_udf.py | 53 ++++++++++++++++
.../python/CometArrowPythonRunnerSuite.scala | 71 ++++++++++++++++++++++
3 files changed, 141 insertions(+), 1 deletion(-)
diff --git a/spark/src/main/spark-4.x/org/apache/spark/sql/execution/python/CometArrowPythonRunnerBase.scala b/spark/src/main/spark-4.x/org/apache/spark/sql/execution/python/CometArrowPythonRunnerBase.scala
index 943287cde1..7ea4a56b41 100644
--- a/spark/src/main/spark-4.x/org/apache/spark/sql/execution/python/CometArrowPythonRunnerBase.scala
+++ b/spark/src/main/spark-4.x/org/apache/spark/sql/execution/python/CometArrowPythonRunnerBase.scala
@@ -334,10 +334,26 @@ private[python] object CometArrowPythonRunnerBase {
ArrowType.ExtensionType.EXTENSION_METADATA_KEY_NAME,
ArrowType.ExtensionType.EXTENSION_METADATA_KEY_METADATA)
+ private def areArrowTypesCompatible(expected: ArrowType, actual: ArrowType): Boolean = {
+ if (expected == actual) {
+ true
+ } else {
+ (expected, actual) match {
+ case (left: ArrowType.Timestamp, right: ArrowType.Timestamp) =>
+ // Native scans use UTC, while date_trunc can retain the equivalent Etc/UTC session
+ // zone. Both interpret the same instants, so preserve the stream schema and buffers.
+ left.getUnit == right.getUnit &&
+ ((left.getTimezone == "UTC" && right.getTimezone == "Etc/UTC") ||
+ (left.getTimezone == "Etc/UTC" && right.getTimezone == "UTC"))
+ case _ => false
+ }
+ }
+ }
+
/** Names, nullability and ordinary field metadata do not change the IPC buffer layout. */
private[python] def hasCompatibleSchema(expected: Seq[Field], actual: Seq[Field]): Boolean = {
expected.size == actual.size && expected.zip(actual).forall { case (left, right) =>
- left.getType == right.getType &&
+ areArrowTypesCompatible(left.getType, right.getType) &&
left.getDictionary == right.getDictionary &&
extensionMetadataKeys.forall(key =>
left.getMetadata.get(key) == right.getMetadata.get(key)) &&
diff --git a/spark/src/test/resources/pyspark/test_pyarrow_udf.py b/spark/src/test/resources/pyspark/test_pyarrow_udf.py
index 5c25cb03ba..9a54300eed 100644
--- a/spark/src/test/resources/pyspark/test_pyarrow_udf.py
+++ b/spark/src/test/resources/pyspark/test_pyarrow_udf.py
@@ -652,6 +652,59 @@ def test_map_in_batch_union_with_compatible_nested_fields(
assert Counter(map(repr, result.collect())) == expected
+@pytest.mark.parametrize("api", ["mapInArrow", "mapInPandas"])
+@pytest.mark.parametrize("timezone", ["UTC", "Etc/UTC"])
+@pytest.mark.parametrize(
+ "truncated_first", [False, True], ids=["scan-first", "truncation-first"]
+)
+def test_map_in_batch_union_with_utc_timezone_alias(
+ spark, tmp_path, accelerated, api, timezone, truncated_first
+):
+ """Native scans use UTC, while date_trunc preserves the equivalent Etc/UTC alias."""
+ previous_timezone = spark.conf.get("spark.sql.session.timeZone")
+ spark.conf.set("spark.sql.session.timeZone", timezone)
+ try:
+ schema = T.StructType(
+ [
+ T.StructField("id", T.LongType()),
+ T.StructField("ts", T.TimestampType()),
+ ]
+ )
+ rows = [
+ (0, None),
+ (1, dt.datetime(2024, 1, 2, 3, 4, 5, 123456)),
+ (2, dt.datetime(2024, 1, 2, 23, 59, 59, 999999)),
+ (3, dt.datetime(2024, 1, 3)),
+ ]
+ src = str(tmp_path / "union_timestamp_timezones.parquet")
+ spark.createDataFrame(rows, schema).coalesce(1).write.parquet(src)
+ source = spark.read.parquet(src)
+ truncated = source.selectExpr("id", "date_trunc('day', ts) AS ts")
+ left, right = (truncated, source) if truncated_first else (source, truncated)
+ # Put both source schemas in one worker's input stream, in either order.
+ combined = left.union(right).coalesce(1)
+ expected = Counter(tuple(row) for row in combined.collect())
+ assert sum(expected.values()) == 2 * len(rows)
+ assert expected[(0, None)] == 2
+
+ def passthrough(iterator):
+ yield from iterator
+
+ result = getattr(combined, api)(passthrough, combined.schema)
+ plan = _executed_plan(result)
+ _assert_plan_matches_mode(
+ plan,
+ accelerated,
+ vanilla_node="MapInArrow" if api == "mapInArrow" else "MapInPandas",
+ )
+ assert "CometUnion" in plan
+ assert "CometCoalesce" in plan
+ assert "CometProject" in plan
+ assert Counter(tuple(row) for row in result.collect()) == expected
+ finally:
+ spark.conf.set("spark.sql.session.timeZone", previous_timezone)
+
+
@pytest.mark.parametrize("api", ["mapInArrow", "mapInPandas"])
def test_map_in_batch_union_with_different_field_metadata(
spark, tmp_path, accelerated, api
diff --git a/spark/src/test/spark-4.x/org/apache/spark/sql/execution/python/CometArrowPythonRunnerSuite.scala b/spark/src/test/spark-4.x/org/apache/spark/sql/execution/python/CometArrowPythonRunnerSuite.scala
index c20deff471..04c0a16cc6 100644
--- a/spark/src/test/spark-4.x/org/apache/spark/sql/execution/python/CometArrowPythonRunnerSuite.scala
+++ b/spark/src/test/spark-4.x/org/apache/spark/sql/execution/python/CometArrowPythonRunnerSuite.scala
@@ -34,6 +34,7 @@ import org.apache.arrow.memory.{BufferAllocator, RootAllocator}
import org.apache.arrow.vector.{FieldVector, IntVector, NullVector, VarCharVector, VectorSchemaRoot}
import org.apache.arrow.vector.complex.{ListVector, MapVector, StructVector}
import org.apache.arrow.vector.ipc.{ArrowStreamReader, ArrowStreamWriter, WriteChannel}
+import org.apache.arrow.vector.types.TimeUnit
import org.apache.arrow.vector.types.pojo.{ArrowType, DictionaryEncoding, Field, FieldType, Schema}
import org.apache.spark.sql.execution.python.CometArrowPythonRunnerBase.{hasCompatibleSchema, serializeBatch}
@@ -124,6 +125,76 @@ class CometArrowPythonRunnerSuite extends AnyFunSuite with Matchers {
fields(dictionary = new DictionaryEncoding(2L, false, intType))) shouldBe false
}
+ test("input schema compatibility accepts UTC timestamp aliases with matching units") {
+ def fields(unit: TimeUnit, timezone: String): Seq[Field] =
+ Seq(new Field("ts", FieldType.nullable(new ArrowType.Timestamp(unit, timezone)), null))
+ def nested(children: Seq[Field]): Seq[Field] =
+ Seq(new Field("outer", FieldType.nullable(ArrowType.Struct.INSTANCE), children.asJava))
+
+ for (unit <- TimeUnit.values()) {
+ val utc = fields(unit, "UTC")
+ val alias = fields(unit, "Etc/UTC")
+ hasCompatibleSchema(utc, alias) shouldBe true
+ hasCompatibleSchema(alias, utc) shouldBe true
+ hasCompatibleSchema(nested(utc), nested(alias)) shouldBe true
+ hasCompatibleSchema(nested(alias), nested(utc)) shouldBe true
+ }
+
+ for {
+ expectedUnit <- TimeUnit.values()
+ actualUnit <- TimeUnit.values()
+ if expectedUnit != actualUnit
+ } {
+ hasCompatibleSchema(
+ fields(expectedUnit, "UTC"),
+ fields(actualUnit, "Etc/UTC")) shouldBe false
+ hasCompatibleSchema(
+ fields(expectedUnit, "Etc/UTC"),
+ fields(actualUnit, "UTC")) shouldBe false
+ }
+ }
+
+ test("UTC timestamp aliases preserve timezone, dictionary and extension constraints") {
+ def fields(
+ timezone: String,
+ metadata: Map[String, String] = Map.empty,
+ dictionary: DictionaryEncoding = null): Seq[Field] = Seq(
+ new Field(
+ "ts",
+ new FieldType(
+ true,
+ new ArrowType.Timestamp(TimeUnit.MICROSECOND, timezone),
+ dictionary,
+ metadata.asJava),
+ null))
+
+ for {
+ utc <- Seq("UTC", "Etc/UTC")
+ other <- Seq(null, "", "GMT", "+00:00", "America/Los_Angeles")
+ } {
+ hasCompatibleSchema(fields(utc), fields(other)) shouldBe false
+ hasCompatibleSchema(fields(other), fields(utc)) shouldBe false
+ }
+ hasCompatibleSchema(fields(null), fields(null)) shouldBe true
+ hasCompatibleSchema(
+ fields("America/Los_Angeles"),
+ fields("America/Los_Angeles")) shouldBe true
+
+ val dictionary = new DictionaryEncoding(1L, false, new ArrowType.Int(32, true))
+ val otherDictionary = new DictionaryEncoding(2L, false, dictionary.getIndexType)
+ hasCompatibleSchema(fields("UTC", dictionary = dictionary), fields("Etc/UTC")) shouldBe false
+ hasCompatibleSchema(
+ fields("UTC", dictionary = dictionary),
+ fields("Etc/UTC", dictionary = otherDictionary)) shouldBe false
+ Seq(
+ ArrowType.ExtensionType.EXTENSION_METADATA_KEY_NAME,
+ ArrowType.ExtensionType.EXTENSION_METADATA_KEY_METADATA).foreach { key =>
+ hasCompatibleSchema(
+ fields("UTC", Map(key -> "before")),
+ fields("Etc/UTC", Map(key -> "after"))) shouldBe false
+ }
+ }
+
test("direct batches retain borrowed buffers without copying them into the writer allocator") {
val sourceAllocator = new RootAllocator(Long.MaxValue)
val writerAllocator = new RootAllocator(1024)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org