Skip to main content

run

Function run 

Source
pub fn run(sql: &str, base_dir: &Path) -> Result<CanonicalValue, Box<dyn Error>>
Expand description

Executes a SELECT statement over files in base_dir.

The FROM clause is resolved relative to base_dir. Each file is decoded using the sheeter engine’s format detection, so a single query can join a CSV, an XLSX sheet, and an NDJSON stream without any manual conversion step. The result is returned as a CanonicalValue::Array whose elements are row objects, ready to be serialized in any format the engine writes.

Column references may be qualified (u.name) or bare (name). Bare names are resolved by suffix match, so name finds users.name when only one table has a column ending in .name. Comparison operands are coerced to f64 when both sides parse as numbers, and to String otherwise, so id = 2 matches a CSV cell that contains the string "2".

§Errors

Returns an error if the SQL does not parse, the FROM clause references a missing file, a glob pattern fails to expand, a referenced column does not exist in any table, or an arithmetic operation cannot coerce its operands.

§Examples

use sheeter_engine::query;
use std::fs;

let dir = tempfile::tempdir().unwrap();
fs::write(dir.path().join("data.csv"), "a,b\n1,x\n2,y\n3,z\n").unwrap();
let result = query::run(
    "SELECT a FROM data.csv WHERE b != 'z' ORDER BY a DESC",
    dir.path(),
).unwrap();
assert_eq!(result.to_json().unwrap(), r#"[{"a":"2"},{"a":"1"}]"#);