1
+ #include " postgres_binary_copy.hpp"
2
+ #include " postgres_binary_writer.hpp"
3
+ #include " duckdb/common/serializer/buffered_file_writer.hpp"
4
+ #include " duckdb/common/file_system.hpp"
5
+
6
+ namespace duckdb {
7
+
8
+ PostgresBinaryCopyFunction::PostgresBinaryCopyFunction () :
9
+ CopyFunction (" postgres_binary" ) {
10
+
11
+ copy_to_bind = PostgresBinaryWriteBind;
12
+ copy_to_initialize_global = PostgresBinaryWriteInitializeGlobal;
13
+ copy_to_initialize_local = PostgresBinaryWriteInitializeLocal;
14
+ copy_to_sink = PostgresBinaryWriteSink;
15
+ copy_to_combine = PostgresBinaryWriteCombine;
16
+ copy_to_finalize = PostgresBinaryWriteFinalize;
17
+ }
18
+
19
+ struct PostgresBinaryCopyGlobalState : public GlobalFunctionData {
20
+ unique_ptr<BufferedFileWriter> file_writer;
21
+
22
+ void Flush (PostgresBinaryWriter &writer) {
23
+ file_writer->WriteData (writer.stream .GetData (), writer.stream .GetPosition ());
24
+ }
25
+
26
+ void WriteHeader () {
27
+ PostgresBinaryWriter writer;
28
+ writer.WriteHeader ();
29
+ Flush (writer);
30
+ }
31
+
32
+ void WriteChunk (DataChunk &chunk) {
33
+ chunk.Flatten ();
34
+ PostgresBinaryWriter writer;
35
+ for (idx_t r = 0 ; r < chunk.size (); r++) {
36
+ writer.BeginRow (chunk.ColumnCount ());
37
+ for (idx_t c = 0 ; c < chunk.ColumnCount (); c++) {
38
+ auto &col = chunk.data [c];
39
+ writer.WriteValue (col, r);
40
+ }
41
+ writer.FinishRow ();
42
+ }
43
+ Flush (writer);
44
+ }
45
+
46
+ void Flush () {
47
+ // write the footer
48
+ PostgresBinaryWriter writer;
49
+ writer.WriteFooter ();
50
+ Flush (writer);
51
+ // flush and close the file
52
+ file_writer->Flush ();
53
+ file_writer.reset ();
54
+ }
55
+ };
56
+
57
+ struct PostgresBinaryWriteBindData : public TableFunctionData {
58
+ };
59
+
60
+ unique_ptr<FunctionData> PostgresBinaryCopyFunction::PostgresBinaryWriteBind (ClientContext &context, CopyFunctionBindInput &input,
61
+ const vector<string> &names, const vector<LogicalType> &sql_types) {
62
+ return make_uniq<PostgresBinaryWriteBindData>();
63
+ }
64
+
65
+ unique_ptr<GlobalFunctionData> PostgresBinaryCopyFunction::PostgresBinaryWriteInitializeGlobal (ClientContext &context, FunctionData &bind_data,
66
+ const string &file_path) {
67
+ auto result = make_uniq<PostgresBinaryCopyGlobalState>();
68
+ auto &fs = FileSystem::GetFileSystem (context);
69
+ result->file_writer = make_uniq<BufferedFileWriter>(fs, file_path);
70
+ // write the header
71
+ result->WriteHeader ();
72
+ return std::move (result);
73
+ }
74
+
75
+ unique_ptr<LocalFunctionData> PostgresBinaryCopyFunction::PostgresBinaryWriteInitializeLocal (ExecutionContext &context, FunctionData &bind_data_p) {
76
+ return make_uniq<LocalFunctionData>();
77
+ }
78
+
79
+ void PostgresBinaryCopyFunction::PostgresBinaryWriteSink (ExecutionContext &context, FunctionData &bind_data_p, GlobalFunctionData &gstate_p,
80
+ LocalFunctionData &lstate, DataChunk &input) {
81
+ auto &gstate = gstate_p.Cast <PostgresBinaryCopyGlobalState>();
82
+ gstate.WriteChunk (input);
83
+ }
84
+
85
+ void PostgresBinaryCopyFunction::PostgresBinaryWriteCombine (ExecutionContext &context, FunctionData &bind_data, GlobalFunctionData &gstate,
86
+ LocalFunctionData &lstate) {
87
+ }
88
+
89
+ void PostgresBinaryCopyFunction::PostgresBinaryWriteFinalize (ClientContext &context, FunctionData &bind_data, GlobalFunctionData &gstate_p) {
90
+ auto &gstate = gstate_p.Cast <PostgresBinaryCopyGlobalState>();
91
+ // write the footer and close the file
92
+ gstate.Flush ();
93
+ }
94
+
95
+
96
+ }
0 commit comments