Skip to content

Commit 31dab71

Browse files
liyuanjianxuanyuanking
authored andcommitted
[SPARK-25072][PYSPARK] Forbid extra value for custom Row
## What changes were proposed in this pull request? Add value length check in `_create_row`, forbid extra value for custom Row in PySpark. ## How was this patch tested? New UT in pyspark-sql Closes #22140 from xuanyuanking/SPARK-25072. Lead-authored-by: liyuanjian <[email protected]> Co-authored-by: Yuanjian Li <[email protected]> Signed-off-by: Bryan Cutler <[email protected]> (cherry picked from commit c84bc40) Signed-off-by: Bryan Cutler <[email protected]>
1 parent 9db81fd commit 31dab71

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

python/pyspark/sql/tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ def test_struct_field_type_name(self):
268268
struct_field = StructField("a", IntegerType())
269269
self.assertRaises(TypeError, struct_field.typeName)
270270

271+
def test_invalid_create_row(self):
272+
row_class = Row("c1", "c2")
273+
self.assertRaises(ValueError, lambda: row_class(1, 2, 3))
274+
271275

272276
class SQLTests(ReusedSQLTestCase):
273277

python/pyspark/sql/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,9 @@ def __contains__(self, item):
15321532
# let object acts like class
15331533
def __call__(self, *args):
15341534
"""create new Row object"""
1535+
if len(args) > len(self):
1536+
raise ValueError("Can not create Row with fields %s, expected %d values "
1537+
"but got %s" % (self, len(self), args))
15351538
return _create_row(self, args)
15361539

15371540
def __getitem__(self, item):

0 commit comments

Comments
 (0)