Skip to content

Commit 016128d

Browse files
committed
fixed to use File.toURI()
1 parent 2c62e3b commit 016128d

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

core/src/main/scala/org/apache/spark/util/Utils.scala

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,27 +1766,15 @@ private[spark] object Utils extends Logging {
17661766
* converted into an absolute path with a file:// scheme.
17671767
*/
17681768
def resolveURI(path: String, testWindows: Boolean = false): URI = {
1769-
1770-
val windows = isWindows || testWindows
1771-
val formattedPath = formatPath(path, windows)
1772-
1773-
val uri = new URI(formattedPath)
1774-
if (uri.getPath == null) {
1775-
throw new IllegalArgumentException(s"Given path is malformed: $uri")
1776-
}
1777-
1778-
Option(uri.getScheme) match {
1779-
case Some(windowsDrive(d)) if windows =>
1780-
new URI("file:/" + uri.toString.stripPrefix("/"))
1781-
case None =>
1782-
// Preserve fragments for HDFS file name substitution (denoted by "#")
1783-
// For instance, in "abc.py#xyz.py", "xyz.py" is the name observed by the application
1784-
val fragment = uri.getFragment
1785-
val part = new File(uri.getPath).toURI
1786-
new URI(part.getScheme, part.getPath, fragment)
1787-
case Some(other) =>
1788-
uri
1769+
try {
1770+
val uri = new URI(path)
1771+
if (uri.getScheme() != null) {
1772+
return uri
1773+
}
1774+
} catch {
1775+
case e: URISyntaxException =>
17891776
}
1777+
new File(path).getAbsoluteFile().toURI()
17901778
}
17911779

17921780
/** Resolve a comma-separated list of paths. */

core/src/test/scala/org/apache/spark/util/UtilsSuite.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,15 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
239239
assertResolves("hdfs:/root/spark.jar", "hdfs:/root/spark.jar")
240240
assertResolves("hdfs:///root/spark.jar#app.jar", "hdfs:/root/spark.jar#app.jar")
241241
assertResolves("spark.jar", s"file:$cwd/spark.jar")
242-
assertResolves("spark.jar#app.jar", s"file:$cwd/spark.jar#app.jar")
242+
assertResolves("spark.jar#app.jar", s"file:$cwd/spark.jar%23app.jar")
243243
assertResolves("path to/file.txt", s"file:$cwd/path%20to/file.txt")
244-
assertResolves("C:/path/to/file.txt", "file:/C:/path/to/file.txt", testWindows = true)
245244
assertResolves("C:\\path\\to\\file.txt", "file:/C:/path/to/file.txt", testWindows = true)
246-
assertResolves("C:/path to/file.txt", "file:/C:/path%20to/file.txt", testWindows = true)
245+
assertResolves("C:\\path to\\file.txt", "file:/C:/path%20to/file.txt", testWindows = true)
247246
assertResolves("file:/C:/path/to/file.txt", "file:/C:/path/to/file.txt", testWindows = true)
248247
assertResolves("file:///C:/path/to/file.txt", "file:/C:/path/to/file.txt", testWindows = true)
249248
assertResolves("file:/C:/file.txt#alias.txt", "file:/C:/file.txt#alias.txt", testWindows = true)
250-
intercept[IllegalArgumentException] { Utils.resolveURI("file:foo") }
251-
intercept[IllegalArgumentException] { Utils.resolveURI("file:foo:baby") }
249+
assertResolves("file:foo", s"file:foo")
250+
assertResolves("file:foo:baby", s"file:foo:baby")
252251
}
253252

254253
test("resolveURIs with multiple paths") {
@@ -268,9 +267,9 @@ class UtilsSuite extends FunSuite with ResetSystemProperties {
268267
assertResolves("file:/jar1,file:/jar2", "file:/jar1,file:/jar2")
269268
assertResolves("hdfs:/jar1,file:/jar2,jar3", s"hdfs:/jar1,file:/jar2,file:$cwd/jar3")
270269
assertResolves("hdfs:/jar1,file:/jar2,jar3,jar4#jar5,path to/jar6",
271-
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:$cwd/jar4#jar5,file:$cwd/path%20to/jar6")
270+
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:$cwd/jar4%23jar5,file:$cwd/path%20to/jar6")
272271
assertResolves("""hdfs:/jar1,file:/jar2,jar3,C:\pi.py#py.pi,C:\path to\jar4""",
273-
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:/C:/pi.py#py.pi,file:/C:/path%20to/jar4",
272+
s"hdfs:/jar1,file:/jar2,file:$cwd/jar3,file:/C:/pi.py%23py.pi,file:/C:/path%20to/jar4",
274273
testWindows = true)
275274
}
276275

0 commit comments

Comments
 (0)