zhengruifeng commented on code in PR #46901:
URL: https://github.com/apache/spark/pull/46901#discussion_r1639499951
##########
python/pyspark/sql/functions/builtin.py:
##########
@@ -17267,9 +17317,21 @@ def map_contains_key(col: "ColumnOrName", value: Any)
-> Column:
+--------------------------+
| false|
+--------------------------+
+
+ Example 3: Check for key using a column
+
+ >>> from pyspark.sql import functions as sf
+ >>> df = spark.sql("SELECT map(1, 'a', 2, 'b') as data, 1 as key")
+ >>> df.select(sf.map_contains_key("data", sf.col("key"))).show()
+ +---------------------------+
+ |map_contains_key(data, key)|
+ +---------------------------+
+ | true|
+ +---------------------------+
"""
from pyspark.sql.classic.column import _to_java_column
+ value = value._jc if isinstance(value, Column) else value
Review Comment:
I see, it was not supported in Classic mode, but supported in Connect mode.
Classic:
```
In [2]: df = spark.sql("select map(1, 2, 3, 4) as m, 1 as k")
...: df.select(F.map_contains_key(df.m, df.k))
---------------------------------------------------------------------------
PySparkTypeError Traceback (most recent call last)
Cell In[2], line 2
1 df = spark.sql("select map(1, 2, 3, 4) as m, 1 as k")
----> 2 df.select(F.map_contains_key(df.m, df.k))
...
File ~/Dev/spark/python/pyspark/sql/classic/column.py:415, in
Column.__iter__(self)
414 def __iter__(self) -> None:
--> 415 raise PySparkTypeError(
416 error_class="NOT_ITERABLE",
message_parameters={"objectName": "Column"}
417 )
PySparkTypeError: [NOT_ITERABLE] Column is not iterable.
```
Connect:
```
In [1]: from pyspark.sql import functions as F
In [2]: df = spark.sql("select map(1, 2, 3, 4) as m, 1 as k")
...: df.select(F.map_contains_key(df.m, df.k))
Out[2]: DataFrame[map_contains_key(m, k): boolean]
```
There is a slight difference in the handling of `value: Any` typed value:
Spark Connect always convert `value: Any` to Column, while some functions (e.g.
`map_contains_key`) in Classic don't do this.
We will need to revisit all the `Any` typed parameters in functions. cc
@HyukjinKwon
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]