yobx.sql — parse module#
SQL parser — translates a SQL string into a structured list of
SqlOperation objects.
The parser handles a small but useful subset of SQL:
SELECT— column selection and simple arithmetic expressions (+,-,*,/).WHERE/ filter — comparison predicates (=,<,>,<=,>=,<>/!=) combined withAND/OR.GROUP BY— column grouping (aggregationsSUM,COUNT,AVG,MIN,MAXin theSELECTlist are recognised).JOIN—[INNER] JOIN … ON col1 = col2linking two input tables.Subqueries in the
FROMclause:SELECT … FROM (SELECT … FROM table) [AS alias].
All names are case-insensitive; they are normalised to lower-case by
parse_sql().
- class yobx.sql.parse.AggExpr(func: str, arg: object)[source]#
An aggregation expression:
SUM(col),COUNT(*), etc.
- class yobx.sql.parse.BinaryExpr(left: object, op: str, right: object)[source]#
A binary expression:
left op right.
- class yobx.sql.parse.ColumnRef(column: str, table: str | None = None)[source]#
A bare column reference, optionally qualified:
table.column.
- class yobx.sql.parse.Condition(left: object, op: str, right: object)[source]#
A WHERE predicate, either a leaf comparison or a compound AND / OR.
- class yobx.sql.parse.FilterOp(condition: Condition = <factory>)[source]#
Represents a
WHEREclause.- Parameters:
condition – the parsed predicate tree.
- class yobx.sql.parse.FuncCallExpr(func: str, args: List[object])[source]#
A call to a user-defined (custom) function:
func_name(arg1, arg2, …).
- class yobx.sql.parse.GroupByOp(columns: List[str] = <factory>)[source]#
Represents a
GROUP BYclause.- Parameters:
columns – the column names to group by.
- class yobx.sql.parse.JoinOp(right_table: str = '', left_key: str = '', right_key: str = '', join_type: str = 'inner')[source]#
Represents a
JOINclause.- Parameters:
right_table – the name of the right-hand table being joined.
left_key – column name from the left table used in the equi-join.
right_key – column name from the right table used in the equi-join.
join_type –
'inner'(default),'left','right', or'full'.
- class yobx.sql.parse.Literal(value: object)[source]#
A scalar literal value (number or quoted string).
- class yobx.sql.parse.ParsedQuery(operations: List[SqlOperation] = <factory>, from_table: str = '', columns: List[str] = <factory>, subquery: ParsedQuery | None = None)[source]#
The result of
parse_sql().- Parameters:
operations – ordered list of
SqlOperationobjects derived from the SQL string. The order reflects the logical execution sequence:JoinOp(if any) →FilterOp(if any) →GroupByOp(if any) →SelectOp.from_table – the primary (left) table name from the
FROMclause, or the alias of the subquery whensubqueryis set.columns – all column names referenced in the query, in the order they appear (deduped).
subquery – when the
FROMclause contains a sub-select (FROM (SELECT …)), this holds the parsed inner query; otherwiseNone.
- class yobx.sql.parse.SelectItem(expr: object, alias: str | None = None)[source]#
One item in the SELECT list: an expression with an optional alias.
- class yobx.sql.parse.SelectOp(items: List[SelectItem] = <factory>, distinct: bool = False)[source]#
Represents the
SELECTclause.- Parameters:
items – the list of
SelectItemobjects to compute.distinct –
Truewhen the query containsSELECT DISTINCT.
- class yobx.sql.parse.SqlOperation[source]#
Base class for all SQL operations produced by
parse_sql().
- yobx.sql.parse.parse_sql(query: str) ParsedQuery[source]#
Parse a SQL query string and return a
ParsedQuery.The parser handles:
SELECT [DISTINCT] expr [AS alias], …FROM tableFROM (SELECT …) [AS alias]— subquery in theFROMclause[INNER|LEFT|RIGHT|FULL [OUTER]] JOIN table ON col = colWHERE condition [AND|OR condition] …GROUP BY col, …
Column names in the returned operations are normalised to lower-case.
- Parameters:
query – the SQL query string to parse.
- Returns:
a
ParsedQuerywith anoperationslist and acolumnslist of all referenced column names.
<<<
from yobx.sql.parse import parse_sql pq = parse_sql("SELECT a, b FROM t WHERE a > 0") for op in pq.operations: print(type(op).__name__, op)
>>>
FilterOp FilterOp(condition=Condition(left=ColumnRef(column='a', table=None), op='>', right=Literal(value=0))) SelectOp SelectOp(items=[SelectItem(expr=ColumnRef(column='a', table=None), alias=None), SelectItem(expr=ColumnRef(column='b', table=None), alias=None)], distinct=False)