-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathwrite.rs
196 lines (166 loc) · 4.92 KB
/
write.rs
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
use std::{any::Any, fmt, sync::Arc};
use async_trait::async_trait;
use datafusion::arrow::datatypes::SchemaRef;
use datafusion::{
catalog::Session,
common::Constraints,
datasource::{TableProvider, TableType},
execution::{SendableRecordBatchStream, TaskContext},
logical_expr::{dml::InsertOp, Expr},
physical_plan::{
insert::{DataSink, DataSinkExec},
metrics::MetricsSet,
DisplayAs, DisplayFormatType, ExecutionPlan,
},
};
use futures::StreamExt;
use snafu::prelude::*;
use crate::util::{
constraints, on_conflict::OnConflict, retriable_error::check_and_mark_retriable_error,
};
use crate::postgres::Postgres;
use super::to_datafusion_error;
#[derive(Debug, Clone)]
pub struct PostgresTableWriter {
pub read_provider: Arc<dyn TableProvider>,
postgres: Arc<Postgres>,
on_conflict: Option<OnConflict>,
}
impl PostgresTableWriter {
pub fn create(
read_provider: Arc<dyn TableProvider>,
postgres: Postgres,
on_conflict: Option<OnConflict>,
) -> Arc<Self> {
Arc::new(Self {
read_provider,
postgres: Arc::new(postgres),
on_conflict,
})
}
pub fn postgres(&self) -> Arc<Postgres> {
Arc::clone(&self.postgres)
}
}
#[async_trait]
impl TableProvider for PostgresTableWriter {
fn as_any(&self) -> &dyn Any {
self
}
fn schema(&self) -> SchemaRef {
self.read_provider.schema()
}
fn table_type(&self) -> TableType {
TableType::Base
}
fn constraints(&self) -> Option<&Constraints> {
Some(self.postgres.constraints())
}
async fn scan(
&self,
state: &dyn Session,
projection: Option<&Vec<usize>>,
filters: &[Expr],
limit: Option<usize>,
) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
self.read_provider
.scan(state, projection, filters, limit)
.await
}
async fn insert_into(
&self,
_state: &dyn Session,
input: Arc<dyn ExecutionPlan>,
op: InsertOp,
) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
Ok(Arc::new(DataSinkExec::new(
input,
Arc::new(PostgresDataSink::new(
Arc::clone(&self.postgres),
op == InsertOp::Overwrite,
self.on_conflict.clone(),
)),
self.schema(),
None,
)) as _)
}
}
#[derive(Clone)]
struct PostgresDataSink {
postgres: Arc<Postgres>,
overwrite: bool,
on_conflict: Option<OnConflict>,
}
#[async_trait]
impl DataSink for PostgresDataSink {
fn as_any(&self) -> &dyn Any {
self
}
fn metrics(&self) -> Option<MetricsSet> {
None
}
async fn write_all(
&self,
mut data: SendableRecordBatchStream,
_context: &Arc<TaskContext>,
) -> datafusion::common::Result<u64> {
let mut num_rows = 0;
let mut db_conn = self.postgres.connect().await.map_err(to_datafusion_error)?;
let postgres_conn = Postgres::postgres_conn(&mut db_conn).map_err(to_datafusion_error)?;
let tx = postgres_conn
.conn
.transaction()
.await
.context(super::UnableToBeginTransactionSnafu)
.map_err(to_datafusion_error)?;
if self.overwrite {
self.postgres
.delete_all_table_data(&tx)
.await
.map_err(to_datafusion_error)?;
}
while let Some(batch) = data.next().await {
let batch = batch.map_err(check_and_mark_retriable_error)?;
let batch_num_rows = batch.num_rows();
if batch_num_rows == 0 {
continue;
};
num_rows += batch_num_rows as u64;
constraints::validate_batch_with_constraints(
&[batch.clone()],
self.postgres.constraints(),
)
.await
.context(super::ConstraintViolationSnafu)
.map_err(to_datafusion_error)?;
self.postgres
.insert_batch(&tx, batch, self.on_conflict.clone())
.await
.map_err(to_datafusion_error)?;
}
tx.commit()
.await
.context(super::UnableToCommitPostgresTransactionSnafu)
.map_err(to_datafusion_error)?;
Ok(num_rows)
}
}
impl PostgresDataSink {
fn new(postgres: Arc<Postgres>, overwrite: bool, on_conflict: Option<OnConflict>) -> Self {
Self {
postgres,
overwrite,
on_conflict,
}
}
}
impl std::fmt::Debug for PostgresDataSink {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "PostgresDataSink")
}
}
impl DisplayAs for PostgresDataSink {
fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter) -> std::fmt::Result {
write!(f, "PostgresDataSink")
}
}