Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,8 @@ impl Dialect for GenericDialect {
fn supports_select_item_multi_column_alias(&self) -> bool {
true
}

fn supports_xml_expressions(&self) -> bool {
true
}
}
11 changes: 11 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,17 @@ pub trait Dialect: Debug + Any {
fn supports_select_item_multi_column_alias(&self) -> bool {
false
}

/// Returns true if the dialect supports XML-related expressions
/// such as `xml '<foo/>'` typed strings, XML functions like
/// `XMLCONCAT`, `XMLELEMENT`, etc.
///
/// When this returns false, `xml` is treated as a regular identifier.
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/functions-xml.html)
fn supports_xml_expressions(&self) -> bool {
false
}
}

/// Operators for which precedence must be defined.
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,8 @@ impl Dialect for PostgreSqlDialect {
fn supports_comma_separated_trim(&self) -> bool {
true
}

fn supports_xml_expressions(&self) -> bool {
true
}
}
26 changes: 25 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,16 @@ impl<'a> Parser<'a> {
}
}

/// Returns true if the given [ObjectName] is a single unquoted
/// identifier matching `expected` (case-insensitive).
fn is_simple_unquoted_object_name(name: &ObjectName, expected: &str) -> bool {
if let [ObjectNamePart::Identifier(ident)] = name.0.as_slice() {
ident.quote_style.is_none() && ident.value.eq_ignore_ascii_case(expected)
} else {
false
}
}

/// Parse an expression prefix.
pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
// allow the dialect to override prefix parsing
Expand Down Expand Up @@ -1720,7 +1730,21 @@ impl<'a> Parser<'a> {
// so given `NOT 'a' LIKE 'b'`, we'd accept `NOT` as a possible custom data type
// name, resulting in `NOT 'a'` being recognized as a `TypedString` instead of
// an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
// `type 'string'` syntax for the custom data types at all.
// `type 'string'` syntax for the custom data types at all ...
//
// ... with the exception of `xml '...'` on dialects that support XML
// expressions, which is a valid PostgreSQL typed string literal.
DataType::Custom(ref name, ref modifiers)
if modifiers.is_empty()
&& Self::is_simple_unquoted_object_name(name, "xml")
&& parser.dialect.supports_xml_expressions() =>
{
Ok(Expr::TypedString(TypedString {
data_type: DataType::Custom(name.clone(), modifiers.clone()),
value: parser.parse_value()?,
uses_odbc_syntax: false,
}))
}
DataType::Custom(..) => parser_err!("dummy", loc),
// MySQL supports using the `BINARY` keyword as a cast to binary type.
DataType::Binary(..) if self.dialect.supports_binary_kw_as_cast() => {
Expand Down
8 changes: 8 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18771,3 +18771,11 @@ fn parse_select_item_multi_column_alias() {
.is_err()
);
}

#[test]
fn parse_non_pg_dialects_keep_xml_names_as_regular_identifiers() {
// On dialects that do NOT support XML expressions, bare `xml` should
// be treated as a regular column identifier, not a typed-string prefix.
let dialects = all_dialects_except(|d| d.supports_xml_expressions());
dialects.verified_only_select("SELECT xml FROM t");
}
19 changes: 19 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3750,6 +3750,25 @@ fn parse_on_commit() {
pg_and_generic().verified_stmt("CREATE TEMPORARY TABLE table (COL INT) ON COMMIT DROP");
}

#[test]
fn parse_xml_typed_string() {
// xml '...' should parse as a TypedString on PostgreSQL and Generic
let sql = "SELECT xml '<foo/>'";
let select = pg_and_generic().verified_only_select(sql);
match expr_from_projection(&select.projection[0]) {
Expr::TypedString(TypedString {
data_type: DataType::Custom(name, modifiers),
value,
uses_odbc_syntax: false,
}) => {
assert_eq!(name.to_string(), "xml");
assert!(modifiers.is_empty());
assert_eq!(value.value, Value::SingleQuotedString("<foo/>".to_string()));
}
other => panic!("Expected TypedString, got: {other:?}"),
}
}

fn pg() -> TestedDialects {
TestedDialects::new(vec![Box::new(PostgreSqlDialect {})])
}
Expand Down
Loading