Skip to content

Commit

Permalink
Merge pull request #5584 from TCeason/ISSUE-5023/support_user_func
Browse files Browse the repository at this point in the history
add func user()
  • Loading branch information
BohuTANG authored May 25, 2022
2 parents 486b866 + dbcf817 commit fc10848
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
2 changes: 2 additions & 0 deletions common/functions/src/scalars/contexts/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::scalars::ConnectionIdFunction;
use crate::scalars::CurrentUserFunction;
use crate::scalars::DatabaseFunction;
use crate::scalars::FunctionFactory;
use crate::scalars::UserFunction;
use crate::scalars::VersionFunction;

#[derive(Clone)]
Expand All @@ -27,5 +28,6 @@ impl ContextFunction {
factory.register("database", DatabaseFunction::desc());
factory.register("version", VersionFunction::desc());
factory.register("current_user", CurrentUserFunction::desc());
factory.register("user", UserFunction::desc());
}
}
2 changes: 2 additions & 0 deletions common/functions/src/scalars/contexts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ mod connection_id;
mod context;
mod current_user;
mod database;
mod user;
mod version;

pub use connection_id::ConnectionIdFunction;
pub use context::ContextFunction;
pub use current_user::CurrentUserFunction;
pub use database::DatabaseFunction;
pub use user::UserFunction;
pub use version::VersionFunction;
68 changes: 68 additions & 0 deletions common/functions/src/scalars/contexts/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt;

use common_datavalues::DataTypeImpl;
use common_datavalues::StringType;
use common_exception::Result;

use crate::scalars::Function;
use crate::scalars::FunctionContext;
use crate::scalars::FunctionDescription;
use crate::scalars::FunctionFeatures;

#[derive(Clone)]
//now impl need same as CurrentUserFunction
//compatible MySQL user function
pub struct UserFunction {}

impl UserFunction {
pub fn try_create(_display_name: &str, _args: &[&DataTypeImpl]) -> Result<Box<dyn Function>> {
Ok(Box::new(UserFunction {}))
}

pub fn desc() -> FunctionDescription {
FunctionDescription::creator(Box::new(Self::try_create)).features(
FunctionFeatures::default()
.context_function()
.num_arguments(1),
)
}
}

impl Function for UserFunction {
fn name(&self) -> &str {
"UserFunction"
}

fn return_type(&self) -> DataTypeImpl {
StringType::new_impl()
}

fn eval(
&self,
_func_ctx: FunctionContext,
columns: &common_datavalues::ColumnsWithField,
_input_rows: usize,
) -> Result<common_datavalues::ColumnRef> {
Ok(columns[0].column().clone())
}
}

impl fmt::Display for UserFunction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "user")
}
}
2 changes: 1 addition & 1 deletion common/planners/src/plan_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::ExpressionVisitor;
use crate::PlanNode;

static OP_SET: Lazy<HashSet<&'static str>> = Lazy::new(|| {
["database", "version", "current_user"]
["database", "version", "current_user", "user"]
.iter()
.copied()
.collect()
Expand Down
2 changes: 1 addition & 1 deletion query/src/common/context_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl ContextFunction {
"version" => vec![Expression::create_literal(DataValue::String(
ctx.get_fuse_version().into_bytes(),
))],
"current_user" => vec![Expression::create_literal(DataValue::String(
"current_user" | "user" => vec![Expression::create_literal(DataValue::String(
ctx.get_current_user()?.identity().to_string().into_bytes(),
))],
"connection_id" => vec![Expression::create_literal(DataValue::String(
Expand Down
12 changes: 11 additions & 1 deletion query/src/sql/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ impl<'a> TypeChecker<'a> {
};
Some(self.resolve_function("version", &[&arg], None).await)
}
"current_user" | "user" => match self.ctx.get_current_user() {
"current_user" => match self.ctx.get_current_user() {
Ok(user) => {
let arg = Expr::Literal {
span: &[],
Expand All @@ -774,6 +774,16 @@ impl<'a> TypeChecker<'a> {
}
Err(e) => Some(Err(e)),
},
"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("user", &[&arg], None).await)
}
Err(e) => Some(Err(e)),
},
"connection_id" => {
let arg = Expr::Literal {
span: &[],
Expand Down
6 changes: 6 additions & 0 deletions query/tests/it/functions/context_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ async fn test_context_function_build_arg_from_ctx() -> Result<()> {
assert_eq!("'root'@'127.0.0.1'", format!("{:?}", args[0]));
}

// Ok.
{
let args = ContextFunction::build_args_from_ctx(ctx.clone(), "user")?;
assert_eq!("'root'@'127.0.0.1'", format!("{:?}", args[0]));
}

// Error.
{
let result = ContextFunction::build_args_from_ctx(ctx, "databasexx").is_err();
Expand Down

0 comments on commit fc10848

Please sign in to comment.