Skip to content

Commit 52eebfc

Browse files
authored
feat: support select session_user; (#5313)
* feat: support `select session_user;` This commit is part of support DBeaver that support function select session_user like postgres did. Signed-off-by: yihong0618 <[email protected]> * fix: lint problem Signed-off-by: yihong0618 <[email protected]> * fix: address comments add tests Signed-off-by: yihong0618 <[email protected]> --------- Signed-off-by: yihong0618 <[email protected]>
1 parent e18416a commit 52eebfc

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

src/common/function/src/system.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod version;
2222
use std::sync::Arc;
2323

2424
use build::BuildFunction;
25-
use database::{CurrentSchemaFunction, DatabaseFunction};
25+
use database::{CurrentSchemaFunction, DatabaseFunction, SessionUserFunction};
2626
use pg_catalog::PGCatalogFunction;
2727
use procedure_state::ProcedureStateFunction;
2828
use timezone::TimezoneFunction;
@@ -36,8 +36,9 @@ impl SystemFunction {
3636
pub fn register(registry: &FunctionRegistry) {
3737
registry.register(Arc::new(BuildFunction));
3838
registry.register(Arc::new(VersionFunction));
39-
registry.register(Arc::new(DatabaseFunction));
4039
registry.register(Arc::new(CurrentSchemaFunction));
40+
registry.register(Arc::new(DatabaseFunction));
41+
registry.register(Arc::new(SessionUserFunction));
4142
registry.register(Arc::new(TimezoneFunction));
4243
registry.register_async(Arc::new(ProcedureStateFunction));
4344
PGCatalogFunction::register(registry);

src/common/function/src/system/database.rs

+28
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ pub struct DatabaseFunction;
2828

2929
#[derive(Clone, Debug, Default)]
3030
pub struct CurrentSchemaFunction;
31+
pub struct SessionUserFunction;
3132

3233
const DATABASE_FUNCTION_NAME: &str = "database";
3334
const CURRENT_SCHEMA_FUNCTION_NAME: &str = "current_schema";
35+
const SESSION_USER_FUNCTION_NAME: &str = "session_user";
3436

3537
impl Function for DatabaseFunction {
3638
fn name(&self) -> &str {
@@ -72,6 +74,26 @@ impl Function for CurrentSchemaFunction {
7274
}
7375
}
7476

77+
impl Function for SessionUserFunction {
78+
fn name(&self) -> &str {
79+
SESSION_USER_FUNCTION_NAME
80+
}
81+
82+
fn return_type(&self, _input_types: &[ConcreteDataType]) -> Result<ConcreteDataType> {
83+
Ok(ConcreteDataType::string_datatype())
84+
}
85+
86+
fn signature(&self) -> Signature {
87+
Signature::uniform(0, vec![], Volatility::Immutable)
88+
}
89+
90+
fn eval(&self, func_ctx: FunctionContext, _columns: &[VectorRef]) -> Result<VectorRef> {
91+
let user = func_ctx.query_ctx.current_user();
92+
93+
Ok(Arc::new(StringVector::from_slice(&[user.username()])) as _)
94+
}
95+
}
96+
7597
impl fmt::Display for DatabaseFunction {
7698
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7799
write!(f, "DATABASE")
@@ -84,6 +106,12 @@ impl fmt::Display for CurrentSchemaFunction {
84106
}
85107
}
86108

109+
impl fmt::Display for SessionUserFunction {
110+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111+
write!(f, "SESSION_USER")
112+
}
113+
}
114+
87115
#[cfg(test)]
88116
mod tests {
89117
use std::sync::Arc;

tests/cases/standalone/common/system/pg_catalog.result

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ create database pg_catalog;
33

44
Error: 1004(InvalidArguments), Schema pg_catalog already exists
55

6+
-- session_user because session_user is based on the current user so is not null is for test
7+
-- SQLNESS PROTOCOL POSTGRES
8+
SELECT session_user is not null;
9+
10+
+----------------------------+
11+
| session_user() IS NOT NULL |
12+
+----------------------------+
13+
| t |
14+
+----------------------------+
15+
16+
-- session_user and current_schema
17+
-- SQLNESS PROTOCOL POSTGRES
18+
select current_schema();
19+
20+
+------------------+
21+
| current_schema() |
22+
+------------------+
23+
| public |
24+
+------------------+
25+
626
-- make sure all the pg_catalog tables are only visible to postgres
727
select * from pg_catalog.pg_class;
828

tests/cases/standalone/common/system/pg_catalog.sql

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
-- should not able to create pg_catalog
22
create database pg_catalog;
33

4+
-- session_user because session_user is based on the current user so is not null is for test
5+
-- SQLNESS PROTOCOL POSTGRES
6+
SELECT session_user is not null;
7+
8+
-- session_user and current_schema
9+
-- SQLNESS PROTOCOL POSTGRES
10+
select current_schema();
11+
412
-- make sure all the pg_catalog tables are only visible to postgres
513
select * from pg_catalog.pg_class;
614
select * from pg_catalog.pg_namespace;

0 commit comments

Comments
 (0)