diff options
author | Davies Liu <davies@databricks.com> | 2016-06-21 10:53:33 -0700 |
---|---|---|
committer | Davies Liu <davies.liu@gmail.com> | 2016-06-21 10:53:33 -0700 |
commit | 2d6919bea9fc213b5af530afab7793b63c6c8b51 (patch) | |
tree | 10855b89fdd50eabb9ed38eb8df5145121164789 /python/pyspark | |
parent | bcb0258ae62f23f71a067c1304232f272d7374aa (diff) | |
download | spark-2d6919bea9fc213b5af530afab7793b63c6c8b51.tar.gz spark-2d6919bea9fc213b5af530afab7793b63c6c8b51.tar.bz2 spark-2d6919bea9fc213b5af530afab7793b63c6c8b51.zip |
[SPARK-16086] [SQL] [PYSPARK] create Row without any fields
## What changes were proposed in this pull request?
This PR allows us to create a Row without any fields.
## How was this patch tested?
Added a test for empty row and udf without arguments.
Author: Davies Liu <davies@databricks.com>
Closes #13812 from davies/no_argus.
Diffstat (limited to 'python/pyspark')
-rw-r--r-- | python/pyspark/sql/tests.py | 9 | ||||
-rw-r--r-- | python/pyspark/sql/types.py | 9 |
2 files changed, 12 insertions, 6 deletions
diff --git a/python/pyspark/sql/tests.py b/python/pyspark/sql/tests.py index c631ad8a46..388ac91922 100644 --- a/python/pyspark/sql/tests.py +++ b/python/pyspark/sql/tests.py @@ -177,6 +177,10 @@ class DataTypeTests(unittest.TestCase): dt = DateType() self.assertEqual(dt.fromInternal(0), datetime.date(1970, 1, 1)) + def test_empty_row(self): + row = Row() + self.assertEqual(len(row), 0) + class SQLTests(ReusedPySparkTestCase): @@ -318,6 +322,11 @@ class SQLTests(ReusedPySparkTestCase): [row] = self.spark.sql("SELECT double(add(1, 2)), add(double(2), 1)").collect() self.assertEqual(tuple(row), (6, 5)) + def test_udf_without_arguments(self): + self.spark.catalog.registerFunction("foo", lambda: "bar") + [row] = self.spark.sql("SELECT foo()").collect() + self.assertEqual(row[0], "bar") + def test_udf_with_array_type(self): d = [Row(l=list(range(3)), d={"key": list(range(5))})] rdd = self.sc.parallelize(d) diff --git a/python/pyspark/sql/types.py b/python/pyspark/sql/types.py index bb2b95404a..f0b56be8da 100644 --- a/python/pyspark/sql/types.py +++ b/python/pyspark/sql/types.py @@ -1401,11 +1401,7 @@ class Row(tuple): if args and kwargs: raise ValueError("Can not use both args " "and kwargs to create Row") - if args: - # create row class or objects - return tuple.__new__(self, args) - - elif kwargs: + if kwargs: # create row objects names = sorted(kwargs.keys()) row = tuple.__new__(self, [kwargs[n] for n in names]) @@ -1413,7 +1409,8 @@ class Row(tuple): return row else: - raise ValueError("No args or kwargs") + # create row class or objects + return tuple.__new__(self, args) def asDict(self, recursive=False): """ |