Skip to content
Snippets Groups Projects
Commit ac507a03 authored by Davies Liu's avatar Davies Liu Committed by Davies Liu
Browse files

[SPARK-6902] [SQL] [PYSPARK] Row should be read-only

Raise an read-only exception when user try to mutable a Row.

Author: Davies Liu <davies@databricks.com>

Closes #8009 from davies/readonly_row and squashes the following commits:

8722f3f [Davies Liu] add tests
05a3d36 [Davies Liu] Row should be read-only
parent 74a6541a
No related branches found
No related tags found
No related merge requests found
...@@ -179,6 +179,21 @@ class SQLTests(ReusedPySparkTestCase): ...@@ -179,6 +179,21 @@ class SQLTests(ReusedPySparkTestCase):
ReusedPySparkTestCase.tearDownClass() ReusedPySparkTestCase.tearDownClass()
shutil.rmtree(cls.tempdir.name, ignore_errors=True) shutil.rmtree(cls.tempdir.name, ignore_errors=True)
def test_row_should_be_read_only(self):
row = Row(a=1, b=2)
self.assertEqual(1, row.a)
def foo():
row.a = 3
self.assertRaises(Exception, foo)
row2 = self.sqlCtx.range(10).first()
self.assertEqual(0, row2.id)
def foo2():
row2.id = 2
self.assertRaises(Exception, foo2)
def test_range(self): def test_range(self):
self.assertEqual(self.sqlCtx.range(1, 1).count(), 0) self.assertEqual(self.sqlCtx.range(1, 1).count(), 0)
self.assertEqual(self.sqlCtx.range(1, 0, -1).count(), 1) self.assertEqual(self.sqlCtx.range(1, 0, -1).count(), 1)
......
...@@ -1246,6 +1246,11 @@ class Row(tuple): ...@@ -1246,6 +1246,11 @@ class Row(tuple):
except ValueError: except ValueError:
raise AttributeError(item) raise AttributeError(item)
def __setattr__(self, key, value):
if key != '__fields__':
raise Exception("Row is read-only")
self.__dict__[key] = value
def __reduce__(self): def __reduce__(self):
"""Returns a tuple so Python knows how to pickle Row.""" """Returns a tuple so Python knows how to pickle Row."""
if hasattr(self, "__fields__"): if hasattr(self, "__fields__"):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment