Skip to content

Commit

Permalink
SNOW-974909 Remove Usage of ParseUtil (#66)
Browse files Browse the repository at this point in the history
* remove usage of ParseUtils

* fix windows tests
  • Loading branch information
sfc-gh-bli authored Nov 27, 2023
1 parent 327882f commit 729d50f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
13 changes: 2 additions & 11 deletions src/main/scala/com/snowflake/snowpark/internal/UDFClassPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.JsonNode
import java.io.File
import java.net.{URI, URLClassLoader}
import com.snowflake.snowpark.Session
import sun.net.www.ParseUtil

object UDFClassPath extends Logging {

Expand Down Expand Up @@ -94,13 +93,7 @@ object UDFClassPath extends Logging {
if (path.contains(":")) {
path = path.substring(path.indexOf(":") + 1)
}
/*
* The URL in class loader is encoded, so we have to decode it to read the
* local file.
*/
path = ParseUtil.decode(path)
// Creating a File fixes the separator based on OS
new File(path).getPath
new URI(path).getPath
}
}

Expand All @@ -111,9 +104,7 @@ object UDFClassPath extends Logging {
* The URL in CodeSource location is encoded, so we have to decode it to read the
* local file.
*/
val parsed = ParseUtil.decode(codeSource.getLocation.getPath)
// Creating a File fixes the separator based on OS
Some(new File(parsed).getPath)
Some(new URI(codeSource.getLocation.getPath).getPath)
} else {
None
}
Expand Down
27 changes: 13 additions & 14 deletions src/test/scala/com/snowflake/snowpark/UDFClasspathSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,7 @@ class UDFClasspathSuite extends SNTestBase {

test("Test for getPathForClass") {
val clazz = classOf[scala.StringBuilder]
val expectedUrl = if (Utils.isWindows) {
// On windows, the directory format is gotten from getCodeSource() is like:
// /C:/Users/runneradmin/.../scala-library-2.12.11.jar
// The directory gotten with UDFClassPath.getPathForClass(clazz) is like:
// C:\Users\runneradmin\...\scala-library-2.12.11.jar
// So, need format conversion for result comparison.
clazz.getProtectionDomain.getCodeSource.getLocation.getPath
.substring(1)
.replace("/", "\\")
} else {
clazz.getProtectionDomain.getCodeSource.getLocation.getPath
}

val expectedUrl = clazz.getProtectionDomain.getCodeSource.getLocation.getPath
val result = UDFClassPath.getPathForClass(clazz)
assert(result.isDefined && result.get.equals(expectedUrl))
}
Expand All @@ -138,7 +126,18 @@ class UDFClasspathSuite extends SNTestBase {
val foundJarPath = UDFClassPath.getPathUsingClassLoader(classToLoad)
assert(foundJarPath.nonEmpty)
assert(new File(foundJarPath.get).exists())
assert(foundJarPath.get.equals(jarFilePath))

val expectedUrl = if (Utils.isWindows) {
// On windows, the directory format is gotten from getCodeSource() is like:
// /C:/Users/runneradmin/.../scala-library-2.12.11.jar
// The directory gotten with UDFClassPath.getPathForClass(clazz) is like:
// C:\Users\runneradmin\...\scala-library-2.12.11.jar
// So, need format conversion for result comparison.
"/" + jarFilePath.replace("\\", "/")
} else {
jarFilePath
}
assert(foundJarPath.get.equals(expectedUrl))
assert(foundJarPath.get.contains(prefix))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.snowflake.snowpark.internal.Utils.clientPackageName
import java.io.{BufferedOutputStream, File, FileOutputStream}
import java.nio.file.{Files, NoSuchFileException}
import com.snowflake.snowpark.internal.{JavaUtils, UDFClassPath}
import sun.net.www.ParseUtil

import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.io.{AbstractFile, VirtualDirectory}
Expand All @@ -23,11 +22,6 @@ class UDFRegistrationSuite extends SNTestBase with FileUtils {
session.runQuery(s"create or replace temporary stage $tempStage")
}

test("Test URL encoding") {
val inputs = Seq("dbiufwhronr==", "fdeswfirn--", "bsdij++", "sb#i", "bxsj%i@9h(nb)")
inputs.foreach(str => assert(ParseUtil.decode(ParseUtil.encodePath(str)) == str))
}

test("Test that jar files are uploaded to stage correctly") {
val udfRegistrar = new UDFRegistration(session)
val classDirs = UDFClassPath.classDirs(session).toList
Expand Down

0 comments on commit 729d50f

Please sign in to comment.