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
67 changes: 67 additions & 0 deletions python/pyspark/sql/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
FloatType,
DateType,
TimestampType,
TimestampNTZType,
DayTimeIntervalType,
YearMonthIntervalType,
CalendarIntervalType,
Expand Down Expand Up @@ -1411,6 +1412,72 @@ def test_tree_string(self):
],
)

def test_tree_string_for_builtin_types(self):
schema = (
StructType()
.add("n", NullType())
.add("str", StringType())
.add("c", CharType(10))
.add("v", VarcharType(10))
.add("bin", BinaryType())
.add("bool", BooleanType())
.add("date", DateType())
.add("ts", TimestampType())
.add("ts_ntz", TimestampNTZType())
.add("dec", DecimalType(10, 2))
.add("double", DoubleType())
.add("float", FloatType())
.add("long", LongType())
.add("int", IntegerType())
.add("short", ShortType())
.add("byte", ByteType())
.add("ym_interval_1", YearMonthIntervalType())
.add("ym_interval_2", YearMonthIntervalType(YearMonthIntervalType.YEAR))
.add(
"ym_interval_3",
YearMonthIntervalType(YearMonthIntervalType.YEAR, YearMonthIntervalType.MONTH),
)
.add("dt_interval_1", DayTimeIntervalType())
.add("dt_interval_2", DayTimeIntervalType(DayTimeIntervalType.DAY))
.add(
"dt_interval_3",
DayTimeIntervalType(DayTimeIntervalType.HOUR, DayTimeIntervalType.SECOND),
)
.add("cal_interval", CalendarIntervalType())
.add("var", VariantType())
)
self.assertEqual(
schema.treeString().split("\n"),
[
"root",
" |-- n: void (nullable = true)",
" |-- str: string (nullable = true)",
" |-- c: char(10) (nullable = true)",
" |-- v: varchar(10) (nullable = true)",
" |-- bin: binary (nullable = true)",
" |-- bool: boolean (nullable = true)",
" |-- date: date (nullable = true)",
" |-- ts: timestamp (nullable = true)",
" |-- ts_ntz: timestamp_ntz (nullable = true)",
" |-- dec: decimal(10,2) (nullable = true)",
" |-- double: double (nullable = true)",
" |-- float: float (nullable = true)",
" |-- long: long (nullable = true)",
" |-- int: integer (nullable = true)",
" |-- short: short (nullable = true)",
" |-- byte: byte (nullable = true)",
" |-- ym_interval_1: interval year to month (nullable = true)",
" |-- ym_interval_2: interval year (nullable = true)",
" |-- ym_interval_3: interval year to month (nullable = true)",
" |-- dt_interval_1: interval day to second (nullable = true)",
" |-- dt_interval_2: interval day (nullable = true)",
" |-- dt_interval_3: interval hour to second (nullable = true)",
" |-- cal_interval: interval (nullable = true)",
" |-- var: variant (nullable = true)",
"",
],
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

checked with scala side:

scala> val schema = new StructType().add("n", NullType).add("str", StringType).add("c", CharType(10)).add("v", VarcharType(10)).add("bin", BinaryType).add("bool", BooleanType).add("date", DateType).add("ts", TimestampType).add("ts_ntz", TimestampNTZType).add("dec", DecimalType(10, 2)).add("double", DoubleType).add("float", FloatType).add("long", LongType).add("int", IntegerType).add("short", ShortType).add("byte", ByteType).add("ym_interval_1", YearMonthIntervalType()).add("ym_interval_2", YearMonthIntervalType(YearMonthIntervalType.YEAR)).add("ym_interval_3",YearMonthIntervalType(YearMonthIntervalType.YEAR, YearMonthIntervalType.MONTH)).add("dt_interval_1", DayTimeIntervalType()).add("dt_interval_2", DayTimeIntervalType(DayTimeIntervalType.DAY)).add("dt_interval_3", DayTimeIntervalType(DayTimeIntervalType.HOUR, DayTimeIntervalType.SECOND)).add("cal_interval", CalendarIntervalType).add("var", VariantType)
val schema: org.apache.spark.sql.types.StructType = StructType(StructField(n,NullType,true),StructField(str,StringType,true),StructField(c,CharType(10),true),StructField(v,VarcharType(10),true),StructField(bin,BinaryType,true),StructField(bool,BooleanType,true),StructField(date,DateType,true),StructField(ts,TimestampType,true),StructField(ts_ntz,TimestampNTZType,true),StructField(dec,DecimalType(10,2),true),StructField(double,DoubleType,true),StructField(float,FloatType,true),StructField(long,LongType,true),StructField(int,IntegerType,true),StructField(short,ShortType,true),StructField(byte,ByteType,true),StructField(ym_interval_1,YearMonthIntervalType(0,1),true),StructField(ym_interval_2,YearMonthIntervalType(0,0),true),StructField(ym_interval_3,YearMonthInter...

scala> println(schema.treeString)
root
 |-- n: void (nullable = true)
 |-- str: string (nullable = true)
 |-- c: char(10) (nullable = true)
 |-- v: varchar(10) (nullable = true)
 |-- bin: binary (nullable = true)
 |-- bool: boolean (nullable = true)
 |-- date: date (nullable = true)
 |-- ts: timestamp (nullable = true)
 |-- ts_ntz: timestamp_ntz (nullable = true)
 |-- dec: decimal(10,2) (nullable = true)
 |-- double: double (nullable = true)
 |-- float: float (nullable = true)
 |-- long: long (nullable = true)
 |-- int: integer (nullable = true)
 |-- short: short (nullable = true)
 |-- byte: byte (nullable = true)
 |-- ym_interval_1: interval year to month (nullable = true)
 |-- ym_interval_2: interval year (nullable = true)
 |-- ym_interval_3: interval year to month (nullable = true)
 |-- dt_interval_1: interval day to second (nullable = true)
 |-- dt_interval_2: interval day (nullable = true)
 |-- dt_interval_3: interval hour to second (nullable = true)
 |-- cal_interval: interval (nullable = true)
 |-- var: variant (nullable = true)


def test_metadata_null(self):
schema = StructType(
[
Expand Down
27 changes: 23 additions & 4 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ def _data_type_build_formatted_string(
if isinstance(dataType, (ArrayType, StructType, MapType)):
dataType._build_formatted_string(prefix, stringConcat, maxDepth - 1)

# The method typeName() is not always the same as the Scala side.
# Add this helper method to make TreeString() compatible with Scala side.
@classmethod
def _get_jvm_type_name(cls, dataType: "DataType") -> str:
if isinstance(
dataType,
(
DecimalType,
CharType,
VarcharType,
DayTimeIntervalType,
YearMonthIntervalType,
),
):
return dataType.simpleString()
else:
return dataType.typeName()


# This singleton pattern does not work with pickle, you will get
# another object after pickle and unpickle
Expand Down Expand Up @@ -758,7 +776,7 @@ def _build_formatted_string(
) -> None:
if maxDepth > 0:
stringConcat.append(
f"{prefix}-- element: {self.elementType.typeName()} "
f"{prefix}-- element: {DataType._get_jvm_type_name(self.elementType)} "
+ f"(containsNull = {str(self.containsNull).lower()})\n"
)
DataType._data_type_build_formatted_string(
Expand Down Expand Up @@ -906,12 +924,12 @@ def _build_formatted_string(
maxDepth: int = JVM_INT_MAX,
) -> None:
if maxDepth > 0:
stringConcat.append(f"{prefix}-- key: {self.keyType.typeName()}\n")
stringConcat.append(f"{prefix}-- key: {DataType._get_jvm_type_name(self.keyType)}\n")
DataType._data_type_build_formatted_string(
self.keyType, f"{prefix} |", stringConcat, maxDepth
)
stringConcat.append(
f"{prefix}-- value: {self.valueType.typeName()} "
f"{prefix}-- value: {DataType._get_jvm_type_name(self.valueType)} "
+ f"(valueContainsNull = {str(self.valueContainsNull).lower()})\n"
)
DataType._data_type_build_formatted_string(
Expand Down Expand Up @@ -1074,7 +1092,8 @@ def _build_formatted_string(
) -> None:
if maxDepth > 0:
stringConcat.append(
f"{prefix}-- {escape_meta_characters(self.name)}: {self.dataType.typeName()} "
f"{prefix}-- {escape_meta_characters(self.name)}: "
+ f"{DataType._get_jvm_type_name(self.dataType)} "
+ f"(nullable = {str(self.nullable).lower()})\n"
)
DataType._data_type_build_formatted_string(
Expand Down