Skip to content

Commit

Permalink
Update cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyutarow committed May 23, 2021
1 parent 643dfaf commit 47fa597
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 70 deletions.
8 changes: 4 additions & 4 deletions samples/q1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
"hr": {
"employees": [
{
"name": "Bob Smith",
"id": 3,
"title": null,
"name": "Bob Smith"
"title": null
},
{
"title": "Dev Mgr",
"id": 4,
"name": "Susan Smith"
},
{
"id": 6,
"title": "Software Eng 2",
"name": "Jane Smith",
"title": "Software Eng 2"
"id": 6
}
]
}
Expand Down
38 changes: 38 additions & 0 deletions samples/q2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"hr": {
"employeesNest": [
{
"title": null,
"id": 3,
"projects": [
{
"name": "AWS Redshift Spectrum querying"
},
{
"name": "AWS Redshift security"
},
{
"name": "AWS Aurora security"
}
],
"name": "Bob Smith"
},
{
"title": "Dev Mgr",
"name": "Susan Smith",
"id": 4,
"projects": []
},
{
"id": 6,
"title": "Software Eng 2",
"projects": [
{
"name": "AWS Redshift security"
}
],
"name": "Jane Smith"
}
]
}
}
46 changes: 14 additions & 32 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::path::{Path, PathBuf};
use regex::Regex;
use structopt::StructOpt;

use partiql::dsql_parser as sql_parser;
use partiql::models::JsonValue;
use partiql::pqlir_parser as parser;
use partiql::sql::Sql;
use partiql::sql_parser;
use partiql::sql::run;

fn read_from_stdin() -> anyhow::Result<String> {
let mut buf = String::new();
Expand Down Expand Up @@ -80,38 +80,20 @@ fn main() -> anyhow::Result<()> {

let sql = sql_parser::sql(&query)?;

if let Some(output) = run(sql, data) {
let s = match to.as_str() {
"json" => {
let s = serde_json::to_string(&output).unwrap();
s
}
_ => {
let s = serde_partiql::to_string(&output).unwrap();
s
}
};
println!("{}", s);
} else {
eprintln!("failed");
}
let output = run(sql, data);
let s = match to.as_str() {
"json" => {
let s = serde_json::to_string(&output).unwrap();
s
}
_ => {
let s = serde_partiql::to_string(&output).unwrap();
s
}
};
println!("{}", s);
}
};

Ok(())
}

fn run(sql: Sql, data: JsonValue) -> Option<JsonValue> {
let from_clause = sql.from_clause.first().unwrap();
let full_path = format!("{}.{}", from_clause.source, from_clause.path);
let from_path = full_path.split(".").collect::<Vec<_>>();

let rows = data.get_path(&from_path).unwrap();

let field_list = sql.select_clause;
let data = rows.select_map(&field_list).unwrap();

let cond = sql.where_clause.unwrap();
let data = data.filter_map(cond).unwrap();
Some(data)
}
46 changes: 46 additions & 0 deletions src/lib/sql/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::collections::HashMap;

use itertools::Itertools;

use crate::dsql_parser;
use crate::models::JsonValue;
use crate::pqlir_parser;
use crate::sql::to_list;
use crate::sql::Bingings;
use crate::sql::DField;
use crate::sql::DSql as Sql;
use crate::sql::DWhereCond;
use crate::sql::Dpath;
use crate::sql_parser;

pub fn run(sql: Sql, data: JsonValue) -> JsonValue {
let fields = sql
.select_clause
.iter()
.chain(sql.from_clause.iter())
.map(|e| e.to_owned())
.collect::<Vec<_>>();
let bindings = Bingings::from(fields.as_slice());

let select_fields = sql
.select_clause
.iter()
.map(|field| field.to_owned().full(&bindings))
.collect::<Vec<_>>();
let bindings_for_select = Bingings::from(select_fields.as_slice());

let value = data.select_by_fields(&select_fields).unwrap();
let list = to_list(value);

let filtered_list = list
.iter()
.filter_map(|value| match &sql.where_clause {
Some(cond) if cond.eval(&value.to_owned(), &bindings, &bindings_for_select) => {
Some(value.to_owned())
}
_ => None,
})
.collect::<Vec<JsonValue>>();

JsonValue::Array(filtered_list)
}
2 changes: 2 additions & 0 deletions src/lib/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::collections::HashMap;
use crate::models::JsonValue;

mod bingings;
mod eval;
mod utils;
pub use bingings::Bingings;
pub use eval::run;
pub use utils::to_list;

#[derive(Debug, Clone, PartialEq)]
Expand Down
35 changes: 1 addition & 34 deletions tests/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use itertools::Itertools;
use partiql::dsql_parser;
use partiql::models::JsonValue;
use partiql::pqlir_parser;
use partiql::sql::run;
use partiql::sql::to_list;
use partiql::sql::Bingings;
use partiql::sql::DField;
Expand All @@ -13,40 +14,6 @@ use partiql::sql::DWhereCond;
use partiql::sql::Dpath;
use partiql::sql_parser;

fn run(sql: Sql, data: JsonValue) -> JsonValue {
let fields = sql
.select_clause
.iter()
.chain(sql.from_clause.iter())
.map(|e| e.to_owned())
.collect::<Vec<_>>();
let bindings = Bingings::from(fields.as_slice());

let select_fields = sql
.select_clause
.iter()
.map(|field| field.to_owned().full(&bindings))
.collect::<Vec<_>>();
let bindings_for_select = Bingings::from(select_fields.as_slice());

let value = data.select_by_fields(&select_fields).unwrap();
let list = to_list(value);
dbg!(&list);
dbg!(&sql);

let filtered_list = list
.iter()
.filter_map(|value| match &sql.where_clause {
Some(cond) if cond.eval(&value.to_owned(), &bindings, &bindings_for_select) => {
Some(value.to_owned())
}
_ => None,
})
.collect::<Vec<JsonValue>>();

JsonValue::Array(filtered_list)
}

#[test]
fn q1() -> anyhow::Result<()> {
let sql = {
Expand Down

0 comments on commit 47fa597

Please sign in to comment.