sql.q
Examples
.sql.render (`sql;`t;enlist (`$">";`price;100);0b;`sym`p!(`sym;`max`price);5;enlist (`$">:";`p)).sql.parse "SELECT sym, max(price) AS p FROM t WHERE price > 100 GROUP BY sym ORDER BY p DESC LIMIT 5"Entity Summary
| Entities | Short Description |
|---|---|
| .sql.parse[sql] | The tree of one SELECT, read by DuckDB's own parser, so it needs DuckDB: a tool for building and checking trees. |
| .sql.render[tree] | The SQL text of a tree, written by q alone. |
| .sql.translate[tree;schema] | The where terms, limit and order of a select that DuckDB can run as SQL, the rest left to q: (sqlTree;params;schema; qTree) for a ? |
Entity Details
.sql.parse[sql]
The tree of one SELECT, read by DuckDB's own parser, so it needs DuckDB: a tool for building and checking trees. What the tree cannot carry signals 'sql: unsupported, naming it (a join, a subquery, HAVING, a window, UNION ...); text DuckDB cannot parse signals 'sql: with DuckDB's reason.
| Parameters: |
|
|---|
Examples
.sql.parse "SELECT a FROM t WHERE a > 1 AND b IS NULL".sql.render[tree]
The SQL text of a tree, written by q alone. A tree the grammar above cannot read signals 'type, a head with the wrong number of arguments 'length.
| Return: | string |
|---|
.sql.translate[tree;schema]
The where terms, limit and order of a select that DuckDB can run as SQL, the rest left to q: (sqlTree;params;schema; qTree) for a ? parse tree over a table whose schema is the envelope .duckdb.getx answers (col dtype logical iskey), the row index i among its rows on a table that stores one. sqlTree selects the columns qTree names (every column when its select phrase is empty; i never, unless the phrase names it bare, then as i AS its q output name) under the moved terms, params binds their literals as $pq1 $pq2 ... in the order the SQL text reads them, schema is the rows of those columns, and qTree is the tree minus what moved with the table name still at slot 1, :: when nothing is left for q. A select phrase of aggregates, or a by, whose every item .sql.i.whole can spell moves whole with the entire where: sqlTree groups, its schema rows carry q's result types with the by keys marked, and qTree is ::. Five stages, each reading the tree and what the stages before it decided: the select phrase (.sql.i.phrase), the where (.sql.i.where), the whole push of aggregates and by (.sql.i.whole), the limit and order (.sql.i.limit) and the assembly of the answer (.sql.i.assemble), which numbers the literals each stage left as placeholders (.sql.i.number).
Examples
.sql.translate[parse "select max price by sym from t where qty>100, f price";
([] col:`sym`qty`price; dtype:("VARCHAR";"BIGINT";"DOUBLE"); logical:`symbol`int64`float64; iskey:000b)]