-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathcopy.rs
139 lines (127 loc) · 4.42 KB
/
copy.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
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.
use std::path::PathBuf;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use super::*;
#[derive(Debug, PartialEq, PartialOrd, Ord, Hash, Eq, Clone, Serialize, Deserialize)]
pub struct ExtSource {
pub path: PathBuf,
pub format: FileFormat,
}
/// File format.
#[derive(Debug, PartialEq, PartialOrd, Ord, Hash, Eq, Clone, Serialize, Deserialize)]
pub enum FileFormat {
Csv {
/// Delimiter to parse.
delimiter: char,
/// Quote to use.
quote: char,
/// Escape character to use.
escape: Option<char>,
/// Whether or not the file has a header line.
header: bool,
},
}
impl std::fmt::Display for ExtSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
impl std::fmt::Display for FileFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
impl FromStr for Box<ExtSource> {
type Err = ();
fn from_str(_s: &str) -> std::result::Result<Self, Self::Err> {
Err(())
}
}
impl Binder {
pub(super) fn bind_copy(
&mut self,
source: CopySource,
to: bool,
target: CopyTarget,
options: &[CopyOption],
) -> Result {
let ext_source = self.egraph.add(Node::ExtSource(Box::new(ExtSource {
path: match target {
CopyTarget::File { filename } => filename.into(),
t => todo!("unsupported copy target: {:?}", t),
},
format: FileFormat::from_options(options),
})));
let copy = if to {
// COPY <source_table> TO <dest_file>
let query = match source {
CopySource::Table {
table_name,
columns,
} => {
let (table, _, _) = self.bind_table_id(&table_name)?;
let cols = self.bind_table_columns(&table_name, &columns)?;
let true_ = self.egraph.add(Node::true_());
self.egraph.add(Node::Scan([table, cols, true_]))
}
CopySource::Query(query) => self.bind_query(*query)?.0,
};
self.egraph.add(Node::CopyTo([ext_source, query]))
} else {
// COPY <dest_table> FROM <source_file>
let (table, cols) = match source {
CopySource::Table {
table_name,
columns,
} => {
let (table, is_system, is_view) = self.bind_table_id(&table_name)?;
if is_system {
return Err(
ErrorKind::CopyTo("system table".into()).with_spanned(&table_name)
);
} else if is_view {
return Err(ErrorKind::CopyTo("view".into()).with_spanned(&table_name));
}
let cols = self.bind_table_columns(&table_name, &columns)?;
(table, cols)
}
CopySource::Query(query) => {
return Err(ErrorKind::CopyTo("query".into()).with_spanned(&*query));
}
};
let types = self.type_(cols)?;
let types = self.egraph.add(Node::Type(types));
let copy = self.egraph.add(Node::CopyFrom([ext_source, types]));
self.egraph.add(Node::Insert([table, cols, copy]))
};
Ok(copy)
}
}
impl FileFormat {
/// Create from copy options.
pub fn from_options(options: &[CopyOption]) -> Self {
let mut delimiter = ',';
let mut quote = '"';
let mut escape = None;
let mut header = false;
for opt in options {
match opt {
CopyOption::Format(fmt) => {
assert_eq!(fmt.value.to_lowercase(), "csv", "only support CSV format")
}
CopyOption::Delimiter(c) => delimiter = *c,
CopyOption::Header(b) => header = *b,
CopyOption::Quote(c) => quote = *c,
CopyOption::Escape(c) => escape = Some(*c),
o => panic!("unsupported copy option: {:?}", o),
}
}
FileFormat::Csv {
delimiter,
quote,
escape,
header,
}
}
}