Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add tests and throw exception when we can't parse input.
  • Loading branch information
viirya committed May 2, 2018
commit e4b886732d5bb844c66116a4751dc9a92173a928
9 changes: 9 additions & 0 deletions python/pyspark/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,15 @@ def test_py4j_exception_message(self):

self.assertTrue('NullPointerException' in _exception_message(context.exception))

def test_parsing_version_string(self):
from pyspark.util import VersionUtils

(major, minor) = VersionUtils.majorMinorVersion("2.4.0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need this since it's tested in doctests.

self.assertEqual(major, 2)
self.assertEqual(minor, 4)

self.assertRaises(ValueError, lambda: VersionUtils.majorMinorVersion("abced"))


@unittest.skipIf(not _have_scipy, "SciPy not installed")
class SciPyTests(PySparkTestCase):
Expand Down
34 changes: 19 additions & 15 deletions python/pyspark/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,28 @@ def _get_argspec(f):
return argspec


def majorMinorVersion(version):
class VersionUtils(object):
"""
Get major and minor version numbers for given Spark version string.

>>> version = "2.4.0"
>>> majorMinorVersion(version)
(2, 4)
Provides utility method to determine Spark versions with given input string.
"""
@staticmethod
def majorMinorVersion(version):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version -> sparkVersion.

"""
Get major and minor version numbers for given Spark version string.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

   * Given a Spark version string, return the (major version number, minor version number).
   * E.g., for 2.0.1-SNAPSHOT, return (2, 0).


>>> version = "abc"
>>> majorMinorVersion(version) is None
True
>>> version = "2.4.0"
>>> majorMinorVersion(version)
(2, 4)
>>> version = "2.3.0-SNAPSHOT"
>>> majorMinorVersion(version)
(2, 3)

"""
m = re.search('^(\d+)\.(\d+)(\..*)?$', version)
if m is None:
return None
else:
return (int(m.group(1)), int(m.group(2)))
"""
m = re.search('^(\d+)\.(\d+)(\..*)?$', version)
if m is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do if m is not None to match the order with Scala side.

raise ValueError("invalid version string: " + version)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we match the message with Scala side?

        throw new IllegalArgumentException(s"Spark tried to parse '$sparkVersion' as a Spark" +
          s" version string, but it could not find the major and minor version numbers.")

else:
return (int(m.group(1)), int(m.group(2)))


if __name__ == "__main__":
Expand Down