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
|
%%% @doc Relation Visualizer - Multiple Rendering Modes
%%%
%%% Provides various visual representations for nested relational data.
%%% Supports different modes optimized for different use cases.
%%%
%%% == Modes ==
%%%
%%% - `table` - Classic flat table (default)
%%% - `nested_table` - Tables within table cells
%%% - `tree` - Hierarchical tree view with indentation
%%% - `linked_tables` - Separate linked tables for nested relations
%%% - `outline` - Indented outline format
%%%
%%% == Usage ==
%%%
%%% ```
%%% Results = query_planner:collect(DB, Plan),
%%% visualizer:render(Results, tree).
%%% visualizer:render(Results, nested_table).
%%% ```
%%%
%%% @author Nekoma Team
%%% @copyright 2025
-module(visualizer).
-export([render/1,
render/2,
modes/0]).
-type render_mode() :: table
| nested_table
| tree
| linked_tables
| outline.
-export_type([render_mode/0]).
%%% Public API
%% @doc Render tuples with default mode (table).
-spec render([map()]) -> ok.
render(Tuples) ->
render(Tuples, table).
%% @doc Render tuples with specified visualization mode.
-spec render([map()], render_mode()) -> ok.
render([], _Mode) ->
io:format("(no results)~n"),
ok;
render(Tuples, table) ->
render_table(Tuples);
render(Tuples, nested_table) ->
render_nested_table(Tuples);
render(Tuples, tree) ->
render_tree(Tuples);
render(Tuples, linked_tables) ->
render_linked_tables(Tuples);
render(Tuples, outline) ->
render_outline(Tuples).
%% @doc List available rendering modes.
-spec modes() -> [render_mode()].
modes() ->
[table, nested_table, tree, linked_tables, outline].
%%% Rendering Implementations
%% Mode 1: Table with proper column width calculation
render_table(Tuples) ->
AllKeys = get_all_keys(Tuples),
% Calculate column widths based on content
Widths = calculate_column_widths(Tuples, AllKeys),
io:format("~n"),
print_separator_with_widths(Widths),
print_row_with_widths([atom_to_list(K) || K <- AllKeys], Widths),
print_separator_with_widths(Widths),
lists:foreach(
fun(Tuple) ->
Values = [format_value_for_table(maps:get(K, Tuple, undefined)) || K <- AllKeys],
print_row_with_widths(Values, Widths)
end,
Tuples
),
print_separator_with_widths(Widths),
io:format("~n~b tuple(s)~n~n", [length(Tuples)]),
ok.
calculate_column_widths(Tuples, Keys) ->
lists:map(
fun(Key) ->
% Start with header width
HeaderWidth = length(atom_to_list(Key)),
% Find max value width in this column
MaxValueWidth = lists:foldl(
fun(Tuple, MaxW) ->
Value = maps:get(Key, Tuple, undefined),
ValueStr = format_value_for_table(Value),
max(MaxW, length(ValueStr))
end,
HeaderWidth,
Tuples
),
max(12, MaxValueWidth) % Minimum width of 12
end,
Keys
).
format_value_for_table(V) when is_map(V) ->
% Format nested map as inline representation
Keys = [K || K <- maps:keys(V), K =/= meta],
Parts = [atom_to_list(K) ++ ":" ++ format_value(maps:get(K, V)) || K <- Keys],
"[" ++ string:join(Parts, ", ") ++ "]";
format_value_for_table(V) ->
format_value(V).
print_separator_with_widths(Widths) ->
io:format("+"),
lists:foreach(
fun(W) ->
io:format("~s+", [lists:duplicate(W + 2, $-)])
end,
Widths
),
io:format("~n").
print_row_with_widths(Values, Widths) ->
io:format("| "),
lists:foreach(
fun({Val, Width}) ->
io:format("~*s | ", [-Width, Val])
end,
lists:zip(Values, Widths)
),
io:format("~n").
%% Mode 2: Nested tables (tables within cells)
render_nested_table(Tuples) ->
io:format("~n=== Nested Table View ===~n~n"),
lists:foreach(
fun(Tuple) ->
render_tuple_nested(Tuple, 0)
end,
Tuples
),
io:format("~n~b tuple(s)~n~n", [length(Tuples)]),
ok.
render_tuple_nested(Tuple, Indent) ->
Keys = [K || K <- maps:keys(Tuple), K =/= meta],
IndentStr = lists:duplicate(Indent, $\s),
io:format("~s+-- Tuple~n", [IndentStr]),
lists:foreach(
fun(Key) ->
Value = maps:get(Key, Tuple),
case is_map(Value) andalso (Key =/= meta) of
true ->
% Nested tuple - render as inner table
io:format("~s| ~s:~n", [IndentStr, atom_to_list(Key)]),
io:format("~s| +-------------------------+~n", [IndentStr]),
NestedKeys = maps:keys(Value),
lists:foreach(
fun(NK) ->
NV = format_value(maps:get(NK, Value)),
io:format("~s| | ~-10s : ~-10s |~n",
[IndentStr, atom_to_list(NK), NV])
end,
NestedKeys
),
io:format("~s| +-------------------------+~n", [IndentStr]);
false ->
% Simple value
io:format("~s| ~-15s : ~s~n",
[IndentStr, atom_to_list(Key), format_value(Value)])
end
end,
Keys
),
io:format("~s+--~n", [IndentStr]).
%% Mode 3: Tree view with hierarchy
render_tree(Tuples) ->
io:format("~n=== Tree View ===~n~n"),
io:format("+-- Relation (~b tuples)~n", [length(Tuples)]),
render_tuples_as_tree(Tuples, "| ", length(Tuples)),
io:format("~n"),
ok.
render_tuples_as_tree([], _Prefix, _Total) ->
ok;
render_tuples_as_tree([Tuple], Prefix, Total) ->
io:format("~s~n~s+-- Row ~b~n", [Prefix, Prefix, Total]),
render_tuple_as_tree(Tuple, Prefix ++ " ");
render_tuples_as_tree([Tuple | Rest], Prefix, Total) ->
Index = Total - length(Rest),
io:format("~s~n~s+-- Row ~b~n", [Prefix, Prefix, Index]),
render_tuple_as_tree(Tuple, Prefix ++ "| "),
render_tuples_as_tree(Rest, Prefix, Total).
render_tuple_as_tree(Tuple, Prefix) ->
Keys = [K || K <- maps:keys(Tuple), K =/= meta],
render_attrs_as_tree(Keys, Tuple, Prefix).
render_attrs_as_tree([], _Tuple, _Prefix) ->
ok;
render_attrs_as_tree([Key], Tuple, Prefix) ->
Value = maps:get(Key, Tuple),
case is_map(Value) andalso (Key =/= meta) of
true ->
io:format("~s+-- ~s:~n", [Prefix, atom_to_list(Key)]),
render_tuple_as_tree(Value, Prefix ++ " ");
false ->
io:format("~s+-- ~s: ~s~n",
[Prefix, atom_to_list(Key), format_value(Value)])
end;
render_attrs_as_tree([Key | Rest], Tuple, Prefix) ->
Value = maps:get(Key, Tuple),
case is_map(Value) andalso (Key =/= meta) of
true ->
io:format("~s+-- ~s:~n", [Prefix, atom_to_list(Key)]),
render_tuple_as_tree(Value, Prefix ++ "| "),
render_attrs_as_tree(Rest, Tuple, Prefix);
false ->
io:format("~s+-- ~s: ~s~n",
[Prefix, atom_to_list(Key), format_value(Value)]),
render_attrs_as_tree(Rest, Tuple, Prefix)
end.
%% Mode 4: Linked tables (parent + separate child tables)
render_linked_tables(Tuples) ->
io:format("~n=== Linked Tables View ===~n~n"),
% Collect nested relations with parent references
{FlatTuples, NestedRels} = extract_nested_relations_with_refs(Tuples),
% Render main table
io:format("Main Relation:~n"),
render_table(FlatTuples),
% Render nested tables with parent references
case NestedRels of
[] -> ok;
_ ->
io:format("~nNested Relations:~n"),
lists:foreach(
fun({RowIdx, AttrName, NestedTuple}) ->
io:format("~n[~s for Row ~b]:~n", [atom_to_list(AttrName), RowIdx]),
render_table([NestedTuple])
end,
NestedRels
)
end,
ok.
extract_nested_relations_with_refs(Tuples) ->
% Process tuples with index tracking
{FlatTuples, NestedRels} = lists:foldl(
fun(Tuple, {FlatAcc, NestedAcc}) ->
RowIdx = length(FlatAcc) + 1,
% Flatten this tuple
FlatTuple = maps:map(
fun(K, V) when is_map(V), K =/= meta ->
lists:flatten(io_lib:format("[~b]", [RowIdx]));
(_K, V) -> V
end,
Tuple
),
% Extract nested relations with parent reference
Nested = lists:filtermap(
fun(K) ->
case maps:get(K, Tuple) of
V when is_map(V), K =/= meta ->
{true, {RowIdx, K, V}};
_ ->
false
end
end,
maps:keys(Tuple)
),
{[FlatTuple | FlatAcc], Nested ++ NestedAcc}
end,
{[], []},
Tuples
),
{lists:reverse(FlatTuples), lists:reverse(NestedRels)}.
%% Mode 5: Outline format (indented text)
render_outline(Tuples) ->
io:format("~n=== Outline View ===~n~n"),
lists:foreach(
fun(Tuple) ->
render_tuple_outline(Tuple, 0)
end,
Tuples
),
io:format("~n~b tuple(s)~n~n", [length(Tuples)]),
ok.
render_tuple_outline(Tuple, Indent) ->
Keys = [K || K <- maps:keys(Tuple), K =/= meta],
IndentStr = lists:duplicate(Indent, $\s),
io:format("~s+-- Tuple~n", [IndentStr]),
lists:foreach(
fun(Key) ->
Value = maps:get(Key, Tuple),
case is_map(Value) andalso (Key =/= meta) of
true ->
io:format("~s| ~s:~n", [IndentStr, atom_to_list(Key)]),
render_tuple_outline(Value, Indent + 4);
false ->
io:format("~s| ~s: ~s~n",
[IndentStr, atom_to_list(Key), format_value(Value)])
end
end,
Keys
),
io:format("~s+--~n", [IndentStr]).
%%% Helper Functions
get_all_keys(Tuples) ->
lists:usort(
lists:flatmap(
fun(Tuple) ->
[K || K <- maps:keys(Tuple), K =/= meta]
end,
Tuples
)
).
format_value(undefined) -> "(null)";
format_value(V) when is_integer(V) -> integer_to_list(V);
format_value(V) when is_float(V) -> float_to_list(V, [{decimals, 2}]);
format_value(V) when is_atom(V) -> atom_to_list(V);
format_value(V) when is_binary(V) -> binary_to_list(V);
format_value(V) when is_list(V) -> V;
format_value(V) when is_map(V) -> "(nested)";
format_value(_) -> "(complex)".
|