-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sql cells * registerTable * prettier * destructuring assignment * register sql files * sql → @observablehq/duckdb * incremental sql update * test sql + data loader * docs; table display * more docs; better display * echo * fix tests, again * remove console * id="[{min, max}]" * more docs
- Loading branch information
Showing
26 changed files
with
431 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Display race | ||
|
||
```js echo | ||
async function sleep(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
``` | ||
|
||
```js echo | ||
const value = (function* () { | ||
yield 2000; | ||
yield 1000; | ||
})(); | ||
``` | ||
|
||
```js echo | ||
await sleep(value); | ||
display(value); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
--- | ||
sql: | ||
gaia: ./lib/gaia-sample.parquet | ||
--- | ||
|
||
# SQL | ||
|
||
Observable Framework includes built-in support for client-side SQL powered by [DuckDB](./lib/duckdb). You can use SQL to query data from [CSV](./lib/csv), [TSV](./lib/csv), [JSON](./javascript/files#json), [Apache Arrow](./lib/arrow), and [Apache Parquet](./lib/arrow#apache-parquet) files, which can either be static or generated by [data loaders](./loaders). | ||
|
||
To use SQL, first register the desired tables in the page’s [front matter](./markdown#front-matter) using the **sql** option. Each key is a table name, and each value is the path to the corresponding data file. For example, to register a table named `gaia` from a Parquet file: | ||
|
||
```yaml | ||
--- | ||
sql: | ||
gaia: ./lib/gaia-sample.parquet | ||
--- | ||
``` | ||
|
||
## SQL code blocks | ||
|
||
To run SQL queries, create a SQL fenced code block (<code>```sql</code>). For example, to query the first 10 rows from the `gaia` table: | ||
|
||
````md | ||
```sql | ||
SELECT * FROM gaia ORDER BY phot_g_mean_mag LIMIT 10 | ||
``` | ||
```` | ||
|
||
This produces a table: | ||
|
||
```sql | ||
SELECT * FROM gaia ORDER BY phot_g_mean_mag LIMIT 10 | ||
``` | ||
|
||
To refer to the results of a query in JavaScript, use the `id` directive. For example, to refer to the results of the previous query as `top10`: | ||
|
||
````md | ||
```sql id=top10 | ||
SELECT * FROM gaia ORDER BY phot_g_mean_mag LIMIT 10 | ||
``` | ||
```` | ||
|
||
```sql id=top10 | ||
SELECT * FROM gaia ORDER BY phot_g_mean_mag LIMIT 10 | ||
``` | ||
|
||
This returns an array of 10 rows, inspected here: | ||
|
||
```js echo | ||
top10 | ||
``` | ||
|
||
When a SQL code block uses the `id` directive, the results are not displayed by default. You can display them by adding the `display` directive, which produces the table shown above. | ||
|
||
````md | ||
```sql id=top10 display | ||
SELECT * FROM gaia ORDER BY phot_g_mean_mag LIMIT 10 | ||
``` | ||
```` | ||
|
||
The `id` directive is often a simple identifier such as `top10` above, but it supports [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment), so you can refer to individual rows and columns using array and object patterns. For example, to pull out the top row: | ||
|
||
````md | ||
```sql id=[top] | ||
SELECT * FROM gaia ORDER BY phot_g_mean_mag LIMIT 1 | ||
``` | ||
```` | ||
|
||
```sql id=[top] | ||
SELECT * FROM gaia ORDER BY phot_g_mean_mag LIMIT 1 | ||
``` | ||
|
||
```js echo | ||
top | ||
``` | ||
|
||
Or to pull out the minimum value of the `phot_g_mean_mag` column: | ||
|
||
````md | ||
```sql id=[{min}] | ||
SELECT MIN(phot_g_mean_mag) AS min FROM gaia | ||
``` | ||
```` | ||
|
||
```sql id=[{min}] | ||
SELECT MIN(phot_g_mean_mag) AS min FROM gaia | ||
``` | ||
|
||
```js echo | ||
min | ||
``` | ||
|
||
<div class="tip"> | ||
|
||
For complex destructuring patterns, you may need to quote the `id` directive. For example, to pull out the column named `min(phot_g_mean_mag)` to the variable named `min`, say <code style="white-space: nowrap;">id="[{'min(phot_g_mean_mag)': min}]"</code>. Or to pull out the `min` and `max` columns, say <code style="white-space: nowrap;">id="[{min, max}]"</code>. | ||
|
||
</div> | ||
|
||
For dynamic or interactive queries that respond to user input, you can interpolate values into SQL queries using inline expressions `${…}`. For example, to show the stars around a given brightness: | ||
|
||
```js echo | ||
const mag = view(Inputs.range([6, 20], {label: "Magnitude"})); | ||
``` | ||
|
||
```sql echo | ||
SELECT * FROM gaia WHERE phot_g_mean_mag BETWEEN ${mag - 0.1} AND ${mag + 0.1}; | ||
``` | ||
|
||
The value of a SQL code block is an [Apache Arrow](./lib/arrow) table. This format is supported by [Observable Plot](./lib/plot), so you can use SQL and Plot together to visualize data. For example, below we count the number of stars in each 2°×2° bin of the sky (where `ra` is [right ascension](https://en.wikipedia.org/wiki/Right_ascension) and `dec` is [declination](https://en.wikipedia.org/wiki/Declination), representing a point on the celestial sphere in the equatorial coordinate system), and then visualize the resulting heatmap using a [raster mark](https://observablehq.com/plot/marks/raster). | ||
|
||
```sql id=bins echo | ||
SELECT | ||
floor(ra / 2) * 2 + 1 AS ra, | ||
floor(dec / 2) * 2 + 1 AS dec, | ||
count() AS count | ||
FROM | ||
gaia | ||
GROUP BY | ||
1, | ||
2 | ||
``` | ||
|
||
```js echo | ||
Plot.plot({ | ||
aspectRatio: 1, | ||
x: {domain: [0, 360]}, | ||
y: {domain: [-90, 90]}, | ||
marks: [ | ||
Plot.frame({fill: 0}), | ||
Plot.raster(bins, { | ||
x: "ra", | ||
y: "dec", | ||
fill: "count", | ||
width: 360 / 2, | ||
height: 180 / 2, | ||
imageRendering: "pixelated" | ||
}) | ||
] | ||
}) | ||
``` | ||
|
||
## SQL literals | ||
|
||
SQL fenced code blocks are shorthand for the `sql` tagged template literal. You can invoke the `sql` tagged template literal directly like so: | ||
|
||
```js echo | ||
const rows = await sql`SELECT random() AS random`; | ||
``` | ||
|
||
```js echo | ||
rows[0].random | ||
``` | ||
|
||
The `sql` tagged template literal is available by default in Markdown, but you can also import it explicitly as: | ||
|
||
```js echo | ||
import {sql} from "npm:@observablehq/duckdb"; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.