apache / datafusion-sqlparser-rs

Extensible SQL Lexer and Parser for Rust
Apache License 2.0
2.8k stars 543 forks source link

Add support for MSSQL's `XQuery` methods #1500

Closed gaoqiangz closed 1 week ago

gaoqiangz commented 2 weeks ago

Supports XQuery methods:

SELECT STUFF((SELECT ',' + name FROM sys.objects FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS T
SELECT CAST(column AS XML).value('.', 'NVARCHAR(MAX)') AS T

See https://learn.microsoft.com/en-us/sql/t-sql/xml/xml-data-type-methods?view=sql-server-ver16.

gaoqiangz commented 2 weeks ago

I redesigned CompositeFunction, please take a look.

gaoqiangz commented 2 weeks ago

I added supports_methods to the Dialect trait and try_parse_method to the Parser, this might be better suited for different scenarios and doesn't break existing behavior.

gaoqiangz commented 1 week ago

The code has been rewritten for the new Method definition, seems more reasonable:

pub struct Method {
    pub expr: Box<Expr>,
    // always non-empty
    pub method_chain: Vec<Function>,
}

Any other comments?

iffyio commented 1 week ago

@gaoqiangz could you take a look at the doc building failure when you get the chance?

gaoqiangz commented 1 week ago

@iffyio CI was fixed.

alamb commented 1 week ago

🚀

alamb commented 1 week ago

Thanks again @gaoqiangz and @lovasoa and @iffyio

yoavcloud commented 2 days ago

@gaoqiangz I've been working on a similar addition, it's great to see you beat me to it :-) One thing I added in my (unpublished) work is data-type specific methods. For example: SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STAsText() FROM customers

How would you support the geography data type here? In my code I added it as a namespace field. I'm trying to avoid expanding ObjectName. Do you think that can work in your code too?