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
|
%%% @doc Query Planner and Prepared Statement Execution
%%%
%%% Provides a Lisp-inspired tuple-based DSL for constructing and executing
%%% relational algebra queries. Queries are represented as nested Erlang tuples
%%% that can be validated, optimized, and compiled into iterator pipelines.
%%%
%%% == Query DSL Syntax ==
%%%
%%% The DSL uses nested tuples to represent relational operations:
%%%
%%% ```
%%% % Scan relation (base case)
%%% {scan, employees}
%%%
%%% % Reference relation (synonym for scan)
%%% {relation, employees}
%%%
%%% % Selection (filter)
%%% {select, {scan, employees}, fun(T) -> maps:get(age, T) > 30 end}
%%%
%%% % Projection
%%% {project, {scan, employees}, [name, age]}
%%%
%%% % Equijoin (natural join on common attribute)
%%% {join, {scan, employees}, {scan, departments}, dept_id}
%%%
%%% % Theta join (join with arbitrary predicate)
%%% {theta_join,
%%% {scan, employees},
%%% {scan, departments},
%%% fun(L, R) -> maps:get(dept_id, L) =:= maps:get(id, R) end}
%%%
%%% % Sort
%%% {sort, {scan, employees}, fun(A, B) -> maps:get(age, A) =< maps:get(age, B) end}
%%%
%%% % Take (limit)
%%% {take, {scan, employees}, 10}
%%%
%%% % Rename attribute
%%% {rename, old_name, new_name, {scan, employees}}
%%%
%%% % Composite query
%%% {project,
%%% {select,
%%% {join,
%%% {scan, employees},
%%% {scan, departments},
%%% dept_id},
%%% fun(T) -> maps:get(age, T) > 30 end},
%%% [name, dept_name]}
%%% ```
%%%
%%% == Usage ==
%%%
%%% ```
%%% % Prepare a query plan
%%% Plan = query_planner:prepare({select, {scan, employees},
%%% fun(T) -> maps:get(age, T) > 30 end}).
%%%
%%% % Execute and get iterator
%%% Iter = query_planner:execute(DB, Plan).
%%%
%%% % Collect all results
%%% Results = query_planner:collect(DB, Plan).
%%% ```
%%%
%%% @author Nekoma Team
%%% @copyright 2025
-module(query_planner).
-export([prepare/1,
execute/2,
collect/2,
explain/1]).
-include("operations.hrl").
%%% Types
-type query_plan() ::
{scan, atom()} % Scan relation
| {relation, atom()} % Reference relation
| {select, query_plan(), fun((map()) -> boolean())} % Filter
| {project, query_plan(), [atom()]} % Projection
| {join, query_plan(), query_plan(), atom()} % Equijoin
| {theta_join, query_plan(), query_plan(),
fun((map(), map()) -> boolean())} % Theta join
| {sort, query_plan(), fun((map(), map()) -> boolean())} % Sort
| {take, query_plan(), pos_integer()} % Limit
| {rename, atom(), atom(), query_plan()} % Rename attribute
| {materialize, query_plan()} % Materialize all
| {materialize, query_plan(), pos_integer()}. % Materialize N tuples
-export_type([query_plan/0]).
%%% Public API
%% @doc Prepare and validate a query plan.
%%
%% Takes a query plan tuple and validates its structure. Returns the
%% plan unchanged if valid, or throws an error if invalid.
%%
%% @param Plan The query plan to prepare
%% @returns The validated query plan
%% @throws {invalid_plan, Reason}
-spec prepare(query_plan()) -> query_plan().
prepare(Plan) ->
case validate_plan(Plan) of
ok -> Plan;
{error, Reason} -> error({invalid_plan, Reason})
end.
%% @doc Execute a prepared query plan against a database.
%%
%% Compiles the query plan into an iterator pipeline and returns
%% the iterator handle. The caller must consume the iterator using
%% operations:next_tuple/1 and close it with operations:close_iterator/1.
%%
%% @param DB The database state
%% @param Plan The prepared query plan
%% @returns Iterator process PID
-spec execute(term(), query_plan()) -> pid().
execute(DB, Plan) ->
compile_to_iterator(DB, Plan).
%% @doc Execute a query plan and collect all results.
%%
%% Convenience function that executes the plan and collects all
%% tuples into a list. Automatically closes the iterator.
%%
%% If the iterator times out (e.g., when sorting an infinite relation),
%% returns partial results with a warning.
%%
%% @param DB The database state
%% @param Plan The prepared query plan
%% @returns List of result tuples, or partial results on timeout
-spec collect(term(), query_plan()) -> [map()].
collect(DB, Plan) ->
Iter = execute(DB, Plan),
Results = operations:collect_all(Iter),
operations:close_iterator(Iter),
case Results of
{error, timeout, PartialResults} ->
io:format("~nWarning: Query timed out. Returning partial results (~b tuples).~n",
[length(PartialResults)]),
io:format("Tip: For infinite relations, use 'take' before operations like 'sort'.~n~n"),
PartialResults;
{error, Reason, PartialResults} ->
io:format("~nWarning: Query failed (~p). Returning partial results (~b tuples).~n~n",
[Reason, length(PartialResults)]),
PartialResults;
_ ->
Results
end.
%% @doc Explain a query plan in human-readable form.
%%
%% Converts the query plan to a readable string representation
%% showing the operation tree.
%%
%% @param Plan The query plan to explain
%% @returns String representation of the plan
-spec explain(query_plan()) -> string().
explain(Plan) ->
explain_plan(Plan, 0).
%%% Internal Functions
%% Validate query plan structure
-spec validate_plan(query_plan()) -> ok | {error, term()}.
validate_plan({scan, Name}) when is_atom(Name) ->
ok;
validate_plan({relation, Name}) when is_atom(Name) ->
ok;
validate_plan({select, SubPlan, Pred}) when is_function(Pred, 1) ->
validate_plan(SubPlan);
validate_plan({project, SubPlan, Attrs}) when is_list(Attrs) ->
case lists:all(fun is_atom/1, Attrs) of
true -> validate_plan(SubPlan);
false -> {error, {invalid_attributes, Attrs}}
end;
validate_plan({join, Left, Right, Attr}) when is_atom(Attr) ->
case {validate_plan(Left), validate_plan(Right)} of
{ok, ok} -> ok;
{{error, _} = Error, _} -> Error;
{_, {error, _} = Error} -> Error
end;
validate_plan({theta_join, Left, Right, Pred}) when is_function(Pred, 2) ->
case {validate_plan(Left), validate_plan(Right)} of
{ok, ok} -> ok;
{{error, _} = Error, _} -> Error;
{_, {error, _} = Error} -> Error
end;
validate_plan({sort, SubPlan, Comp}) when is_function(Comp, 2) ->
validate_plan(SubPlan);
validate_plan({take, SubPlan, N}) when is_integer(N), N > 0 ->
validate_plan(SubPlan);
validate_plan({rename, OldAttr, NewAttr, SubPlan}) when is_atom(OldAttr), is_atom(NewAttr) ->
validate_plan(SubPlan);
validate_plan({materialize, SubPlan}) ->
validate_plan(SubPlan);
validate_plan({materialize, SubPlan, N}) when is_integer(N), N > 0 ->
validate_plan(SubPlan);
validate_plan(Plan) ->
{error, {unknown_operator, Plan}}.
%% Compile query plan to iterator pipeline
-spec compile_to_iterator(term(), query_plan()) -> pid().
compile_to_iterator(DB, {scan, Name}) ->
operations:get_tuples_iterator(DB, Name);
compile_to_iterator(DB, {relation, Name}) ->
operations:get_tuples_iterator(DB, Name);
compile_to_iterator(DB, {select, SubPlan, Predicate}) ->
SubIter = compile_to_iterator(DB, SubPlan),
operations:select_iterator(SubIter, Predicate);
compile_to_iterator(DB, {project, SubPlan, Attributes}) ->
SubIter = compile_to_iterator(DB, SubPlan),
operations:project_iterator(SubIter, Attributes);
compile_to_iterator(DB, {join, LeftPlan, RightPlan, Attribute}) ->
LeftIter = compile_to_iterator(DB, LeftPlan),
RightIter = compile_to_iterator(DB, RightPlan),
operations:equijoin_iterator(LeftIter, RightIter, Attribute);
compile_to_iterator(DB, {theta_join, LeftPlan, RightPlan, Predicate}) ->
LeftIter = compile_to_iterator(DB, LeftPlan),
RightIter = compile_to_iterator(DB, RightPlan),
operations:theta_join_iterator(LeftIter, RightIter, Predicate);
compile_to_iterator(DB, {sort, SubPlan, Comparator}) ->
SubIter = compile_to_iterator(DB, SubPlan),
operations:sort_iterator(SubIter, Comparator);
compile_to_iterator(DB, {take, SubPlan, N}) ->
SubIter = compile_to_iterator(DB, SubPlan),
operations:take_iterator(SubIter, N);
compile_to_iterator(DB, {rename, OldAttr, NewAttr, SubPlan}) ->
SubIter = compile_to_iterator(DB, SubPlan),
rename_iterator(SubIter, OldAttr, NewAttr);
compile_to_iterator(DB, {materialize, SubPlan}) ->
% Materialize returns all tuples as a "static" iterator
SubIter = compile_to_iterator(DB, SubPlan),
Results = operations:collect_all(SubIter),
operations:close_iterator(SubIter),
static_iterator(Results);
compile_to_iterator(DB, {materialize, SubPlan, Limit}) ->
% Materialize with limit
SubIter = compile_to_iterator(DB, SubPlan),
LimitedIter = operations:take_iterator(SubIter, Limit),
Results = operations:collect_all(LimitedIter),
operations:close_iterator(LimitedIter),
static_iterator(Results).
%% Static iterator - serves pre-materialized tuples
-spec static_iterator([map()]) -> pid().
static_iterator(Tuples) ->
spawn(fun() -> static_loop(Tuples) end).
static_loop(Tuples) ->
receive
{next, Caller} ->
case Tuples of
[] ->
Caller ! done,
static_loop([]);
[Tuple | Rest] ->
Caller ! {tuple, Tuple},
static_loop(Rest)
end;
{close, Caller} ->
Caller ! ok
end.
%% Rename iterator - renames an attribute in each tuple
-spec rename_iterator(pid(), atom(), atom()) -> pid().
rename_iterator(SourceIter, OldAttr, NewAttr) ->
spawn(fun() -> rename_loop(SourceIter, OldAttr, NewAttr) end).
rename_loop(SourceIter, OldAttr, NewAttr) ->
receive
{next, Caller} ->
case operations:next_tuple(SourceIter) of
done ->
Caller ! done,
rename_loop(SourceIter, OldAttr, NewAttr);
{ok, Tuple} ->
RenamedTuple = case maps:is_key(OldAttr, Tuple) of
true ->
Value = maps:get(OldAttr, Tuple),
Tuple1 = maps:remove(OldAttr, Tuple),
maps:put(NewAttr, Value, Tuple1);
false ->
Tuple
end,
Caller ! {tuple, RenamedTuple},
rename_loop(SourceIter, OldAttr, NewAttr)
end;
{close, Caller} ->
operations:close_iterator(SourceIter),
Caller ! ok
end.
%% Pretty-print query plan
-spec explain_plan(query_plan(), non_neg_integer()) -> string().
explain_plan({scan, Name}, Indent) ->
indent(Indent) ++ "Scan: " ++ atom_to_list(Name) ++ "\n";
explain_plan({relation, Name}, Indent) ->
indent(Indent) ++ "Relation: " ++ atom_to_list(Name) ++ "\n";
explain_plan({select, SubPlan, _Pred}, Indent) ->
indent(Indent) ++ "Select (filter)\n" ++
explain_plan(SubPlan, Indent + 2);
explain_plan({project, SubPlan, Attrs}, Indent) ->
AttrStr = string:join([atom_to_list(A) || A <- Attrs], ", "),
indent(Indent) ++ "Project [" ++ AttrStr ++ "]\n" ++
explain_plan(SubPlan, Indent + 2);
explain_plan({join, LeftPlan, RightPlan, Attr}, Indent) ->
indent(Indent) ++ "Join on " ++ atom_to_list(Attr) ++ "\n" ++
explain_plan(LeftPlan, Indent + 2) ++
explain_plan(RightPlan, Indent + 2);
explain_plan({theta_join, LeftPlan, RightPlan, _Pred}, Indent) ->
indent(Indent) ++ "Theta Join\n" ++
explain_plan(LeftPlan, Indent + 2) ++
explain_plan(RightPlan, Indent + 2);
explain_plan({sort, SubPlan, _Comp}, Indent) ->
indent(Indent) ++ "Sort\n" ++
explain_plan(SubPlan, Indent + 2);
explain_plan({take, SubPlan, N}, Indent) ->
indent(Indent) ++ "Take " ++ integer_to_list(N) ++ "\n" ++
explain_plan(SubPlan, Indent + 2);
explain_plan({rename, OldAttr, NewAttr, SubPlan}, Indent) ->
indent(Indent) ++ "Rename " ++ atom_to_list(OldAttr) ++ " -> " ++ atom_to_list(NewAttr) ++ "\n" ++
explain_plan(SubPlan, Indent + 2);
explain_plan({materialize, SubPlan}, Indent) ->
indent(Indent) ++ "Materialize (all)\n" ++
explain_plan(SubPlan, Indent + 2);
explain_plan({materialize, SubPlan, N}, Indent) ->
indent(Indent) ++ "Materialize (" ++ integer_to_list(N) ++ " tuples)\n" ++
explain_plan(SubPlan, Indent + 2).
%% Helper: indent string
-spec indent(non_neg_integer()) -> string().
indent(N) -> lists:duplicate(N, $\s).
|