✔ PASS
Search & Retrieval
task_073_Q73_search_term_cooccurrence_matrix
Scaled: 1M+ Records
Ordered: Yes
Search Term Co-Occurrence Matrix (Market Basket of Search Queries)
Candidate Latency (1M Rows)
0.87 ms
(1.1x faster than golden)
Golden Latency (1M Rows)
0.92 ms
DuckDB in-memory engine
Underlying Data Volume
2,500 rows
58.6 KiB across 1 table
Evaluated Model
gemini-3.8-flash-low
Gen Latency: 12.8s
✔ 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
To suggest related searches, engines analyze queries searched together in the same session (e.g. users searching 'monitor' also search 'hdmi cable' 42% of the time).
⚡ The Frontier Trap (Why LLMs Fail)
Self-joining search sessions without ordering (`a.term < b.term`) doubles matrix size with duplicate reversed pairs.
Prompt / User Request Given to Model
To suggest related searches, engines analyze queries searched together in the same session (e.g. users searching 'monitor' also search 'hdmi cable' 42% of the time). Execute the required analytical SQL calculation in DuckDB. Expected output columns: term_a, term_b, cooccurrence_count. Results should be ordered as specified.
2. Underlying Tables, Row Counts & Data Volume
1 Tables (2,500 rows • 58.6 KiB)Production-scale fixtures loaded into the isolated DuckDB sandbox with at least 1,000,000 records per table:
📊 session_searches
2,500 rows
58.6 KiB Data Volume
session_id (INTEGER) term (VARCHAR)Data Preview (showing sample rows from 2,500 total rows):
| # | session_id | term |
|---|---|---|
| 1 | 1 | laptop |
| 2 | 1 | mouse |
| 3 | 1 | backpack |
| 4 | 2 | laptop |
| 5 | 2 | mouse |
| 6 | 3 | laptop |
| 7 | 3 | keyboard |
| 8 | 3 | laptop |
| 9 | 4 | mouse |
| 10 | 5 | backpack |
| 11 | 6 | keyboard |
| 12 | 7 | laptop |
| 13 | 8 | mouse |
| 14 | 9 | backpack |
| 15 | 10 | keyboard |
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:
0.3%
Latency: 0.00 ms
Rows In/Out: 4
Data Vol: 160 B
📁 Projections: ['__internal_decomp...
20.2%
Latency: 0.18 ms
Rows In/Out: 4
Data Vol: 160 B
0.5%
Latency: 0.00 ms
Rows In/Out: 4
Data Vol: 160 B
📁 Projections: ['__internal_compre...
1.0%
Latency: 0.01 ms
Rows In/Out: 4
Data Vol: 160 B
📁 Projections: ['__internal_decomp...
20.7%
Latency: 0.18 ms
Rows In/Out: 4
Data Vol: 160 B
0.0%
Latency: 0.00 ms
Rows In/Out: 6
Data Vol: 192 B
📁 Projections: ['term', 'term']
0.3%
Latency: 0.00 ms
Rows In/Out: 6
Data Vol: 192 B
📁 Projections: ['__internal_compre...
40.6%
Latency: 0.35 ms
Rows In/Out: 6
Data Vol: 192 B
8.1%
Latency: 0.07 ms
Rows In/Out: 2,500
Data Vol: 0.05 MB
📁 memory.main.session_searches
8.4%
Latency: 0.07 ms
Rows In/Out: 2,500
Data Vol: 0.05 MB
📁 memory.main.session_searches
| Operator | Cost % | Latency | Processed Rows | Data Volume | Details / Table | Status |
|---|---|---|---|---|---|---|
| HASH_JOIN | 40.6% | 0.35 ms | 6 | 0.00 MB | - | Normal |
| HASH_GROUP_BY | 20.7% | 0.18 ms | 4 | 0.00 MB | - | Normal |
| ORDER_BY | 20.2% | 0.18 ms | 4 | 0.00 MB | - | Normal |
| TABLE_SCAN | 8.4% | 0.07 ms | 2,500 | 0.05 MB | memory.main.session_searches | Normal |
| TABLE_SCAN | 8.1% | 0.07 ms | 2,500 | 0.05 MB | memory.main.session_searches | Normal |
| PROJECTION | 1.0% | 0.01 ms | 4 | 0.00 MB | Projections: ['__internal_decompress_string(#0)' | Normal |
| PROJECTION | 0.5% | 0.00 ms | 4 | 0.00 MB | Projections: ['__internal_compress_string_uhugei | Normal |
| PROJECTION | 0.3% | 0.00 ms | 4 | 0.00 MB | Projections: ['__internal_decompress_string(#0)' | Normal |
| PROJECTION | 0.3% | 0.00 ms | 6 | 0.00 MB | Projections: ['__internal_compress_string_uhugei | Normal |
| PROJECTION | 0.0% | 0.00 ms | 6 | 0.00 MB | Projections: ['term', 'term'] | Normal |
4. Queries Executed (Candidate vs Golden Ground Truth)
Beautified & Syntax HighlightedSELECT a.term AS term_a,
b.term AS term_b,
count(*) AS cooccurrence_count
FROM session_searches a
JOIN session_searches b ON a.session_id = b.session_id
AND a.term < b.term
GROUP BY a.term,
b.term
ORDER BY cooccurrence_count DESC,
term_a,
term_b;
-- Find pairs of terms most frequently searched in the same user session
SELECT a.term AS term_a,
b.term AS term_b,
count(*) AS cooccurrence_count
FROM session_searches a
JOIN session_searches b ON a.session_id = b.session_id
AND a.term < b.term
GROUP BY a.term,
b.term
ORDER BY cooccurrence_count DESC,
term_a,
term_b;
CREATE TABLE session_searches (session_id INT, term VARCHAR);
INSERT INTO session_searches
VALUES (1, 'laptop'),
(1, 'mouse'),
(1, 'backpack'),
(2, 'laptop'),
(2, 'mouse'),
(3, 'laptop'),
(3, 'keyboard');
5. Query Results & Expected Ground Truth
Tabular Comparison
Actual Candidate Output
(4 rows × 3 cols)
| # | term_a | term_b | cooccurrence_count |
|---|---|---|---|
| 1 | keyboard | laptop | 2 |
| 2 | laptop | mouse | 2 |
| 3 | backpack | laptop | 1 |
| 4 | backpack | mouse | 1 |
Expected Golden Output
(4 rows × 3 cols)
| # | term_a | term_b | cooccurrence_count |
|---|---|---|---|
| 1 | keyboard | laptop | 2 |
| 2 | laptop | mouse | 2 |
| 3 | backpack | laptop | 1 |
| 4 | backpack | mouse | 1 |