aboutsummaryrefslogtreecommitdiffstats
path: root/src/operations.erl
blob: f4143c870cbf25fbb582c20bc1855f7c7a9471eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
%%% @doc Extended Relational Database Operations (RM/T)
%%%
%%% This module implements core database operations for Domino, an extended
%%% relational database engine based on E. F. Codd's RM/T (1979).
%%%
%%% == Architecture ==
%%%
%%% The system uses:
%%% <ul>
%%%   <li><b>Content-Addressed Storage</b> - All entities identified by SHA-256 hashes</li>
%%%   <li><b>Immutable Data Structures</b> - Operations create new versions</li>
%%%   <li><b>Merkle Trees</b> - Efficient versioning and diffing via merklet</li>
%%%   <li><b>Mnesia Backend</b> - ACID transactions and persistent storage</li>
%%%   <li><b>Volcano Iterator Model</b> - Lazy evaluation with process-based iterators</li>
%%% </ul>
%%%
%%% == Data Model ==
%%%
%%% The system maintains four tables in Mnesia:
%%% <ul>
%%%   <li>`attribute' - Stores atomic values with their hashes</li>
%%%   <li>`tuple' - Stores tuples as maps from attribute names to value hashes</li>
%%%   <li>`relation' - Stores relations with merkle trees of tuple hashes</li>
%%%   <li>`database_state' - Stores database snapshots with relation merkle trees</li>
%%% </ul>
%%%
%%% == RM/T Integrity Rules ==
%%%
%%% <ul>
%%%   <li><b>Rule 3 (Entity Integrity)</b> - E-relations accept insertions/deletions, not updates</li>
%%%   <li><b>Rule 4 (Property Integrity)</b> - Tuples require entity existence in E-relation</li>
%%% </ul>
%%%
%%% @author Nekoma Team
%%% @copyright 2025
%%% @version 0.1.0
%%% @reference E. F. Codd (1979). "Extending the Database Relational Model to Capture More Meaning"
%%% @reference E. F. Codd (1970). "A Relational Model of Data for Large Shared Data Banks"

-module(operations).
-include("../include/operations.hrl").
-export([
	 create_database/1,
	 create_relation/3,
	 create_immutable_relation/2,
	 update_relation_constraints/3,
	 create_tuple/3,
	 create_all_tuples/3,
	 retract_tuple/3,
	 retract_tuple/4,
	 clear_relation/2,
	 clear_relation/3,
	 retract_relation/2,
	 retract_relation/3,
	 get_relation_hash/2,
	 get_relations/1,
	 hashes_from_tuple/1,
	 get_tuples_iterator/2,
	 get_iterator_from_generator/2,
	 next_tuple/1,
	 close_iterator/1,
	 collect_all/1,
	 %% Iterator operators (lazy)
	 select_iterator/2,
	 project_iterator/2,
	 sort_iterator/2,
	 take_iterator/2,
	 %% Join operators
	 equijoin_iterator/3,
	 theta_join_iterator/3,
	 %% Materialization (eager)
	 materialize/3
	]).

%% @private
%% @doc Compute SHA-256 hash of an Erlang term.
%%
%% This is the foundational hashing function used throughout the system for
%% content addressing. All entities (attributes, tuples, relations, databases)
%% are identified by hashes computed with this function.
%%
%% @param Value Any Erlang term to hash
%% @returns Binary SHA-256 hash (32 bytes)
%%
%% @see create_tuple/3
%% @see create_relation/3
hash(Value) ->
    crypto:hash(sha256, term_to_binary(Value)).

%% @doc Insert a tuple into a relation and update the database state.
%%
%% This is the primary operation for adding data to the database. It:
%% <ol>
%%   <li>Stores each attribute value with its hash (automatic deduplication)</li>
%%   <li>Creates a tuple record mapping attribute names to value hashes</li>
%%   <li>Inserts the tuple hash into the relation's merkle tree</li>
%%   <li>Computes new relation hash from updated tree</li>
%%   <li>Updates database merkle tree with new relation hash</li>
%%   <li>Computes new database hash</li>
%% </ol>
%%
%% All operations execute within a Mnesia transaction for ACID guarantees.
%%
%% == Example ==
%% <pre>
%% DB = operations:create_database(my_db),
%% {DB1, _} = operations:create_relation(DB, users, #{name => string, age => integer}),
%% {DB2, _} = operations:create_tuple(DB1, users, #{name => "Alice", age => 30}).
%% </pre>
%%
%% @param Database `#database_state{}' record
%% @param RelationName Atom naming the relation
%% @param Tuple Map of attribute names to values (e.g., `#{name => "Alice", age => 30}')
%% @returns `{UpdatedDatabase, UpdatedRelation}' tuple
%%
%% @see create_relation/3
%% @see get_tuples_iterator/2
create_tuple(Database, RelationName, Tuple) when is_map(Tuple), is_record(Database, database_state) ->
    %% Check if relation is immutable before attempting insert
    CurrentRelationHash = maps:get(RelationName, Database#database_state.relations),
    [RelationRecord] = mnesia:dirty_read(relation, CurrentRelationHash),
    %% Validate tuple against relation schema before insertion
    case constraint:validate_tuple(Database, Tuple, RelationRecord) of
        ok ->
            create_tuple_internal(Database, RelationName, Tuple, RelationRecord);
        {error, Reason} ->
            {error, {constraint_violation, Reason}}
    end.

%% @private
%% Internal function for tuple creation
create_tuple_internal(Database, RelationName, Tuple, _RelationRecord) ->
    F = fun() ->
        %% Step 1: Store each attribute value and build attribute_map
        AttributeMap = maps:map(fun(_AttrName, Value) ->
            ValueHash = hash(Value),
            mnesia:write(attribute, #attribute{hash = ValueHash, value = Value}, write),
            ValueHash
        end, Tuple),
        
        %% Step 2: Compute tuple hash and store tuple
        TupleHash = hash(Tuple),
        mnesia:write(tuple, #tuple{hash = TupleHash, relation = RelationName, attribute_map = AttributeMap}, write),
        
        %% Step 3: Get current relation hash from database
        CurrentRelationHash = maps:get(RelationName, Database#database_state.relations),
        
        %% Step 4: Read current relation
        [RelationRecord] = mnesia:read(relation, CurrentRelationHash),
        
        %% Step 5: Insert tuple hash into relation merkle tree
        %% We don't need to store the content of a tuple hash, just the key
        %% merklet can spot the difference in both keys and values
        %% sets:set follows the same strategy of an empty value
        NewRelationTree = merklet:insert({TupleHash, <<>>}, RelationRecord#relation.tree),
        
        %% Step 6: Compute new relation hash as the root of the new merkle tree and store
        {_,NewRelationTreeHash,_,_} = NewRelationTree,
	NewRelationHash = hash({RelationRecord#relation.name, RelationRecord#relation.schema, NewRelationTreeHash}),

        %% Update cardinality: increment by 1
        NewCardinality = case RelationRecord#relation.cardinality of
            {finite, N} -> {finite, N + 1};
            Other -> Other  % Shouldn't happen for finite relations
        end,

        UpdatedRelation = #relation{
            hash = NewRelationHash,
            name = RelationName,
            tree = NewRelationTree,
            schema = RelationRecord#relation.schema,
            constraints = RelationRecord#relation.constraints,
            cardinality = NewCardinality,
            generator = RelationRecord#relation.generator,
            provenance = RelationRecord#relation.provenance,
            lineage = RelationRecord#relation.lineage,
            membership_criteria = RelationRecord#relation.membership_criteria
        },
        mnesia:write(relation, UpdatedRelation, write),
        
        %% Step 7: Update database relations map
        NewRelations = maps:put(RelationName, NewRelationHash, Database#database_state.relations),
        
        %% Step 8: Update database tree (for diffing)
        RelationKey = atom_to_binary(RelationName, utf8),
        NewDatabaseTree = merklet:insert({RelationKey, NewRelationHash}, Database#database_state.tree),
        
        %% Step 9: Compute new database hash and store
        {_,NewDatabaseHash,_,_} = NewDatabaseTree,
        UpdatedDatabase = #database_state{
            name = Database#database_state.name,
            hash = NewDatabaseHash,
            tree = NewDatabaseTree,
            relations = NewRelations,
            timestamp = erlang:timestamp()
        },
        mnesia:write(database_state, UpdatedDatabase, write),
        
        {UpdatedDatabase, UpdatedRelation}
    end,
    
    {atomic, Result} = mnesia:transaction(F),
    Result.

%% @doc Clear all tuples from a relation (truncate).
%%
%% Wrapper for `clear_relation/3' with transactions enabled.
%%
%% @param Database `#database_state{}' record
%% @param RelationHash Binary hash identifying the relation
%% @returns `{atomic, {UpdatedDatabase, UpdatedRelation}}' or `{aborted, Reason}'
%%
%% @see clear_relation/3
%% @see retract_relation/2
clear_relation(Database, RelationHash) ->
    clear_relation(Database, RelationHash, true).

%% @doc Clear all tuples from a relation with optional transaction control.
%%
%% Removes all tuples from the relation but preserves its schema. The relation's
%% merkle tree is set to `undefined', and a new relation hash is computed.
%%
%% @param Database `#database_state{}' record
%% @param RelationHash Binary hash identifying the relation
%% @param Transact Boolean - if `true', wraps operation in Mnesia transaction
%% @returns When `Transact =:= true': `{atomic, {UpdatedDatabase, UpdatedRelation}}'
%%          When `Transact =:= false': `{UpdatedDatabase, UpdatedRelation}'
%%
%% @see retract_relation/2
clear_relation(Database, RelationHash, Transact) ->
    [Relation] = mnesia:dirty_read(relation, RelationHash),
    NewRelationHash = hash({Relation#relation.name, Relation#relation.schema, undefined}),
    UpdatedRelation =
	Relation#relation{hash = NewRelationHash,
			  tree = undefined},
    DatabaseTreeWithoutTuples = merklet:insert({atom_to_binary(Relation#relation.name, utf8), NewRelationHash}, Database#database_state.tree),
    {_,NewDatabaseHash,_,_} = DatabaseTreeWithoutTuples,
    NewRelations = maps:put(Relation#relation.name, NewRelationHash, Database#database_state.relations),
    UpdatedDatabase =
	Database#database_state{timestamp = erlang:timestamp(),
			        hash = NewDatabaseHash,
			        tree = DatabaseTreeWithoutTuples,
			        relations = NewRelations},
     TX = fun () ->
		 mnesia:write(relation, UpdatedRelation, write),
		 mnesia:write(database_state, UpdatedDatabase, write),
		 {UpdatedDatabase, UpdatedRelation}
	 end,
    case Transact of
	true -> mnesia:transaction(TX);
	false -> {UpdatedDatabase, UpdatedRelation}
    end.

%% @doc Remove a tuple from a relation.
%%
%% Wrapper for `retract_tuple/4' with transactions enabled.
%%
%% @param Database `#database_state{}' record
%% @param RelationHash Binary hash identifying the relation
%% @param TupleHash Binary hash identifying the tuple to remove
%% @returns `{atomic, {UpdatedDatabase, UpdatedRelation}}' or `{aborted, Reason}'
%%
%% @see retract_tuple/4
%% @see create_tuple/3
retract_tuple(Database, RelationHash, TupleHash) ->
    retract_tuple(Database, RelationHash, TupleHash, true).

%% @doc Remove a tuple from a relation with optional transaction control.
%%
%% Deletes a specific tuple from the relation's merkle tree and recomputes
%% the relation and database hashes.
%%
%% @param Database `#database_state{}' record
%% @param RelationHash Binary hash identifying the relation
%% @param TupleHash Binary hash identifying the tuple to remove
%% @param Transact Boolean - if `true', wraps operation in Mnesia transaction
%% @returns When `Transact =:= true': `{atomic, {UpdatedDatabase, UpdatedRelation}}'
%%          When `Transact =:= false': `{UpdatedDatabase, UpdatedRelation}'
%%
%% @see create_tuple/3
retract_tuple(Database, RelationHash, TupleHash, Transact)
  when is_record(Database, database_state) ->
    [Relation] = mnesia:dirty_read(relation, RelationHash),
    RelationTreeWithoutTuple = merklet:delete(TupleHash, Relation#relation.tree),
    {_,NewRelationTreeHash,_,_} = RelationTreeWithoutTuple,
    NewRelationHash = hash({Relation#relation.name, Relation#relation.schema, NewRelationTreeHash}),
    DatabaseTreeWithoutTuple = merklet:insert({atom_to_binary(Relation#relation.name, utf8), NewRelationHash}, Database#database_state.tree),
    {_,NewDatabaseHash,_,_} = DatabaseTreeWithoutTuple,
    NewRelations = maps:put(Relation#relation.name, NewRelationHash, Database#database_state.relations),
    UpdatedDatabase =
	Database#database_state{timestamp = erlang:timestamp(),
				hash = NewDatabaseHash,
			        tree = DatabaseTreeWithoutTuple,
			        relations = NewRelations},
    UpdatedRelation =
	Relation#relation{hash = NewRelationHash,
			  tree = RelationTreeWithoutTuple},
    TX = fun () ->
		 mnesia:write(relation, UpdatedRelation, write),
		 mnesia:write(database_state, UpdatedDatabase, write),
		 {UpdatedDatabase, UpdatedRelation}
	 end,
    case Transact of
	true -> mnesia:transaction(TX);
	false -> {UpdatedDatabase, UpdatedRelation}
    end.

%% @doc Remove a relation from the database.
%%
%% Wrapper for `retract_relation/3' with transactions enabled.
%%
%% @param Database `#database_state{}' record
%% @param Name Atom naming the relation to remove
%% @returns `{atomic, UpdatedDatabase}' or `{aborted, Reason}'
%%
%% @see retract_relation/3
%% @see clear_relation/2
retract_relation(Database, Name) ->
    retract_relation(Database, Name, true).

%% @doc Remove a relation from the database with optional transaction control.
%%
%% First clears all tuples from the relation, then removes the relation entirely
%% from the database's relation map and merkle tree.
%%
%% @param Database `#database_state{}' record
%% @param Name Atom naming the relation to remove
%% @param Transact Boolean - if `true', wraps operation in Mnesia transaction
%% @returns When `Transact =:= true': `{atomic, UpdatedDatabase}'
%%          When `Transact =:= false': `UpdatedDatabase'
%%
%% @see create_relation/3
retract_relation(Database, Name, Transact) ->
    RelationHash = maps:get(Name, Database#database_state.relations, {error, retract_relation_could_not_find}),
    {DatabaseWithClearedRelation, _ClearedRelation} = clear_relation(Database, RelationHash, false),
    NewTree = merklet:delete(atom_to_binary(Name, utf8), DatabaseWithClearedRelation#database_state.tree),
    NewHash = case NewTree of
                  undefined -> <<>>;
                  {_,H,_,_} -> H
              end,
    UpdatedDatabase =
	DatabaseWithClearedRelation#database_state{relations = maps:remove(Name, DatabaseWithClearedRelation#database_state.relations),
						   hash = NewHash,
						   tree = NewTree,
						   timestamp = erlang:timestamp()},
    TX = fun () ->
		 mnesia:write(database_state, UpdatedDatabase, write),
		 UpdatedDatabase
	 end,
    case Transact of
	true -> mnesia:transaction(TX);
	false -> UpdatedDatabase
    end.

%% @doc Create a new relation in the database with a schema definition.
%%
%% Creates an empty relation (no tuples) with the specified schema. The schema
%% is stored but not yet enforced - it serves as documentation and provides a
%% foundation for future domain validation.
%%
%% <b>Important:</b> The relation's identity is tied to its name. Two relations
%% with identical schemas but different names are semantically distinct and can
%% contain different tuples.
%%
%% == Example ==
%% <pre>
%% DB = operations:create_database(my_db),
%% Schema = #{name => string, age => integer, email => string},
%% {DB1, _Rel} = operations:create_relation(DB, users, Schema).
%% </pre>
%%
%% @param Database `#database_state{}' record
%% @param Name Atom naming the new relation
%% @param Definition Map specifying schema (e.g., `#{name => string, age => integer}')
%% @returns `{UpdatedDatabase, NewRelation}' tuple
%%
%% @see create_database/1
%% @see create_tuple/3
%% @see retract_relation/2
create_relation(Database, Name, Definition) ->
    F = fun() ->
        %% Create relation with empty tree
        %% The meaning of a relation needs to be attached to its name
        %% It may sound nominalistic but that is a physical concern.
        %% Two relations might share definitions but their name gives another interpretation
        %% and therefore different tuples
        RelationHash = hash({Name, Definition, undefined}),

        %% Returns a generator function (not a PID) that yields tuples one at a time
        %% TODO: investigate if a generator should or should not be bound to a specific DATABASE VERSION. If so, create the fun passing the DB as an attribute
        GeneratorFun = fun() ->
            [CurrentRelation] = mnesia:dirty_index_read(relation, Name, #relation.name),
            CurrentHash = CurrentRelation#relation.hash,
            TupleHashes = case CurrentRelation#relation.tree of
                undefined -> [];
                _ -> hashes_from_tuple(CurrentHash)
            end,
            InnerGen = make_finite_generator(TupleHashes),
            spawn(fun() -> generator_iterator_loop(InnerGen) end)
        end,

        %% Build membership criteria from schema (1OPiC declarations)
        MembershipCriteria = constraint:build_membership_criteria(Database, Definition),

        NewRelation = #relation{
            hash = RelationHash,
            name = Name,
            tree = undefined,
            schema = Definition,
            constraints = constraint:empty_constraints(),  % No constraints by default
            cardinality = {finite, 0},      % Empty relation
            generator = GeneratorFun,       % Function generator for finite relations
            membership_criteria = MembershipCriteria,  % Domain membership from schema
            provenance = build_base_provenance(Definition, Name), % Base relation provenance
            lineage = {base, Name}          % Base relation lineage
        },
        mnesia:write(relation, NewRelation, write),
        
        %% Update database relations map
        %% TODO: maybe we can purge this later after including the name on the merkle trees
        NewRelations = maps:put(Name, RelationHash, Database#database_state.relations),
        
        %% Update database tree (for diffing)
	%% Note that the name here is important as a key because
        %% we gotta go from a name to a hash within a database state
        NewTree = merklet:insert({atom_to_binary(Name), RelationHash}, Database#database_state.tree),
        {_,NewHash,_,_} = NewTree,
        
        UpdatedDatabase = #database_state{
            name = Database#database_state.name,
            hash = NewHash,
            tree = NewTree,
            relations = NewRelations,
            timestamp = erlang:timestamp()
        },
        mnesia:write(database_state, UpdatedDatabase, write),
        
        {UpdatedDatabase, NewRelation}
    end,
    {atomic, Result} = mnesia:transaction(F),
    Result.

%% @doc Update a relation's constraints.
%%
%% This function updates the constraints on an existing relation and returns
%% an updated database state with the new relation version. The merklet tree
%% automatically replaces the old relation hash with the new one.
%%
%% == Example ==
%% <pre>
%% %% Create constraints
%% AgeConstraint = constraint:create_1op(age, integer, [constraint:gte(18), constraint:lt(70)]),
%% SalaryConstraint = constraint:create_1op(salary, integer, [constraint:between(30000, 200000)]),
%% C1 = constraint:add_attribute_constraint(undefined, age, AgeConstraint),
%% C2 = constraint:add_attribute_constraint(C1, salary, SalaryConstraint),
%%
%% %% Update relation with constraints
%% {DB2, UpdatedRel} = operations:update_relation_constraints(DB1, employees, C2).
%% </pre>
%%
%% @param Database Current database state
%% @param RelationName Name of the relation to update
%% @param Constraints The #relation_constraints{} record to set
%% @returns {UpdatedDatabase, UpdatedRelation}
update_relation_constraints(Database, RelationName, Constraints)
  when is_record(Database, database_state),
       is_atom(RelationName),
       is_record(Constraints, relation_constraints) ->
    F = fun() ->
        %% Get current relation hash from database state
        CurrentRelationHash = maps:get(RelationName, Database#database_state.relations),

        %% Read current relation from mnesia
        case mnesia:read(relation, CurrentRelationHash) of
            [] ->
                %% Relation not found, might be using wrong database version
                mnesia:abort({relation_not_found, RelationName, CurrentRelationHash});
            [CurrentRelation] ->
                update_relation_with_constraints(CurrentRelation, RelationName, Constraints, Database)
        end
    end,
    {atomic, Result} = mnesia:transaction(F),
    Result.

%% @private
update_relation_with_constraints(CurrentRelation, RelationName, Constraints, Database) ->
    %% Create updated relation with new constraints
    %% The hash changes because constraints are part of the relation definition
    NewRelationHash = hash({CurrentRelation#relation.name,
                            CurrentRelation#relation.schema,
                            Constraints}),

    UpdatedRelation = CurrentRelation#relation{
        hash = NewRelationHash,
        constraints = Constraints
    },

    %% Write updated relation
    mnesia:write(relation, UpdatedRelation, write),

    %% Update database relations map
    NewRelations = maps:put(RelationName, NewRelationHash, Database#database_state.relations),

    %% Update database tree (merklet:insert automatically replaces old value)
    NewTree = merklet:insert({atom_to_binary(RelationName), NewRelationHash},
                             Database#database_state.tree),
    {_, NewHash, _, _} = NewTree,

    UpdatedDatabase = #database_state{
        name = Database#database_state.name,
        hash = NewHash,
        tree = NewTree,
        relations = NewRelations,
        timestamp = erlang:timestamp()
    },
    mnesia:write(database_state, UpdatedDatabase, write),

    {UpdatedDatabase, UpdatedRelation}.

%% @doc Create a new database state.
%%
%% Initializes an empty database with no relations. The database starts with:
%% <ul>
%%   <li>Empty relation map</li>
%%   <li>Undefined merkle tree (will be created when first relation is added)</li>
%%   <li>Empty hash</li>
%%   <li>Timestamp of creation</li>
%% </ul>
%%
%% All changes are committed to Mnesia within a transaction.
%%
%% == Example ==
%% <pre>
%% DB = operations:create_database(my_app).
%% </pre>
%%
%% @param Name Atom identifying the database
%% @returns `#database_state{}' record
%%
%% @see setup/0
%% @see create_relation/3
create_database(Name) ->
    DB = #database_state{
	    name = Name,
	    hash = <<>>,
	    tree = undefined,
	    relations = #{},
	    timestamp = erlang:timestamp()
	   },
    %% Standard immutable relations (domains and their predicates)
    %% TODO: Since 1OP constraints were added, the constraints passed to the base models now must be supported.
    %% The idea is to have the generators be able to produce the constrained subsets
    StandardTypeRelations = [
     #domain{
	name = integer,
	schema = #{value => integer},
	generator = fun() -> spawn(fun() -> generator_iterator_loop(generators:integer(#{})) end) end,
	membership_criteria = #{test => fun(#{value := V}) -> is_integer(V) end},
	cardinality = aleph_zero
       },
     #domain{
	name = natural,
	schema = #{value => natural},
	generator = fun() -> spawn(fun() -> generator_iterator_loop(generators:natural(#{})) end) end,
	membership_criteria = #{test => fun(#{value := V}) -> is_integer(V) andalso V >= 0 end},
	cardinality = aleph_zero
       },
     #domain{
	name = rational,
	schema = #{numerator => integer, denominator => integer},
	generator = fun() -> spawn(fun() -> generator_iterator_loop(generators:rational(#{})) end) end,
	membership_criteria = #{test => fun(#{numerator := N, denominator := D}) ->
						is_integer(N) andalso is_integer(D) andalso D /= 0
					end
			       },
	cardinality = aleph_zero
       },
     #domain{
	name = string,
	schema = #{value => string},
	generator = undefined,
	membership_criteria = #{test => fun(#{value := V}) -> is_list(V) orelse is_binary(V) end},
	cardinality = aleph_zero
       },
     #domain{
	name = atom,
	schema = #{value => atom},
	generator = undefined,
	membership_criteria = #{test => fun(#{value := V}) -> is_atom(V) end},
	cardinality = aleph_zero
       },
      #domain{
	name = term,
	schema = #{value => term},
	generator = undefined,
	membership_criteria = #{test => fun(_) -> true end},
	cardinality = aleph_zero
       },
      constraint:less_than(term, aleph_zero),
      constraint:less_than_or_equal(term, aleph_zero),
      constraint:greater_than(term, aleph_zero),
      constraint:greater_than_or_equal(term, aleph_zero),
      constraint:equal(term, aleph_zero),
      constraint:not_equal(term, aleph_zero)
    ],
    Folder = fun (Elem, AccDB) ->
		     {NextDB, _} = operations:create_immutable_relation(AccDB, Elem),
		     NextDB
	     end,
    StandardDB = lists:foldl(Folder, DB, StandardTypeRelations),
    {atomic, ok} = mnesia:transaction(fun () ->
					      mnesia:write(database_state, StandardDB, write)
				      end),
    StandardDB.

%% @doc Create an infinite relation with a generator.
%%
%% Creates a relation with infinite cardinality (aleph_zero or continuum).
%% The relation is defined by a generator function that produces tuples
%% lazily based on constraints.
%%
%% == Primitive Infinite Relations ==
%%
%% <ul>
%%   <li>`naturals' - Natural numbers: {0, 1, 2, 3, ...}</li>
%%   <li>`integers' - Integers: {0, 1, -1, 2, -2, ...}</li>
%%   <li>`rationals' - Rationals via Stern-Brocot tree</li>
%% </ul>
%%
%% == Example ==
%% <pre>
%% %% Create immutable relation of natural numbers
%% {DB1, Naturals} = operations:create_immutable_relation(DB, #domain{
%%     name = naturals,
%%     schema = #{value => naturals},
%%     generator = {generators, naturals},
%%     membership_criteria = #{value => {'and', {gte, 0}, is_integer}},
%%     cardinality = aleph_zero
%% }).
%%
%% %% Query with bounds
%% Iterator = operations:get_tuples_iterator(DB1, naturals,
%%     #{value => {range, 0, 100}}).
%% </pre>
%%
%% @param Database `#database_state{}' record
%% @param Specification `#domain{}' record
%% @returns `{UpdatedDatabase, NewRelation}'
%%
%% @see create_relation/3
%% @see get_tuples_iterator/3
create_immutable_relation(Database, Specification)
  when is_record(Database, database_state)
       andalso is_record(Specification, domain) ->
    Name = Specification#domain.name,
    Schema = Specification#domain.schema,
    Generator = Specification#domain.generator,
    MembershipCriteria = Specification#domain.membership_criteria,
    Cardinality = Specification#domain.cardinality,
    F = fun() ->
        RelationHash = hash({Name, Schema, undefined}),
        NewRelation = #relation{
            hash = RelationHash,
            name = Name,
            tree = undefined, % Immutable 'relations' have no merkle tree
            schema = Schema,
            constraints = constraint:empty_constraints(), % TODO: No constraints unless by domain extension
            cardinality = Cardinality,
            generator = Generator,
            membership_criteria = MembershipCriteria,
            provenance = build_base_provenance(Schema, Name), % Base relation provenance
            lineage = {base, Name} % Base relation lineage
        },
        mnesia:write(relation, NewRelation, write),
        NewRelations = maps:put(Name, RelationHash, Database#database_state.relations),
        NewTree = merklet:insert({atom_to_binary(Name), RelationHash}, Database#database_state.tree),
        {_,NewHash,_,_} = NewTree,
        UpdatedDatabase = #database_state{
            name = Database#database_state.name,
            hash = NewHash,
            tree = NewTree,
            relations = NewRelations,
            timestamp = erlang:timestamp()
        },
        mnesia:write(database_state, UpdatedDatabase, write),
        {UpdatedDatabase, NewRelation}
    end,
    {atomic, Result} = mnesia:transaction(F),
    Result;

create_immutable_relation(Database, Specification)
  when is_record(Database, database_state)
       andalso is_record(Specification, relation) ->
    F = fun() ->
        RelationHash = hash({Specification#relation.name, Specification#relation.schema, undefined}),
        UpdatedRelation = Specification#relation{hash = RelationHash},
        mnesia:write(relation, UpdatedRelation, write),
        NewRelations = maps:put(Specification#relation.name, RelationHash, Database#database_state.relations),
        NewTree = merklet:insert({atom_to_binary(Specification#relation.name), RelationHash}, Database#database_state.tree),
        {_,NewHash,_,_} = NewTree,
        UpdatedDatabase = #database_state{
            name = Database#database_state.name,
            hash = NewHash,
            tree = NewTree,
            relations = NewRelations,
            timestamp = erlang:timestamp()
        },
        mnesia:write(database_state, UpdatedDatabase, write),

        {UpdatedDatabase, Specification}
    end,
    {atomic, Result} = mnesia:transaction(F),
    Result.

%% @doc Get relation hash by name.
%%
%% Looks up a relation's hash in the database's relation map. This hash
%% can be used to retrieve the full relation record from Mnesia.
%%
%% @param Database `#database_state{}' record
%% @param RelationName Atom naming the relation
%% @returns `{ok, Hash}' on success, `{error, not_found}' if relation doesn't exist
%%
%% @see get_relations/1
%% @see hashes_from_tuple/1
get_relation_hash(Database, RelationName) ->
    case maps:find(RelationName, Database#database_state.relations) of
        error -> {error, not_found};
        X -> X
    end.

%% @doc Get list of all relation names in the database.
%%
%% Returns a list of all relation names (atoms) currently stored in the database.
%% This includes both user-defined relations and built-in infinite relations.
%%
%% @param Database `#database_state{}' record
%% @returns List of relation names (atoms)
-spec get_relations(#database_state{}) -> [atom()].
get_relations(Database) ->
    maps:keys(Database#database_state.relations).

%% @doc Resolve a tuple hash to actual values.
%%
%% This is an internal function that materializes a tuple by:
%% <ol>
%%   <li>Reading the tuple record from Mnesia (contains attribute_map)</li>
%%   <li>For each attribute, reading the actual value using its hash</li>
%%   <li>Building a map with attribute names and materialized values</li>
%% </ol>
%%
%% Uses dirty reads since all data is immutable.
%%
%% @param TupleHash Binary hash identifying the tuple
%% @returns Map with attribute names and actual values (e.g., `#{name => "Alice", age => 30}')
%%
%% @see get_tuples_iterator/2
%% @see create_tuple/3
resolve_tuple(TupleHash) ->
    [#tuple{attribute_map = AttributeMap}] = mnesia:dirty_read(tuple, TupleHash),
    maps:map(fun(_AttrName, ValueHash) ->
        [#attribute{value = Value}] = mnesia:dirty_read(attribute, ValueHash),
        Value
    end, AttributeMap).

%% @doc Extract all tuple hashes from a relation.
%%
%% Retrieves the list of all tuple hashes stored in the relation's merkle tree.
%% Returns an empty list if the relation has no tuples (tree is `undefined').
%%
%% Uses dirty reads since the relation data is immutable.
%%
%% @param RelationHash Binary hash identifying the relation
%% @returns List of binary tuple hashes, or empty list `[]' if relation is empty
%%
%% @see get_tuples_iterator/2
%% @see create_tuple/3
hashes_from_tuple(RelationHash) ->
    [Relation] = mnesia:dirty_read(relation, RelationHash),
    case Relation#relation.tree of
        undefined -> [];
        Tree -> merklet:keys(Tree)
    end.

%% @doc Create an iterator for streaming tuples from a relation with constraints.
%%
%% Extended version of get_tuples_iterator/2 that accepts boundedness constraints.
%% For infinite relations, constraints are mandatory to ensure termination.
%%
%% <b>All tuples include metadata tracking</b> with a nested `meta' field containing:
%% <ul>
%%   <li>`provenance' - Tracks immediate source of each attribute</li>
%%   <li>`lineage' - Tracks complete operational history</li>
%% </ul>
%%
%% == Options Format ==
%%
%% Nested format (preferred):
%% <pre>
%% #{constraints => #{age => {gt, 30}}}
%% </pre>
%%
%% Flat format (backward compatible):
%% <pre>
%% #{age => {gt, 30}}
%% </pre>
%%
%% == Constraints ==
%%
%% Constraints bound the iteration over infinite relations:
%% <ul>
%%   <li>`{range, Min, Max}' - Range constraint</li>
%%   <li>`{eq, Value}' - Equality constraint</li>
%%   <li>`{lt, Value}', `{lte, Value}' - Upper bounds</li>
%%   <li>`{gt, Value}', `{gte, Value}' - Lower bounds</li>
%%   <li>`{in, List}' - Membership constraint</li>
%% </ul>
%%
%% == Example ==
%% <pre>
%% %% Iterate employees with age > 30 (metadata always included)
%% Iterator = operations:get_tuples_iterator(DB, employees,
%%     #{constraints => #{age => {gt, 30}}}).
%%
%% %% Results will have:
%% %% #{id => 1, name => "Alice", age => 35,
%% %%   meta => #{
%% %%     provenance => #{id => {employees, id}, ...},
%% %%     lineage => {select, Fun, {base, employees}}
%% %%   }}
%% </pre>
%%
%% @param Database `#database_state{}' record
%% @param RelationName Atom naming the relation to iterate
%% @returns Pid of iterator process
%%
%% @see get_tuples_iterator/2
%% @see take/3
get_tuples_iterator(Database, RelationName) when is_record(Database, database_state) ->
    case maps:get(RelationName, Database#database_state.relations, error) of
        error ->
            erlang:error({error_tuple_iterator_init, relation_not_found, RelationName});
        RelationHash ->
            [Relation] = mnesia:dirty_read(relation, RelationHash),
            case Relation#relation.cardinality of
                {finite, _} when Relation#relation.tree =/= undefined ->
                    TupleHashes = hashes_from_tuple(RelationHash),
                    spawn(fun() -> tuple_iterator_loop(TupleHashes) end);
                _ ->
                    get_iterator_from_generator(Relation#relation.name, Relation#relation.generator)
            end
    end.

%% @doc Get an iterator out of a generator.
get_iterator_from_generator(RelationName, GeneratorFun) ->
    case GeneratorFun of
	undefined -> spawn(fun() -> tuple_iterator_loop([]) end);
	Generator when is_function(Generator) -> Generator();
        _ -> erlang:error({error_tuple_iterator_init, non_function_generator, RelationName})
    end.

%% @doc Get next tuple from iterator.
%%
%% Sends a `{next, self()}' message to the iterator process and waits for
%% the response. The iterator will materialize the tuple on demand.
%%
%% Timeout is set to 5 seconds to prevent indefinite blocking.
%%
%% @param IteratorPid Pid returned from `get_tuples_iterator/2'
%% @returns `{ok, Tuple}' with materialized tuple map,
%%          `done' when no more tuples remain, or
%%          `{error, timeout}' if iterator doesn't respond
%%
%% @see get_tuples_iterator/2
%% @see close_iterator/1
next_tuple(IteratorPid) ->
    IteratorPid ! {next, self()},
    receive
        {tuple, Tuple} -> {ok, Tuple};
        done -> done;
        {error, Reason} -> {error, Reason}
    after 5000 ->
        {error, timeout}
    end.

%% @doc Close iterator process.
%%
%% Sends a `stop' message to terminate the iterator process. This should be
%% called when you're done consuming tuples to avoid leaving processes running.
%%
%% @param IteratorPid Pid returned from `get_tuples_iterator/2'
%% @returns `ok'
%%
%% @see get_tuples_iterator/2
%% @see collect_all/1
close_iterator(IteratorPid) ->
    IteratorPid ! stop,
    ok.

%% @private
%% Iterator loop implementation (internal).
%% This is the process loop for the Volcano Iterator Model. It maintains a list
%% of tuple hashes and resolves them lazily when requested.
%%
%% Protocol:
%% <ul>
%%   <li>`{next, Caller}' - Resolve next tuple and send `{tuple, ResolvedTuple}' or `done'</li>
%%   <li>`stop' - Terminate the process</li>
%% </ul>
%%
%% @param TupleHashes List of binary tuple hashes to stream
tuple_iterator_loop([]) ->
    receive
        {next, Caller} ->
            Caller ! done;
        stop ->
            ok
    end;
tuple_iterator_loop([TupleHash | Rest]) ->
    receive
        {next, Caller} ->
            %% Resolve tuple on demand
            ResolvedTuple = resolve_tuple(TupleHash),
            Caller ! {tuple, ResolvedTuple},
            tuple_iterator_loop(Rest);
        stop ->
            ok
    end.

%% @doc Helper to collect all tuples from an iterator into a list.
%%
%% This is a convenience function primarily used for testing. It exhausts the
%% iterator by repeatedly calling `next_tuple/1' until `done' is received,
%% accumulating all tuples into a list.
%%
%% <b>Warning:</b> This materializes all tuples in memory, defeating the purpose
%% of lazy evaluation. Only use for small relations or testing.
%%
%% == Example ==
%% <pre>
%% Iterator = operations:get_tuples_iterator(DB, users),
%% AllTuples = operations:collect_all(Iterator).
%% %% Iterator is automatically closed
%% </pre>
%%
%% @param IteratorPid Pid returned from `get_tuples_iterator/2'
%% @returns List of tuple maps, `{error, Reason, PartialResults}' on timeout
%%
%% @see get_tuples_iterator/2
%% @see next_tuple/1
collect_all(IteratorPid) ->
    collect_all(IteratorPid, []).

collect_all(IteratorPid, Acc) ->
    case next_tuple(IteratorPid) of
        {ok, Tuple} -> collect_all(IteratorPid, [Tuple | Acc]);
        done ->
            close_iterator(IteratorPid),
            lists:reverse(Acc);  % Preserve order
        {error, Reason} ->
            close_iterator(IteratorPid),
            {error, Reason, lists:reverse(Acc)}
    end.

%% @doc Select iterator: Filters tuples based on a predicate.
%%
%% Lazy operator that wraps a child iterator and only emits tuples that
%% satisfy the predicate function.
%%
%% == Example ==
%% <pre>
%% BaseIter = get_tuples_iterator(DB, employees, #{}),
%% FilteredIter = select_iterator(BaseIter, fun(E) -&gt; maps:get(age, E) &gt; 30 end),
%% Tuples = collect_all(FilteredIter).
%% </pre>
%%
%% @param ChildIterator Iterator process to filter
%% @param Predicate Function: tuple() -> boolean()
%% @returns Iterator process (Pid)
select_iterator(ChildIterator, Predicate) when is_function(Predicate, 1) ->
    spawn(fun() -> select_loop(ChildIterator, Predicate) end).

select_loop(ChildPid, Predicate) ->
    receive
        {next, Caller} ->
            case next_tuple(ChildPid) of
                {ok, Tuple} ->
                    case Predicate(Tuple) of
                        true ->
                            Caller ! {tuple, Tuple},
                            select_loop(ChildPid, Predicate);
                        false ->
                            self() ! {next, Caller},
                            select_loop(ChildPid, Predicate)
                    end;
                done ->
                    close_iterator(ChildPid),
                    Caller ! done;
                {error, Reason} ->
                    close_iterator(ChildPid),
                    Caller ! {error, Reason}
            end;
        stop ->
            close_iterator(ChildPid),
            ok
    end.

%% @doc Project iterator: Projects tuples onto specified attributes.
%%
%% Lazy operator that transforms tuples by keeping only the specified
%% attributes. This is the relational projection operator (π).
%%
%% == Example ==
%% <pre>
%% BaseIter = get_tuples_iterator(DB, employees, #{}),
%% ProjectedIter = project_iterator(BaseIter, [name, age]),
%% Tuples = collect_all(ProjectedIter).
%% </pre>
%%
%% @param ChildIterator Iterator process to project
%% @param Attributes List of attribute names to keep
%% @returns Iterator process (Pid)
project_iterator(ChildIterator, Attributes) when is_list(Attributes) ->
    spawn(fun() -> project_loop(ChildIterator, Attributes) end).

project_loop(ChildPid, Attributes) ->
    receive
        {next, Caller} ->
            case next_tuple(ChildPid) of
                {ok, Tuple} ->
                    % Project data attributes only - no metadata handling
                    ProjectedTuple = maps:with(Attributes, Tuple),
                    Caller ! {tuple, ProjectedTuple},
                    project_loop(ChildPid, Attributes);
                done ->
                    close_iterator(ChildPid),
                    Caller ! done;
                {error, Reason} ->
                    close_iterator(ChildPid),
                    Caller ! {error, Reason}
            end;
        stop ->
            close_iterator(ChildPid),
            ok
    end.

%% @doc Sort iterator: Sorts all tuples from child iterator.
%%
%% <b>Blocking operator</b> - must consume ALL tuples from child before
%% emitting any output. Materializes and sorts tuples in memory.
%%
%% <b>Warning:</b> Do not use on unbounded streams! Ensure child iterator
%% produces finite tuples (via constraints or take).
%%
%% == Example ==
%% <pre>
%% BaseIter = get_tuples_iterator(DB, employees, #{}),
%% SortedIter = sort_iterator(BaseIter, fun(A, B) -&gt;
%%     maps:get(age, A) =&lt; maps:get(age, B)
%% end),
%% Tuples = collect_all(SortedIter).
%% </pre>
%%
%% @param ChildIterator Iterator process to sort
%% @param CompareFun Function: (tuple(), tuple()) -> boolean()
%% @returns Iterator process (Pid)
sort_iterator(ChildIterator, CompareFun) when is_function(CompareFun, 2) ->
    spawn(fun() ->
        %% Sync all state blocking the pipeline
        AllTuples = collect_all(ChildIterator),
        %% Sort in memory
        SortedTuples = lists:sort(CompareFun, AllTuples),
        sorted_emit_loop(SortedTuples)
    end).

sorted_emit_loop([]) ->
    receive
        {next, Caller} -> Caller ! done;
        stop -> ok
    end;
sorted_emit_loop([Tuple | Rest]) ->
    receive
        {next, Caller} ->
            Caller ! {tuple, Tuple},
            sorted_emit_loop(Rest);
        stop -> ok
    end.

%% @doc Take iterator: Limits output to N tuples.
%%
%% Lazy operator that emits at most N tuples from the child iterator,
%% then closes the child and signals done.
%%
%% == Example ==
%% <pre>
%% BaseIter = get_tuples_iterator(DB, naturals, #{value => {range, 0, 1000}}),
%% LimitedIter = take_iterator(BaseIter, 10),
%% Tuples = collect_all(LimitedIter).  % Gets exactly 10 tuples
%% </pre>
%%
%% @param ChildIterator Iterator process to limit
%% @param N Maximum number of tuples to emit
%% @returns Iterator process (Pid)
take_iterator(ChildIterator, N) when is_integer(N), N > 0 ->
    spawn(fun() -> take_iter_loop(ChildIterator, N, 0, N) end).

take_iter_loop(ChildPid, Limit, Count, OriginalN) when Count < Limit ->
    receive
        {next, Caller} ->
            case next_tuple(ChildPid) of
                {ok, Tuple} ->
                    %% Pass through tuple without metadata injection  
                    Caller ! {tuple, Tuple},
                    take_iter_loop(ChildPid, Limit, Count + 1, OriginalN);
                done ->
                    close_iterator(ChildPid),
                    Caller ! done;
                {error, Reason} ->
                    close_iterator(ChildPid),
                    Caller ! {error, Reason}
            end;
        stop ->
            close_iterator(ChildPid),
            ok
    end;
take_iter_loop(ChildPid, _Limit, _Count, _OriginalN) ->
    %% Reached limit - close child and signal done
    close_iterator(ChildPid),
    receive
        {next, Caller} -> Caller ! done;
        stop -> ok
    end.

%%% ============================================================================
%%% Join Operators
%%% ============================================================================

%% @doc Equijoin iterator: Natural join on matching attribute values.
%%
%% Performs an equijoin between two relations on a common attribute.
%% For each tuple from the left iterator, finds all matching tuples from
%% the right iterator where the specified attributes are equal.
%%
%% The result tuples merge attributes from both sides. If attribute names
%% conflict, right side attributes are prefixed with "right_".
%%
%% Implementation: Nested loop join (simple but works for small-medium datasets).
%% Future: Hash join for better performance.
%%
%% == Example ==
%% <pre>
%% %% Join employees with departments on dept_id
%% LeftIter = operations:get_tuples_iterator(DB, employees, #{}),
%% RightIter = operations:get_tuples_iterator(DB, departments, #{}),
%% JoinIter = operations:equijoin_iterator(LeftIter, RightIter, dept_id)
%% </pre>
%%
%% @param LeftIterator Iterator for left relation
%% @param RightIterator Iterator for right relation
%% @param JoinAttribute Attribute name to join on (must exist in both relations)
%% @returns Iterator process that emits joined tuples
equijoin_iterator(LeftIterator, RightIterator, JoinAttribute) ->
    spawn(fun() -> equijoin_loop(LeftIterator, RightIterator, JoinAttribute) end).

%% @doc Theta-join iterator: Join with arbitrary predicate.
%%
%% Performs a theta-join between two relations using a user-provided
%% predicate function. For each pair of tuples (left, right), the predicate
%% is evaluated. If true, the tuples are merged and emitted.
%%
%% == Example ==
%% <pre>
%% %% Join where employee.age &gt; department.min_age
%% Predicate = fun(EmpTuple, DeptTuple) -&gt;
%%     maps:get(age, EmpTuple) &gt; maps:get(min_age, DeptTuple)
%% end,
%% JoinIter = operations:theta_join_iterator(LeftIter, RightIter, Predicate)
%% </pre>
%%
%% @param LeftIterator Iterator for left relation
%% @param RightIterator Iterator for right relation
%% @param Predicate Function `fun(LeftTuple, RightTuple) -> boolean()'
%% @returns Iterator process that emits joined tuples
theta_join_iterator(LeftIterator, RightIterator, Predicate) ->
    spawn(fun() -> theta_join_loop(LeftIterator, RightIterator, Predicate) end).

%%% ============================================================================
%%% Materialization (Eager - Relation Creation)
%%% ============================================================================

%% @doc Materialize an iterator pipeline into a new relation.
%%
%% This is the boundary between lazy evaluation (iterator pipeline) and
%% eager materialization (stored relation). Consumes all tuples from the
%% iterator and stores them as a new finite relation in the database.
%%
%% == Example ==
%% <pre>
%% %% Build lazy pipeline
%% Pipeline = get_tuples_iterator(DB, employees, #{})
%%            |&gt; select_iterator(fun(E) -&gt; maps:get(age, E) &gt; 30 end)
%%            |&gt; sort_iterator(fun(A, B) -&gt; maps:get(age, A) =&lt; maps:get(age, B) end)
%%            |&gt; take_iterator(10)
%%            |&gt; project_iterator([name, age]),
%%
%% %% Materialize into new relation
%% {DB1, SeniorEmployees} = materialize(DB, Pipeline, senior_employees).
%% </pre>
%%
%% @param Database Current database state
%% @param SourceIterator Iterator process (pipeline endpoint)
%% @param ResultName Atom naming the new relation
%% @returns `{UpdatedDatabase, NewRelation}' tuple
materialize(Database, SourceIterator, ResultName) when is_record(Database, database_state) ->
    %% 1. Pull all tuples from pipeline
    AllTuples = collect_all(SourceIterator),

    case AllTuples of
        [] ->
            %% Empty result - create empty relation
            Schema = #{},  % Empty schema
            create_relation(Database, ResultName, Schema);

        [FirstTuple | _] ->
            %% 2. Infer schema from first tuple
            Schema = infer_schema_from_tuple(FirstTuple),

            %% 3. Create new relation
            {DB1, _Relation} = create_relation(Database, ResultName, Schema),

            %% 4. Insert all tuples
            {FinalDB, FinalRelation} = create_all_tuples(DB1, ResultName, AllTuples),

            {FinalDB, FinalRelation}
    end.

%% @private
%% Infer schema from a tuple.
infer_schema_from_tuple(Tuple) when is_map(Tuple) ->
    maps:map(fun(_AttrName, Value) -> infer_type(Value) end, Tuple).

%% @private
%% Infer type from value.
infer_type(Value) when is_integer(Value) -> integer;
infer_type(Value) when is_float(Value) -> float;
infer_type(Value) when is_binary(Value) -> binary;
infer_type(Value) when is_list(Value) -> string;
infer_type(Value) when is_atom(Value) -> atom;
infer_type(Value) when is_boolean(Value) -> boolean;
infer_type(_) -> term.

%% @private
%% Insert multiple tuples into a relation.
create_all_tuples(DB, RelationName, Tuples) ->
    lists:foldl(
        fun(Tuple, {DBacc, _RelAcc}) ->
            create_tuple(DBacc, RelationName, Tuple)
        end,
        {DB, undefined},
        Tuples
    ).

%%% ============================================================================
%%% Generator Support Functions
%%% ============================================================================


%% @private
%% Generator iterator loop for infinite relations.
%% Similar to tuple_iterator_loop but works with generator functions instead
%% of tuple hashes.
generator_iterator_loop(Generator) ->
    receive
        {next, Caller} ->
            case Generator(next) of
                done ->
                    Caller ! done;
                {value, Tuple, NextGenerator} ->
                    Caller ! {tuple, Tuple},
                    generator_iterator_loop(NextGenerator);
                {error, Reason} ->
                    Caller ! {error, Reason}
            end;
        stop ->
            ok
    end.

%% @private
%% Equijoin loop: nested loop join on attribute equality.
%% Strategy:
%% 1. Collect all tuples from right iterator into memory
%% 2. For each left tuple, scan right tuples for matches
%% 3. Emit merged tuple when join attribute values match
%%
%% @param LeftIter Left iterator process
%% @param RightIter Right iterator process
%% @param JoinAttr Attribute to join on
equijoin_loop(LeftIter, RightIter, JoinAttr) ->
    % Materialize right side (for nested loop)
    RightTuples = collect_all(RightIter),
    equijoin_emit_loop(LeftIter, RightTuples, JoinAttr).

equijoin_emit_loop(LeftIter, RightTuples, JoinAttr) ->
    receive
        {next, Caller} ->
            case next_tuple(LeftIter) of
                done ->
                    Caller ! done,
                    ok;  % Stop after sending done
                {ok, LeftTuple} ->
                    % Find all right tuples that match on join attribute
                    case maps:get(JoinAttr, LeftTuple, undefined) of
                        undefined ->
                            % Left tuple doesn't have join attribute - try next
                            self() ! {next, Caller},
                            equijoin_emit_loop(LeftIter, RightTuples, JoinAttr);
                        LeftValue ->
                            % Find matching right tuples
                            Matches = [merge_tuples(LeftTuple, RightTuple)
                                      || RightTuple <- RightTuples,
                                         maps:get(JoinAttr, RightTuple, undefined) =:= LeftValue],
                            case Matches of
                                [] ->
                                    % No matches - try next left tuple
                                    self() ! {next, Caller},
                                    equijoin_emit_loop(LeftIter, RightTuples, JoinAttr);
                                [FirstMatch | RestMatches] ->
                                    % Emit first match, buffer rest
                                    Caller ! {tuple, FirstMatch},
                                    equijoin_emit_buffered(LeftIter, RightTuples, JoinAttr, RestMatches)
                            end
                    end;
                {error, Reason} ->
                    close_iterator(LeftIter),
                    Caller ! {error, Reason},
                    ok  % Stop after sending error
            end;
        stop ->
            close_iterator(LeftIter),
            ok
    end.

equijoin_emit_buffered(LeftIter, RightTuples, JoinAttr, [Match | Rest]) ->
    receive
        {next, Caller} ->
            Caller ! {tuple, Match},
            equijoin_emit_buffered(LeftIter, RightTuples, JoinAttr, Rest);
        stop ->
            close_iterator(LeftIter),
            ok
    end;
equijoin_emit_buffered(LeftIter, RightTuples, JoinAttr, []) ->
    % Buffer empty - continue with next left tuple
    equijoin_emit_loop(LeftIter, RightTuples, JoinAttr).

%% @private
%% Theta-join loop: nested loop join with predicate.
%% @param LeftIter Left iterator process
%% @param RightIter Right iterator process
%% @param Pred Predicate function fun(LeftTuple, RightTuple) -> boolean()
theta_join_loop(LeftIter, RightIter, Pred) ->
    % Materialize right side
    RightTuples = collect_all(RightIter),
    theta_join_emit_loop(LeftIter, RightTuples, Pred).

theta_join_emit_loop(LeftIter, RightTuples, Pred) ->
    receive
        {next, Caller} ->
            case next_tuple(LeftIter) of
                done ->
                    Caller ! done;
                {ok, LeftTuple} ->
                    % Find all right tuples that satisfy predicate
                    Matches = [merge_tuples(LeftTuple, RightTuple)
                              || RightTuple <- RightTuples,
                                 Pred(LeftTuple, RightTuple)],
                    case Matches of
                        [] ->
                            % No matches - continue
                            self() ! {next, Caller},
                            theta_join_emit_loop(LeftIter, RightTuples, Pred);
                        [FirstMatch | RestMatches] ->
                            Caller ! {tuple, FirstMatch},
                            theta_join_emit_buffered(LeftIter, RightTuples, Pred, RestMatches)
                    end;
                {error, Reason} ->
                    close_iterator(LeftIter),
                    Caller ! {error, Reason}
            end;
        stop ->
            close_iterator(LeftIter),
            ok
    end.

theta_join_emit_buffered(LeftIter, RightTuples, Pred, [Match | Rest]) ->
    receive
        {next, Caller} ->
            Caller ! {tuple, Match},
            theta_join_emit_buffered(LeftIter, RightTuples, Pred, Rest);
        stop ->
            close_iterator(LeftIter),
            ok
    end;
theta_join_emit_buffered(LeftIter, RightTuples, Pred, []) ->
    theta_join_emit_loop(LeftIter, RightTuples, Pred).

%% @private
%% @doc Simple tuple merge for joins
%% Equally named and typed attributes are not repeated.
%%
%% @param LeftTuple Left side tuple
%% @param RightTuple Right side tuple  
merge_tuples(LeftTuple, RightTuple) ->
    maps:fold(
        fun(RightKey, RightValue, LeftTupleAcc) ->
            case maps:get(RightKey, LeftTupleAcc, undefined) of
                LeftValue when LeftValue =:= RightValue ->
                    % Equal attributes, keep single value
                    LeftTupleAcc;
                undefined ->
                    % No conflict, add right attribute
                    maps:put(RightKey, RightValue, LeftTupleAcc);
                _ ->
                    % Different attributes
                    maps:put(RightKey, RightValue, LeftTupleAcc)
            end
        end,
        LeftTuple,
        RightTuple
    ).

%% @private
%% Build base provenance mapping for a relation.
%% Maps each attribute to its source relation and attribute name.
%%
%% @param Schema Map of attribute names to types
%% @param RelationName Name of the source relation
%% @returns Provenance map #{attr => {relation, attr}}
build_base_provenance(Schema, RelationName) ->
    maps:fold(
        fun(AttrName, _Type, Acc) ->
            maps:put(AttrName, {RelationName, AttrName}, Acc)
        end,
        #{},
        Schema
    ).

%% The issue here is that the creation of a generator on the create_relation is wrong.
%% I assume it was because of the fun(Constraints), which I believe should receive an
%% optional database version, or just build from the current relation attributes as a factory.

%% @private
%% @doc Create a generator function from a list of tuple hashes.
%%
%% Converts a list of tuple hashes into a generator function compatible
%% with the generator protocol used by infinite relations. The generator
%% reads tuples from Mnesia on demand.
%%
%% @param TupleHashes List of tuple hashes to iterate over
%% @returns Generator function that accepts 'next' and returns {value, Tuple, NextGen} or done
make_finite_generator([]) ->
    fun(_) -> done end;
make_finite_generator([Hash | Rest]) ->
    fun(next) ->
        ResolvedTuple = resolve_tuple(Hash),
        NextGen = make_finite_generator(Rest),
        {value, ResolvedTuple, NextGen}
    end.