You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "HyukjinKwon (via GitHub)" <gi...@apache.org> on 2023/09/12 02:34:48 UTC

[GitHub] [spark] HyukjinKwon commented on a diff in pull request #42875: [SPARK-45119][PYTHON][DOCS] Refine docstring of inline

HyukjinKwon commented on code in PR #42875:
URL: https://github.com/apache/spark/pull/42875#discussion_r1322290305


##########
python/pyspark/sql/functions.py:
##########
@@ -12451,37 +12451,132 @@ def inline(col: "ColumnOrName") -> Column:
     """
     Explodes an array of structs into a table.
 
+    This function takes an input column containing an array of structs and returns a
+    new column where each struct in the array is exploded into a separate row.
+
     .. versionadded:: 3.4.0
 
     Parameters
     ----------
     col : :class:`~pyspark.sql.Column` or str
-        input column of values to explode.
+        Input column of values to explode.
 
     Returns
     -------
     :class:`~pyspark.sql.Column`
-        generator expression with the inline exploded result.
+        Generator expression with the inline exploded result.
 
     See Also
     --------
-    :meth:`explode`
-
-    Notes
-    -----
-    Supports Spark Connect.
+    :meth:`pyspark.functions.explode`
+    :meth:`pyspark.functions.inline_outer`
 
     Examples
     --------
+    Example 1: Using inline with a single struct array column
+
+    >>> import pyspark.sql.functions as sf
+    >>> from pyspark.sql import Row
+    >>> df = spark.createDataFrame([Row(structlist=[Row(a=1, b=2), Row(a=3, b=4)])])
+    >>> df.select(sf.inline(df.structlist)).show()
+    +---+---+
+    |  a|  b|
+    +---+---+
+    |  1|  2|
+    |  3|  4|
+    +---+---+
+    
+    Example 2: Using inline with a column name
+
+    >>> import pyspark.sql.functions as sf
     >>> from pyspark.sql import Row
     >>> df = spark.createDataFrame([Row(structlist=[Row(a=1, b=2), Row(a=3, b=4)])])
-    >>> df.select(inline(df.structlist)).show()
+    >>> df.select(sf.inline("structlist")).show()
     +---+---+
     |  a|  b|
     +---+---+
     |  1|  2|
     |  3|  4|
     +---+---+
+
+    Example 3: Using inline with an alias
+
+    >>> import pyspark.sql.functions as sf
+    >>> from pyspark.sql import Row
+    >>> df = spark.createDataFrame([Row(structlist=[Row(a=1, b=2), Row(a=3, b=4)])])
+    >>> df.select(sf.inline("structlist").alias("c1", "c2")).show()
+    +---+---+
+    | c1| c2|
+    +---+---+
+    |  1|  2|
+    |  3|  4|
+    +---+---+
+
+    Example 4: Using inline with multiple struct array columns
+
+    >>> import pyspark.sql.functions as sf
+    >>> from pyspark.sql import Row
+    >>> df = spark.createDataFrame([
+    ...     Row(structlist1=[Row(a=1, b=2), Row(a=3, b=4)],
+    ...         structlist2=[Row(c=5, d=6), Row(c=7, d=8)])
+    ... ])
+    >>> df.select(sf.inline("structlist1"), "structlist2") \
+    ...     .select("a", "b", sf.inline("structlist2")).show()
+    +---+---+---+---+
+    |  a|  b|  c|  d|
+    +---+---+---+---+
+    |  1|  2|  5|  6|
+    |  1|  2|  7|  8|
+    |  3|  4|  5|  6|
+    |  3|  4|  7|  8|
+    +---+---+---+---+
+
+    Example 5: Using inline with a nested struct array column
+
+    >>> import pyspark.sql.functions as sf
+    >>> from pyspark.sql import Row
+    >>> df = spark.createDataFrame([
+    ...     Row(structlist=[Row(a=1, b=2, nested=[Row(c=3, d=4), Row(c=5, d=6)])])
+    ... ])
+    >>> df.select(sf.inline("structlist")).show()
+    +---+---+----------------+
+    |  a|  b|          nested|
+    +---+---+----------------+
+    |  1|  2|[{3, 4}, {5, 6}]|
+    +---+---+----------------+
+    >>> df.select(sf.inline("structlist")).select(sf.inline("nested")).show()

Review Comment:
   ```
   >>> df.select("structlist.nested").show()
   +------------------+
   |            nested|
   +------------------+
   |[[{3, 4}, {5, 6}]]|
   +------------------+
   ```
   
   it fails because `structlist.nested` is an array of an array of structs.
   
   
   ```
   >>> df.select(sf.inline("structlist")).show()
   +---+---+----------------+
   |  a|  b|          nested|
   +---+---+----------------+
   |  1|  2|[{3, 4}, {5, 6}]|
   +---+---+----------------+
   >>> df.select(sf.inline("structlist")).select(sf.inline("nested")).show()
   +---+---+
   |  c|  d|
   +---+---+
   |  3|  4|
   |  5|  6|
   +---+---+
   ```
   
   works because `nested` is an array of structs (?)
   



-- 
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