forked from Canner/wren-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
74 lines (64 loc) · 2.33 KB
/
Copy pathlib.rs
File metadata and controls
74 lines (64 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::sync::Arc;
use base64::prelude::*;
use pyo3::prelude::*;
use wren_core::mdl;
use wren_core::mdl::manifest::Manifest;
use wren_core::mdl::AnalyzedWrenMDL;
use crate::errors::CoreError;
mod errors;
#[pyfunction]
fn transform_sql(mdl_base64: &str, sql: &str) -> Result<String, CoreError> {
let mdl_json_bytes = BASE64_STANDARD
.decode(mdl_base64)
.map_err(CoreError::from)?;
let mdl_json = String::from_utf8(mdl_json_bytes).map_err(CoreError::from)?;
let manifest = serde_json::from_str::<Manifest>(&mdl_json)?;
let Ok(analyzed_mdl) = AnalyzedWrenMDL::analyze(manifest) else {
return Err(CoreError::new("Failed to analyze manifest"));
};
match mdl::transform_sql(Arc::new(analyzed_mdl), sql) {
Ok(transformed_sql) => Ok(transformed_sql),
Err(e) => Err(CoreError::new(&e.to_string())),
}
}
#[pymodule]
#[pyo3(name = "wren_core")]
fn wren_core_wrapper(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(transform_sql, m)?)?;
Ok(())
}
#[cfg(test)]
mod tests {
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use serde_json::Value;
use crate::transform_sql;
#[test]
fn test_transform_sql() {
let data = r#"
{
"catalog": "my_catalog",
"schema": "my_schema",
"models": [
{
"name": "customer",
"tableReference": "main.customer",
"columns": [
{"name": "custkey", "expression": "c_custkey", "type": "integer"},
{"name": "name", "expression": "c_name", "type": "varchar"}
],
"primaryKey": "custkey"
}
]
}"#;
let v: Value = serde_json::from_str(data).unwrap();
let mdl_base64: String = BASE64_STANDARD.encode(v.to_string().as_bytes());
let transformed_sql =
transform_sql(&mdl_base64, "SELECT * FROM my_catalog.my_schema.customer")
.unwrap();
assert_eq!(
transformed_sql,
r#"SELECT customer.custkey, customer."name" FROM (SELECT customer.custkey, customer."name" FROM (SELECT main.customer.c_custkey AS custkey, main.customer.c_name AS "name" FROM main.customer) AS customer) AS customer"#
);
}
}