Skip to content

Commit

Permalink
Self review
Browse files Browse the repository at this point in the history
  • Loading branch information
am357 committed Feb 16, 2023
1 parent 7ff6995 commit f97a1ab
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 63 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ partiql-eval = { git = "https://github.com/partiql/partiql-lang-rust.git", vers
partiql-value = { git = "https://github.com/partiql/partiql-lang-rust.git", features=["serde"], version = "0.2.*"}
partiql-logical = { git = "https://github.com/partiql/partiql-lang-rust.git", features=["serde"], version = "0.2.*"}

ion-rs = "0.14"

rust_decimal = "1.27"
serde_json = "1.*"
wasm-bindgen = "0.2"

35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
# PartiQL Playground (Proof of Concept)

PartiQL Playground is intended to provide the required code for enabling execution of PartiQL queries on web.
PartiQL Playground provides a functionality to execute PartiQL on client on web environment. It also provides
REST API endpoints for the same functionality.

_Please note, at this stage the code within this package is considered experimental and should not be used for production._
_Please note, at this stage, the code as is in this package is considered experimental and should not be used for production._

## Features
- Ability to `parse`, `explain`, and `evaluate` given query statements.
- Ability to share sessions with session import and export.
- Ability to run both as a docker container and standalone node server.
- Expose `/parse`, `/explain`, and `/eval` REST APIs.
```bash
# Example for parsing of `SELECT * FROM {'a': 1}` statement
curl -H 'Content-Type: application/json; charset=UTF-8' -H "Accept: application/json" --data '{"query": "SELECT * FROM {'a': 1}"}' -X POST http://localhost:8000/parse
# Example for parsing `SELECT * FROM {'a': 1}` statement
curl -H 'Content-Type: application/json; charset=UTF-8' \
-H "Accept: application/json" \
--data '{"query": "SELECT * FROM {'a': 1}"}' \
-X POST http://localhost:8000/parse

"{\"text\":\"SELECT * FROM {a: 1}\",\"offsets\":{\"line_starts\":[0]},\"ast\":{\"Query\":{\"id\":9,\"node\":{\"set\":{\"id\":8,\"node\":{\"Select\":{\"id\":7,\"node\":{\"project\":{\"id\":1,\"node\":{\"kind\":\"ProjectStar\",\"setq\":\"All\"}},\"from\":{\"id\":6,\"node\":{\"source\":{\"FromLet\":{\"id\":5,\"node\":{\"expr\":{\"Struct\":{\"id\":4,\"node\":{\"fields\":[{\"first\":{\"VarRef\":{\"id\":2,\"node\":{\"name\":{\"value\":\"a\",\"case\":\"CaseInsensitive\"},\"qualifier\":\"Unqualified\"}}},\"second\":{\"Lit\":{\"id\":3,\"node\":{\"Int64Lit\":1}}}}]}}},\"kind\":\"Scan\",\"as_alias\":null,\"at_alias\":null,\"by_alias\":null}}}}},\"from_let\":null,\"where_clause\":null,\"group_by\":null,\"having\":null}}}},\"order_by\":null,\"limit\":null,\"offset\":null}}},\"locations\":{\"1\":{\"start\":0,\"end\":8},\"2\":{\"start\":15,\"end\":16},\"3\":{\"start\":18,\"end\":19},\"4\":{\"start\":14,\"end\":20},\"5\":{\"start\":14,\"end\":20},\"6\":{\"start\":9,\"end\":20},\"7\":{\"start\":0,\"end\":20},\"8\":{\"start\":0,\"end\":20},\"9\":{\"start\":0,\"end\":20}}}"%

# Example for explaining (logical planning) of `SELECT * FROM {'a': 1}` statement
curl -H 'Content-Type: application/json; charset=UTF-8' -H "Accept: application/json" --data '{"query": "SELECT * FROM {'a': 1}"}' -X POST http://localhost:8000/explain
# Example for explaining (logical planning) `SELECT * FROM {'a': 1}` statement
curl -H 'Content-Type: application/json; charset=UTF-8' \
-H "Accept: application/json" \
--data '{"query": "SELECT * FROM {'a': 1}"}' \
-X POST http://localhost:8000/explain

"\"LogicalPlan { nodes: [ProjectAll, Scan(Scan { expr: TupleExpr(TupleExpr { attrs: [VarRef(CaseInsensitive(\\\"a\\\"))], values: [Lit(1)] }), as_key: \\\"_1\\\", at_key: None }), Sink], edges: [(OpId(2), OpId(1), 0), (OpId(1), OpId(3), 0)] }\""%

# Example for evaluation of `SELECT * FROM env` statement
curl -H 'Content-Type: application/json; charset=UTF-8' -H "Accept: application/json" --data '{"query": "SELECT * FROM env", "env": "{\"a\": 1, \"b\": 2}"}' -X POST http://localhost:8000/eval
# Example for evaluating `SELECT * FROM env` statement
curl -H 'Content-Type: application/json; charset=UTF-8' \
-H "Accept: application/json" \
--data '{"query": "SELECT * FROM env", "env": "{\"a\": 1, \"b\": 2}"}' \
-X POST http://localhost:8000/eval

"{\"Bag\":[{\"Tuple\":{\"attrs\":[\"a\",\"b\"],\"vals\":[{\"Integer\":1},{\"Integer\":2}]}}]}"%
```

## TODO
- Logical Plan visualization
- More readable error reporting
- More structured and rigorous UI testing
- Better UX for query execution (E.g. using keyboard shortcuts)

## Local Usage
For local usage follow the below steps.

Expand Down
38 changes: 0 additions & 38 deletions src/Cargo.toml

This file was deleted.

18 changes: 11 additions & 7 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ <h3>The PartiQL Playground</h3>
<option value="0.2.0">0.2.0</option>
</select>
<button name="exec" id="btnExec" class="btn btn-dark">Run</button>
<button type="btnExport" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="Export">
<button type="btnExport" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false" title="Export">
Export
</button>
<div class="dropdown-menu">
<a class="dropdown-item" data-target="#exportModal" data-toggle="modal">session as URL...</a>
</div>
</div>
<div class="modal fade" id="exportModal" tabindex="-1" role="dialog" aria-labelledby="exportModalLabel" aria-hidden="true">
<div class="modal fade" id="exportModal" tabindex="-1" role="dialog" aria-labelledby="exportModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
Expand All @@ -56,8 +58,10 @@ <h5 class="modal-title" id="exportModalLabel">Export</h5>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="openNewUrlWindow()">Open in new tab</button>
<button type="button" class="btn btn-primary" id="btnUrlCopy" onclick="copyUrlClip()" data-dismiss="modal">
<button type="button" class="btn btn-primary" onclick="openNewUrlWindow()">Open in new tab
</button>
<button type="button" class="btn btn-primary" id="btnUrlCopy" onclick="copyUrlClip()"
data-dismiss="modal">
Copy and Close
</button>
</div>
Expand Down Expand Up @@ -232,7 +236,7 @@ <h5 class="modal-title" id="exportModalLabel">Export</h5>
btnJsonAst.addEventListener('click', (e) => tabClick(e, 'json'));
btnGraphAst.addEventListener('click', (e) => tabClick(e, 'graph'));
btnRawJson.addEventListener('click', (e) => tabClick(e, 'rawjson'));

if (session) {
fetchSession(session)
} else {
Expand All @@ -250,7 +254,7 @@ <h5 class="modal-title" id="exportModalLabel">Export</h5>

let session = btoa(ion.dumpText(value));

return `${location.origin.split('?')[0] }/?session=${session}`;
return `${location.origin.split('?')[0]}/?session=${session}`;
}

function fetchSession(session) {
Expand All @@ -261,8 +265,8 @@ <h5 class="modal-title" id="exportModalLabel">Export</h5>

if (e != '') {
action.value = 'eval';
envData.value = e;
envDiv.style.display = 'block';
envData.value = e;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use partiql_value::ion::parse_ion;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
/// Parses the given query and returns the json serialized String, and outputs a string json.
/// Parses the given query and returns the json serialized string.
pub fn parse_as_json(query: &str) -> String {
let parser = Parser::default();
let res = parser.parse(query);
Expand All @@ -20,7 +20,7 @@ pub fn parse_as_json(query: &str) -> String {
}
}

/// Evaluates the given query using the given environment, and outputs a string json.
/// Evaluates the given query using the given environment and returns the json serialized string.
#[wasm_bindgen]
pub fn eval_as_json(statement: &str, env: &str) -> String {
let parsed = parse(statement);
Expand All @@ -36,7 +36,7 @@ pub fn eval_as_json(statement: &str, env: &str) -> String {
}
}

/// Evaluates the given query using the given environment, and outputs a string.
/// Evaluates the given query using the given environment and returns the output string.
#[wasm_bindgen]
pub fn eval_as_string(statement: &str, env: &str) -> String {
let parsed = parse(statement);
Expand All @@ -61,7 +61,7 @@ fn eval(p: &Parsed, env: &str) -> partiql_value::Value {
evaluate(lowered, bindings)
}

/// Creates a logical plan for the given query using and outputs a string json.
/// Creates a logical plan for the given query using and returns the json serialized string.
#[wasm_bindgen]
pub fn explain_as_json(statement: &str) -> String {
let parsed = parse(statement);
Expand All @@ -71,7 +71,7 @@ pub fn explain_as_json(statement: &str) -> String {
.expect("Error in unwrapping json serde")
}

/// Creates a logical plan for the given query using and outputs a string json.
/// Creates a logical plan for the given query using and returns the output string.
#[wasm_bindgen]
pub fn explain_as_string(statement: &str) -> String {
let parsed = parse(statement);
Expand All @@ -80,7 +80,7 @@ pub fn explain_as_string(statement: &str) -> String {
format!("{lowered}")
}

pub(crate) fn evaluate(
fn evaluate(
logical: logical::LogicalPlan<logical::BindingsOp>,
bindings: MapBindings<partiql_value::Value>,
) -> partiql_value::Value {
Expand Down
1 change: 0 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ router.post('/explain', (req, res) => {
});

router.post('/eval', (req, res) => {
console.log(req.body.env);
res.status(200)
.json(JSON.stringify(JSON
.parse(partiql.eval_as_json(req.body.query, `{'env': ${req.body.env}}`))
Expand Down

0 comments on commit f97a1ab

Please sign in to comment.