Skip to content

Commit

Permalink
sqlp: natural joins and DuckDB-like columns function are now supported
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Jul 1, 2024
1 parent f5277bb commit 2d3c7c2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cmd/sqlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Example queries:
qsv sqlp data.csv data2.csv 'SELECT col1 FROM data WHERE col1 IN (SELECT col2 FROM data2)'
# Unions and Joins are supported.
qsv sqlp data1.csv data2.csv 'SELECT * FROM data1 UNION ALL BY NAME SELECT * FROM data2'
qsv sqlp tbl_a.csv tbl_b.csv tbl_c.csv "SELECT * FROM tbl_a \
Expand All @@ -46,6 +47,10 @@ Example queries:
qsv sqlp tbl1.csv "SELECT x FROM tbl1 WHERE x IN (SELECT y FROM tbl1)"
# Natural Joins are supported too! (https://www.w3resource.com/sql/joins/natural-join.php)
qsv sqlp data1.csv data2.csv data3.csv \
"SELECT COLUMNS('^[^:]+$') FROM data1 NATURAL JOIN data2 NATURAL JOIN data3 ORDER BY COMPANY_ID",
# Use a SQL script to run a long, complex SQL query or to run SEVERAL SQL queries.
# When running several queries, each query needs to be separated by a semicolon,
# the last query will be returned as the result.
Expand Down
62 changes: 62 additions & 0 deletions tests/test_sqlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,68 @@ fn sqlp_string_like_ops() {
assert_eq!(got, expected);
}

#[test]
fn sqlp_natural_join() {
let wrk = Workdir::new("sqlp_natural_join");

wrk.create(
"data1.csv",
vec![
svec!["CharacterID", "FirstName", "LastName"],
svec!["1", "Jerna Morat", "Gurgeh"],
svec!["2", "Cheradenine", "Zakalwe"],
svec!["3", "Byr", "Genar-Hofoen"],
],
);

wrk.create(
"data2.csv",
vec![
svec!["CharacterID", "Book"],
svec!["1", "Player of Games"],
svec!["2", "Use of Weapons"],
svec!["3", "Excession"],
],
);

wrk.create(
"data3.csv",
vec![
svec!["CharacterID", "Ship"],
svec!["1", "Limiting Factor"],
svec!["2", "Xenophobe"],
svec!["3", "Grey Area"],
],
);

let mut cmd = wrk.command("sqlp");
cmd.args(["data1.csv", "data2.csv", "data3.csv"]).arg(
r#"SELECT COLUMNS('^[^:]+$')
FROM data1
NATURAL JOIN data2
NATURAL JOIN data3
ORDER BY CharacterID"#,
);

wrk.assert_success(&mut cmd);

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["CharacterID", "FirstName", "LastName", "Book", "Ship"],
svec![
"1",
"Jerna Morat",
"Gurgeh",
"Player of Games",
"Limiting Factor"
],
svec!["2", "Cheradenine", "Zakalwe", "Use of Weapons", "Xenophobe"],
svec!["3", "Byr", "Genar-Hofoen", "Excession", "Grey Area"],
];

assert_eq!(got, expected);
}

#[test]
fn sqlp_star_ilike() {
let wrk = Workdir::new("sqlp_star_ilike");
Expand Down

0 comments on commit 2d3c7c2

Please sign in to comment.