-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-25659][PYTHON][TEST] Test type inference specification for createDataFrame in PySpark #22653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1149,6 +1149,75 @@ def test_infer_schema(self): | |
| result = self.spark.sql("SELECT l[0].a from test2 where d['key'].d = '2'") | ||
| self.assertEqual(1, result.head()[0]) | ||
|
|
||
| def test_infer_schema_specification(self): | ||
| from decimal import Decimal | ||
|
|
||
| class A(object): | ||
| def __init__(self): | ||
| self.a = 1 | ||
|
|
||
| data = [ | ||
| True, | ||
| 1, | ||
| "a", | ||
| u"a", | ||
| datetime.date(1970, 1, 1), | ||
| datetime.datetime(1970, 1, 1, 0, 0), | ||
| 1.0, | ||
| array.array("d", [1]), | ||
| [1], | ||
| (1, ), | ||
| {"a": 1}, | ||
| bytearray(1), | ||
| Decimal(1), | ||
| Row(a=1), | ||
| Row("a")(1), | ||
| A(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't this was possible - does it just look at the variable attributes in the object to get the fields?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, it uses |
||
| ] | ||
|
|
||
| df = self.spark.createDataFrame([data]) | ||
| actual = list(map(lambda x: x.dataType.simpleString(), df.schema)) | ||
| expected = [ | ||
| 'boolean', | ||
| 'bigint', | ||
| 'string', | ||
| 'string', | ||
| 'date', | ||
| 'timestamp', | ||
| 'double', | ||
| 'array<double>', | ||
| 'array<bigint>', | ||
| 'struct<_1:bigint>', | ||
| 'map<string,bigint>', | ||
| 'binary', | ||
| 'decimal(38,18)', | ||
| 'struct<a:bigint>', | ||
| 'struct<a:bigint>', | ||
| 'struct<a:bigint>', | ||
| ] | ||
| self.assertEqual(actual, expected) | ||
|
|
||
| actual = list(df.first()) | ||
| expected = [ | ||
| True, | ||
| 1, | ||
| 'a', | ||
| u"a", | ||
| datetime.date(1970, 1, 1), | ||
| datetime.datetime(1970, 1, 1, 0, 0), | ||
| 1.0, | ||
| [1.0], | ||
| [1], | ||
| Row(_1=1), | ||
| {"a": 1}, | ||
| bytearray(b'\x00'), | ||
| Decimal('1.000000000000000000'), | ||
| Row(a=1), | ||
| Row(a=1), | ||
| Row(a=1), | ||
| ] | ||
| self.assertEqual(actual, expected) | ||
|
|
||
| def test_infer_schema_not_enough_names(self): | ||
| df = self.spark.createDataFrame([["a", "b"]], ["col1"]) | ||
| self.assertEqual(df.columns, ['col1', '_2']) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: since this is for unicode string, how about using a non-ascii string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point. Will change when I fix some codes around here.