Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11492#discussion_r54870946
  
    --- Diff: python/pyspark/sql/types.py ---
    @@ -1137,7 +1148,28 @@ def _verify_type(obj, dataType):
             if type(obj) not in _acceptable_types[_type]:
                 raise TypeError("%s can not accept object %r in type %s" % 
(dataType, obj, type(obj)))
     
    -    if isinstance(dataType, ArrayType):
    +    if isinstance(dataType, ByteType):
    +        if obj < -128 or obj > 127:
    +            raise ValueError("object of ByteType out of range, got: %s" % 
obj)
    +
    +    elif isinstance(dataType, ShortType):
    +        if obj < -32768 or obj > 32767:
    +            raise ValueError("object of ShortType out of range, got: %s" % 
obj)
    +
    +    elif isinstance(dataType, IntegerType):
    +        if obj < -2147483648 or obj > 2147483647:
    +            raise ValueError("object of IntegerType out of range, got: %s" 
% obj)
    +
    +    elif isinstance(dataType, FloatType):
    +        from math import isinf
    +        from struct import pack, unpack
    +
    +        if not isinf(obj):
    +            f = unpack("f", pack("f", obj))[0]
    --- End diff --
    
    Here I try to convert the `obj` to binary string as a `float32`, and then 
convert it back. If it becomes an `inf`, then it means the `obj` can not fit in 
`float32`.
    
    Is there a better way to check if a python float can fit in `float32`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to