You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhengruifeng (via GitHub)" <gi...@apache.org> on 2024/05/23 03:26:48 UTC

[PR] [SPARK-48395][PYTHON] Fix `StructType.treeString` for parameterized types [spark]

zhengruifeng opened a new pull request, #46711:
URL: https://github.com/apache/spark/pull/46711

   ### What changes were proposed in this pull request?
   this PR is a follow up of https://github.com/apache/spark/pull/46685.
   
   
   
   
   ### Why are the changes needed?
   `StructType.treeString` uses `DataType.typeName` to generate the tree string, however, the `typeName` in python is a class method and can not return the same string for parameterized types. 
   
   ```
   In [2]: schema = StructType().add("c", CharType(10), True).add("v", VarcharType(10), True).add("d", DecimalType(10, 2), True).add("ym00", YearM
      ...: onthIntervalType(0, 0)).add("ym01", YearMonthIntervalType(0, 1)).add("ym11", YearMonthIntervalType(1, 1))
   
   In [3]: print(schema.treeString())
   root
    |-- c: char (nullable = true)
    |-- v: varchar (nullable = true)
    |-- d: decimal (nullable = true)
    |-- ym00: yearmonthinterval (nullable = true)
    |-- ym01: yearmonthinterval (nullable = true)
    |-- ym11: yearmonthinterval (nullable = true)
   ```
   
   it should be
   ```
   In [4]: print(schema.treeString())
   root
    |-- c: char(10) (nullable = true)
    |-- v: varchar(10) (nullable = true)
    |-- d: decimal(10,2) (nullable = true)
    |-- ym00: interval year (nullable = true)
    |-- ym01: interval year to month (nullable = true)
    |-- ym11: interval month (nullable = true)
   ```
   
   
   
   ### Does this PR introduce _any_ user-facing change?
   no, this feature was just added and not release out yet.
   
   
   ### How was this patch tested?
   added tests
   
   ### Was this patch authored or co-authored using generative AI tooling?
   no


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-48395][PYTHON] Fix `StructType.treeString` for parameterized types [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng closed pull request #46711: [SPARK-48395][PYTHON] Fix `StructType.treeString` for parameterized types
URL: https://github.com/apache/spark/pull/46711


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-48395][PYTHON] Fix `StructType.treeString` for parameterized types [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #46711:
URL: https://github.com/apache/spark/pull/46711#discussion_r1610916678


##########
python/pyspark/sql/tests/test_types.py:
##########
@@ -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)",
+                "",
+            ],
+        )

Review Comment:
   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)
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-48395][PYTHON] Fix `StructType.treeString` for parameterized types [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #46711:
URL: https://github.com/apache/spark/pull/46711#issuecomment-2126499594

   merged to master


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org