forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-16281][SQL] Implement parse_url SQL function
## What changes were proposed in this pull request? This PR adds parse_url SQL functions in order to remove Hive fallback. A new implementation of apache#13999 ## How was this patch tested? Pass the exist tests including new testcases. Author: wujian <[email protected]> Closes apache#14008 from janplus/SPARK-16281.
- Loading branch information
Showing
5 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -726,6 +726,57 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | |
checkEvaluation(FindInSet(Literal("ab,"), Literal("abc,b,ab,c,def")), 0) | ||
} | ||
|
||
test("ParseUrl") { | ||
def checkParseUrl(expected: String, urlStr: String, partToExtract: String): Unit = { | ||
checkEvaluation( | ||
ParseUrl(Seq(Literal(urlStr), Literal(partToExtract))), expected) | ||
} | ||
def checkParseUrlWithKey( | ||
expected: String, | ||
urlStr: String, | ||
partToExtract: String, | ||
key: String): Unit = { | ||
checkEvaluation( | ||
ParseUrl(Seq(Literal(urlStr), Literal(partToExtract), Literal(key))), expected) | ||
} | ||
|
||
checkParseUrl("spark.apache.org", "http://spark.apache.org/path?query=1", "HOST") | ||
checkParseUrl("/path", "http://spark.apache.org/path?query=1", "PATH") | ||
checkParseUrl("query=1", "http://spark.apache.org/path?query=1", "QUERY") | ||
checkParseUrl("Ref", "http://spark.apache.org/path?query=1#Ref", "REF") | ||
checkParseUrl("http", "http://spark.apache.org/path?query=1", "PROTOCOL") | ||
checkParseUrl("/path?query=1", "http://spark.apache.org/path?query=1", "FILE") | ||
checkParseUrl("spark.apache.org:8080", "http://spark.apache.org:8080/path?query=1", "AUTHORITY") | ||
checkParseUrl("userinfo", "http://[email protected]/path?query=1", "USERINFO") | ||
checkParseUrlWithKey("1", "http://spark.apache.org/path?query=1", "QUERY", "query") | ||
|
||
// Null checking | ||
checkParseUrl(null, null, "HOST") | ||
checkParseUrl(null, "http://spark.apache.org/path?query=1", null) | ||
checkParseUrl(null, null, null) | ||
checkParseUrl(null, "test", "HOST") | ||
checkParseUrl(null, "http://spark.apache.org/path?query=1", "NO") | ||
checkParseUrl(null, "http://spark.apache.org/path?query=1", "USERINFO") | ||
checkParseUrlWithKey(null, "http://spark.apache.org/path?query=1", "HOST", "query") | ||
checkParseUrlWithKey(null, "http://spark.apache.org/path?query=1", "QUERY", "quer") | ||
checkParseUrlWithKey(null, "http://spark.apache.org/path?query=1", "QUERY", null) | ||
checkParseUrlWithKey(null, "http://spark.apache.org/path?query=1", "QUERY", "") | ||
|
||
// exceptional cases | ||
intercept[java.util.regex.PatternSyntaxException] { | ||
evaluate(ParseUrl(Seq(Literal("http://spark.apache.org/path?"), | ||
Literal("QUERY"), Literal("???")))) | ||
} | ||
|
||
// arguments checking | ||
assert(ParseUrl(Seq(Literal("1"))).checkInputDataTypes().isFailure) | ||
assert(ParseUrl(Seq(Literal("1"), Literal("2"), Literal("3"), Literal("4"))) | ||
.checkInputDataTypes().isFailure) | ||
assert(ParseUrl(Seq(Literal("1"), Literal(2))).checkInputDataTypes().isFailure) | ||
assert(ParseUrl(Seq(Literal(1), Literal("2"))).checkInputDataTypes().isFailure) | ||
assert(ParseUrl(Seq(Literal("1"), Literal("2"), Literal(3))).checkInputDataTypes().isFailure) | ||
} | ||
|
||
test("Sentences") { | ||
val nullString = Literal.create(null, StringType) | ||
checkEvaluation(Sentences(nullString, nullString, nullString), null) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,6 +228,21 @@ class StringFunctionsSuite extends QueryTest with SharedSQLContext { | |
Row("???hi", "hi???", "h", "h")) | ||
} | ||
|
||
test("string parse_url function") { | ||
val df = Seq[String](("http://[email protected]/path?query=1#Ref")) | ||
.toDF("url") | ||
|
||
checkAnswer( | ||
df.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')"), | ||
Row("spark.apache.org", "/path", "query=1", "Ref", | ||
"http", "/path?query=1", "[email protected]", "userinfo", "1")) | ||
} | ||
|
||
test("string repeat function") { | ||
val df = Seq(("hi", 2)).toDF("a", "b") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters