-
Notifications
You must be signed in to change notification settings - Fork 315
feat: add support for url_encode, url_decode, and try_url_decode #4231
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
70 changes: 70 additions & 0 deletions
70
spark/src/test/resources/sql-tests/expressions/url/try_url_decode.sql
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| -- Licensed to the Apache Software Foundation (ASF) under one | ||
| -- or more contributor license agreements. See the NOTICE file | ||
| -- distributed with this work for additional information | ||
| -- regarding copyright ownership. The ASF licenses this file | ||
| -- to you under the Apache License, Version 2.0 (the | ||
| -- "License"); you may not use this file except in compliance | ||
| -- with the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, | ||
| -- software distributed under the License is distributed on an | ||
| -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| -- KIND, either express or implied. See the License for the | ||
| -- specific language governing permissions and limitations | ||
| -- under the License. | ||
|
|
||
| -- try_url_decode is Spark 4.0+. It rewrites to UrlDecode(_, failOnError=false), | ||
| -- which becomes StaticInvoke(UrlCodec, "decode", [child, Literal(false)], ...). | ||
| -- CometUrlDecodeStaticInvoke detects failOnError=false and emits try_url_decode. | ||
|
|
||
| -- MinSparkVersion: 4.0 | ||
|
|
||
| statement | ||
| CREATE TABLE test_try_decode(s string) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_try_decode VALUES | ||
| ('https%3A%2F%2Fspark.apache.org'), | ||
| ('hello+world'), | ||
| ('a%2Bb%3Dc%26d%3De'), | ||
| ('caf%C3%A9'), | ||
| (''), | ||
| (NULL), | ||
| ('no+encoding+needed'), | ||
| ('%21%40%23%24%25%5E%26%2A%28%29%5F%2B'), | ||
| ('%2a%2b%2c'), | ||
| ('http%3A%2F%2spark.apache.org') | ||
|
|
||
| query | ||
| SELECT try_url_decode(s) FROM test_try_decode | ||
|
|
||
| -- literal arguments | ||
| query | ||
| SELECT try_url_decode('https%3A%2F%2Fspark.apache.org') | ||
|
|
||
| query | ||
| SELECT try_url_decode('hello+world') | ||
|
|
||
| query | ||
| SELECT try_url_decode('') | ||
|
|
||
| query | ||
| SELECT try_url_decode(NULL) | ||
|
|
||
| -- roundtrip: encode then decode | ||
| query | ||
| SELECT try_url_decode(url_encode('hello world & goodbye')) | ||
|
|
||
| -- multibyte UTF-8 | ||
| query | ||
| SELECT try_url_decode('%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%83%86%E3%82%B9%E3%83%88') | ||
|
|
||
| -- lowercase hex (RFC 3986 says hex digits are case-insensitive) | ||
| query | ||
| SELECT try_url_decode('%2a%2b%2c') | ||
|
|
||
| -- malformed percent-encoding: try_url_decode returns NULL instead of erroring | ||
| query | ||
| SELECT try_url_decode('http%3A%2F%2spark.apache.org') |
65 changes: 65 additions & 0 deletions
65
spark/src/test/resources/sql-tests/expressions/url/url_decode.sql
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| -- Licensed to the Apache Software Foundation (ASF) under one | ||
| -- or more contributor license agreements. See the NOTICE file | ||
| -- distributed with this work for additional information | ||
| -- regarding copyright ownership. The ASF licenses this file | ||
| -- to you under the Apache License, Version 2.0 (the | ||
| -- "License"); you may not use this file except in compliance | ||
| -- with the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, | ||
| -- software distributed under the License is distributed on an | ||
| -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| -- KIND, either express or implied. See the License for the | ||
| -- specific language governing permissions and limitations | ||
| -- under the License. | ||
|
|
||
| -- url_decode function | ||
| statement | ||
| CREATE TABLE test_decode(s string) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_decode VALUES | ||
| ('https%3A%2F%2Fspark.apache.org'), | ||
| ('hello+world'), | ||
| ('a%2Bb%3Dc%26d%3De'), | ||
| ('caf%C3%A9'), | ||
| (''), | ||
| (NULL), | ||
| ('no+encoding+needed'), | ||
| ('%21%40%23%24%25%5E%26%2A%28%29%5F%2B'), | ||
| ('%2a%2b%2c') | ||
|
|
||
| query | ||
| SELECT url_decode(s) FROM test_decode | ||
|
|
||
| -- literal arguments | ||
| query | ||
| SELECT url_decode('https%3A%2F%2Fspark.apache.org') | ||
|
|
||
| query | ||
| SELECT url_decode('hello+world') | ||
|
|
||
| query | ||
| SELECT url_decode('') | ||
|
|
||
| query | ||
| SELECT url_decode(NULL) | ||
|
|
||
| -- roundtrip: encode then decode | ||
| query | ||
| SELECT url_decode(url_encode('hello world & goodbye')) | ||
|
|
||
| -- multibyte UTF-8 | ||
| query | ||
| SELECT url_decode('%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%83%86%E3%82%B9%E3%83%88') | ||
|
|
||
| -- lowercase hex (RFC 3986 says hex digits are case-insensitive) | ||
| query | ||
| SELECT url_decode('%2a%2b%2c') | ||
|
|
||
| -- malformed percent-encoding: both Spark and Comet must error and the bad | ||
| -- sequence must appear in the error message | ||
| query expect_error(%2s) | ||
| SELECT url_decode('http%3A%2F%2spark.apache.org') |
69 changes: 69 additions & 0 deletions
69
spark/src/test/resources/sql-tests/expressions/url/url_encode.sql
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| -- Licensed to the Apache Software Foundation (ASF) under one | ||
| -- or more contributor license agreements. See the NOTICE file | ||
| -- distributed with this work for additional information | ||
| -- regarding copyright ownership. The ASF licenses this file | ||
| -- to you under the Apache License, Version 2.0 (the | ||
| -- "License"); you may not use this file except in compliance | ||
| -- with the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, | ||
| -- software distributed under the License is distributed on an | ||
| -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| -- KIND, either express or implied. See the License for the | ||
| -- specific language governing permissions and limitations | ||
| -- under the License. | ||
|
|
||
| -- url_encode function | ||
| statement | ||
| CREATE TABLE test_encode(s string) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_encode VALUES | ||
| ('https://spark.apache.org'), | ||
| ('hello world'), | ||
| ('a+b=c&d=e'), | ||
| ('café'), | ||
| (''), | ||
| (NULL), | ||
| ('foo bar/baz?x=1&y=2'), | ||
| ('~*''()'), | ||
| ('a%20b'), | ||
| ('\t\n\r') | ||
|
|
||
| query | ||
| SELECT url_encode(s) FROM test_encode | ||
|
|
||
| -- literal arguments | ||
| query | ||
| SELECT url_encode('https://spark.apache.org') | ||
|
|
||
| query | ||
| SELECT url_encode('hello world') | ||
|
|
||
| query | ||
| SELECT url_encode('') | ||
|
|
||
| query | ||
| SELECT url_encode(NULL) | ||
|
|
||
| -- special characters | ||
| query | ||
| SELECT url_encode('a b+c&d=e/f') | ||
|
|
||
| -- multibyte UTF-8 | ||
| query | ||
| SELECT url_encode('日本語テスト') | ||
|
|
||
| -- boundary characters in the preserved set | ||
| query | ||
| SELECT url_encode('~*''()') | ||
|
|
||
| -- already-encoded input (verify double-encoding of percent) | ||
| query | ||
| SELECT url_encode('a%20b') | ||
|
|
||
| -- whitespace control characters | ||
| query | ||
| SELECT url_encode('\t\n\r') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the Spark source for UrlEncode / UrlDecode, the rewrite uses:
StaticInvoke(UrlCodec.getClass, dataType, "encode", Seq(child), ...)
StaticInvoke(UrlCodec.getClass, dataType, "decode", Seq(child, Literal(failOnError)), ...)
The third argument is the JVM method name on UrlCodec, which is literally "encode" and "decode". The user-facing SQL names url_encode / url_decode come from prettyName, not functionName.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@comphead These correspond to the names in Spark code -
UrlCodec.encode(...)andUrlCodec.decode(...).