Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions crates/spark/src/function/url/parse_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use arrow::datatypes::DataType;
use datafusion_common::cast::{
as_large_string_array, as_string_array, as_string_view_array,
};
use datafusion_common::{Result, exec_datafusion_err, exec_err};
use datafusion_common::{DataFusionError, Result, exec_datafusion_err, exec_err};
use datafusion_expr::{
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature,
Volatility,
Expand Down Expand Up @@ -97,12 +97,10 @@ impl ParseUrl {
_ => None,
})
} else {
Err(exec_datafusion_err!(
"The url is invalid: {value}. Use `try_parse_url` to tolerate invalid URL and return NULL instead. SQLSTATE: 22P02"
))
Err(Self::invalid_url_err(value))
};
}
url.map_err(|e| exec_datafusion_err!("{e:?}"))
url.map_err(|_| Self::invalid_url_err(value))
.map(|url| match part {
"HOST" => url.host_str().map(String::from),
"PATH" => {
Expand Down Expand Up @@ -137,6 +135,13 @@ impl ParseUrl {
})
}

/// The same error for every URL that can't be parsed
fn invalid_url_err(value: &str) -> DataFusionError {
exec_datafusion_err!(
"The url is invalid: {value}. Use `try_parse_url` to tolerate invalid URL and return NULL instead. SQLSTATE: 22P02"
)
}

fn path<'a>(value: &str, url: &'a Url) -> &'a str {
let path = url.path();
if path == "/" && Self::absolute_url_has_empty_path(value) {
Expand Down
10 changes: 9 additions & 1 deletion crates/sqllogictest/test_files/url/parse_url.slt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ SELECT parse_url('inva lid://spark.apache.org/path?query=1', 'QUERY');
----
NULL

query T
SELECT parse_url('http://[::1/path', 'HOST');
Comment thread
TechWizard9999 marked this conversation as resolved.
Resolved
----
NULL

# xfail spark: Spark does not range-check the port, so it returns the host for a port above 65535 (https://github.com/apache/datafusion-spark/issues/365)
query T
SELECT parse_url('http://spark.apache.org:99999/path', 'HOST');
Expand All @@ -361,8 +366,11 @@ set datafusion.execution.enable_ansi_mode = true;
query error DataFusion error: Execution error: The url is invalid: inva lid://spark\.apache\.org/path\?query=1\. Use `try_parse_url` to tolerate invalid URL and return NULL instead\. SQLSTATE: 22P02
SELECT parse_url('inva lid://spark.apache.org/path?query=1', 'QUERY');

query error DataFusion error: Execution error: The url is invalid: http://\[::1/path\. Use `try_parse_url` to tolerate invalid URL and return NULL instead\. SQLSTATE: 22P02
SELECT parse_url('http://[::1/path', 'HOST');

# xfail spark: Spark does not range-check the port, so it returns the host for a port above 65535 (https://github.com/apache/datafusion-spark/issues/365)
query error DataFusion error: Execution error: InvalidPort
query error DataFusion error: Execution error: The url is invalid: http://spark\.apache\.org:99999/path\. Use `try_parse_url` to tolerate invalid URL and return NULL instead\. SQLSTATE: 22P02
SELECT parse_url('http://spark.apache.org:99999/path', 'HOST');

Comment thread
TechWizard9999 marked this conversation as resolved.
Resolved
# try_parse_url still returns NULL when ANSI mode is on
Expand Down
Loading