lincoln-lil commented on code in PR #27351:
URL: https://github.com/apache/flink/pull/27351#discussion_r2910295228
##########
docs/data/sql_functions.yml:
##########
@@ -492,6 +492,13 @@ string:
description:
Decodes a given string in 'application/x-www-form-urlencoded' format
using the UTF-8 encoding scheme.
If the input is NULL, or there is an issue with the decoding
process(such as encountering an illegal escape pattern), or the encoding scheme
is not supported, the function returns NULL.
+ - sql: URL_DECODE_RECURSIVE(string, boolean)
Review Comment:
If we decide to introduce a new function, the `boolean` param seems
unnecessary because the function name already indicate the `recursive`
behavior, and for the non-recursive usage users can use `url_decode` function,
WDYT?
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/UrlDecodeFunction.java:
##########
@@ -49,4 +61,56 @@ public
UrlDecodeFunction(SpecializedFunction.SpecializedContext context) {
return null;
}
}
+
+ /**
+ * Decodes a URL-encoded string with optional recursive decoding.
+ *
+ * @param value the URL-encoded string to decode, can be null
+ * @param recursive if true, performs recursive decoding until no further
decoding is possible;
+ * if false or null, performs only a single decode operation
+ * @return the decoded string as StringData, or null if input is null or
decoding fails
+ */
+ public @Nullable StringData eval(StringData value, Boolean recursive) {
+ if (value == null) {
+ return null;
+ }
+
+ final Charset charset = StandardCharsets.UTF_8;
+
+ // If recursive is false or null, perform only one decode
+ if (recursive == null || !recursive) {
+ try {
+ return
StringData.fromString(URLDecoder.decode(value.toString(), charset.name()));
+ } catch (UnsupportedEncodingException | RuntimeException e) {
+ return null;
+ }
+ }
+
+ // If recursive is true, perform cascading decode until no further
decoding is possible
+ String currentValue = value.toString();
+ String previousValue = currentValue;
+ int maxIterations = 10; // Prevent infinite loops
Review Comment:
The hard-coded magic number here is not recommend, compare to the current
boolean param in the function, the max recursive depth looks more meanful to
users.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/UrlDecodeFunction.java:
##########
@@ -49,4 +61,56 @@ public
UrlDecodeFunction(SpecializedFunction.SpecializedContext context) {
return null;
}
}
+
+ /**
+ * Decodes a URL-encoded string with optional recursive decoding.
+ *
+ * @param value the URL-encoded string to decode, can be null
+ * @param recursive if true, performs recursive decoding until no further
decoding is possible;
+ * if false or null, performs only a single decode operation
+ * @return the decoded string as StringData, or null if input is null or
decoding fails
+ */
+ public @Nullable StringData eval(StringData value, Boolean recursive) {
+ if (value == null) {
+ return null;
+ }
+
+ final Charset charset = StandardCharsets.UTF_8;
+
+ // If recursive is false or null, perform only one decode
+ if (recursive == null || !recursive) {
+ try {
+ return
StringData.fromString(URLDecoder.decode(value.toString(), charset.name()));
+ } catch (UnsupportedEncodingException | RuntimeException e) {
+ return null;
+ }
+ }
+
+ // If recursive is true, perform cascading decode until no further
decoding is possible
+ String currentValue = value.toString();
+ String previousValue = currentValue;
+ int maxIterations = 10; // Prevent infinite loops
+ int iteration = 0;
+
+ try {
+ do {
+ previousValue = currentValue;
+ currentValue = URLDecoder.decode(currentValue, charset.name());
+ iteration++;
+
+ // Stop if we reach max iterations or if decoding produces the
same result
+ if (iteration >= maxIterations ||
currentValue.equals(previousValue)) {
+ break;
+ }
+ } while (true);
Review Comment:
This can be a normal while block without break.
##########
docs/data/sql_functions.yml:
##########
@@ -492,6 +492,13 @@ string:
description:
Decodes a given string in 'application/x-www-form-urlencoded' format
using the UTF-8 encoding scheme.
If the input is NULL, or there is an issue with the decoding
process(such as encountering an illegal escape pattern), or the encoding scheme
is not supported, the function returns NULL.
+ - sql: URL_DECODE_RECURSIVE(string, boolean)
+ table: STRING.urlDecode(BOOLEAN)
Review Comment:
Consider the new `BuiltInFunctionDefinition` with the new function name
'URL_DECODE_RECURSIVE', we'd better align the naming in table api.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]