Skip to content

Commit f6a9b86

Browse files
wForgetyaooqinn
authored andcommitted
[SPARK-48806][SQL] Pass actual exception when url_decode fails
### What changes were proposed in this pull request? Pass actual exception for url_decode. Follow-up to https://issues.apache.org/jira/browse/SPARK-40156 ### Why are the changes needed? Currently url_decode function ignores actual exception, which contains information that is useful for quickly locating the problem. Like executing this sql: ``` select url_decode('https%3A%2F%2spark.apache.org'); ``` We only get the error message: ``` org.apache.spark.SparkIllegalArgumentException: [CANNOT_DECODE_URL] The provided URL cannot be decoded: https%3A%2F%2spark.apache.org. Please ensure that the URL is properly formatted and try again. at org.apache.spark.sql.errors.QueryExecutionErrors$.illegalUrlError(QueryExecutionErrors.scala:376) at org.apache.spark.sql.catalyst.expressions.UrlCodec$.decode(urlExpressions.scala:118) at org.apache.spark.sql.catalyst.expressions.UrlCodec.decode(urlExpressions.scala) ``` However, the actual useful exception information is ignored: ``` java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - Error at index 1 in: "2s" ``` After this pr we will get: ``` org.apache.spark.SparkIllegalArgumentException: [CANNOT_DECODE_URL] The provided URL cannot be decoded: https%3A%2F%2spark.apache.org. Please ensure that the URL is properly formatted and try again. SQLSTATE: 22546 at org.apache.spark.sql.errors.QueryExecutionErrors$.illegalUrlError(QueryExecutionErrors.scala:372) at org.apache.spark.sql.catalyst.expressions.UrlCodec$.decode(urlExpressions.scala:119) at org.apache.spark.sql.catalyst.expressions.UrlCodec.decode(urlExpressions.scala) ... Caused by: java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - Error at index 1 in: "2s" at java.base/java.net.URLDecoder.decode(URLDecoder.java:237) at java.base/java.net.URLDecoder.decode(URLDecoder.java:147) at org.apache.spark.sql.catalyst.expressions.UrlCodec$.decode(urlExpressions.scala:116) ... 135 more ``` ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? unit test ### Was this patch authored or co-authored using generative AI tooling? No Closes apache#47211 from wForget/SPARK-48806. Lead-authored-by: wforget <643348094@qq.com> Co-authored-by: Kent Yao <yao@apache.org> Signed-off-by: Kent Yao <yao@apache.org>
1 parent 76e9703 commit f6a9b86

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/urlExpressions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ object UrlCodec {
116116
UTF8String.fromString(URLDecoder.decode(src.toString, enc.toString))
117117
} catch {
118118
case e: IllegalArgumentException =>
119-
throw QueryExecutionErrors.illegalUrlError(src)
119+
throw QueryExecutionErrors.illegalUrlError(src, e)
120120
}
121121
}
122122
}

sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,11 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase with ExecutionE
374374
cause = e)
375375
}
376376

377-
def illegalUrlError(url: UTF8String): Throwable = {
377+
def illegalUrlError(url: UTF8String, e: IllegalArgumentException): Throwable = {
378378
new SparkIllegalArgumentException(
379379
errorClass = "CANNOT_DECODE_URL",
380-
messageParameters = Map("url" -> url.toString)
380+
messageParameters = Map("url" -> url.toString),
381+
cause = e
381382
)
382383
}
383384

sql/core/src/test/scala/org/apache/spark/sql/StringFunctionsSuite.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.apache.spark.sql
1919

20-
import org.apache.spark.{SPARK_DOC_ROOT, SparkRuntimeException}
20+
import org.apache.spark.{SPARK_DOC_ROOT, SparkIllegalArgumentException, SparkRuntimeException}
2121
import org.apache.spark.sql.catalyst.expressions.Cast._
2222
import org.apache.spark.sql.execution.FormattedMode
2323
import org.apache.spark.sql.functions._
@@ -1273,4 +1273,13 @@ class StringFunctionsSuite extends QueryTest with SharedSparkSession {
12731273
)
12741274
)
12751275
}
1276+
1277+
test("SPARK-48806: url_decode exception") {
1278+
val e = intercept[SparkIllegalArgumentException] {
1279+
sql("select url_decode('https%3A%2F%2spark.apache.org')").collect()
1280+
}
1281+
assert(e.getCause.isInstanceOf[IllegalArgumentException] &&
1282+
e.getCause.getMessage
1283+
.startsWith("URLDecoder: Illegal hex characters in escape (%) pattern - "))
1284+
}
12761285
}

0 commit comments

Comments
 (0)