Skip to content

Commit 79810da

Browse files
vogievetskyclaude
andauthored
Support pipe SQL syntax (#121)
* Support pipe SQL syntax Add FROM queries (SqlFromQuery) and pipe queries (SqlPipesQuery) with the SELECT, WHERE, AGGREGATE, ORDER BY, LIMIT, EXTEND, SET and DROP pipe operators. The inherited ORDER BY / LIMIT / OFFSET methods of a pipes query work on its trailing |> ORDER BY and |> LIMIT operators. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add SqlPipesQuery#unpipe Convert a pipe query into an equivalent SqlQuery without pipes, folding the operators into one SELECT where the SQL evaluation order allows it and nesting sub queries where it does not. Clauses that carry over keep their casing and spacing. Also ignore /resources/ and fix a VALUES test whose EXPR$0 had been mangled by shell expansion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add API docs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add failing tests * fix small issues * fix small issues * Fix npm audit issues and install-script warnings Update vulnerable transitive deps, override js-yaml to v4 under @istanbuljs/load-nyc-config to drop sprintf-js, deny unneeded install scripts, and remove type assertions newly flagged by typescript-eslint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d723a11 commit 79810da

64 files changed

Lines changed: 8206 additions & 1190 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/pipe-sql-syntax.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'druid-query-toolkit': minor
3+
---
4+
5+
Support pipe SQL syntax: `FROM <table>` queries (`SqlFromQuery`) and pipe queries (`SqlPipesQuery`) with the `SELECT`, `WHERE`, `AGGREGATE`, `ORDER BY`, `LIMIT`, `EXTEND`, `SET` and `DROP` pipe operators, and `SqlPipesQuery#unpipe` to convert a pipe query into a query without pipes

‎.gitignore‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99

1010
/node_modules/
1111
/coverage/
12-
/coverage_old/
13-
/build/
1412
/dist/
1513
/types/
1614
/src/sql/parser/index.ts
1715
/src/sql/parser/.DS_Store
1816
.claude/
17+
/resources/
1918

2019
# TypeScript cache
2120
*.tsbuildinfo

‎README.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ There are a number of use cases for this toolkit and one of the chief use cases
88
Search for uses within [web-console/src](https://github.com/apache/druid/tree/master/web-console/src) for some examples.
99
Specifically the [query view](https://github.com/apache/druid/tree/master/web-console/src/views/workbench-view) uses these tools a lot.
1010

11+
## Documentation
12+
13+
The [docs](docs/README.md) cover the whole API with examples:
14+
15+
- [Getting started](docs/getting-started.md) and [why the text is preserved](docs/text-preservation.md)
16+
- [Parsing](docs/parsing.md), [building SQL](docs/building.md), [expressions](docs/expressions.md) and [walking the tree](docs/traversal.md)
17+
- [Queries](docs/queries.md), [SELECT queries](docs/select-queries.md) and [pipe syntax](docs/pipe-syntax.md)
18+
- Use cases: [editing queries from UI actions](docs/use-case-ui-edits.md), [validating user input](docs/use-case-validation.md) and [rewriting queries](docs/use-case-rewriting.md)
19+
- [API reference](docs/api.md)
20+
1121
## Parts
1222

1323
At a high level there are 4 parts to this toolkit:
@@ -75,6 +85,36 @@ ORDER BY 5 DESC
7585
*/
7686
```
7787

88+
#### Pipe syntax
89+
90+
Pipe syntax queries (`FROM t |> WHERE ... |> AGGREGATE ...`) parse into a `SqlPipesQuery`, which holds the root query (often a `SqlFromQuery`, a bare `FROM <table>`) and a list of pipe operators: `SqlSelectPipeOperator`, `SqlWherePipeOperator`, `SqlAggregatePipeOperator`, `SqlOrderByPipeOperator`, `SqlLimitPipeOperator`, `SqlExtendPipeOperator`, `SqlSetPipeOperator` and `SqlDropPipeOperator`.
91+
The ORDER BY and LIMIT methods that every query has work on the trailing `|> ORDER BY` and `|> LIMIT` operators.
92+
`unpipe()` converts a pipe query into an equivalent `SqlQuery` without pipes, folding the operators into one `SELECT` where it can and nesting sub queries where it has to: `From t |> Where x=1` becomes `SELECT * From t Where x=1`.
93+
94+
```javascript
95+
import { SqlColumn, SqlExpression, SqlWherePipeOperator } from 'druid-query-toolkit';
96+
97+
const sql = SqlExpression.parse(`
98+
FROM wikipedia
99+
|> WHERE channel = '#en.wikipedia'
100+
|> AGGREGATE COUNT(*) AS edits GROUP BY cityName
101+
`);
102+
103+
sql
104+
.appendPipeOperator(SqlWherePipeOperator.create(SqlColumn.optionalQuotes('edits').greaterThan(10)))
105+
.changeLimitValue(100)
106+
.toString()
107+
/* →
108+
`
109+
FROM wikipedia
110+
|> WHERE channel = '#en.wikipedia'
111+
|> AGGREGATE COUNT(*) AS edits GROUP BY cityName
112+
|> WHERE edits > 10
113+
|> LIMIT 100
114+
`
115+
*/
116+
```
117+
78118
For more examples, check out the unit tests.
79119

80120
#### ToDo

‎docs/README.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Druid Query Toolkit documentation
2+
3+
Druid Query Toolkit parses [Druid SQL](https://druid.apache.org/docs/latest/querying/sql) into an immutable tree that keeps every space, comment and keyword spelling, so a program can edit a query the way a person would: change one clause and leave the rest as written.
4+
5+
```js
6+
import { C, SqlQuery } from 'druid-query-toolkit';
7+
8+
SqlQuery.parse(`SELECT channel, COUNT(*) AS "Count" FROM wikipedia GROUP BY 1`)
9+
.addWhere(C('channel').equal('#en.wikipedia'))
10+
.changeLimitValue(10)
11+
.toString();
12+
//=> SELECT channel, COUNT(*) AS "Count" FROM wikipedia
13+
// WHERE "channel" = '#en.wikipedia' GROUP BY 1
14+
// LIMIT 10
15+
```
16+
17+
The new `WHERE` goes on its own line, and `GROUP BY 1` stays where it was, right after the gap it had before.
18+
19+
## Table of contents
20+
21+
### Basics
22+
23+
- 🏁 [Getting started](getting-started.md)
24+
- ✍️ [Text preservation](text-preservation.md): why the library keeps the user's text, and how
25+
- 📖 [Parsing](parsing.md): the parse methods, what they return, and errors
26+
- 🧱 [Building SQL](building.md): `C`, `T`, `L`, `F`, the `sql` tag and templates
27+
28+
### Working with the tree
29+
30+
- 🧮 [Expressions](expressions.md): comparisons, `AND`/`OR`, taking filters apart, aliases, placeholders
31+
- 🌳 [Walking the tree](traversal.md): questions about a query, `walk` and friends
32+
- 📜 [Queries](queries.md): what every query has (context, `EXPLAIN`, `INSERT`/`REPLACE`, `ORDER BY`, `LIMIT`, `UNION ALL`)
33+
- 🔎 [SELECT queries](select-queries.md): select list, `FROM` and joins, `WHERE`, `GROUP BY`, `HAVING`, `WITH`
34+
- 🚰 [Pipe syntax](pipe-syntax.md): `FROM t |> WHERE ...` queries and `unpipe()`
35+
- 💅 [Formatting and comparing](formatting.md): `prettify`, capitalization, equality, parentheses, spacing
36+
37+
### Use cases
38+
39+
- 🖱️ [Editing queries from UI actions](use-case-ui-edits.md): the Druid web console's result table and column tree actions
40+
- 🛡️ [Validating user input](use-case-validation.md): checking formulas before they go into generated queries
41+
- 🔁 [Rewriting queries](use-case-rewriting.md): expanding macros into joins, adding mandatory filters
42+
43+
### Beyond SQL
44+
45+
- 📦 [Running queries and results](results.md): `QueryRunner`, `QueryResult`, introspection, filter patterns
46+
47+
### Reference
48+
49+
- 📚 [API reference](api.md)
50+
51+
## More examples
52+
53+
The unit tests (`src/**/*.spec.ts`) cover every method and are a good source of examples. The largest real world user is the [Druid web console](https://github.com/apache/druid/tree/master/web-console/src), especially its [query view](https://github.com/apache/druid/tree/master/web-console/src/views/workbench-view) and [explore view](https://github.com/apache/druid/tree/master/web-console/src/views/explore-view).

0 commit comments

Comments
 (0)