@@ -67,25 +67,40 @@ impl ImportFormat {
6767 ) -> Result < Pin < Box < dyn Stream < Item = Result < Option < Row > , CubeError > > + Send + ' a > > , CubeError >
6868 {
6969 match self {
70- ImportFormat :: CSV | ImportFormat :: CSVNoHeader => {
70+ ImportFormat :: CSV | ImportFormat :: CSVNoHeader | ImportFormat :: CSVOptions { .. } => {
7171 let lines_stream: Pin < Box < dyn Stream < Item = Result < String , CubeError > > + Send > > =
7272 Box :: pin ( CsvLineStream :: new ( reader) ) ;
7373
7474 let mut header_mapping = match self {
75- ImportFormat :: CSV => None ,
76- ImportFormat :: CSVNoHeader => Some (
75+ ImportFormat :: CSVNoHeader
76+ | ImportFormat :: CSVOptions {
77+ has_header : false , ..
78+ } => Some (
7779 columns
7880 . iter ( )
7981 . enumerate ( )
8082 . map ( |( i, c) | ( i, c. clone ( ) ) )
8183 . collect ( ) ,
8284 ) ,
85+ _ => None ,
8386 } ;
8487
88+ let delimiter = match self {
89+ ImportFormat :: CSV | ImportFormat :: CSVNoHeader => ',' ,
90+ ImportFormat :: CSVOptions { delimiter, .. } => delimiter. unwrap_or ( ',' ) ,
91+ } ;
92+
93+ if delimiter as u16 > 255 {
94+ return Err ( CubeError :: user ( format ! (
95+ "Non ASCII delimiters are unsupported: '{}'" ,
96+ delimiter
97+ ) ) ) ;
98+ }
99+
85100 let rows = lines_stream. map ( move |line| -> Result < Option < Row > , CubeError > {
86101 let str = line?;
87102
88- let mut parser = CsvLineParser :: new ( str. as_str ( ) ) ;
103+ let mut parser = CsvLineParser :: new ( delimiter as u8 , str. as_str ( ) ) ;
89104
90105 if header_mapping. is_none ( ) {
91106 let mut mapping = Vec :: new ( ) ;
@@ -257,13 +272,15 @@ fn parse_binary_data(value: &str) -> Result<Vec<u8>, CubeError> {
257272}
258273
259274struct CsvLineParser < ' a > {
275+ delimiter : u8 ,
260276 line : & ' a str ,
261277 remaining : & ' a str ,
262278}
263279
264280impl < ' a > CsvLineParser < ' a > {
265- fn new ( line : & ' a str ) -> Self {
281+ fn new ( delimiter : u8 , line : & ' a str ) -> Self {
266282 Self {
283+ delimiter,
267284 line,
268285 remaining : line,
269286 }
@@ -308,7 +325,7 @@ impl<'a> CsvLineParser<'a> {
308325 . remaining
309326 . as_bytes ( )
310327 . iter ( )
311- . position ( |c| * c == b',' )
328+ . position ( |c| * c == self . delimiter )
312329 . unwrap_or ( self . remaining . len ( ) ) ;
313330 let res = & self . remaining [ 0 ..next_comma] ;
314331 self . remaining = self . remaining [ next_comma..] . as_ref ( ) ;
@@ -318,8 +335,10 @@ impl<'a> CsvLineParser<'a> {
318335 }
319336
320337 fn advance ( & mut self ) -> Result < ( ) , CubeError > {
321- if let Some ( b',' ) = self . remaining . as_bytes ( ) . iter ( ) . nth ( 0 ) {
322- self . remaining = self . remaining [ 1 ..] . as_ref ( )
338+ if let Some ( c) = self . remaining . as_bytes ( ) . iter ( ) . nth ( 0 ) {
339+ if * c == self . delimiter {
340+ self . remaining = self . remaining [ 1 ..] . as_ref ( )
341+ }
323342 }
324343 Ok ( ( ) )
325344 }
0 commit comments