gaogaotiantian commented on code in PR #57911:
URL: https://github.com/apache/spark/pull/57911#discussion_r3763192465


##########
python/pyspark/sql/conversion.py:
##########
@@ -742,9 +763,14 @@ def convert_binary(value: Any) -> Any:
                     if not nullable:
                         raise PySparkValueError(f"input for {dataType} must 
not be None")
                     return None
-                else:
-                    assert isinstance(value, (bytes, bytearray))
-                    return bytes(value)
+                if type(value) is bytes:

Review Comment:
   `if` and `elif` is technically equivalent here, but let's use `elif` to be 
explicit about exclusiveness. Same for the code below. If we can use `elif` we 
should use elif.



##########
python/pyspark/sql/conversion.py:
##########
@@ -804,13 +830,16 @@ def convert_string(value: Any) -> Any:
                     if not nullable:
                         raise PySparkValueError(f"input for {dataType} must 
not be None")
                     return None
-                else:
-                    if isinstance(value, bool):
-                        # To match the PySpark Classic which convert bool to 
string in
-                        # the JVM side (python.EvaluatePython.makeFromJava)
-                        return str(value).lower()
-                    else:
-                        return str(value)
+                if type(value) is str:
+                    # Fast path: `str(value)` returns `value` itself for a 
`str`
+                    # input (no copy), but still pays the constructor dispatch 
per
+                    # element. Returning it directly skips that and the bool 
check.
+                    return value
+                if isinstance(value, bool):

Review Comment:
   If you are interested in micro-benchmark optimization, you can probably 
achieve some by changing this to `is True` then another `elif` for `is False`. 
Then you can return a constant string. I think you'll have a even prettier 
micro benchmark result for booleans.



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

Reply via email to