Skip to content

Commit 65b814b

Browse files
jkbradleyyanboliang
authored andcommitted
[SPARK-17456][CORE] Utility for parsing Spark versions
## What changes were proposed in this pull request? This patch adds methods for extracting major and minor versions as Int types in Scala from a Spark version string. Motivation: There are many hacks within Spark's codebase to identify and compare Spark versions. We should add a simple utility to standardize these code paths, especially since there have been mistakes made in the past. This will let us add unit tests as well. Currently, I want this functionality to check Spark versions to provide backwards compatibility for ML model persistence. ## How was this patch tested? Unit tests Author: Joseph K. Bradley <joseph@databricks.com> Closes apache#15017 from jkbradley/version-parsing.
1 parent 92ce8d4 commit 65b814b

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.util
19+
20+
/**
21+
* Utilities for working with Spark version strings
22+
*/
23+
private[spark] object VersionUtils {
24+
25+
private val majorMinorRegex = """^(\d+)\.(\d+)(\..*)?$""".r
26+
27+
/**
28+
* Given a Spark version string, return the major version number.
29+
* E.g., for 2.0.1-SNAPSHOT, return 2.
30+
*/
31+
def majorVersion(sparkVersion: String): Int = majorMinorVersion(sparkVersion)._1
32+
33+
/**
34+
* Given a Spark version string, return the minor version number.
35+
* E.g., for 2.0.1-SNAPSHOT, return 0.
36+
*/
37+
def minorVersion(sparkVersion: String): Int = majorMinorVersion(sparkVersion)._2
38+
39+
/**
40+
* Given a Spark version string, return the (major version number, minor version number).
41+
* E.g., for 2.0.1-SNAPSHOT, return (2, 0).
42+
*/
43+
def majorMinorVersion(sparkVersion: String): (Int, Int) = {
44+
majorMinorRegex.findFirstMatchIn(sparkVersion) match {
45+
case Some(m) =>
46+
(m.group(1).toInt, m.group(2).toInt)
47+
case None =>
48+
throw new IllegalArgumentException(s"Spark tried to parse '$sparkVersion' as a Spark" +
49+
s" version string, but it could not find the major and minor version numbers.")
50+
}
51+
}
52+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.util
19+
20+
import org.apache.spark.SparkFunSuite
21+
22+
class VersionUtilsSuite extends SparkFunSuite {
23+
24+
import org.apache.spark.util.VersionUtils._
25+
26+
test("Parse Spark major version") {
27+
assert(majorVersion("2.0") === 2)
28+
assert(majorVersion("12.10.11") === 12)
29+
assert(majorVersion("2.0.1-SNAPSHOT") === 2)
30+
assert(majorVersion("2.0.x") === 2)
31+
withClue("majorVersion parsing should fail for invalid major version number") {
32+
intercept[IllegalArgumentException] {
33+
majorVersion("2z.0")
34+
}
35+
}
36+
withClue("majorVersion parsing should fail for invalid minor version number") {
37+
intercept[IllegalArgumentException] {
38+
majorVersion("2.0z")
39+
}
40+
}
41+
}
42+
43+
test("Parse Spark minor version") {
44+
assert(minorVersion("2.0") === 0)
45+
assert(minorVersion("12.10.11") === 10)
46+
assert(minorVersion("2.0.1-SNAPSHOT") === 0)
47+
assert(minorVersion("2.0.x") === 0)
48+
withClue("minorVersion parsing should fail for invalid major version number") {
49+
intercept[IllegalArgumentException] {
50+
minorVersion("2z.0")
51+
}
52+
}
53+
withClue("minorVersion parsing should fail for invalid minor version number") {
54+
intercept[IllegalArgumentException] {
55+
minorVersion("2.0z")
56+
}
57+
}
58+
}
59+
60+
test("Parse Spark major and minor versions") {
61+
assert(majorMinorVersion("2.0") === (2, 0))
62+
assert(majorMinorVersion("12.10.11") === (12, 10))
63+
assert(majorMinorVersion("2.0.1-SNAPSHOT") === (2, 0))
64+
assert(majorMinorVersion("2.0.x") === (2, 0))
65+
withClue("majorMinorVersion parsing should fail for invalid major version number") {
66+
intercept[IllegalArgumentException] {
67+
majorMinorVersion("2z.0")
68+
}
69+
}
70+
withClue("majorMinorVersion parsing should fail for invalid minor version number") {
71+
intercept[IllegalArgumentException] {
72+
majorMinorVersion("2.0z")
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)