aboutsummaryrefslogblamecommitdiffstats
path: root/test/provenance_test.erl
blob: c98e372ce72db9431b04c5599d46c7a6dbeda829 (plain) (tree)
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



































































































































































































































































































































































































































                                                                                                                         
-module(provenance_test).
-include_lib("eunit/include/eunit.hrl").
-include("operations.hrl").

%%% Provenance & Lineage Tracking Test Suite
%%%
%%% Tests for tuple metadata tracking in relational operations.
%%% Metadata is ALWAYS tracked and included in every tuple via the nested 'meta' field:
%%% - Provenance: Tracks the immediate source of each attribute
%%% - Lineage: Tracks the complete operational history of tuple creation
%%%
%%% NOTE ON TUPLE ORDERING:
%%% These tests observe tuples in a specific order (e.g., Alice before Bob) because
%%% collect_all/1 currently returns results in Merkle tree traversal order for finite
%%% relations. This ordering is an IMPLEMENTATION DETAIL and is NOT guaranteed.
%%% Relations are unordered sets - the current order is based on how tuples were
%%% inserted and hashed into the Merkle tree. Future implementations may return tuples
%%% in different orders. Tests use lists:all/2 to verify properties hold for ALL tuples
%%% rather than relying on specific ordering.

provenance_test_() ->
    {foreach,
     fun setup/0,
     fun cleanup/1,
     [
      fun test_base_provenance/1,
      fun test_join_provenance/1,
      fun test_join_with_conflicts_provenance/1,
      fun test_equijoin_preserves_both_sources/1,
      fun test_base_lineage/1,
      fun test_join_lineage/1,
      fun test_select_lineage/1,
      fun test_project_lineage/1,
      fun test_sort_lineage/1,
      fun test_take_lineage/1,
      fun test_multi_operation_pipeline_lineage/1,
      fun test_theta_join_lineage/1,
      fun test_complex_nested_lineage/1
     ]}.

%%% Setup and Cleanup

setup() ->
    operations:setup(),
    DB = operations:create_database(test_db),

    % Create employees relation
    {DB1, _} = operations:create_relation(DB, employees, #{
        id => integer,
        name => string,
        dept_id => integer
    }),

    {DB2, _} = operations:create_tuple(DB1, employees, #{id => 1, name => "Alice", dept_id => 10}),
    {DB3, _} = operations:create_tuple(DB2, employees, #{id => 2, name => "Bob", dept_id => 20}),

    % Create departments relation
    {DB4, _} = operations:create_relation(DB3, departments, #{
        dept_id => integer,
        dept_name => string,
        budget => integer
    }),

    {DB5, _} = operations:create_tuple(DB4, departments, #{dept_id => 10, dept_name => "Engineering", budget => 100000}),
    {DB6, _} = operations:create_tuple(DB5, departments, #{dept_id => 20, dept_name => "Sales", budget => 80000}),

    DB6.

cleanup(_DB) ->
    ok.

%%% Base Provenance Tests

test_base_provenance(DB) ->
    % Query tuples (metadata always included)
    Iter = operations:get_tuples_iterator(DB, employees, #{}),
    Results = operations:collect_all(Iter),

    % All tuples should have correct provenance structure
    [
     ?_assertEqual(2, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           maps:is_key(meta, T) andalso
                           begin
                               Meta = maps:get(meta, T),
                               maps:is_key(provenance, Meta)
                           end
                       end, Results)),
     % Check ALL tuples have correct provenance mapping
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Prov = maps:get(provenance, Meta),
                           % All attributes should map to {employees, attr_name}
                           maps:get(id, Prov) =:= {employees, id} andalso
                           maps:get(name, Prov) =:= {employees, name} andalso
                           maps:get(dept_id, Prov) =:= {employees, dept_id}
                       end, Results))
    ].

%%% Join Provenance Tests

test_join_provenance(DB) ->
    % Create iterators (metadata always included)
    EmpIter = operations:get_tuples_iterator(DB, employees, #{}),
    DeptIter = operations:get_tuples_iterator(DB, departments, #{}),

    % Join them
    JoinIter = operations:equijoin_iterator(EmpIter, DeptIter, dept_id),
    Results = operations:collect_all(JoinIter),

    % All results should have metadata with provenance
    [
     ?_assertEqual(2, length(Results)),
     ?_assert(lists:all(fun(T) -> maps:is_key(meta, T) end, Results)),
     % Check that ALL tuples have correct provenance structure
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Prov = maps:get(provenance, Meta),
                           % Employee attributes should come from employees
                           maps:get(id, Prov) =:= {employees, id} andalso
                           maps:get(name, Prov) =:= {employees, name} andalso
                           % Department attributes should come from departments
                           maps:get(dept_name, Prov) =:= {departments, dept_name} andalso
                           maps:get(budget, Prov) =:= {departments, budget}
                       end, Results))
    ].

test_join_with_conflicts_provenance(DB) ->
    % Create relations with conflicting attribute names
    {DB1, _} = operations:create_relation(DB, table_a, #{
        id => integer,
        value => integer
    }),
    {DB2, _} = operations:create_tuple(DB1, table_a, #{id => 1, value => 100}),

    {DB3, _} = operations:create_relation(DB2, table_b, #{
        id => integer,
        value => integer  % Conflict!
    }),
    {DB4, _} = operations:create_tuple(DB3, table_b, #{id => 1, value => 200}),

    % Create iterators with provenance
    AIter = operations:get_tuples_iterator(DB4, table_a, #{}),
    BIter = operations:get_tuples_iterator(DB4, table_b, #{}),

    JoinIter = operations:equijoin_iterator(AIter, BIter, id),
    Results = operations:collect_all(JoinIter),

    % Should be exactly one result tuple
    ?_assertEqual(1, length(Results)),

    % Check provenance handles conflicts correctly
    [Result] = Results,
    Meta = maps:get(meta, Result),
    Prov = maps:get(provenance, Meta),

    [
     % Left 'value' provenance
     ?_assertEqual({table_a, value}, maps:get(value, Prov)),
     % Right 'value' provenance (prefixed)
     ?_assertEqual({table_b, value}, maps:get(right_value, Prov)),
     % Join attribute from left side
     ?_assertEqual({table_a, id}, maps:get(id, Prov)),
     % Join attribute from right side (prefixed)
     ?_assertEqual({table_b, id}, maps:get(right_id, Prov))
    ].

test_equijoin_preserves_both_sources(DB) ->
    % Even though equijoin only keeps one dept_id value,
    % provenance should track both sources
    EmpIter = operations:get_tuples_iterator(DB, employees, #{}),
    DeptIter = operations:get_tuples_iterator(DB, departments, #{}),

    JoinIter = operations:equijoin_iterator(EmpIter, DeptIter, dept_id),
    Results = operations:collect_all(JoinIter),

    % Check that ALL tuples have both dept_id sources in provenance
    [
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Prov = maps:get(provenance, Meta),
                           % Left side dept_id
                           maps:is_key(dept_id, Prov) andalso
                           % Right side dept_id (prefixed)
                           maps:is_key(right_dept_id, Prov)
                       end, Results)),
     % Verify the sources are correct for ALL tuples
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Prov = maps:get(provenance, Meta),
                           maps:get(dept_id, Prov) =:= {employees, dept_id} andalso
                           maps:get(right_dept_id, Prov) =:= {departments, dept_id}
                       end, Results))
    ].

%%% Lineage Tests

test_base_lineage(DB) ->
    % Query tuples (metadata always included)
    Iter = operations:get_tuples_iterator(DB, employees, #{}),
    Results = operations:collect_all(Iter),

    % All tuples should have lineage showing base relation
    [
     ?_assertEqual(2, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           Lineage =:= {base, employees}
                       end, Results))
    ].

test_join_lineage(DB) ->
    % Create iterators (metadata always included)
    EmpIter = operations:get_tuples_iterator(DB, employees, #{}),
    DeptIter = operations:get_tuples_iterator(DB, departments, #{}),

    % Join them
    JoinIter = operations:equijoin_iterator(EmpIter, DeptIter, dept_id),
    Results = operations:collect_all(JoinIter),

    % All results should have lineage showing join operation
    [
     ?_assertEqual(2, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           % Should be a join of two base relations
                           case Lineage of
                               {join, dept_id, {base, employees}, {base, departments}} -> true;
                               _ -> false
                           end
                       end, Results))
    ].

%%% Comprehensive Lineage Tests

test_select_lineage(DB) ->
    % Test lineage through select (filter) operation
    Iter = operations:get_tuples_iterator(DB, employees, #{}),

    Predicate = fun(T) -> maps:get(id, T, 0) =:= 1 end,
    FilteredIter = operations:select_iterator(Iter, Predicate),
    Results = operations:collect_all(FilteredIter),

    % Should have lineage: select wrapping base
    [
     ?_assertEqual(1, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           case Lineage of
                               {select, Pred, {base, employees}} when is_function(Pred) -> true;
                               _ -> false
                           end
                       end, Results))
    ].

test_project_lineage(DB) ->
    % Test lineage through project operation
    Iter = operations:get_tuples_iterator(DB, employees, #{}),
    ProjectedIter = operations:project_iterator(Iter, [name, dept_id]),
    Results = operations:collect_all(ProjectedIter),

    % Should have lineage: project wrapping base
    [
     ?_assertEqual(2, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           case Lineage of
                               {project, [name, dept_id], {base, employees}} -> true;
                               _ -> false
                           end
                       end, Results))
    ].

test_sort_lineage(DB) ->
    % Test lineage through sort operation
    Iter = operations:get_tuples_iterator(DB, employees, #{}),

    CompareFun = fun(A, B) -> maps:get(id, A, 0) =< maps:get(id, B, 0) end,
    SortedIter = operations:sort_iterator(Iter, CompareFun),
    Results = operations:collect_all(SortedIter),

    % Should have lineage: sort wrapping base
    [
     ?_assertEqual(2, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           case Lineage of
                               {sort, Cmp, {base, employees}} when is_function(Cmp) -> true;
                               _ -> false
                           end
                       end, Results))
    ].

test_take_lineage(DB) ->
    % Test lineage through take (limit) operation
    Iter = operations:get_tuples_iterator(DB, employees, #{}),
    TakeIter = operations:take_iterator(Iter, 1),
    Results = operations:collect_all(TakeIter),

    % Should have lineage: take wrapping base
    [
     ?_assertEqual(1, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           case Lineage of
                               {take, 1, {base, employees}} -> true;
                               _ -> false
                           end
                       end, Results))
    ].

test_multi_operation_pipeline_lineage(DB) ->
    % Test complex pipeline: employees → select(id=1) → join(departments) → project([name, dept_name])

    % 1. Start with employees base relation
    EmpIter = operations:get_tuples_iterator(DB, employees, #{}),

    % 2. Filter to Alice (id=1)
    Predicate = fun(T) -> maps:get(id, T, 0) =:= 1 end,
    FilteredEmp = operations:select_iterator(EmpIter, Predicate),

    % 3. Join with departments
    DeptIter = operations:get_tuples_iterator(DB, departments, #{}),
    JoinedIter = operations:equijoin_iterator(FilteredEmp, DeptIter, dept_id),

    % 4. Project to [name, dept_name]
    ProjectedIter = operations:project_iterator(JoinedIter, [name, dept_name]),

    Results = operations:collect_all(ProjectedIter),

    % Expected lineage:
    % {project, [name, dept_name],
    %   {join, dept_id,
    %     {select, Pred, {base, employees}},
    %     {base, departments}}}

    [
     ?_assertEqual(1, length(Results)),  % Only Alice
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           case Lineage of
                               {project, [name, dept_name],
                                {join, dept_id,
                                 {select, Pred, {base, employees}},
                                 {base, departments}}} when is_function(Pred) -> true;
                               _ -> false
                           end
                       end, Results))
    ].

test_theta_join_lineage(DB) ->
    % Test lineage for theta-join operation
    EmpIter = operations:get_tuples_iterator(DB, employees, #{}),
    DeptIter = operations:get_tuples_iterator(DB, departments, #{}),

    Predicate = fun(Emp, Dept) ->
        maps:get(dept_id, Emp) =:= maps:get(dept_id, Dept)
    end,

    JoinIter = operations:theta_join_iterator(EmpIter, DeptIter, Predicate),
    Results = operations:collect_all(JoinIter),

    % Should have theta_join lineage
    [
     ?_assertEqual(2, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           case Lineage of
                               {theta_join, Pred, {base, employees}, {base, departments}}
                                   when is_function(Pred) -> true;
                               _ -> false
                           end
                       end, Results))
    ].

test_complex_nested_lineage(DB) ->
    % Test deeply nested lineage:
    % employees → select(id=1) → project([name, dept_id]) → join(departments → select(budget>50000)) → take(1)

    % Left side: employees → select → project
    EmpIter = operations:get_tuples_iterator(DB, employees, #{}),
    FilteredEmp = operations:select_iterator(EmpIter, fun(T) -> maps:get(id, T, 0) =:= 1 end),
    ProjectedEmp = operations:project_iterator(FilteredEmp, [name, dept_id]),

    % Right side: departments → select
    DeptIter = operations:get_tuples_iterator(DB, departments, #{}),
    FilteredDept = operations:select_iterator(DeptIter, fun(D) -> maps:get(budget, D, 0) > 50000 end),

    % Join both sides
    JoinedIter = operations:equijoin_iterator(ProjectedEmp, FilteredDept, dept_id),

    % Take first result
    TakeIter = operations:take_iterator(JoinedIter, 1),

    Results = operations:collect_all(TakeIter),

    % Expected lineage should be deeply nested
    [
     ?_assertEqual(1, length(Results)),
     ?_assert(lists:all(fun(T) ->
                           Meta = maps:get(meta, T),
                           Lineage = maps:get(lineage, Meta),
                           case Lineage of
                               {take, 1,
                                {join, dept_id,
                                 {project, [name, dept_id],
                                  {select, _, {base, employees}}},
                                 {select, _, {base, departments}}}} -> true;
                               _ -> false
                           end
                       end, Results))
    ].