Github user HyukjinKwon commented on a diff in the pull request:
https://github.com/apache/spark/pull/18521#discussion_r125387151
--- Diff: python/pyspark/sql/types.py ---
@@ -1249,121 +1249,201 @@ def _infer_schema_type(obj, dataType):
}
-def _verify_type(obj, dataType, nullable=True):
+def _make_type_verifier(dataType, nullable=True, name=None):
"""
Verify the type of obj against dataType, raise a TypeError if they do
not match.
Also verify the value of obj against datatype, raise a ValueError if
it's not within the allowed
range, e.g. using 128 as ByteType will overflow. Note that, Python
float is not checked, so it
will become infinity when cast to Java float if it overflows.
- >>> _verify_type(None, StructType([]))
- >>> _verify_type("", StringType())
- >>> _verify_type(0, LongType())
- >>> _verify_type(list(range(3)), ArrayType(ShortType()))
- >>> _verify_type(set(), ArrayType(StringType())) # doctest:
+IGNORE_EXCEPTION_DETAIL
+ >>> _make_type_verifier(StructType([]))(None)
+ >>> _make_type_verifier(StringType())("")
+ >>> _make_type_verifier(LongType())(0)
+ >>> _make_type_verifier(ArrayType(ShortType()))(list(range(3)))
+ >>> _make_type_verifier(ArrayType(StringType()))(set()) # doctest:
+IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError:...
- >>> _verify_type({}, MapType(StringType(), IntegerType()))
- >>> _verify_type((), StructType([]))
- >>> _verify_type([], StructType([]))
- >>> _verify_type([1], StructType([])) # doctest:
+IGNORE_EXCEPTION_DETAIL
+ >>> _make_type_verifier(MapType(StringType(), IntegerType()))({})
+ >>> _make_type_verifier(StructType([]))(())
+ >>> _make_type_verifier(StructType([]))([])
+ >>> _make_type_verifier(StructType([]))([1]) # doctest:
+IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError:...
>>> # Check if numeric values are within the allowed range.
- >>> _verify_type(12, ByteType())
- >>> _verify_type(1234, ByteType()) # doctest: +IGNORE_EXCEPTION_DETAIL
+ >>> _make_type_verifier(ByteType())(12)
+ >>> _make_type_verifier(ByteType())(1234) # doctest:
+IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError:...
- >>> _verify_type(None, ByteType(), False) # doctest:
+IGNORE_EXCEPTION_DETAIL
+ >>> _make_type_verifier(ByteType(), False)(None) # doctest:
+IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError:...
- >>> _verify_type([1, None], ArrayType(ShortType(), False)) # doctest:
+IGNORE_EXCEPTION_DETAIL
+ >>> _make_type_verifier(
+ ... ArrayType(ShortType(), False))([1, None]) # doctest:
+IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError:...
- >>> _verify_type({None: 1}, MapType(StringType(), IntegerType()))
+ >>> _make_type_verifier(MapType(StringType(), IntegerType()))({None:
1})
Traceback (most recent call last):
...
ValueError:...
>>> schema = StructType().add("a", IntegerType()).add("b",
StringType(), False)
- >>> _verify_type((1, None), schema) # doctest: +IGNORE_EXCEPTION_DETAIL
+ >>> _make_type_verifier(schema)((1, None)) # doctest:
+IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError:...
"""
- if obj is None:
- if nullable:
- return
+
+ if name is None:
+ new_msg = lambda msg: msg
+ new_name = lambda n: "field %s" % n
+ else:
+ new_msg = lambda msg: "%s: %s" % (name, msg)
+ new_name = lambda n: "field %s in %s" % (n, name)
+
+ def verify_nullability(obj):
+ if obj is None:
+ if nullable:
+ return True
+ else:
+ raise ValueError(new_msg("This field is not nullable, but
got None"))
else:
- raise ValueError("This field is not nullable, but got None")
+ return False
--- End diff --
sounds good. Will give a shot.
---
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]