Skip to content

Commit 314ced3

Browse files
committed
fix: improve separator detection and error handling in CSV processing
1 parent 69ea98c commit 314ced3

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/filterx/src/files/csv.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::args::{CsvCommand, ShareArgs};
2+
use filterx_core::{util, writer::FilterxWriter, FilterxResult};
23
use filterx_engine::vm::Vm;
34
use filterx_source::{detect_columns, DataframeSource, Source, SourceType};
45

5-
use filterx_core::{util, writer::FilterxWriter, FilterxResult};
6-
76
pub fn filterx_csv(cmd: CsvCommand) -> FilterxResult<()> {
87
let CsvCommand {
98
share_args:
@@ -25,7 +24,13 @@ pub fn filterx_csv(cmd: CsvCommand) -> FilterxResult<()> {
2524
} = cmd;
2625
let separator = match separator {
2726
Some(s) => Some(s),
28-
None => util::detect_separator(path.as_str(), 20)?,
27+
None => match util::detect_separator(path.as_str(), 20)? {
28+
Some(s) => Some(s),
29+
None => {
30+
eprintln!("Cannot detect separator, parse as one column.");
31+
None
32+
}
33+
},
2934
};
3035
let output_separator;
3136
if _output_separator.is_none() {

src/filterx_core/src/util.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use polars::{
66
};
77

88
use crate::{
9-
sep::Separator, thread_size::ThreadSize, writer::FilterxWriter, FilterxError, FilterxResult,
9+
reader::FilterxReader, sep::Separator, thread_size::ThreadSize, writer::FilterxWriter,
10+
FilterxError, FilterxResult,
1011
};
1112
use std::io::Write;
1213
use std::num::NonZero;
@@ -146,10 +147,9 @@ pub fn write_df(
146147
}
147148

148149
pub fn collect_comment_lines(path: &str, comment_prefix: &str) -> FilterxResult<Vec<String>> {
149-
use std::fs::File;
150150
use std::io::BufRead;
151151
use std::io::BufReader;
152-
let file = File::open(path)?;
152+
let file = FilterxReader::new(path)?;
153153
let mut reader = BufReader::new(file);
154154
let mut line = String::new();
155155
let mut comment_lines = Vec::new();
@@ -166,10 +166,9 @@ pub fn collect_comment_lines(path: &str, comment_prefix: &str) -> FilterxResult<
166166
}
167167

168168
pub fn detect_separator(path: &str, nline: usize) -> FilterxResult<Option<String>> {
169-
use std::fs::File;
170169
use std::io::BufRead;
171170
use std::io::BufReader;
172-
let file = File::open(path)?;
171+
let file = FilterxReader::new(path)?;
173172
let mut reader = BufReader::new(file);
174173
let mut line = String::new();
175174
let mut lines = Vec::with_capacity(nline);

0 commit comments

Comments
 (0)