2.3 KiB
2.3 KiB
Plan: ID Generators & Sequence System
Goal
Add auto-generated ID support to BaraDB so users don't need to manually supply IDs on INSERT.
Phase 1: ID Generators
1.1 AUTO_INCREMENT on INTEGER columns
- Add
AUTO_INCREMENTkeyword to lexer - Parse in CREATE TABLE:
id INTEGER PRIMARY KEY AUTO_INCREMENT - Store auto-increment state per table in ExecutionContext (counter)
- On INSERT without explicit ID → auto-populate with next value
- Thread-safe counter (atomic increment)
1.2 SERIAL / BIGSERIAL as syntactic sugar
SERIAL=INTEGER AUTO_INCREMENTBIGSERIAL=BIGINT AUTO_INCREMENT- Already partially parsed — wire to auto-increment logic
1.3 UUID generation
- Add
gen_random_uuid()oruuid()as built-in function - Can be used in INSERT:
INSERT INTO t (id) VALUES (uuid()) - Also usable as DEFAULT:
id UUID DEFAULT uuid() - Use Nim's std/oids or crypto random
1.4 RETURNING clause
- After INSERT, return generated values
INSERT INTO t (name) VALUES ('x') RETURNING id- Already partially parsed — wire to execution
1.5 CREATE SEQUENCE / nextval / currval
CREATE SEQUENCE seq_name START 1 INCREMENT 1nextval('seq_name')→ returns next valuecurrval('seq_name')→ returns current value- Store sequences in ExecutionContext
1.6 Snowflake ID (distributed)
- 64-bit ID = timestamp(41) + node_id(10) + sequence(12)
snowflake_id(node_id)function- For future distributed use
Phase 2: JOIN Optimizations ✅
2.1 Hash Join ✅
- For equi-join ON a.col = b.col
- Build hash table on smaller side, probe with larger
- O(N+M) instead of O(N*M)
2.2 Index Nested Loop Join ✅
- If index exists on join column → probe index per left row
- O(N * log M) instead of O(N*M)
2.3 Merge Join
- For sorted inputs
- Two-pointer sweep O(N+M)
- Status: Not yet implemented — can be added if needed
Phase 3: Foreign Key Enforcement ✅
3.1 CASCADE DELETE ✅
3.2 SET NULL on delete ✅
3.3 RESTRICT on delete ✅
3.4 ON UPDATE CASCADE ✅
3.5 ON UPDATE SET NULL ✅
3.6 ON UPDATE RESTRICT ✅
3.7 FK check on UPDATE (not just INSERT) ✅
Implementation Order
- AUTO_INCREMENT (lexer + parser + executor)
- SERIAL/BIGSERIAL sugar
- UUID function
- RETURNING clause
- Sequences (CREATE SEQUENCE / nextval / currval)
- Snowflake ID function