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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.spark.sql.catalyst.expressions

import java.net.{MalformedURLException, URL}
import java.net.{URI, URISyntaxException}
import java.text.{BreakIterator, DecimalFormat, DecimalFormatSymbols}
import java.util.{HashMap, Locale, Map => JMap}
import java.util.regex.Pattern
Expand Down Expand Up @@ -749,25 +749,44 @@ case class ParseUrl(children: Seq[Expression])
Pattern.compile(REGEXPREFIX + key.toString + REGEXSUBFIX)
}

private def getUrl(url: UTF8String): URL = {
private def getUrl(url: UTF8String): URI = {
try {
new URL(url.toString)
new URI(url.toString)
} catch {
case e: MalformedURLException => null
case e: URISyntaxException => null
Copy link
Member

Choose a reason for hiding this comment

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

Don't change this unless you need to make another change anyway, but this can be case _: ... I know it wasn't like that before

}
}

private def getExtractPartFunc(partToExtract: UTF8String): URL => String = {
private def getExtractPartFunc(partToExtract: UTF8String): URI => String = {

// partToExtract match {
// case HOST => _.toURL().getHost
// case PATH => _.toURL().getPath
// case QUERY => _.toURL().getQuery
// case REF => _.toURL().getRef
// case PROTOCOL => _.toURL().getProtocol
// case FILE => _.toURL().getFile
// case AUTHORITY => _.toURL().getAuthority
// case USERINFO => _.toURL().getUserInfo
// case _ => (url: URI) => null
// }

partToExtract match {
case HOST => _.getHost
case PATH => _.getPath
case QUERY => _.getQuery
case REF => _.getRef
case PROTOCOL => _.getProtocol
case FILE => _.getFile
case AUTHORITY => _.getAuthority
case USERINFO => _.getUserInfo
case _ => (url: URL) => null
case PATH => _.getRawPath
case QUERY => _.getRawQuery
case REF => _.getRawFragment
case PROTOCOL => _.getScheme
case FILE =>
(url: URI) =>
if (url.getRawQuery ne null) {
Copy link
Member

Choose a reason for hiding this comment

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

We really need the 'raw' elements in each of these? I'd think they need to be parsed. Your tests show this code not parsing escapes. Is that how it behaved before? then OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does seem so:

scala> new URL("http://example.com/path%20?query=x%20#hash%20").getQuery()
res1: String = query=x%20

scala> new URL("http://example.com/path%20?query=x%20#hash%20").getRef()
res2: String = hash%20

url.getRawPath + "?" + url.getRawQuery
} else {
url.getRawPath
}
case AUTHORITY => _.getRawAuthority
case USERINFO => _.getRawUserInfo
case _ => (url: URI) => null
}
}

Expand All @@ -780,7 +799,7 @@ case class ParseUrl(children: Seq[Expression])
}
}

private def extractFromUrl(url: URL, partToExtract: UTF8String): UTF8String = {
private def extractFromUrl(url: URI, partToExtract: UTF8String): UTF8String = {
if (cachedExtractPartFunc ne null) {
UTF8String.fromString(cachedExtractPartFunc.apply(url))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,48 @@ class StringFunctionsSuite extends QueryTest with SharedSQLContext {
}

test("string parse_url function") {
val df = Seq[String](("http://userinfo@spark.apache.org/path?query=1#Ref"))
.toDF("url")

checkAnswer(
df.selectExpr(
def testUrl(url: String, expected: Row) {
checkAnswer(Seq[String]((url)).toDF("url").selectExpr(
"parse_url(url, 'HOST')", "parse_url(url, 'PATH')",
"parse_url(url, 'QUERY')", "parse_url(url, 'REF')",
"parse_url(url, 'PROTOCOL')", "parse_url(url, 'FILE')",
"parse_url(url, 'AUTHORITY')", "parse_url(url, 'USERINFO')",
"parse_url(url, 'QUERY', 'query')"),
"parse_url(url, 'QUERY', 'query')"), expected)
}

testUrl(
"http://userinfo@spark.apache.org/path?query=1#Ref",
Row("spark.apache.org", "/path", "query=1", "Ref",
"http", "/path?query=1", "userinfo@spark.apache.org", "userinfo", "1"))

testUrl(
"https://use%20r:pas%20s@example.com/dir%20/pa%20th.HTML?query=x%20y&q2=2#Ref%20two",
Row("example.com", "/dir%20/pa%20th.HTML", "query=x%20y&q2=2", "Ref%20two",
"https", "/dir%20/pa%20th.HTML?query=x%20y&q2=2", "use%20r:pas%20s@example.com",
"use%20r:pas%20s", "x%20y"))

testUrl(
"http://user:pass@host",
Row("host", "", null, null, "http", "", "user:pass@host", "user:pass", null))

testUrl(
"http://user:pass@host/",
Row("host", "/", null, null, "http", "/", "user:pass@host", "user:pass", null))

testUrl(
"http://user:pass@host/?#",
Row("host", "/", "", "", "http", "/?", "user:pass@host", "user:pass", null))

testUrl(
"http://user:pass@host/file;param?query;p2",
Row("host", "/file;param", "query;p2", null, "http", "/file;param?query;p2",
"user:pass@host", "user:pass", null))

testUrl(
"inva lid://user:pass@host/file;param?query;p2",
Row(null, null, null, null, null, null, null, null, null))

}

test("string repeat function") {
Expand Down