✔ PASS
Vector & Embeddings
task_047_Q47_hierarchical_vector_coarse_fine
Scaled: 1M+ Records
Ordered: Yes
Hierarchical Coarse-to-Fine Multi-Level Vector Filtering
Candidate Latency (1M Rows)
0.37 ms
(1.0x faster than golden)
Golden Latency (1M Rows)
0.38 ms
DuckDB in-memory engine
Underlying Data Volume
1,000,002 rows
34.3 MiB across 2 tables
Evaluated Model
gemini-3.8-flash-low
Gen Latency: 14.6s
✔ Correctness Verified on 1M+ Records
Candidate SQL output matches ground truth golden query across all multiset rows and numeric tolerances (tol=1e-3). Zero data skew or anti-patterns detected on full dataset scale.
1. Analytical Significance & What It Means
Scenario & Frontier TrapBusiness Scenario & Problem
In massive enterprise catalogs, searching 100 million items is accelerated by first matching high-level department embedding centroids, then searching specific item vectors inside matching departments.
⚡ The Frontier Trap (Why LLMs Fail)
Flat brute-force vector search over hundreds of millions of items consumes excessive compute.
Prompt / User Request Given to Model
In massive enterprise catalogs, searching 100 million items is accelerated by first matching high-level department embedding centroids, then searching specific item vectors inside matching departments. Execute the required analytical SQL calculation in DuckDB. Expected output columns: item_id, item_name, fine_sim_score. Results should be ordered as specified.
2. Underlying Tables, Row Counts & Data Volume
2 Tables (1,000,002 rows • 34.3 MiB)Production-scale fixtures loaded into the isolated DuckDB sandbox with at least 1,000,000 records per table:
📊 departments
2 rows
64 B Data Volume
dept_id (INTEGER) dept_name (VARCHAR) dept_centroid (FLOAT[2])Data Preview (showing sample rows from 2 total rows):
| # | dept_id | dept_name | dept_centroid |
|---|---|---|---|
| 1 | 1 | Electronics | [0.9 0.1] |
| 2 | 2 | Gardening | [0.1 0.9] |
📊 items
1,000,000 rows
34.3 MiB Data Volume
item_id (INTEGER) dept_id (INTEGER) item_name (VARCHAR) item_vector (FLOAT[2])Data Preview (showing sample rows from 1,000,000 total rows):
| # | item_id | dept_id | item_name | item_vector |
|---|---|---|---|---|
| 1 | 101 | 1 | Noise-canceling earphones | [0.92 0.08] |
| 2 | 102 | 1 | USB-C Cable | [0.85 0.15] |
| 3 | 201 | 2 | Lawn Mower | [0.12 0.88] |
| 4 | 201 | 2 | Noise-canceling earphones | [0.929 0.0845] |
| 5 | 202 | 3 | USB-C Cable | [0.867 0.0663] |
| 6 | 203 | 4 | Lawn Mower | [0.8698 0.1118] |
| 7 | 204 | 5 | Noise-canceling earphones | [0.8179 0.1452] |
| 8 | 205 | 6 | USB-C Cable | [0.8257 0.1332] |
| 9 | 206 | 7 | Lawn Mower | [0.9027 0.0792] |
| 10 | 207 | 8 | Noise-canceling earphones | [0.8539 0.1043] |
| 11 | 208 | 9 | USB-C Cable | [0.8653 0.0707] |
| 12 | 209 | 10 | Lawn Mower | [0.8714 0.1177] |
| 13 | 210 | 11 | Noise-canceling earphones | [0.8695 0.1147] |
| 14 | 211 | 12 | USB-C Cable | [0.7993 0.2177] |
| 15 | 212 | 13 | Lawn Mower | [0.9018 0.0976] |
3. Graphical Query Execution Plan & Cost Bottlenecks
Physical Operator Tree (DuckDB 2.0 Profiler)Visual flowchart of the physical execution pipeline. Operators with high relative costs (≥ 20% of query runtime or heavy latency) are visually highlighted:
22.6%
Latency: 0.08 ms
Rows In/Out: 2
Data Vol: 48 B
2.2%
Latency: 0.01 ms
Rows In/Out: 2
Data Vol: 48 B
📁 Projections: ['item_id', 'item_n...
16.3%
Latency: 0.06 ms
Rows In/Out: 2
Data Vol: 88 B
6.5%
Latency: 0.02 ms
Rows In/Out: 2
Data Vol: 64 B
📁 memory.main.items
0.8%
Latency: 0.00 ms
Rows In/Out: 1
Data Vol: 8 B
📁 Projections: ['CAST(#0 AS INTEGE...
16.4%
Latency: 0.06 ms
Rows In/Out: 1
Data Vol: 5 B
0.7%
Latency: 0.00 ms
Rows In/Out: 1
Data Vol: 5 B
📁 Projections: ['CAST(#0 AS TINYIN...
1.2%
Latency: 0.00 ms
Rows In/Out: 1
Data Vol: 8 B
📁 Projections: ['dept_id', 'array_...
22.5%
Latency: 0.08 ms
Rows In/Out: 1
Data Vol: 12 B
3.6%
Latency: 0.01 ms
Rows In/Out: 1
Data Vol: 20 B
📁 memory.main.departments
3.5%
Latency: 0.01 ms
Rows In/Out: 1
Data Vol: 12 B
1.0%
Latency: 0.00 ms
Rows In/Out: 2
Data Vol: 24 B
📁 Projections: ['array_cosine_simi...
2.6%
Latency: 0.01 ms
Rows In/Out: 2
Data Vol: 32 B
📁 memory.main.departments
| Operator | Cost % | Latency | Processed Rows | Data Volume | Details / Table | Status |
|---|---|---|---|---|---|---|
| ORDER_BY | 22.6% | 0.08 ms | 2 | 0.00 MB | - | Normal |
| HASH_JOIN | 22.5% | 0.08 ms | 1 | 0.00 MB | - | Normal |
| ORDER_BY | 16.4% | 0.06 ms | 1 | 0.00 MB | - | Normal |
| HASH_JOIN | 16.3% | 0.06 ms | 2 | 0.00 MB | - | Normal |
| TABLE_SCAN | 6.5% | 0.02 ms | 2 | 0.00 MB | memory.main.items | Normal |
| TABLE_SCAN | 3.6% | 0.01 ms | 1 | 0.00 MB | memory.main.departments | Normal |
| TOP_N | 3.5% | 0.01 ms | 1 | 0.00 MB | - | Normal |
| TABLE_SCAN | 2.6% | 0.01 ms | 2 | 0.00 MB | memory.main.departments | Normal |
| PROJECTION | 2.2% | 0.01 ms | 2 | 0.00 MB | Projections: ['item_id', 'item_name', 'fine_sim_ | Normal |
| PROJECTION | 1.2% | 0.00 ms | 1 | 0.00 MB | Projections: ['dept_id', 'array_cosine_similarit | Normal |
| PROJECTION | 1.0% | 0.00 ms | 2 | 0.00 MB | Projections: ['array_cosine_similarity(dept_cent | Normal |
| PROJECTION | 0.8% | 0.00 ms | 1 | 0.00 MB | Projections: ['CAST(#0 AS INTEGER)', '#1'] | Normal |
| PROJECTION | 0.7% | 0.00 ms | 1 | 0.00 MB | Projections: ['CAST(#0 AS TINYINT)', '#1'] | Normal |
4. Queries Executed (Candidate vs Golden Ground Truth)
Beautified & Syntax HighlightedWITH top_dept AS
(SELECT dept_id
FROM departments
ORDER BY array_cosine_similarity(dept_centroid, [0.95, 0.05]::FLOAT[2]) DESC
LIMIT 1)
SELECT i.item_id,
i.item_name,
round(array_cosine_similarity(i.item_vector, [0.95, 0.05]::FLOAT[2]), 4) AS fine_sim_score
FROM items i
JOIN top_dept d ON i.dept_id = d.dept_id
ORDER BY fine_sim_score DESC;
-- Find best matching department, then retrieve top items strictly within that department
WITH top_dept AS
(SELECT dept_id
FROM departments
ORDER BY array_cosine_similarity(dept_centroid, [0.95, 0.05]::FLOAT[2]) DESC
LIMIT 1)
SELECT i.item_id,
i.item_name,
round(array_cosine_similarity(i.item_vector, [0.95, 0.05]::FLOAT[2]), 4) AS fine_sim_score
FROM items i
JOIN top_dept d ON i.dept_id = d.dept_id
ORDER BY fine_sim_score DESC;
CREATE TABLE departments (dept_id INT, dept_name VARCHAR, dept_centroid FLOAT[2]);
CREATE TABLE items (item_id INT, dept_id INT, item_name VARCHAR, item_vector FLOAT[2]);
INSERT INTO departments
VALUES (1, 'Electronics', [0.9, 0.1]::FLOAT[2]),
(2, 'Gardening', [0.1, 0.9]::FLOAT[2]);
INSERT INTO items
VALUES (101, 1, 'Noise-canceling earphones', [0.92, 0.08]::FLOAT[2]),
(102, 1, 'USB-C Cable', [0.85, 0.15]::FLOAT[2]),
(201, 2, 'Lawn Mower', [0.12, 0.88]::FLOAT[2]);
5. Query Results & Expected Ground Truth
Tabular Comparison
Actual Candidate Output
(2 rows × 3 cols)
| # | item_id | item_name | fine_sim_score |
|---|---|---|---|
| 1 | 101 | Noise-canceling earphones | 0.9994 |
| 2 | 102 | USB-C Cable | 0.9926 |
Expected Golden Output
(2 rows × 3 cols)
| # | item_id | item_name | fine_sim_score |
|---|---|---|---|
| 1 | 101 | Noise-canceling earphones | 0.9994 |
| 2 | 102 | USB-C Cable | 0.9926 |