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

add func user() #5584

Merged
merged 1 commit into from
May 25, 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
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 @@ -726,7 +726,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 @@ -736,6 +736,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