You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/09/23 03:41:27 UTC

[GitHub] [spark] itholic commented on a diff in pull request #37966: [SPARK-40462][PYTHON] Support np.ndarray for `functions.lit`.

itholic commented on code in PR #37966:
URL: https://github.com/apache/spark/pull/37966#discussion_r978243957


##########
python/pyspark/sql/functions.py:
##########
@@ -164,13 +166,24 @@ def lit(col: Any) -> Column:
     +--------------+
     |     [1, 2, 3]|
     +--------------+
+
+    Create a literal from numpy.ndarray.
+
+    >>> spark.range(1).select(lit(np.ndarray([1, 2]))).show(truncate=False)
+    +------------------------------------------------------+
+    |array(array(2.058335917824E-312, 2.334195370625E-312))|
+    +------------------------------------------------------+
+    |[[2.058335917824E-312, 2.334195370625E-312]]          |

Review Comment:
   Seems like the type in this example is correct since `np.ndarray([1, 2])` returns `np.float64` as below:
   
   e.g.
   ```python
   >>> np.ndarray([1, 2])
   array([[2.05833592e-312, 2.33419537e-312]])
   >>> np.ndarray([1, 2]).dtype
   dtype('float64')
   >>> spark.range(1).select(lit(np.ndarray([1, 2]))).dtypes
   [('array(array(6.94998724E-310, 6.95001394E-310))', 'array<array<double>>')]
   ```
   
   However, they do not respect NumPy types in  other cases as below:
   
   ```python
   >>> spark.range(1).select(lit(np.array([[np.int8(1), np.int8(2)]]))).dtypes
   [('array(array(1, 2))', 'array<array<int>>')]
   ```
   
   This should return `smallint` rather than `int` as below:
   
   ```python
   >>> spark.range(1).select(lit(np.array([[np.int8(1), np.int8(2)]]))).dtypes
   [('ARRAY(ARRAY(1S, 2S))', 'array<array<smallint>>')]
   ```
   
   So, I think the PR title should be addressed such as "Support np.ndarray for `functions.lit` for multi dimensions"
   
   Now we only respect the NumPy type only for the single dimension.
   
   Let me address it.



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