Skip to content

Commit de1fbdc

Browse files
author
MPCoreDeveloper
committed
documentation update joins subqueries
1 parent 46135a2 commit de1fbdc

3 files changed

Lines changed: 195 additions & 6 deletions

File tree

README.md

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,103 @@ var rows = db.ExecuteQuery("SELECT * FROM users WHERE age > 25");
9696
- **DDL**: CREATE TABLE, DROP TABLE, CREATE INDEX, DROP INDEX
9797
- **DML**: INSERT, SELECT, UPDATE, DELETE, INSERT BATCH
9898
- **Queries**: WHERE, ORDER BY, LIMIT, OFFSET, BETWEEN
99-
- **Aggregates**: COUNT, SUM, AVG, MIN, MAX, GROUP BY
100-
- **Advanced**: JOINs, subqueries, complex expressions
99+
- **Aggregates**: COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING
100+
- **JOINs**: ✅ **INNER, LEFT, RIGHT, FULL OUTER, CROSS** (Q1 2026 - **Complete**)
101+
- **Subqueries**: ✅ **WHERE, FROM, SELECT, IN, EXISTS, Correlated** (Q1 2026 - **Complete**)
102+
- **Advanced**: Complex expressions, multi-table queries, query optimization
103+
104+
---
105+
106+
## **Recently Completed Features (Q1 2026)**
107+
108+
### 🔗 **Full JOIN Support** - PRODUCTION READY
109+
110+
All JOIN types are fully implemented with hash join optimization:
111+
112+
```csharp
113+
// INNER JOIN - Only matched rows
114+
var results = db.ExecuteQuery(@"
115+
SELECT u.name, o.amount
116+
FROM users u
117+
INNER JOIN orders o ON u.id = o.user_id
118+
WHERE o.amount > 100
119+
");
120+
121+
// LEFT OUTER JOIN - All left rows + matched right (NULL for unmatched)
122+
var results = db.ExecuteQuery(@"
123+
SELECT u.name, o.amount
124+
FROM users u
125+
LEFT JOIN orders o ON u.id = o.user_id
126+
");
127+
128+
// FULL OUTER JOIN - All rows from both sides
129+
var results = db.ExecuteQuery(@"
130+
SELECT u.name, o.amount
131+
FROM users u
132+
FULL OUTER JOIN orders o ON u.id = o.user_id
133+
");
134+
135+
// Multi-table JOINs
136+
var results = db.ExecuteQuery(@"
137+
SELECT c.name, r.name as region, SUM(o.amount) as total
138+
FROM customers c
139+
LEFT JOIN regions r ON c.region_id = r.id
140+
LEFT JOIN orders o ON o.customer_id = c.id
141+
GROUP BY c.name, r.name
142+
");
143+
```
144+
145+
**Performance**: Hash join (O(n+m)) for large datasets, nested loop for small datasets
146+
**Implementation**: `JoinExecutor.cs` with automatic algorithm selection
147+
148+
---
149+
150+
### 📊 **Full Subquery Support** - PRODUCTION READY
151+
152+
All subquery types with automatic caching:
153+
154+
```csharp
155+
// Scalar subquery in SELECT (cached for performance)
156+
var results = db.ExecuteQuery(@"
157+
SELECT name,
158+
salary,
159+
(SELECT AVG(salary) FROM employees) as avg_salary,
160+
salary - (SELECT AVG(salary) FROM employees) as diff
161+
FROM employees
162+
");
163+
164+
// Derived table in FROM
165+
var results = db.ExecuteQuery(@"
166+
SELECT dept_id, avg_salary
167+
FROM (
168+
SELECT department_id as dept_id,
169+
AVG(salary) as avg_salary
170+
FROM employees
171+
GROUP BY department_id
172+
) dept_avg
173+
WHERE avg_salary > 50000
174+
");
175+
176+
// IN with subquery
177+
var results = db.ExecuteQuery(@"
178+
SELECT * FROM orders
179+
WHERE customer_id IN (
180+
SELECT id FROM customers WHERE country = 'USA'
181+
)
182+
");
183+
184+
// Correlated EXISTS
185+
var results = db.ExecuteQuery(@"
186+
SELECT * FROM orders o
187+
WHERE EXISTS (
188+
SELECT 1 FROM customers c
189+
WHERE c.id = o.customer_id AND c.active = 1
190+
)
191+
");
192+
```
193+
194+
**Performance**: Non-correlated subqueries cached (O(1) after first execution)
195+
**Implementation**: `SubqueryExecutor.cs` with streaming execution and caching
101196

102197
---
103198

@@ -221,6 +316,9 @@ var rows = db.ExecuteQuery("SELECT * FROM users WHERE age > 25");
221316
| **Pure .NET** || ❌ (P/Invoke) ||
222317
| **Hash Indexes** |**O(1)** |||
223318
| **B-tree Indexes** |**O(log n)** |||
319+
| **JOINs (All Types)** |**Full (INNER/LEFT/RIGHT/FULL/CROSS)** |||
320+
| **Subqueries (All Types)** |**Full (WHERE/FROM/SELECT/IN/EXISTS)** |||
321+
| **Correlated Subqueries** |**Full with caching** |||
224322
| **AVX-512/AVX2** ||||
225323
| **NativeAOT Ready** ||| ⚠️ Limited |
226324
| **Async/Await** |**Full** | ⚠️ Limited | ⚠️ Limited |

docs/FEATURE_STATUS.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,94 @@ This document provides a comprehensive overview of all features in SharpCoreDB,
102102

103103
| Feature | Status | ETA | Notes |
104104
|---------|--------|-----|-------|
105-
| **JOINs** | 🚧 Partial | Q1 2026 | INNER JOIN supported, LEFT/RIGHT in progress |
106-
| **Subqueries** | 🚧 Partial | Q1 2026 | WHERE subqueries supported |
105+
| **JOINs (INNER/LEFT/RIGHT/FULL/CROSS)** | **Complete** | **Q1 2026 DONE** | All JOIN types fully implemented with hash join optimization |
106+
| **Subqueries (all types)** | **Complete** | **Q1 2026 DONE** | WHERE, FROM, SELECT, IN, EXISTS, correlated - all supported |
107107
| **Triggers** | 🚧 Planning | Q2 2026 | BEFORE/AFTER INSERT/UPDATE/DELETE |
108108
| **Stored Procedures** | 🚧 Planning | Q2 2026 | Pre-compiled SQL routines |
109109
| **Views** | 🚧 Planning | Q2 2026 | Virtual tables |
110110

111111
---
112112

113+
## ✅ Recently Completed (Q1 2026)
114+
115+
### JOINs - **PRODUCTION READY**
116+
117+
| Feature | Status | Implementation | Performance |
118+
|---------|--------|---------------|-------------|
119+
| **INNER JOIN** | ✅ Complete | Hash join (O(n+m)) + nested loop fallback | Production |
120+
| **LEFT OUTER JOIN** | ✅ Complete | Preserves all left rows, NULL for unmatched | Production |
121+
| **RIGHT OUTER JOIN** | ✅ Complete | Preserves all right rows, NULL for unmatched | Production |
122+
| **FULL OUTER JOIN** | ✅ Complete | Preserves all rows from both sides | Production |
123+
| **CROSS JOIN** | ✅ Complete | Cartesian product | Production |
124+
| **Multi-table JOINs** | ✅ Complete | Chain multiple JOINs | Production |
125+
| **Qualified columns** | ✅ Complete | `table.column` alias handling | Production |
126+
127+
**Implementation Files:**
128+
- `src/SharpCoreDB/Execution/JoinExecutor.cs` - Core execution engine
129+
- `src/SharpCoreDB/Execution/JoinConditionEvaluator.cs` - ON clause evaluation
130+
- Tests: `tests/SharpCoreDB.DemoJoinsSubQ/JoinScenarios.cs`
131+
132+
**Example:**
133+
```sql
134+
SELECT u.name, o.amount
135+
FROM users u
136+
LEFT JOIN orders o ON u.id = o.user_id
137+
WHERE o.amount > 100
138+
-- 10K users × 100K orders = ~100ms with hash join
139+
```
140+
141+
---
142+
143+
### Subqueries - **PRODUCTION READY**
144+
145+
| Feature | Status | Implementation | Performance |
146+
|---------|--------|---------------|-------------|
147+
| **Scalar Subqueries** | ✅ Complete | SELECT list, single value | O(1) cached |
148+
| **Derived Tables** | ✅ Complete | FROM (SELECT ...) | Streaming |
149+
| **WHERE Subqueries** | ✅ Complete | IN, EXISTS, NOT EXISTS | O(n+m) |
150+
| **Correlated Subqueries** | ✅ Complete | Outer row binding | O(n×m) optimized |
151+
| **Subquery Caching** | ✅ Complete | Non-correlated automatic cache | 100-1000x speedup |
152+
153+
**Implementation Files:**
154+
- `src/SharpCoreDB/Execution/SubqueryExecutor.cs` - Execution engine
155+
- `src/SharpCoreDB/Services/SubqueryNode.cs` - AST nodes
156+
- `src/SharpCoreDB/Execution/SubqueryCache.cs` - Result caching
157+
- `src/SharpCoreDB/Execution/SubqueryPlanner.cs` - Query planning
158+
- Tests: `tests/SharpCoreDB.DemoJoinsSubQ/SubqueryScenarios.cs`
159+
160+
**Examples:**
161+
```sql
162+
-- Scalar subquery (cached)
163+
SELECT name,
164+
(SELECT AVG(salary) FROM employees) as avg_salary
165+
FROM employees;
166+
167+
-- Derived table
168+
SELECT dept_id, avg_salary
169+
FROM (
170+
SELECT department_id as dept_id,
171+
AVG(salary) as avg_salary
172+
FROM employees
173+
GROUP BY department_id
174+
) dept_avg
175+
WHERE avg_salary > 50000;
176+
177+
-- IN with subquery
178+
SELECT * FROM orders
179+
WHERE customer_id IN (
180+
SELECT id FROM customers WHERE country = 'USA'
181+
);
182+
183+
-- Correlated EXISTS
184+
SELECT * FROM orders o
185+
WHERE EXISTS (
186+
SELECT 1 FROM customers c
187+
WHERE c.id = o.customer_id AND c.active = 1
188+
);
189+
```
190+
191+
---
192+
113193
## Performance Characteristics
114194

115195
### Insert Performance

src/SharpCoreDB.Provider.YesSql/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,19 @@ Each thread has its own `last_insert_rowid` value via `AsyncLocal<long>`, ensuri
343343
| **INTEGER PRIMARY KEY** |||
344344
| **AUTOINCREMENT** || ✅ (but YesSql doesn't use it) |
345345
| **LIMIT/OFFSET** |||
346-
| **JOINs** || 🚧 Partial (basic JOINs work) |
347-
| **Subqueries** || 🚧 Partial (WHERE subqueries work) |
346+
| **JOINs (INNER)** ||**Full** |
347+
| **JOINs (LEFT)** ||**Full** |
348+
| **JOINs (RIGHT)** ||**Full** |
349+
| **JOINs (FULL OUTER)** ||**Full** |
350+
| **JOINs (CROSS)** ||**Full** |
351+
| **Subqueries (WHERE)** ||**Full** |
352+
| **Subqueries (FROM)** ||**Full** (derived tables) |
353+
| **Subqueries (SELECT)** ||**Full** (scalar subqueries) |
354+
| **IN (subquery)** ||**Full** |
355+
| **EXISTS/NOT EXISTS** ||**Full** |
356+
| **GROUP BY** ||**Full** |
357+
| **HAVING** ||**Full** |
358+
| **Correlated Subqueries** ||**Full** |
348359
| **Triggers** || 🚧 Planned Q2 2026 |
349360

350361
---

0 commit comments

Comments
 (0)