Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-33468][SQL] ParseUrl in ANSI mode should fail if input string is not a valid url #30399

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions docs/sql-ref-ansi-compliance.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ The behavior of some SQL functions can be different under ANSI mode (`spark.sql.
- `element_at`: This function throws `ArrayIndexOutOfBoundsException` if using invalid indices.
- `element_at`: This function throws `NoSuchElementException` if key does not exist in map.
- `elt`: This function throws `ArrayIndexOutOfBoundsException` if using invalid indices.
- `parse_url`: This function throws `IllegalArgumentException` if input string is not a valid url.
Copy link
Member

Choose a reason for hiding this comment

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

nit: input string -> an input string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry missed here. Updated !


### SQL Operators

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ object ParseUrl {
1
""",
since = "2.0.0")
case class ParseUrl(children: Seq[Expression])
case class ParseUrl(children: Seq[Expression], failOnError: Boolean = SQLConf.get.ansiEnabled)
extends Expression with ExpectsInputTypes with CodegenFallback {

override def nullable: Boolean = true
Expand Down Expand Up @@ -1404,7 +1404,9 @@ case class ParseUrl(children: Seq[Expression])
try {
new URI(url.toString)
} catch {
case e: URISyntaxException => null
case e: URISyntaxException if failOnError =>
throw new IllegalArgumentException(s"Find an invaild url string ${url.toString}", e)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To clarify, the raw exception message is something like Illegal character in query at index 33: https://a.b.c/index.php?params1=a|b&params2=x.

case _: URISyntaxException => null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,20 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
GenerateUnsafeProjection.generate(ParseUrl(Seq(Literal("\"quote"), Literal("\"quote"))) :: Nil)
}

test("SPARK-33468: ParseUrl in ANSI mode should fail if input string is not a valid url") {
withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
val msg = intercept[IllegalArgumentException] {
evaluateWithoutCodegen(
ParseUrl(Seq("https://a.b.c/index.php?params1=a|b&params2=x", "HOST")))
}.getMessage
assert(msg.contains("Find an invaild url string"))
}
withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") {
checkEvaluation(
ParseUrl(Seq("https://a.b.c/index.php?params1=a|b&params2=x", "HOST")), null)
}
}

test("Sentences") {
val nullString = Literal.create(null, StringType)
checkEvaluation(Sentences(nullString, nullString, nullString), null)
Expand Down