-
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 3 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.comet.serde | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, ParseUrl} | ||
|
|
||
| import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, optExprWithInfo, scalarFunctionExprToProto} | ||
|
|
||
| object CometParseUrl extends CometExpressionSerde[ParseUrl] { | ||
|
|
||
| // The full list of edge-case divergences is tracked at | ||
| // https://github.com/apache/datafusion/issues/21943. | ||
| private val incompatibleReason = | ||
| "Native parse_url diverges from Spark on several edge cases. " + | ||
| "See https://github.com/apache/datafusion/issues/21943." | ||
|
|
||
| override def getIncompatibleReasons(): Seq[String] = Seq(incompatibleReason) | ||
|
|
||
| override def getSupportLevel(expr: ParseUrl): SupportLevel = | ||
| Incompatible(Some(incompatibleReason)) | ||
|
|
||
| override def convert( | ||
| expr: ParseUrl, | ||
| inputs: Seq[Attribute], | ||
| binding: Boolean): Option[ExprOuterClass.Expr] = { | ||
| val funcName = if (expr.failOnError) "parse_url" else "try_parse_url" | ||
| val childExprs = expr.children.map(exprToProtoInternal(_, inputs, binding)) | ||
| val optExpr = scalarFunctionExprToProto(funcName, childExprs: _*) | ||
| optExprWithInfo(optExpr, expr, expr.children: _*) | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
spark/src/test/resources/sql-tests/expressions/url/parse_url.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,37 @@ | ||
| -- 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. | ||
|
|
||
| -- parse_url is marked Incompatible (see CometParseUrl). Known divergences from | ||
| -- Spark, tracked upstream at https://github.com/apache/datafusion/issues/21943: | ||
| -- 1. empty-string URL returns NULL instead of "" for any part | ||
| -- 2. FILE on a URL without an explicit path returns "/?..." instead of "?..." | ||
| -- 3. PATH on a URL with a bare trailing slash returns "" instead of "/" | ||
| -- In the default configuration, Comet falls back to Spark. The two queries below | ||
| -- both verify a normal-shape URL takes the fallback path, and exercise one of | ||
| -- the divergent shapes (trailing-slash PATH) to lock in that fallback handles | ||
| -- it correctly. See parse_url_native.sql for native-execution coverage. | ||
|
|
||
| query expect_fallback(not fully compatible with Spark) | ||
| SELECT parse_url('http://spark.apache.org/path?query=1', 'HOST') | ||
|
|
||
| query expect_fallback(not fully compatible with Spark) | ||
| SELECT parse_url('http://spark.apache.org/path?query=1', 'QUERY', 'query') | ||
|
|
||
| -- Trailing-slash PATH: Spark returns "/", native impl returns "". Verifying | ||
| -- the fallback path emits Spark's "/". | ||
| query expect_fallback(not fully compatible with Spark) | ||
| SELECT parse_url('http://example.com/', 'PATH') |
85 changes: 85 additions & 0 deletions
85
spark/src/test/resources/sql-tests/expressions/url/parse_url_native.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,85 @@ | ||
| -- 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. | ||
|
|
||
| -- Exercises the native parse_url implementation. Inputs are restricted to | ||
| -- URLs with explicit paths because the native implementation diverges from | ||
| -- Spark for empty-string input and for FILE extraction on path-less URLs. | ||
| -- Tracked upstream at https://github.com/apache/datafusion/issues/21943. | ||
|
|
||
| -- Config: spark.comet.expression.ParseUrl.allowIncompatible=true | ||
| -- Config: spark.sql.ansi.enabled=true | ||
|
|
||
| statement | ||
| CREATE TABLE test_urls_native(url string) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_urls_native VALUES | ||
| ('http://spark.apache.org/path?query=1'), | ||
| ('http://user:password@host:8080/path?key=value&key2=value2#ref'), | ||
| ('http://example.com/path'), | ||
| (NULL) | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'HOST') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'PATH') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'QUERY') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'REF') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'PROTOCOL') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'FILE') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'AUTHORITY') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'USERINFO') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'QUERY', 'query') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'QUERY', 'key') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'QUERY', 'key2') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url(url, 'QUERY', 'nonexistent') FROM test_urls_native | ||
|
|
||
| query | ||
| SELECT parse_url('http://spark.apache.org/path?query=1', 'HOST') | ||
|
|
||
| query | ||
| SELECT parse_url('http://spark.apache.org/path?query=1', 'QUERY', 'query') | ||
|
|
||
| query | ||
| SELECT parse_url(NULL, 'HOST') | ||
|
|
||
| -- ANSI-mode invalid URL: parse_url's failOnError is driven by spark.sql.ansi.enabled | ||
| -- (set above). Both Spark (INVALID_URL error class) and Comet's native impl | ||
| -- produce a message starting "The url is invalid". | ||
| query expect_error(The url is invalid) | ||
| SELECT parse_url('inva lid://user:pass@host/file', 'HOST') |
40 changes: 40 additions & 0 deletions
40
spark/src/test/resources/sql-tests/expressions/url/try_parse_url.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,40 @@ | ||
| -- 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_parse_url is Spark 4.0+. It rewrites to ParseUrl(_, failOnError=false), | ||
| -- so Comet emits the native try_parse_url scalar function. parse_url is marked | ||
| -- Incompatible (see CometParseUrl), so this test opts in via allowIncompatible. | ||
|
|
||
| -- MinSparkVersion: 4.0 | ||
| -- Config: spark.comet.expression.ParseUrl.allowIncompatible=true | ||
|
|
||
| -- Valid URL: same answer as parse_url. | ||
| query | ||
| SELECT try_parse_url('http://spark.apache.org/path?query=1', 'HOST') | ||
|
|
||
| query | ||
| SELECT try_parse_url('http://spark.apache.org/path?query=1', 'QUERY', 'query') | ||
|
|
||
| -- Malformed URL with a scheme: Spark returns NULL, Comet's try_parse_url | ||
| -- returns NULL (failOnError=false propagates through CometParseUrl to the | ||
| -- native try_parse_url UDF). | ||
| query | ||
| SELECT try_parse_url('inva lid://user:pass@host/file', 'HOST') | ||
|
|
||
| -- NULL input. | ||
| query | ||
| SELECT try_parse_url(NULL, 'HOST') |
Oops, something went wrong.
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(...).