Skip to content

Commit

Permalink
Returns original string when decoded error
Browse files Browse the repository at this point in the history
  • Loading branch information
herunkang2018 committed Oct 26, 2023
1 parent d6bde22 commit 7fbea22
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
11 changes: 6 additions & 5 deletions core/src/main/java/org/apache/calcite/runtime/UrlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ private UrlFunctions() {

private static final Charset UTF_8 = StandardCharsets.UTF_8;

/** The "URL_DECODE(string)" function. */
/** The "URL_DECODE(string)" function for Hive and Spark,
* which returns original value when decoded error. */
public static String urlDecode(String value) {
String url;
try {
url = URLDecoder.decode(value, UTF_8.name());
return URLDecoder.decode(value, UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw RESOURCE.charsetEncoding(value, UTF_8.name()).ex();
} catch (RuntimeException e) {
return value;
}
return url;
}

/** The "URL_ENCODE(string)" function. */
/** The "URL_ENCODE(string)" function for Hive and Spark. */
public static String urlEncode(String url) {
String value;
try {
Expand Down
2 changes: 1 addition & 1 deletion site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2854,7 +2854,7 @@ BigQuery's type system uses confusingly different names for types and functions:
| b | UNIX_MILLIS(timestamp) | Returns the number of milliseconds since 1970-01-01 00:00:00
| b | UNIX_SECONDS(timestamp) | Returns the number of seconds since 1970-01-01 00:00:00
| b | UNIX_DATE(date) | Returns the number of days since 1970-01-01
| s | URL_DECODE(string) | Decodes a *string* in 'application/x-www-form-urlencoded' format using a specific encoding scheme
| s | URL_DECODE(string) | Decodes a *string* in 'application/x-www-form-urlencoded' format using a specific encoding scheme, returns original *string* when decoded error
| s | URL_ENCODE(string) | Translates a *string* into 'application/x-www-form-urlencoded' format using a specific encoding scheme
| o | XMLTRANSFORM(xml, xslt) | Applies XSLT transform *xslt* to XML string *xml* and returns the result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5512,13 +5512,12 @@ private static void checkIf(SqlOperatorFixture f) {
f.checkString("URL_DECODE('')",
"",
"VARCHAR NOT NULL");
f.checkFails("URL_DECODE('https%%3A%2F%2Fcalcite.apache.org')",
"URLDecoder: Illegal hex characters in escape (%) pattern - "
+ "For input string: \"%3\"",
true);
f.checkFails("URL_DECODE('https%3A%2F%2Fcalcite.apache.org%')",
"URLDecoder: Incomplete trailing escape (%) pattern",
true);
f.checkString("URL_DECODE('https%%3A%2F%2Fcalcite.apache.org')",
"https%%3A%2F%2Fcalcite.apache.org",
"VARCHAR NOT NULL");
f.checkString("URL_DECODE('https%3A%2F%2Fcalcite.apache.org%')",
"https%3A%2F%2Fcalcite.apache.org%",
"VARCHAR NOT NULL");
f.checkNull("URL_DECODE(cast(null as varchar))");
}

Expand Down

0 comments on commit 7fbea22

Please sign in to comment.