Skip to content

Commit d7714ac

Browse files
authored
Mn/optimizerfix (#225)
2 parents 5e0732a + 1a447ac commit d7714ac

File tree

13 files changed

+1801
-519
lines changed

13 files changed

+1801
-519
lines changed

docs/docs/javascripts/noql/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/docs/javascripts/playground/playground.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,56 @@ playgroundButton.onclick = function () {
4343
let nodeQuery = '';
4444
let errorMessage = '';
4545

46+
let optimizedPipeline = null;
47+
4648
document.getElementById('playground-error-container').style.display =
4749
'none';
4850
document.getElementById('playground-output-container').style.display =
4951
'none';
5052

5153
let dbDialect = 'postgresql';
52-
if (document.getElementById('dialect-mysql').checked) {
53-
dbDialect = 'mysql';
54-
}
5554
const inputSql = editor.session.getValue();
5655
try {
56+
const unwindJoins = !!document.getElementById('unwind-joins').checked;
57+
const optimizeJoins =
58+
!!document.getElementById('optimize-joins').checked;
59+
5760
if (document.getElementById('force-aggregate').checked) {
5861
noqlOutput = SqlToMongo.makeMongoAggregate(inputSql, {
5962
database: dbDialect,
63+
unwindJoins: unwindJoins,
64+
optimizeJoins: optimizeJoins,
6065
});
6166
} else {
62-
noqlOutput = SqlToMongo.parseSQL(inputSql, {database: dbDialect});
67+
noqlOutput = SqlToMongo.parseSQL(inputSql, {
68+
database: dbDialect,
69+
unwindJoins: unwindJoins,
70+
optimizeJoins: optimizeJoins,
71+
});
6372
}
73+
74+
if (noqlOutput.type === 'aggregate' && noqlOutput.pipeline) {
75+
optimizedPipeline = SqlToMongo.optimizeMongoAggregate(
76+
noqlOutput.pipeline,
77+
{}
78+
);
79+
}
80+
6481
shellQuery = constructShellQuery(noqlOutput);
6582
nodeQuery = constructNodeQuery(noqlOutput);
83+
6684
document.getElementById('playground-noql-result').textContent =
6785
JSON.stringify(noqlOutput, null, 4);
6886
document.getElementById('playground-mongo-result').textContent =
6987
shellQuery;
7088
document.getElementById('playground-node-result').textContent =
7189
nodeQuery;
90+
document.getElementById(
91+
'playground-noql-optimized-result'
92+
).textContent = optimizedPipeline
93+
? JSON.stringify(optimizedPipeline, null, 4)
94+
: '';
95+
7296
document.getElementById('playground-output-container').style.display =
7397
'block';
7498
document.getElementById('playground-output-container').scrollIntoView({

docs/docs/overrides/home.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,10 @@ <h2>
300300
src="assets/sql-database-generic-svgrepo-com.svg"
301301
width="36px"
302302
/>
303-
mySQL or Postgres
303+
Postgres Syntax
304304
</h2>
305305
<p>
306-
Write in mySQL or Postgres dialects. Use the language you already
306+
Use the language you already
307307
know to generate MongoDB queries. You don't need to figure out Mongo
308308
query syntax to use Mongo!
309309
</p>

docs/docs/playground/index.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ Enter a SQL statement below to see NoQL's output, as well as the equivalent Mong
1616

1717
<div>
1818
<div class="admonition example">
19-
<p class="admonition-title">Choose your dialect:
20-
<input type="radio" id="dialect-postgres" name="dialect" value="postgresql" checked="checked">
21-
  <label for="postgres">Postgresql</label>
22-
<input type="radio" id="dialect-mysql" name="dialect" value="mysql">
23-
  <label for="mysql">mySQL</label>
24-
&nbsp;|&nbsp;
25-
<input type="checkbox" id="force-aggregate" name="force-aggregate">
26-
<label for="force-aggregate">Alway create an aggregate</label>
27-
</p>
19+
<div style="display: flex; align-items: center; gap:1rem;margin-bottom: 0.5rem;margin-top:0.5rem">
20+
<div style="display: flex; align-items: center; gap:0.25rem;">
21+
<input type="checkbox" id="force-aggregate" name="force-aggregate">
22+
<label for="force-aggregate">Always create an aggregate</label>
23+
</div>
24+
<div style="display: flex; align-items: center; gap:0.25rem;">
25+
<input type="checkbox" id="unwind-joins" name="unwind-joins">
26+
<label for="unwind-joins">Automatically Unwind Joins</label>
27+
</div>
28+
<div style="display: flex; align-items: center; gap:0.25rem;">
29+
<input type="checkbox" id="optimize-joins" name="unwind-joins">
30+
<label for="optimize-joins">Optimize Joins</label>
31+
</div>
32+
</div>
2833
<div class="playground-code-input" id="playground-sql-input">SELECT * FROM rockets</div>
2934
</div>
3035
<button class="md-button md-button--primary" id="submit-sql">Convert</button>
@@ -42,10 +47,12 @@ Enter a SQL statement below to see NoQL's output, as well as the equivalent Mong
4247
<input checked="checked" id="mongo-shell-output" name="__tabbed_5" type="radio">
4348
<input id="node-code-output" name="__tabbed_5" type="radio">
4449
<input id="noql-output" name="__tabbed_5" type="radio">
50+
<input id="noql-optimized-output" name="__tabbed_5" type="radio">
4551
<div class="tabbed-labels tabbed-labels--linked">
4652
<label for="mongo-shell-output">Mongo Shell Query</label>
4753
<label for="node-code-output">Node.js Code</label>
4854
<label for="noql-output">NoQL Output</label>
55+
<label for="noql-optimized-output">Optimized Pipeline</label>
4956
</div>
5057
<div class="tabbed-content">
5158
<div class="tabbed-block">
@@ -63,6 +70,11 @@ Enter a SQL statement below to see NoQL's output, as well as the equivalent Mong
6370
<pre><code class="playground-code-output language-javascript hljs" id="playground-noql-result"></code></pre>
6471
</div>
6572
</div>
73+
<div class="tabbed-block">
74+
<div class="language-javascript highlight">
75+
<pre><code class="playground-code-output language-javascript hljs" id="playground-noql-optimized-result"></code></pre>
76+
</div>
77+
</div>
6678
</div>
6779
<div class="tabbed-control tabbed-control--prev" hidden=""><button class="tabbed-button" tabindex="-1"
6880
aria-hidden="true"></button></div>

docs/docs/sql-syntax/aggregates.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Returns the distinct count of a specific field in the given group.
115115

116116
```sql
117117
SELECT
118-
COUNT(DISTINCT `Address.Town`) AS countVal
118+
COUNT(DISTINCT "Address.Town") AS countVal
119119
,`Address.City` AS City
120120
FROM
121121
`customers`

docs/docs/sql-syntax/arrays.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Using '$$ROOT' in sub select promotes the field to the root value of the array
2121

2222
```sql
2323
SELECT
24-
(SELECT filmId AS '$$ROOT' FROM Rentals WHERE staffId=2) AS t
24+
(SELECT filmId AS `$$ROOT` FROM Rentals WHERE staffId=2) AS t
2525
FROM
2626
`customers`
2727
```
@@ -103,10 +103,24 @@ Returns true when all elements in the array are true.
103103

104104
```sql
105105
SELECT id,
106-
(CASE WHEN ALL_ELEMENTS_TRUE(Rentals) THEN Yes ELSE ‘No’ END) AS test
106+
(CASE WHEN ALL_ELEMENTS_TRUE(Rentals) THEN 'Yes' ELSE 'No' END) AS test
107107
FROM `customers`;
108108
```
109109

110+
### JOIN
111+
112+
`JOIN(array expr, delimiter)`
113+
114+
Joins all values into a string.
115+
116+
???+ example "Example `JOIN` usage"
117+
118+
```sql
119+
SELECT id,
120+
JOIN((SELECT Name FROM Rentals),',') AS RentalNames
121+
FROM customers;
122+
```
123+
110124
### ANY_ELEMENT_TRUE
111125

112126
`ANY_ELEMENT_TRUE(array expr)`
@@ -117,7 +131,7 @@ Returns true when any element in the array is true.
117131

118132
```sql
119133
SELECT id,
120-
(CASE WHEN ANY_ELEMENT_TRUE(Rentals) THEN Yes ELSE ‘No’ END) AS test
134+
(CASE WHEN ANY_ELEMENT_TRUE(Rentals) THEN 'Yes' ELSE 'No' END) AS test
121135
FROM `customers`;
122136
```
123137

@@ -173,7 +187,7 @@ Concatenate the provided list of arrays.
173187

174188
```sql
175189
SELECT id,
176-
CONCAT_ARRAYS((SELECT `Film Title` AS $$ROOT FROM `Rentals`), ARRAY_RANGE(0,10,2)) AS test
190+
CONCAT_ARRAYS((SELECT `Film Title` AS `$$ROOT` FROM `Rentals`), ARRAY_RANGE(0,10,2)) AS test
177191
FROM `customers`;
178192
```
179193

@@ -201,7 +215,7 @@ Returns the index of the value in the array.
201215

202216
```sql
203217
SELECT id,
204-
INDEXOF_ARRAY((SELECT `Film Title` AS $$ROOT FROM `Rentals`),5) AS test
218+
INDEXOF_ARRAY((SELECT `Film Title` AS `$$ROOT` FROM `Rentals`),5) AS test
205219
FROM `customers`;
206220
```
207221

@@ -215,7 +229,7 @@ Returns true when the field is an array.
215229

216230
```sql
217231
SELECT id,
218-
(CASE WHEN IS_ARRAY(Rentals) THEN Yes ELSE ‘No’ END) AS test
232+
(CASE WHEN IS_ARRAY(Rentals) THEN 'Yes' ELSE 'No' END) AS test
219233
FROM `customers`;
220234
```
221235

@@ -271,7 +285,7 @@ Returns an array as the difference of the provided arrays.
271285

272286
```sql
273287
SELECT id,
274-
SET_DIFFERENCE((SELECT `Film Title` AS $$ROOT FROM `Rentals`), PARSE_JSON([ 1,2,3,4])) AS test
288+
SET_DIFFERENCE((SELECT `Film Title` AS `$$ROOT` FROM `Rentals`), PARSE_JSON('[ 1,2,3,4]')) AS test
275289
FROM `customers`;
276290
```
277291

@@ -285,7 +299,7 @@ Returns true or false if the arrays are equal.
285299

286300
```sql
287301
SELECT id,
288-
SET_EQUALS((SELECT `Film Title` AS $$ROOT FROM `Rentals`), PARSE_JSON([ 1,2,3,4])) AS test
302+
SET_EQUALS((SELECT `Film Title` AS `$$ROOT` FROM `Rentals`), PARSE_JSON('[ 1,2,3,4]')) AS test
289303
FROM `customers`;
290304
```
291305

@@ -299,7 +313,7 @@ Returns an array as the difference of the provided arrays.
299313

300314
```sql
301315
SELECT id,
302-
SET_INTERSECTION((SELECT filmId AS $$ROOT FROM `Rentals`), PARSE_JSON([ 1,2,3,4])) AS
316+
SET_INTERSECTION((SELECT filmId AS `$$ROOT` FROM `Rentals`), PARSE_JSON('[ 1,2,3,4]')) AS
303317
test FROM `customers`;
304318
```
305319

@@ -313,7 +327,7 @@ Returns whether an array is a subset of another.
313327

314328
```sql
315329
SELECT id,
316-
SET_IS_SUBSET((SELECT filmId AS $$ROOT FROM `Rentals`), PARSE_JSON(‘[ 1,2,3,4] ‘)) AS test
330+
SET_IS_SUBSET((SELECT filmId AS `$$ROOT` FROM `Rentals`), PARSE_JSON(‘[ 1,2,3,4] ‘)) AS test
317331
FROM `customers`;
318332
```
319333

@@ -327,7 +341,7 @@ Returns an array as the union of the provided arrays.
327341

328342
```sql
329343
SELECT id,
330-
SET_UNION((SELECT filmId AS $$ROOT FROM `Rentals`), PARSE_JSON(‘[ 1,2,3,4] ‘)) AS test
344+
SET_UNION((SELECT filmId AS `$$ROOT` FROM `Rentals`), PARSE_JSON(‘[ 1,2,3,4] ‘)) AS test
331345
FROM `customers`;
332346
```
333347

@@ -355,7 +369,7 @@ Sums the values in an array given an array field or sub-select and the field to
355369

356370
```sql
357371
SELECT
358-
SUM_ARRAY(`Rentals`, staffId) AS totalStaffIds
372+
SUM_ARRAY(`Rentals`, 'staffId') AS totalStaffIds
359373
FROM `customers`;
360374
```
361375

@@ -382,7 +396,7 @@ Averages the values in an array given an array field or sub-select and the field
382396

383397
```sql
384398
SELECT
385-
AVG_ARRAY(`Rentals`, staffId) AS avgStaffIds
399+
AVG_ARRAY(`Rentals`, 'staffId') AS avgStaffIds
386400
FROM `customers`;
387401
```
388402

@@ -397,6 +411,6 @@ Transposes an array of input arrays so that the first element of the output arra
397411
```sql
398412
SELECT id,
399413
ZIP_ARRAY(
400-
(SELECT `Film Title` AS $$ROOT FROM `Rentals`),ARRAY_RANGE(0,10,2)) AS test
414+
(SELECT `Film Title` AS `$$ROOT` FROM `Rentals`),ARRAY_RANGE(0,10,2)) AS test
401415
FROM `customers`;
402416
```

docs/docs/sql-syntax/objects.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,28 @@ Creates an empty object.
114114
END AS addressStatus
115115
FROM `customers`;
116116
```
117+
118+
[//]: # (todo add back when flatten implemented)
119+
[//]: # (### FLATTEN)
120+
121+
[//]: # ()
122+
[//]: # (`FLATTEN&#40;field, prefix&#41;`)
123+
124+
[//]: # ()
125+
[//]: # (Flattens an object into a set of fields.)
126+
127+
[//]: # ()
128+
[//]: # (???+ example "Example `FLATTEN` usage")
129+
130+
[//]: # ()
131+
[//]: # ( ```sql)
132+
133+
[//]: # ( SELECT)
134+
135+
[//]: # ( id,)
136+
137+
[//]: # ( FLATTEN&#40;`address`,'addr_'&#41;)
138+
139+
[//]: # ( FROM `customers`;)
140+
141+
[//]: # ( ```)

0 commit comments

Comments
 (0)