|
| 1 | +use std::path::PathBuf; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use crate::planner::operator::copy_from_file::CopyFromFileOperator; |
| 5 | +use crate::planner::operator::copy_to_file::CopyToFileOperator; |
| 6 | +use crate::planner::operator::Operator; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use sqlparser::ast::{CopyOption, CopySource, CopyTarget}; |
| 9 | + |
| 10 | +use super::*; |
| 11 | + |
| 12 | +#[derive(Debug, PartialEq, PartialOrd, Ord, Hash, Eq, Clone, Serialize, Deserialize)] |
| 13 | +pub struct ExtSource { |
| 14 | + pub path: PathBuf, |
| 15 | + pub format: FileFormat, |
| 16 | +} |
| 17 | + |
| 18 | +/// File format. |
| 19 | +#[derive(Debug, PartialEq, PartialOrd, Ord, Hash, Eq, Clone, Serialize, Deserialize)] |
| 20 | +pub enum FileFormat { |
| 21 | + Csv { |
| 22 | + /// Delimiter to parse. |
| 23 | + delimiter: char, |
| 24 | + /// Quote to use. |
| 25 | + quote: char, |
| 26 | + /// Escape character to use. |
| 27 | + escape: Option<char>, |
| 28 | + /// Whether or not the file has a header line. |
| 29 | + header: bool, |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +impl std::fmt::Display for ExtSource { |
| 34 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 35 | + write!(f, "{self:?}") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl std::fmt::Display for FileFormat { |
| 40 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 41 | + write!(f, "{self:?}") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl FromStr for ExtSource { |
| 46 | + type Err = (); |
| 47 | + fn from_str(_s: &str) -> std::result::Result<Self, Self::Err> { |
| 48 | + Err(()) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<S: Storage> Binder<S> { |
| 53 | + pub(super) async fn bind_copy( |
| 54 | + &mut self, |
| 55 | + source: CopySource, |
| 56 | + to: bool, |
| 57 | + target: CopyTarget, |
| 58 | + options: &[CopyOption], |
| 59 | + ) -> Result<LogicalPlan, BindError> { |
| 60 | + let (table_name, ..) = match source { |
| 61 | + CopySource::Table { |
| 62 | + table_name, |
| 63 | + columns, |
| 64 | + } => (table_name, columns), |
| 65 | + CopySource::Query(_) => { |
| 66 | + return Err(BindError::UnsupportedCopySource( |
| 67 | + "bad copy source".to_string(), |
| 68 | + )); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + if let Some(table) = self.context.storage.table(&table_name.to_string()).await { |
| 73 | + let cols = table.all_columns(); |
| 74 | + let ext_source = ExtSource { |
| 75 | + path: match target { |
| 76 | + CopyTarget::File { filename } => filename.into(), |
| 77 | + t => todo!("unsupported copy target: {:?}", t), |
| 78 | + }, |
| 79 | + format: FileFormat::from_options(options), |
| 80 | + }; |
| 81 | + let types = cols.iter().map(|c| c.desc.column_datatype).collect(); |
| 82 | + |
| 83 | + let copy = if to { |
| 84 | + // COPY <source_table> TO <dest_file> |
| 85 | + LogicalPlan { |
| 86 | + operator: Operator::CopyToFile(CopyToFileOperator { source: ext_source }), |
| 87 | + childrens: vec![], |
| 88 | + } |
| 89 | + } else { |
| 90 | + // COPY <dest_table> FROM <source_file> |
| 91 | + LogicalPlan { |
| 92 | + operator: Operator::CopyFromFile(CopyFromFileOperator { |
| 93 | + source: ext_source, |
| 94 | + types, |
| 95 | + columns: cols, |
| 96 | + table: table_name.to_string(), |
| 97 | + }), |
| 98 | + childrens: vec![], |
| 99 | + } |
| 100 | + }; |
| 101 | + Ok(copy) |
| 102 | + } else { |
| 103 | + Err(BindError::InvalidTable(format!( |
| 104 | + "not found table {}", |
| 105 | + table_name |
| 106 | + ))) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl FileFormat { |
| 112 | + /// Create from copy options. |
| 113 | + pub fn from_options(options: &[CopyOption]) -> Self { |
| 114 | + let mut delimiter = ','; |
| 115 | + let mut quote = '"'; |
| 116 | + let mut escape = None; |
| 117 | + let mut header = false; |
| 118 | + for opt in options { |
| 119 | + match opt { |
| 120 | + CopyOption::Format(fmt) => { |
| 121 | + assert_eq!(fmt.value.to_lowercase(), "csv", "only support CSV format") |
| 122 | + } |
| 123 | + CopyOption::Delimiter(c) => delimiter = *c, |
| 124 | + CopyOption::Header(b) => header = *b, |
| 125 | + CopyOption::Quote(c) => quote = *c, |
| 126 | + CopyOption::Escape(c) => escape = Some(*c), |
| 127 | + o => panic!("unsupported copy option: {:?}", o), |
| 128 | + } |
| 129 | + } |
| 130 | + FileFormat::Csv { |
| 131 | + delimiter, |
| 132 | + quote, |
| 133 | + escape, |
| 134 | + header, |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments