1#include "duckdb/planner/operator/logical_copy_to_file.hpp"
2
3#include "duckdb/catalog/catalog_entry/copy_function_catalog_entry.hpp"
4#include "duckdb/common/field_writer.hpp"
5#include "duckdb/function/copy_function.hpp"
6
7namespace duckdb {
8
9// Warning: some fields here were added while this code appears untested
10// -> copy test in test/api/test_plan_serialization.cpp was commented out as WIP
11void LogicalCopyToFile::Serialize(FieldWriter &writer) const {
12 writer.WriteString(val: file_path);
13 writer.WriteField(element: use_tmp_file);
14 writer.WriteField(element: overwrite_or_ignore);
15 writer.WriteField(element: per_thread_output);
16 writer.WriteList<idx_t>(elements: partition_columns);
17
18 D_ASSERT(!function.name.empty());
19 writer.WriteString(val: function.name);
20
21 writer.WriteField(element: bind_data != nullptr);
22 if (bind_data && !function.serialize) {
23 throw InvalidInputException("Can't serialize copy function %s", function.name);
24 }
25
26 function.serialize(writer, *bind_data, function);
27}
28
29unique_ptr<LogicalOperator> LogicalCopyToFile::Deserialize(LogicalDeserializationState &state, FieldReader &reader) {
30 auto file_path = reader.ReadRequired<string>();
31 auto use_tmp_file = reader.ReadRequired<bool>();
32 auto overwrite_or_ignore = reader.ReadRequired<bool>();
33 auto per_thread_output = reader.ReadRequired<bool>();
34 auto partition_columns = reader.ReadRequiredList<idx_t>();
35
36 auto copy_func_name = reader.ReadRequired<string>();
37
38 auto has_bind_data = reader.ReadRequired<bool>();
39
40 auto &context = state.gstate.context;
41 auto &copy_func_catalog_entry =
42 Catalog::GetEntry<CopyFunctionCatalogEntry>(context, INVALID_CATALOG, DEFAULT_SCHEMA, name: copy_func_name);
43 auto &copy_func = copy_func_catalog_entry.function;
44
45 unique_ptr<FunctionData> bind_data;
46 if (has_bind_data) {
47 if (!copy_func.deserialize) {
48 throw SerializationException("Have bind info but no deserialization function for %s", copy_func.name);
49 }
50 bind_data = copy_func.deserialize(context, reader, copy_func);
51 }
52
53 auto result = make_uniq<LogicalCopyToFile>(args&: copy_func, args: std::move(bind_data));
54 result->file_path = file_path;
55 result->use_tmp_file = use_tmp_file;
56 result->overwrite_or_ignore = overwrite_or_ignore;
57 result->per_thread_output = per_thread_output;
58 result->partition_columns = std::move(partition_columns);
59 return std::move(result);
60}
61
62idx_t LogicalCopyToFile::EstimateCardinality(ClientContext &context) {
63 return 1;
64}
65
66} // namespace duckdb
67