Skip to content

[SPARK-52837][CONNECT][PYTHON] Support TimeType in pyspark #51515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dengziming
Copy link
Member

@dengziming dengziming commented Jul 16, 2025

What changes were proposed in this pull request?

This is the follow-up of #51462 to support TimeType literal in pyspark connect.

Why are the changes needed?

To align the Python Connect client with the Java/Scala Connect client.

Does this PR introduce any user-facing change?

Yes, we can use TimeType literal in several ways, for example, PySparkSession.sql("SELECT TIME '12:13:14'")
and pyspark.sql.connect.functions.lit(datetime.time(12, 13, 14)).

How was this patch tested?

  1. Add some local literal convert tests
  2. Add pyspark SQL tests

Was this patch authored or co-authored using generative AI tooling?

No

@dengziming dengziming changed the title wip [SPARK-52779][CONNECT][PYTHON] Support TimeType literal in Connect [SPARK-52779][CONNECT][PYTHON] Support TimeType literal in Connect Jul 17, 2025
@dengziming
Copy link
Member Author

Hello @zhengruifeng @MaxGekk @peter-toth , please take a look at this, we should also support time literals in Connect.

@zhengruifeng zhengruifeng requested a review from HyukjinKwon July 17, 2025 06:13
Copy link
Member

@MaxGekk MaxGekk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dengziming Please, open new ticket specifically for the python client.

@dengziming dengziming changed the title [SPARK-52779][CONNECT][PYTHON] Support TimeType literal in Connect [SPARK-51162][CONNECT][PYTHON] Support TimeType literal in Connect Jul 17, 2025
@dengziming dengziming changed the title [SPARK-51162][CONNECT][PYTHON] Support TimeType literal in Connect [SPARK-52837][CONNECT][PYTHON] Support TimeType literal in Connect Jul 17, 2025
@dengziming
Copy link
Member Author

@MaxGekk I have created a separate ticket.

@@ -384,6 +385,33 @@ def fromInternal(self, v: int) -> datetime.date:
return datetime.date.fromordinal(v + self.EPOCH_ORDINAL)


class TimeType(AtomicType):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR actually add a new datatype in pyspark, not just python client.
It should work with both python client and pyspark classic.
We'd better also add test for pyspark classic.

Copy link
Contributor

@zhengruifeng zhengruifeng Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I just notice that the class hierarchy is not consistent with the JVM side:

case class TimeType(precision: Int) extends AnyTimeType

We'd better make them the same, by also introducing the AnyTimeType in pyspark. If we need to touch other existing date time types, we can do it in a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very professional with PySpark, this will take some time, I changed it to WIP by now.

@dengziming dengziming marked this pull request as draft July 18, 2025 02:23
@dengziming dengziming force-pushed the SPARK-52779 branch 2 times, most recently from 0942af7 to 84d55ae Compare July 18, 2025 07:49
@dengziming dengziming marked this pull request as ready for review July 18, 2025 12:10
@dengziming
Copy link
Member Author

@zhengruifeng Fortunately, it's not at all complicated to make TimeType and DatetimeType consistent with JVM side definitions, I have finished these changes, please take a look.

@dengziming dengziming changed the title [SPARK-52837][CONNECT][PYTHON] Support TimeType literal in Connect [SPARK-52837][CONNECT][PYTHON] Support TimeType literal in pyspark Jul 19, 2025
@dengziming
Copy link
Member Author

Hello @zhengruifeng @HyukjinKwon , please take a look at this PR when you are free, I have made a thorough check of the code.


EPOCH_ORDINAL = datetime.datetime(1970, 1, 1).toordinal()
class DatetimeType(AtomicType):
"""Super class of all datetime data type."""

def needConversion(self) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DatetimeType should not have needConversion


def needConversion(self) -> bool:
return True


class DateType(DatetimeType, metaclass=DataTypeSingleton):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateType's needConversion is missing

@@ -384,11 +390,47 @@ def fromInternal(self, v: int) -> datetime.date:
return datetime.date.fromordinal(v + self.EPOCH_ORDINAL)


class TimestampType(AtomicType, metaclass=DataTypeSingleton):
"""Timestamp (datetime.datetime) data type."""
class AnyTimeType(DatetimeType):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep the needConversion in each child class

@@ -1508,6 +1508,16 @@ def condition():

eventually(catch_assertions=True)(condition)()

def test_time_lit(self) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test seems redundant.
Tests in test_column and test_functions will be reused on connect mode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test file is mainly for directly comparing behavior between connect and classic

Copy link
Contributor

@zhengruifeng zhengruifeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general LGTM, left a few comments

@zhengruifeng zhengruifeng changed the title [SPARK-52837][CONNECT][PYTHON] Support TimeType literal in pyspark [SPARK-52837][CONNECT][PYTHON] Support TimeType in pyspark Jul 23, 2025
@dengziming
Copy link
Member Author

Thank you for the instructions @zhengruifeng . I fixed them, PTAL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants