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 2023/11/21 07:59:01 UTC

[PR] [SPARK-46028][CONNECT][PYTHON] Make `Column.__getitem__` accept input column [spark]

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

   ### What changes were proposed in this pull request?
   Make `Column.__getitem__` accept input column
   
   
   ### Why are the changes needed?
   
   `Column.__getitem__` should accept column as input
   
   ```
   In [1]: from pyspark.sql.functions import col,lit,create_map
      ...: from itertools import chain
      ...:
      ...: mapping = {
      ...:   'A': '20',
      ...:   'B': '28',
      ...:   'C': '34'
      ...: }
      ...:
      ...: x = [['A','10'],['B','14'],['C','17']]
      ...: df = spark.createDataFrame(data=x, schema = ["key", "value"])
      ...:
      ...: mapping_expr = create_map([lit(x) for x in chain(*mapping.items())])
      ...: df = df.withColumn("square_value", mapping_expr[col("key")])
      ...: df.show()
   ---------------------------------------------------------------------------
   PySparkTypeError                          Traceback (most recent call last)
   Cell In[1], line 14
        11 df = spark.createDataFrame(data=x, schema = ["key", "value"])
        13 mapping_expr = create_map([lit(x) for x in chain(*mapping.items())])
   ---> 14 df = df.withColumn("square_value", mapping_expr[col("key")])
        15 df.show()
   
   File ~/Dev/spark/python/pyspark/sql/connect/column.py:465, in Column.__getitem__(self, k)
       463     return self.substr(k.start, k.stop)
       464 else:
   --> 465     return Column(UnresolvedExtractValue(self._expr, LiteralExpression._from_value(k)))
   
   File ~/Dev/spark/python/pyspark/sql/connect/expressions.py:336, in LiteralExpression._from_value(cls, value)
       334 @classmethod
       335 def _from_value(cls, value: Any) -> "LiteralExpression":
   --> 336     return LiteralExpression(value=value, dataType=LiteralExpression._infer_type(value))
   
   File ~/Dev/spark/python/pyspark/sql/connect/expressions.py:329, in LiteralExpression._infer_type(cls, value)
       323         raise PySparkTypeError(
       324             error_class="CANNOT_INFER_ARRAY_TYPE",
       325             message_parameters={},
       326         )
       327     return ArrayType(LiteralExpression._infer_type(first), True)
   --> 329 raise PySparkTypeError(
       330     error_class="UNSUPPORTED_DATA_TYPE",
       331     message_parameters={"data_type": type(value).__name__},
       332 )
   
   PySparkTypeError: [UNSUPPORTED_DATA_TYPE] Unsupported DataType `Column`.
   ```
   
   
   ### Does this PR introduce _any_ user-facing change?
   yes
   
   ### How was this patch tested?
   added ut
   
   ### 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-46028][CONNECT][PYTHON] Make `Column.__getitem__` accept input column [spark]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #43930: [SPARK-46028][CONNECT][PYTHON] Make `Column.__getitem__` accept input column
URL: https://github.com/apache/spark/pull/43930


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