Skip to content
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

feature(planner): Support context function in new planner #5369

Merged
merged 2 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion query/src/sql/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub struct TypeChecker<'a> {
bind_context: &'a BindContext,
ctx: Arc<QueryContext>,

// true if current expr is inside an aggregate function.
// This is used to check if there is nested aggregate function.
in_aggregate_function: bool,
}

Expand Down Expand Up @@ -242,9 +244,15 @@ impl<'a> TypeChecker<'a> {
params,
..
} => {
let args: Vec<&Expr> = args.iter().collect();
let func_name = name.name.as_str();

// Check if current function is a context function, e.g. `database`, `version`
if let Some(ctx_func_result) = self.try_resolve_context_function(func_name).await {
return ctx_func_result;
}

let args: Vec<&Expr> = args.iter().collect();

if AggregateFunctionFactory::instance().check(func_name) {
if self.in_aggregate_function {
// Reset the state
Expand Down Expand Up @@ -580,6 +588,39 @@ impl<'a> TypeChecker<'a> {
Ok((subquery_expr.into(), data_type))
}

async fn try_resolve_context_function(
&mut self,
func_name: &str,
) -> Option<Result<(Scalar, DataTypeImpl)>> {
match func_name.to_lowercase().as_str() {
"database" => {
let arg = Expr::Literal {
span: &[],
lit: Literal::String(self.ctx.get_current_database()),
};
Some(self.resolve_function("database", &[&arg], None).await)
}
"version" => {
let arg = Expr::Literal {
span: &[],
lit: Literal::String(self.ctx.get_fuse_version()),
};
Some(self.resolve_function("version", &[&arg], None).await)
}
"current_user" | "user" => match self.ctx.get_current_user() {
Ok(user) => {
let arg = Expr::Literal {
span: &[],
lit: Literal::String(user.identity().to_string()),
};
Some(self.resolve_function("current_user", &[&arg], None).await)
}
Err(e) => Some(Err(e)),
},
_ => None,
}
}

/// Resolve literal values.
pub fn resolve_literal(
&self,
Expand Down
2 changes: 2 additions & 0 deletions tests/suites/0_stateless/20+_others/20_0001_planner_v2.result
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,5 @@ new_planner
18
====Memory Table====
1
====Context Function====
default
4 changes: 4 additions & 0 deletions tests/suites/0_stateless/20+_others/20_0001_planner_v2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ insert into temp values (1);
select a from temp;
drop table temp;

select '====Context Function====';
use default;
select database();

set enable_planner_v2 = 0;