Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions python/pyspark/sql/connect/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,20 @@ def to_plan(self, session: "SparkConnectClient") -> "proto.Expression":
def __repr__(self) -> str:
if self._value is None:
return "NULL"
else:
return f"{self._value}"
elif isinstance(self._dataType, DateType):
dt = DateType().fromInternal(self._value)
if dt is not None and isinstance(dt, datetime.date):
return dt.strftime("%Y-%m-%d")
elif isinstance(self._dataType, TimestampType):
ts = TimestampType().fromInternal(self._value)
if ts is not None and isinstance(ts, datetime.datetime):
return ts.strftime("%Y-%m-%d %H:%M:%S.%f")
elif isinstance(self._dataType, TimestampNTZType):
ts = TimestampNTZType().fromInternal(self._value)
if ts is not None and isinstance(ts, datetime.datetime):
return ts.strftime("%Y-%m-%d %H:%M:%S.%f")
# TODO(SPARK-49693): Refine the string representation of timedelta
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't find a standard method to convert timedelta to a ISO string like the JVM side

return f"{self._value}"


class ColumnReference(Expression):
Expand Down
9 changes: 9 additions & 0 deletions python/pyspark/sql/tests/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from enum import Enum
from itertools import chain
import datetime

from pyspark.sql import Column, Row
from pyspark.sql import functions as sf
from pyspark.sql.types import StructType, StructField, IntegerType, LongType
Expand Down Expand Up @@ -280,6 +282,13 @@ def test_expr_str_representation(self):
when_cond = sf.when(expression, sf.lit(None))
self.assertEqual(str(when_cond), "Column<'CASE WHEN foo THEN NULL END'>")

def test_lit_time_representation(self):
dt = datetime.date(2021, 3, 4)
self.assertEqual(str(sf.lit(dt)), "Column<'2021-03-04'>")

ts = datetime.datetime(2021, 3, 4, 12, 34, 56, 1234)
self.assertEqual(str(sf.lit(ts)), "Column<'2021-03-04 12:34:56.001234'>")

def test_enum_literals(self):
class IntEnum(Enum):
X = 1
Expand Down