-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support template interceptor #63
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Copyright 2024 CeresDB Project Authors. Licensed under Apache-2.0. | ||
|
||
use minijinja::Environment; | ||
use serde_json::Value; | ||
|
||
use super::{Interceptor, InterceptorFactory, InterceptorRef}; | ||
|
||
pub struct TemplateInterceptorFactory; | ||
|
||
const PREFIX: &str = "TEMPLATE"; | ||
|
||
/// Templated query, powered by [minijinja](https://github.com/mitsuhiko/minijinja). | ||
/// The template syntax can be found [here](https://docs.rs/minijinja/latest/minijinja/syntax/index.html). | ||
/// | ||
/// Grammar: | ||
/// ``` text | ||
/// -- SQLNESS TEMPLATE <json> | ||
/// ``` | ||
/// | ||
/// `json` define data bindings passed to template, it should be a valid JSON string. | ||
/// | ||
/// # Example | ||
/// `.sql` file: | ||
/// ``` sql | ||
/// -- SQLNESS TEMPLATE {"name": "test"} | ||
/// SELECT * FROM table where name = "{{name}}" | ||
/// ``` | ||
/// | ||
/// `.result` file: | ||
/// ``` sql | ||
/// -- SQLNESS TEMPLATE {"name": "test"} | ||
/// SELECT * FROM table where name = "test"; | ||
/// ``` | ||
/// | ||
/// In order to generate multiple queries, you can use the builtin function | ||
/// `sql_delimiter()` to insert a delimiter. | ||
Comment on lines
+35
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need a special function to insert delimiter, but not write it in literal? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -- SQLNESS TEMPLATE {"aggr": ["sum", "avg", "count"]}
{% for item in aggr %}
SELECT {{item}}(c) from t {%if not loop.last %} {{sql_delimiter()}} {% endif %}
{% endfor %}
; See this demo, |
||
/// | ||
#[derive(Debug)] | ||
pub struct TemplateInterceptor { | ||
json_ctx: String, | ||
} | ||
|
||
fn sql_delimiter() -> Result<String, minijinja::Error> { | ||
Ok(";".to_string()) | ||
} | ||
|
||
impl Interceptor for TemplateInterceptor { | ||
fn before_execute(&self, execute_query: &mut Vec<String>, _context: &mut crate::QueryContext) { | ||
let input = execute_query.join("\n"); | ||
let mut env = Environment::new(); | ||
env.add_function("sql_delimiter", sql_delimiter); | ||
env.add_template("sql", &input).unwrap(); | ||
let tmpl = env.get_template("sql").unwrap(); | ||
let bindings: Value = if self.json_ctx.is_empty() { | ||
serde_json::from_str("{}").unwrap() | ||
} else { | ||
serde_json::from_str(&self.json_ctx).unwrap() | ||
}; | ||
let rendered = tmpl.render(bindings).unwrap(); | ||
*execute_query = rendered | ||
.split('\n') | ||
.map(|v| v.to_string()) | ||
.collect::<Vec<_>>(); | ||
} | ||
|
||
fn after_execute(&self, _result: &mut String) {} | ||
} | ||
|
||
impl InterceptorFactory for TemplateInterceptorFactory { | ||
fn try_new(&self, interceptor: &str) -> Option<InterceptorRef> { | ||
Self::try_new_from_str(interceptor).map(|i| Box::new(i) as _) | ||
} | ||
} | ||
|
||
impl TemplateInterceptorFactory { | ||
fn try_new_from_str(interceptor: &str) -> Option<TemplateInterceptor> { | ||
if interceptor.starts_with(PREFIX) { | ||
let json_ctx = interceptor.trim_start_matches(PREFIX).to_string(); | ||
Some(TemplateInterceptor { json_ctx }) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn basic_template() { | ||
let interceptor = TemplateInterceptorFactory | ||
.try_new(r#"TEMPLATE {"name": "test"}"#) | ||
.unwrap(); | ||
|
||
let mut input = vec!["SELECT * FROM table where name = '{{name}}'".to_string()]; | ||
interceptor.before_execute(&mut input, &mut crate::QueryContext::default()); | ||
|
||
assert_eq!(input, vec!["SELECT * FROM table where name = 'test'"]); | ||
} | ||
|
||
#[test] | ||
fn vector_template() { | ||
let interceptor = TemplateInterceptorFactory | ||
.try_new(r#"TEMPLATE {"aggr": ["sum", "count", "avg"]}"#) | ||
.unwrap(); | ||
|
||
let mut input = [ | ||
"{%- for item in aggr %}", | ||
"SELECT {{item}}(c) from t;", | ||
"{%- endfor %}", | ||
] | ||
.map(|v| v.to_string()) | ||
.to_vec(); | ||
interceptor.before_execute(&mut input, &mut crate::QueryContext::default()); | ||
|
||
assert_eq!( | ||
input, | ||
[ | ||
"", | ||
"SELECT sum(c) from t;", | ||
"SELECT count(c) from t;", | ||
"SELECT avg(c) from t;" | ||
] | ||
.map(|v| v.to_string()) | ||
.to_vec() | ||
); | ||
} | ||
|
||
#[test] | ||
fn range_template() { | ||
let interceptor = TemplateInterceptorFactory.try_new(r#"TEMPLATE"#).unwrap(); | ||
|
||
let mut input = [ | ||
"INSERT INTO t (c) VALUES", | ||
"{%- for num in range(1, 5) %}", | ||
"({{ num }}){%if not loop.last %}, {% endif %}", | ||
"{%- endfor %}", | ||
";", | ||
] | ||
.map(|v| v.to_string()) | ||
.to_vec(); | ||
interceptor.before_execute(&mut input, &mut crate::QueryContext::default()); | ||
|
||
assert_eq!( | ||
input, | ||
[ | ||
"INSERT INTO t (c) VALUES", | ||
"(1), ", | ||
"(2), ", | ||
"(3), ", | ||
"(4)", | ||
";" | ||
] | ||
.map(|v| v.to_string()) | ||
.to_vec() | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reuse this old example to add test, also I plan to combine interceptor demos into one since their logic are the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍