wseaton / sqloxide

Python bindings for sqlparser-rs
MIT License
168 stars 20 forks source link

To which of the subqueries the "ORDER BY" relates #250

Closed niryarden closed 1 year ago

niryarden commented 1 year ago

When I run parse_sql on this query:

SELECT ProductID, Name  
FROM Product  
WHERE ProductID NOT IN (3, 4)  
UNION  
SELECT ProductModelID, Name  
FROM ProductModel   
ORDER BY Name;

I receive the JSON (removed irrelevant parts):

"Query": {
    "body": { "SetOperation": { "left": {...}, "right": {...} } },
    "order_by": {...}
}

I find this structure confusing. The ORDER BY in the query relates to the Name column in the second subquery (right), not to the whole unionized query. so why would the "order_by" field in the JSON be outside of the separation between them? How can I tell to which one of the two it relates? (And inside the "right" section there's no data about the ORDER BY clause.)

wseaton commented 1 year ago

Hi @niryarden, the format of the AST is not API stable and is something we inherit directly from upstream (sqlparser-rs). If you have questions about the output I would go ahead and open an issue there.