OData Query Parser and Transpiler
raw JSON → 0.10.0 verified Mon Apr 27 auth: no python
A Python library for parsing OData query strings and transpiling them into SQL or other formats. Current version 0.10.0, supports Python 3.7+ (<4.0). Releases are infrequent with minor updates.
pip install odata-query Common errors
error ModuleNotFoundError: No module named 'odata_query' ↓
cause Library not installed or installed in a different environment.
fix
Run 'pip install odata-query' in your active environment.
error SyntaxError: Unexpected token '&' in query string ↓
cause The OData parser expects individual query options, not URL-encoded string with '&'? May need to split on '&' first.
fix
Pass each option separately: ODataQuery("$filter=Name eq 'Alice'") and handle multiple options via the parser's built-in URL handling? Actually ODataQuery class takes a full query string with '&'. This error arises if using invalid OData syntax. Ensure the query is well-formed.
error NotImplementedError: Transpilation of 'contains' not implemented ↓
cause The SqlTranspiler does not support all OData functions.
fix
Use only supported functions (eq, ne, gt, ge, lt, le, and, or, not, substringof, startswith, endswith). For custom functions, extend the transpiler.
error AttributeError: 'ODataQuery' object has no attribute 'filter_ast' ↓
cause Older versions of the library may not have the '_ast' attributes. Version <0.9?
fix
Use the .filter attribute or upgrade to >=0.9.0.
Warnings
gotcha The library does not build full SQL queries; SqlTranspiler only handles filter, orderby, top, skip. You must provide the SELECT and FROM parts manually. ↓
fix Use SqlTranspiler to get WHERE, ORDER BY, LIMIT clauses, then combine with your own SELECT/FROM.
gotcha ODataQuery object attributes (filter, top, etc.) are strings, not structured objects. Do not expect them to be parsed into AST nodes by default. ↓
fix To access the parsed AST, use query.filter_ast, query.top_ast, etc. (attributes ending with _ast).
deprecated The 'SqlTranspiler' class is experimental and may change in future versions. ↓
fix Check the documentation for the latest transpilation API. Consider contributing to the library if you rely on this.
gotcha Filter expression strings may use OData functions like 'contains', 'startswith', but SqlTranspiler may not support all of them. Unsupported functions cause a NotImplementedError. ↓
fix Sanitize or limit OData query functionality before transpiling. Check the source for supported functions.
Imports
- ODataQuery wrong
from odata_query import OdataQuerycorrectfrom odata_query import ODataQuery - ast
from odata_query import ast
Quickstart
from odata_query import ODataQuery
# Parse an OData query string
query = ODataQuery("$filter=Name eq 'Alice'&$top=10&$orderby=Age desc")
# Access parsed components
print(query.filter) # Name eq 'Alice'
print(query.top) # 10
print(query.orderby) # Age desc
# Transpile to SQL (experimental)
from odata_query.transpiler import SqlTranspiler
transpiler = SqlTranspiler()
sql = transpiler.transpile(query)
print(sql)
# Output: SELECT * FROM table WHERE Name = 'Alice' ORDER BY Age DESC LIMIT 10