Skip to content

Commit 3a996c2

Browse files
authored
feat: add set search_path to 'xxx' for pg (#5342)
* feat: add set search_path to 'xxx' for pg Signed-off-by: yihong0618 <[email protected]> * fix: address comments Signed-off-by: yihong0618 <[email protected]> --------- Signed-off-by: yihong0618 <[email protected]>
1 parent 45d4065 commit 3a996c2

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

src/operator/src/statement.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ use table::table_name::TableName;
7272
use table::table_reference::TableReference;
7373
use table::TableRef;
7474

75-
use self::set::{set_bytea_output, set_datestyle, set_timezone, validate_client_encoding};
75+
use self::set::{
76+
set_bytea_output, set_datestyle, set_search_path, set_timezone, validate_client_encoding,
77+
};
7678
use crate::error::{
7779
self, CatalogSnafu, ExecLogicalPlanSnafu, ExternalSnafu, InvalidSqlSnafu, NotSupportedSnafu,
7880
PlanStatementSnafu, Result, SchemaNotFoundSnafu, StatementTimeoutSnafu,
@@ -408,6 +410,16 @@ impl StatementExecutor {
408410
.fail();
409411
}
410412
}
413+
"SEARCH_PATH" => {
414+
if query_ctx.channel() == Channel::Postgres {
415+
set_search_path(set_var.value, query_ctx)?
416+
} else {
417+
return NotSupportedSnafu {
418+
feat: format!("Unsupported set variable {}", var_name),
419+
}
420+
.fail();
421+
}
422+
}
411423
_ => {
412424
// for postgres, we give unknown SET statements a warning with
413425
// success, this is prevent the SET call becoming a blocker

src/operator/src/statement/set.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ pub fn set_bytea_output(exprs: Vec<Expr>, ctx: QueryContextRef) -> Result<()> {
8181
Ok(())
8282
}
8383

84+
pub fn set_search_path(exprs: Vec<Expr>, ctx: QueryContextRef) -> Result<()> {
85+
let search_expr = exprs.first().context(NotSupportedSnafu {
86+
feat: "No search path find in set variable statement",
87+
})?;
88+
match search_expr {
89+
Expr::Value(Value::SingleQuotedString(search_path))
90+
| Expr::Value(Value::DoubleQuotedString(search_path)) => {
91+
ctx.set_current_schema(&search_path.clone());
92+
Ok(())
93+
}
94+
expr => NotSupportedSnafu {
95+
feat: format!(
96+
"Unsupported search path expr {} in set variable statement",
97+
expr
98+
),
99+
}
100+
.fail(),
101+
}
102+
}
103+
84104
pub fn validate_client_encoding(set: SetVariables) -> Result<()> {
85105
let Some((encoding, [])) = set.value.split_first() else {
86106
return InvalidSqlSnafu {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ show search_path;
3333
| public |
3434
+-------------+
3535

36+
-- set search_path for pg using schema for now FIXME when support real search_path
37+
create database test;
38+
39+
Affected Rows: 1
40+
41+
-- SQLNESS PROTOCOL POSTGRES
42+
set search_path to 'test';
43+
44+
Affected Rows: 0
45+
46+
drop database test;
47+
48+
Affected Rows: 0
49+
50+
-- SQLNESS PROTOCOL POSTGRES
51+
set search_path to 'public';
52+
53+
Affected Rows: 0
54+
55+
-- SQLNESS PROTOCOL POSTGRES
56+
select current_schema();
57+
58+
+------------------+
59+
| current_schema() |
60+
+------------------+
61+
| public |
62+
+------------------+
63+
3664
-- make sure all the pg_catalog tables are only visible to postgres
3765
select * from pg_catalog.pg_class;
3866

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ select current_schema();
1313
-- SQLNESS PROTOCOL POSTGRES
1414
show search_path;
1515

16+
-- set search_path for pg using schema for now FIXME when support real search_path
17+
create database test;
18+
-- SQLNESS PROTOCOL POSTGRES
19+
set search_path to 'test';
20+
drop database test;
21+
-- SQLNESS PROTOCOL POSTGRES
22+
set search_path to 'public';
23+
24+
-- SQLNESS PROTOCOL POSTGRES
25+
select current_schema();
26+
1627
-- make sure all the pg_catalog tables are only visible to postgres
1728
select * from pg_catalog.pg_class;
1829
select * from pg_catalog.pg_namespace;

0 commit comments

Comments
 (0)