1#include "duckdb/function/table/summary.hpp"
2#include "duckdb/function/table_function.hpp"
3#include "duckdb/function/function_set.hpp"
4#include "duckdb/common/file_system.hpp"
5
6// this function makes not that much sense on its own but is a demo for table-parameter table-producing functions
7
8namespace duckdb {
9
10static unique_ptr<FunctionData> SummaryFunctionBind(ClientContext &context, TableFunctionBindInput &input,
11 vector<LogicalType> &return_types, vector<string> &names) {
12
13 return_types.emplace_back(args: LogicalType::VARCHAR);
14 names.emplace_back(args: "summary");
15
16 for (idx_t i = 0; i < input.input_table_types.size(); i++) {
17 return_types.push_back(x: input.input_table_types[i]);
18 names.emplace_back(args&: input.input_table_names[i]);
19 }
20
21 return make_uniq<TableFunctionData>();
22}
23
24static OperatorResultType SummaryFunction(ExecutionContext &context, TableFunctionInput &data_p, DataChunk &input,
25 DataChunk &output) {
26 output.SetCardinality(input.size());
27
28 for (idx_t row_idx = 0; row_idx < input.size(); row_idx++) {
29 string summary_val = "[";
30
31 for (idx_t col_idx = 0; col_idx < input.ColumnCount(); col_idx++) {
32 summary_val += input.GetValue(col_idx, index: row_idx).ToString();
33 if (col_idx < input.ColumnCount() - 1) {
34 summary_val += ", ";
35 }
36 }
37 summary_val += "]";
38 output.SetValue(col_idx: 0, index: row_idx, val: Value(summary_val));
39 }
40 for (idx_t col_idx = 0; col_idx < input.ColumnCount(); col_idx++) {
41 output.data[col_idx + 1].Reference(other&: input.data[col_idx]);
42 }
43 return OperatorResultType::NEED_MORE_INPUT;
44}
45
46void SummaryTableFunction::RegisterFunction(BuiltinFunctions &set) {
47 TableFunction summary_function("summary", {LogicalType::TABLE}, nullptr, SummaryFunctionBind);
48 summary_function.in_out_function = SummaryFunction;
49 set.AddFunction(function: summary_function);
50}
51
52} // namespace duckdb
53