Github user dgingrich commented on a diff in the pull request: https://github.com/apache/spark/pull/17227#discussion_r123099962 --- Diff: python/pyspark/sql/tests.py --- @@ -2367,6 +2380,151 @@ def range_frame_match(): importlib.reload(window) + +class TypesTest(unittest.TestCase): + + def test_verify_type_ok_nullable(self): + for obj, data_type in [ + (None, IntegerType()), + (None, FloatType()), + (None, StringType()), + (None, StructType([]))]: + _verify_type(obj, data_type, nullable=True) + msg = "_verify_type(%s, %s, nullable=True)" % (obj, data_type) + self.assertTrue(True, msg) + + def test_verify_type_not_nullable(self): + import array + import datetime + import decimal + + MyStructType = StructType([ + StructField('s', StringType(), nullable=False), + StructField('i', IntegerType(), nullable=True)]) + + class MyObj: + def __init__(self, **ka): + for k, v in ka.items(): + setattr(self, k, v) + + # obj, data_type, exception (None for success or Exception subclass for error) + spec = [ + # Strings (match anything but None) + ("", StringType(), None), + (u"", StringType(), None), + (1, StringType(), None), + (1.0, StringType(), None), + ([], StringType(), None), + ({}, StringType(), None), + (None, StringType(), ValueError), # Only None test + + # UDT + (ExamplePoint(1.0, 2.0), ExamplePointUDT(), None), + (ExamplePoint(1.0, 2.0), PythonOnlyUDT(), ValueError), + + # Boolean + (True, BooleanType(), None), + (1, BooleanType(), TypeError), + ("True", BooleanType(), TypeError), + ([1], BooleanType(), TypeError), + + # Bytes + (-(2**7) - 1, ByteType(), ValueError), + (-(2**7), ByteType(), None), + (2**7 - 1, ByteType(), None), + (2**7, ByteType(), ValueError), + ("1", ByteType(), TypeError), + (1.0, ByteType(), TypeError), + + # Shorts + (-(2**15) - 1, ShortType(), ValueError), + (-(2**15), ShortType(), None), + (2**15 - 1, ShortType(), None), + (2**15, ShortType(), ValueError), + + # Integer + (-(2**31) - 1, IntegerType(), ValueError), + (-(2**31), IntegerType(), None), + (2**31 - 1, IntegerType(), None), + (2**31, IntegerType(), ValueError), + + # Long + (2**64, LongType(), None), + + # Float & Double + (1.0, FloatType(), None), + (1, FloatType(), TypeError), + (1.0, DoubleType(), None), + (1, DoubleType(), TypeError), + + # Decimal + (decimal.Decimal("1.0"), DecimalType(), None), + (1.0, DecimalType(), TypeError), + (1, DecimalType(), TypeError), + ("1.0", DecimalType(), TypeError), + + # Binary + (bytearray([1, 2]), BinaryType(), None), + (1, BinaryType(), TypeError), + + # Date/Time + (datetime.date(2000, 1, 2), DateType(), None), + (datetime.datetime(2000, 1, 2, 3, 4), DateType(), None), + ("2000-01-02", DateType(), TypeError), + (datetime.datetime(2000, 1, 2, 3, 4), TimestampType(), None), + (946811040, TimestampType(), TypeError), + + # Array + ([], ArrayType(IntegerType()), None), + (["1", None], ArrayType(StringType(), containsNull=True), None), + ([1, 2], ArrayType(IntegerType()), None), + ([1, "2"], ArrayType(IntegerType()), TypeError), + ((1, 2), ArrayType(IntegerType()), None), + (array.array('h', [1, 2]), ArrayType(IntegerType()), None), + + # Map + ({}, MapType(StringType(), IntegerType()), None), + ({"a": 1}, MapType(StringType(), IntegerType()), None), + ({"a": 1}, MapType(IntegerType(), IntegerType()), TypeError), + ({"a": "1"}, MapType(StringType(), IntegerType()), TypeError), + ({"a": None}, MapType(StringType(), IntegerType(), valueContainsNull=True), None), --- End diff -- Added
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org