-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathon_conflict.rs
216 lines (187 loc) · 7.29 KB
/
on_conflict.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use datafusion::arrow::datatypes::SchemaRef;
use itertools::Itertools;
use sea_query::{self, Alias};
use snafu::prelude::*;
use std::fmt::Display;
use super::column_reference::{self, ColumnReference};
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display(r#"Invalid column reference: {source}"#))]
InvalidColumnReference { source: column_reference::Error },
#[snafu(display("Expected do_nothing or upsert, found: {token}"))]
UnexpectedToken { token: String },
#[snafu(display("Expected semicolon in: {token}"))]
ExpectedSemicolon { token: String },
}
#[derive(Debug, Clone, PartialEq)]
pub enum OnConflict {
DoNothingAll,
DoNothing(ColumnReference),
Upsert(ColumnReference),
}
impl OnConflict {
#[must_use]
pub fn build_on_conflict_statement(&self, schema: &SchemaRef) -> String {
match self {
OnConflict::DoNothingAll => "ON CONFLICT DO NOTHING".to_string(),
OnConflict::DoNothing(column) => {
format!(
r#"ON CONFLICT ("{}") DO NOTHING"#,
column.iter().join(r#"", ""#)
)
}
OnConflict::Upsert(column) => {
let non_constraint_columns = schema
.fields()
.iter()
.filter(|f| !column.contains(f.name()))
.map(|f| f.name().to_string())
.collect::<Vec<String>>();
let mut update_cols = String::new();
for (i, col) in non_constraint_columns.iter().enumerate() {
update_cols.push_str(&format!(r#""{col}" = EXCLUDED."{col}""#));
if i < non_constraint_columns.len() - 1 {
update_cols.push_str(", ");
}
}
format!(
r#"ON CONFLICT ("{}") DO UPDATE SET {update_cols}"#,
column.iter().join(r#"", ""#)
)
}
}
}
#[must_use]
pub fn build_sea_query_on_conflict(&self, schema: &SchemaRef) -> sea_query::OnConflict {
match self {
OnConflict::DoNothingAll => {
let mut on_conflict = sea_query::OnConflict::new();
on_conflict.do_nothing();
on_conflict
}
OnConflict::DoNothing(column) => {
let mut on_conflict = sea_query::OnConflict::columns::<Vec<Alias>, Alias>(
column.iter().map(Alias::new).collect(),
);
on_conflict.do_nothing();
on_conflict
}
OnConflict::Upsert(column) => {
let mut on_conflict = sea_query::OnConflict::columns::<Vec<Alias>, Alias>(
column.iter().map(Alias::new).collect(),
);
let non_constraint_columns = schema
.fields()
.iter()
.filter(|f| !column.contains(f.name()))
.map(|f| Alias::new(f.name()))
.collect::<Vec<Alias>>();
on_conflict.update_columns(non_constraint_columns);
on_conflict
}
}
}
}
impl Display for OnConflict {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OnConflict::DoNothingAll => write!(f, "do_nothing_all"),
OnConflict::DoNothing(column) => write!(f, "do_nothing:{column}"),
OnConflict::Upsert(column) => write!(f, "upsert:{column}"),
}
}
}
impl TryFrom<&str> for OnConflict {
type Error = Error;
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
if value == "do_nothing_all" {
return Ok(OnConflict::DoNothingAll);
}
let parts: Vec<&str> = value.split(':').collect();
if parts.len() != 2 {
return ExpectedSemicolonSnafu {
token: value.to_string(),
}
.fail();
}
let column_ref =
ColumnReference::try_from(parts[1]).context(InvalidColumnReferenceSnafu)?;
let on_conflict_behavior = parts[0];
match on_conflict_behavior {
"do_nothing" => Ok(OnConflict::DoNothing(column_ref)),
"upsert" => Ok(OnConflict::Upsert(column_ref)),
_ => UnexpectedTokenSnafu {
token: parts[0].to_string(),
}
.fail(),
}
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use datafusion::arrow::datatypes::{DataType, Field, Schema};
use crate::util::{column_reference::ColumnReference, on_conflict::OnConflict};
#[test]
fn test_on_conflict_from_str() {
let on_conflict = OnConflict::try_from("do_nothing_all").expect("valid on conflict");
assert_eq!(on_conflict, OnConflict::DoNothingAll);
let on_conflict = OnConflict::try_from("do_nothing:col1").expect("valid on conflict");
assert_eq!(
on_conflict,
OnConflict::DoNothing(ColumnReference::new(vec!["col1".to_string()]))
);
let on_conflict = OnConflict::try_from("upsert:col2").expect("valid on conflict");
assert_eq!(
on_conflict,
OnConflict::Upsert(ColumnReference::new(vec!["col2".to_string()]))
);
let err = OnConflict::try_from("do_nothing").expect_err("invalid on conflict");
assert_eq!(
err.to_string(),
"Expected semicolon in: do_nothing".to_string()
);
}
#[test]
fn test_roundtrip() {
let on_conflict = OnConflict::DoNothingAll.to_string();
assert_eq!(
OnConflict::try_from(on_conflict.as_str()).expect("valid on conflict"),
OnConflict::DoNothingAll
);
let on_conflict =
OnConflict::DoNothing(ColumnReference::new(vec!["col1".to_string()])).to_string();
assert_eq!(
OnConflict::try_from(on_conflict.as_str()).expect("valid on conflict"),
OnConflict::DoNothing(ColumnReference::new(vec!["col1".to_string()]))
);
let on_conflict =
OnConflict::Upsert(ColumnReference::new(vec!["col2".to_string()])).to_string();
assert_eq!(
OnConflict::try_from(on_conflict.as_str()).expect("valid on conflict"),
OnConflict::Upsert(ColumnReference::new(vec!["col2".to_string()]))
);
}
#[test]
fn test_build_on_conflict_statement() {
let schema = Arc::new(Schema::new(vec![
Field::new("col1", DataType::Int64, false),
Field::new("col2", DataType::Int64, false),
]));
let on_conflict = OnConflict::DoNothingAll;
assert_eq!(
on_conflict.build_on_conflict_statement(&schema),
"ON CONFLICT DO NOTHING".to_string()
);
let on_conflict = OnConflict::DoNothing(ColumnReference::new(vec!["col1".to_string()]));
assert_eq!(
on_conflict.build_on_conflict_statement(&schema),
r#"ON CONFLICT ("col1") DO NOTHING"#.to_string()
);
let on_conflict = OnConflict::Upsert(ColumnReference::new(vec!["col2".to_string()]));
assert_eq!(
on_conflict.build_on_conflict_statement(&schema),
r#"ON CONFLICT ("col2") DO UPDATE SET "col1" = EXCLUDED."col1""#.to_string()
);
}
}