Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions python/pyspark/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,10 @@ 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
self.assertRaises(ValueError, lambda: VersionUtils.majorMinorVersion("abced"))


@unittest.skipIf(not _have_scipy, "SciPy not installed")
class SciPyTests(PySparkTestCase):
Expand Down
37 changes: 22 additions & 15 deletions python/pyspark/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,31 @@ 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(sparkVersion):
"""
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
>>> sparkVersion = "2.4.0"
>>> VersionUtils.majorMinorVersion(sparkVersion)
(2, 4)
>>> sparkVersion = "2.3.0-SNAPSHOT"
>>> VersionUtils.majorMinorVersion(sparkVersion)
(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+)(\..*)?$', sparkVersion)
if m is not None:
return (int(m.group(1)), int(m.group(2)))
else:
raise ValueError("Spark tried to parse '%s' as a Spark" % sparkVersion +
" version string, but it could not find the major and minor" +
" version numbers.")


if __name__ == "__main__":
Expand Down