Skip to content

Commit 17433df

Browse files
committed
revised prompt
1 parent 5d7804a commit 17433df

1 file changed

Lines changed: 36 additions & 54 deletions

File tree

src/wetlands_mcp_server/duckdb-prompt.md

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
# DuckDB Query Guide
22

3+
## Query Template
4+
5+
**EVERY query must start with this setup:**
6+
7+
```sql
8+
SET THREADS=100;
9+
SET preserve_insertion_order=false;
10+
SET enable_object_cache=true;
11+
SET temp_directory='/tmp';
12+
INSTALL httpfs; LOAD httpfs;
13+
INSTALL h3 FROM community; LOAD h3;
14+
CREATE OR REPLACE SECRET s3 (TYPE S3, ENDPOINT 'rook-ceph-rgw-nautiluss3.rook', URL_STYLE 'path', USE_SSL 'false', KEY_ID '', SECRET '');
15+
```
16+
17+
**Why these settings?**
18+
19+
- `THREADS=100` - Parallel S3 reads (I/O bound)
20+
- `preserve_insertion_order=false` - Faster aggregation
21+
- `enable_object_cache=true` - Reduces S3 requests
22+
- `httpfs` - Required for S3 access
23+
- `h3` - Required for H3 functions
324

25+
Note: `rook-ceph-rgw-nautiluss3.rook` is an internal endpoint that only your tool running on k8s can access. The publicly accessible external endpoint is `s3-west.nrp-nautilus.io`, which requires `USE_SSL true` and `SET THREADS=2`. Always use the internal endpoint to run queries.
426

527
## H3 Geospatial Indexing
628

7-
**All data uses H3 hexagons** (https://h3geo.org) - uniform hexagonal grid covering Earth
29+
**Most of these data uses H3 hexagons** (https://h3geo.org) - uniform hexagonal grid covering Earth
830

931
### Key Facts:
32+
1033
- Each h8 hexagon = **73.7327598 hectares** (≈ 0.737 km²)
1134
- Always report AREAS, not hex counts
12-
- **Always use** `APPROX_COUNT_DISTINCT(h8)` when counting (avoids double-counting)
35+
- **Use** `APPROX_COUNT_DISTINCT(h8)` when counting hexes to compute areas -- this avoids double-counting and is reasonably fast enough.
1336

1437
### Area Conversion:
1538
```sql
16-
SELECT APPROX_COUNT_DISTINCT(h8) * 73.7327598 as area_hectares FROM ...
1739
SELECT APPROX_COUNT_DISTINCT(h8) * 0.737327598 as area_km2 FROM ...
1840
```
1941

2042
### Joining Different Resolutions:
43+
2144
Some datasets use different H3 resolutions (h8 vs h0-h4). Use `h3_cell_to_parent()` to convert:
2245

2346
```sql
@@ -27,33 +50,20 @@ JOIN read_parquet('s3://public-inat/range-maps/hex/**') pos
2750
AND wetlands.h0 = pos.h0 -- Always include h0 for partition pruning!
2851
```
2952

30-
## Query Template
31-
32-
**EVERY query must start with this setup:**
53+
**Generating Output Files:**
3354

3455
```sql
35-
SET THREADS=100; SET preserve_insertion_order=false; SET enable_object_cache=true; SET temp_directory='/tmp';
36-
INSTALL httpfs; LOAD httpfs; INSTALL h3 FROM community; LOAD h3;
37-
CREATE OR REPLACE SECRET s3 (TYPE S3, ENDPOINT 'rook-ceph-rgw-nautiluss3.rook', URL_STYLE 'path', USE_SSL 'false', KEY_ID '', SECRET '');
38-
CREATE OR REPLACE SECRET outputs (TYPE S3, ENDPOINT 'minio.carlboettiger.info', URL_STYLE 'path', SCOPE 's3://public-outputs');
39-
40-
-- Your query here
41-
SELECT ...
56+
COPY (SELECT ...) TO 's3://public-output/unique-file-name.csv' (FORMAT CSV, HEADER, OVERWRITE_OR_IGNORE);
4257
```
4358

44-
**Why these settings?**
45-
- `THREADS=100` - Parallel S3 reads (I/O bound)
46-
- `preserve_insertion_order=false` - Faster aggregation
47-
- `enable_object_cache=true` - Reduces S3 requests
48-
- `httpfs` - Required for S3 access
49-
- `h3` - Required for H3 functions
50-
- Two secrets: readonly data access + write access for outputs
59+
Then tell the user the *public https* address (note the use of the public, not private endpoint): it should have the format like: `https://s3-west.nrp-nautilus.io/public-output/unique-file-name.csv` (adjust `unique-file-name.csv` part appropriately.)
60+
61+
62+
63+
Note: s3://public-output has a 30-day expiration and 1 Gb object size limit. CORS headers will permit files to be placed here and rendered by other tools.
64+
65+
5166

52-
**Generating Output Files:**
53-
```sql
54-
COPY (SELECT ...) TO 's3://public-outputs/wetlands/filename.csv' (FORMAT CSV, HEADER, OVERWRITE_OR_IGNORE);
55-
```
56-
Then provide download link: `https://minio.carlboettiger.info/public-outputs/wetlands/filename.csv`
5767

5868
## Query Optimization Essentials
5969

@@ -68,41 +78,13 @@ SELECT ... FROM filtered JOIN read_parquet('s3://public-wetlands/glwd/hex/**') w
6878
ON filtered.h8 = w.h8 AND filtered.h0 = w.h0
6979
```
7080

71-
### 2. Pre-filter Taxonomy
72-
```sql
73-
-- Good: Filter to birds before joining position data
74-
WITH birds AS (
75-
SELECT id, scientificName FROM read_parquet('s3://public-inat/taxonomy/...')
76-
WHERE class = 'Aves'
77-
)
78-
SELECT ... FROM birds JOIN read_parquet('s3://public-inat/range-maps/hex/**') ...
79-
```
81+
### 2. ALWAYS Include h0 in Joins
8082

81-
### 3. ALWAYS Include h0 in Joins
8283
```sql
8384
-- Enables partition pruning → 5-20x faster
8485
JOIN table2 ON table1.h8 = table2.h8 AND table1.h0 = table2.h0
8586
```
8687

87-
## Example Query Pattern
88-
89-
```sql
90-
-- Setup (always include)
91-
SET THREADS=100; SET preserve_insertion_order=false; SET enable_object_cache=true; SET temp_directory='/tmp';
92-
INSTALL httpfs; LOAD httpfs; INSTALL h3 FROM community; LOAD h3;
93-
CREATE OR REPLACE SECRET s3 (TYPE S3, ENDPOINT 'rook-ceph-rgw-nautiluss3.rook', URL_STYLE 'path', USE_SSL 'false', KEY_ID '', SECRET '');
94-
CREATE OR REPLACE SECRET outputs (TYPE S3, ENDPOINT 'minio.carlboettiger.info', URL_STYLE 'path', SCOPE 's3://public-outputs');
95-
96-
-- Example: Wetlands by category
97-
SELECT c.category,
98-
APPROX_COUNT_DISTINCT(w.h8) as hex_count,
99-
ROUND(hex_count * 73.7327598, 2) as area_hectares
100-
FROM read_parquet('s3://public-wetlands/glwd/hex/**') w
101-
JOIN read_csv('s3://public-wetlands/glwd/category_codes.csv') c ON w.Z = c.Z
102-
WHERE w.Z > 0
103-
GROUP BY c.category
104-
ORDER BY area_hectares DESC;
105-
```
10688

10789
## DuckDB SQL Syntax Reference
10890

0 commit comments

Comments
 (0)