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

feat: add system function user, current_user and current_role #10366

Merged
merged 3 commits into from
Jun 19, 2023
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
45 changes: 40 additions & 5 deletions src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ use crate::expr::{
};
use crate::utils::Condition;

// Defines system functions that without args, ref: https://www.postgresql.org/docs/current/functions-info.html
pub const SYS_FUNCTION_WITHOUT_ARGS: &[&str] = &[
"session_user",
"user",
"current_user",
"current_role",
"current_schema",
"current_timestamp",
];

impl Binder {
pub(in crate::binder) fn bind_function(&mut self, f: Function) -> Result<ExprImpl> {
let function_name = match f.name.0.as_slice() {
Expand Down Expand Up @@ -477,6 +487,32 @@ impl Binder {
})
}

// `SESSION_USER` is the user name of the user that is connected to the database.
fn session_user() -> Handle {
guard_by_len(
0,
raw(|binder, _inputs| {
Ok(ExprImpl::literal_varchar(
binder.auth_context.user_name.clone(),
))
}),
)
}

// `CURRENT_USER` is the user name of the user that is executing the command,
// `CURRENT_ROLE`, `USER` are synonyms for `CURRENT_USER`. Since we don't support
// `SET ROLE xxx` for now, they will all returns session user name.
fn current_user() -> Handle {
guard_by_len(
0,
raw(|binder, _inputs| {
Ok(ExprImpl::literal_varchar(
binder.auth_context.user_name.clone(),
))
}),
)
}

static HANDLES: LazyLock<HashMap<&'static str, Handle>> = LazyLock::new(|| {
[
(
Expand Down Expand Up @@ -723,11 +759,10 @@ impl Binder {
DataType::Varchar,
))
})),
("session_user", guard_by_len(0, raw(|binder, _inputs| {
Ok(ExprImpl::literal_varchar(
binder.auth_context.user_name.clone(),
))
}))),
("session_user", session_user()),
("current_role", current_user()),
("current_user", current_user()),
("user", current_user()),
("pg_get_userbyid", guard_by_len(1, raw(|binder, inputs|{
let input = &inputs[0];
let bound_query = binder.bind_get_user_by_id_select(input)?;
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/binder/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use risingwave_sqlparser::ast::{
TrimWhereField, UnaryOperator,
};

use crate::binder::expr::function::SYS_FUNCTION_WITHOUT_ARGS;
use crate::binder::Binder;
use crate::expr::{Expr as _, ExprImpl, ExprType, FunctionCall, Parameter, SubqueryKind};

Expand Down Expand Up @@ -87,7 +88,7 @@ impl Binder {
Expr::Row(exprs) => self.bind_row(exprs),
// input ref
Expr::Identifier(ident) => {
if ["session_user", "current_schema", "current_timestamp"]
if SYS_FUNCTION_WITHOUT_ARGS
.iter()
.any(|e| ident.real_value().as_str() == *e)
{
Expand Down