Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yezizp2012 committed Jun 19, 2023
1 parent d43601f commit e3cbe0c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
50 changes: 40 additions & 10 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,16 +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(),
))
}))),
("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
12 changes: 4 additions & 8 deletions 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,14 +88,9 @@ impl Binder {
Expr::Row(exprs) => self.bind_row(exprs),
// input ref
Expr::Identifier(ident) => {
if [
"session_user",
"user",
"current_schema",
"current_timestamp",
]
.iter()
.any(|e| ident.real_value().as_str() == *e)
if SYS_FUNCTION_WITHOUT_ARGS
.iter()
.any(|e| ident.real_value().as_str() == *e)
{
// Rewrite a system variable to a function call, e.g. `SELECT current_schema;`
// will be rewritten to `SELECT current_schema();`.
Expand Down

0 comments on commit e3cbe0c

Please sign in to comment.