Skip to content

Commit

Permalink
add option to disable analysis (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
hderms authored Apr 28, 2021
1 parent cb0b6d8 commit 024694e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
31 changes: 30 additions & 1 deletion src/csv/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ impl ColumnInference {
ColumnInference { columns_to_types }
}

/// build column 'inference' with every column artificially inferred as a String
pub fn default_inference(csv: &CsvData) -> ColumnInference {
let mut columns_to_types: HashMap<String, CsvType> = HashMap::new();
for header in csv.headers.iter() {
columns_to_types.insert(String::from(header), CsvType::String);
}
debug!("Using default column type of string for all columns in file {}: {:?} ", csv.filename, columns_to_types);
ColumnInference { columns_to_types }
}

/// get the type of a column, referenced by its string name
pub fn get_type(&self, s: String) -> Option<&CsvType> {
self.columns_to_types.get(s.as_str())
Expand Down Expand Up @@ -58,12 +68,12 @@ mod test {
}
#[test]
fn it_should_recognize_integer_column() {
let filename: String = String::from("foo.csv");
let headers = StringRecord::from(vec!["foo", "bar"]);
let records = vec![
StringRecord::from(vec!["entry1", "1"]),
StringRecord::from(vec!["entry2", "2"]),
];
let filename = String::from("foo.csv");
let inference = ColumnInference::from_csv(&CsvData { headers, records, filename });
assert_eq!(
inference.get_type(String::from("foo")),
Expand All @@ -74,4 +84,23 @@ mod test {
Some(&CsvType::Numeric)
);
}

#[test]
fn it_should_use_default_column_type_if_inference_disabled() {
let headers = StringRecord::from(vec!["foo", "bar"]);
let filename: String = String::from("foo.csv");
let records = vec![
StringRecord::from(vec!["entry1", "1"]),
StringRecord::from(vec!["entry2", "2"]),
];
let inference = ColumnInference::default_inference(&CsvData { headers, records, filename });
assert_eq!(
inference.get_type(String::from("foo")),
Some(&CsvType::String)
);
assert_eq!(
inference.get_type(String::from("bar")),
Some(&CsvType::String)
);
}
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct Opts {
delimiter: char,
#[clap(long)]
trim: bool,
#[clap(long)]
textonly: bool,
}

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -26,7 +28,8 @@ fn main() -> Result<(), Box<dyn Error>> {
let query = opts.query;
let delimiter = opts.delimiter;
let trim = opts.trim;
let options = Options{delimiter, trim};
let textonly = opts.textonly;
let options = Options{delimiter, trim, textonly};
let results = execute_query(query.as_str(), &options)?;
write_to_stdout(results)?;
Ok(())
Expand Down
9 changes: 7 additions & 2 deletions src/qsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use log::debug;
type Rows = Vec<Vec<String>>;
pub struct Options {
pub delimiter: char,
pub trim: bool
pub trim: bool,
pub textonly: bool,
}

///Executes a query, possibly returning Rows
Expand Down Expand Up @@ -53,7 +54,11 @@ fn maybe_load_file(
debug!("Attempting to load identifier from SQL as file: {}", filename);
let table_name = path.file_stem(); //TODO: should we canonicalize path?
let table_name = sanitize(table_name).unwrap_or_else(|| Uuid::new_v4().to_string());
let inference = ColumnInference::from_csv(&csv);
let inference = if options.textonly {
ColumnInference::default_inference(&csv)
} else {
ColumnInference::from_csv(&csv)
};
let table_parameters = to_table_parameters(&csv, &inference);
let table_parameters: Vec<&str> = table_parameters.iter().map(|s| s.as_str()).collect();
let table_name = table_name.as_str();
Expand Down
4 changes: 4 additions & 0 deletions testdata/sortable_columns.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
column
12
99
9
9 changes: 9 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,12 @@ fn it_will_run_with_trim() -> Result<(), Box<dyn std::error::Error>> {
cmd.assert().success();
Ok(())
}

#[test]
fn it_will_run_with_textonly() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("qsv")?;
cmd.arg("select md5(column) from testdata/sortable_columns.csv");
cmd.arg("--textonly");
cmd.assert().success();
Ok(())
}

0 comments on commit 024694e

Please sign in to comment.