Skip to content

feat: add set search_path to 'xxx' for pg #5342

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

Merged
merged 2 commits into from
Jan 10, 2025
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
14 changes: 13 additions & 1 deletion src/operator/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ use table::table_name::TableName;
use table::table_reference::TableReference;
use table::TableRef;

use self::set::{set_bytea_output, set_datestyle, set_timezone, validate_client_encoding};
use self::set::{
set_bytea_output, set_datestyle, set_search_path, set_timezone, validate_client_encoding,
};
use crate::error::{
self, CatalogSnafu, ExecLogicalPlanSnafu, ExternalSnafu, InvalidSqlSnafu, NotSupportedSnafu,
PlanStatementSnafu, Result, SchemaNotFoundSnafu, StatementTimeoutSnafu,
Expand Down Expand Up @@ -408,6 +410,16 @@ impl StatementExecutor {
.fail();
}
}
"SEARCH_PATH" => {
if query_ctx.channel() == Channel::Postgres {
set_search_path(set_var.value, query_ctx)?
} else {
return NotSupportedSnafu {
feat: format!("Unsupported set variable {}", var_name),
}
.fail();
}
}
_ => {
// for postgres, we give unknown SET statements a warning with
// success, this is prevent the SET call becoming a blocker
Expand Down
20 changes: 20 additions & 0 deletions src/operator/src/statement/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ pub fn set_bytea_output(exprs: Vec<Expr>, ctx: QueryContextRef) -> Result<()> {
Ok(())
}

pub fn set_search_path(exprs: Vec<Expr>, ctx: QueryContextRef) -> Result<()> {
let search_expr = exprs.first().context(NotSupportedSnafu {
feat: "No search path find in set variable statement",
})?;
match search_expr {
Expr::Value(Value::SingleQuotedString(search_path))
| Expr::Value(Value::DoubleQuotedString(search_path)) => {
ctx.set_current_schema(&search_path.clone());
Ok(())
}
expr => NotSupportedSnafu {
feat: format!(
"Unsupported search path expr {} in set variable statement",
expr
),
}
.fail(),
}
}

pub fn validate_client_encoding(set: SetVariables) -> Result<()> {
let Some((encoding, [])) = set.value.split_first() else {
return InvalidSqlSnafu {
Expand Down
28 changes: 28 additions & 0 deletions tests/cases/standalone/common/system/pg_catalog.result
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ show search_path;
| public |
+-------------+

-- set search_path for pg using schema for now FIXME when support real search_path
create database test;

Affected Rows: 1

-- SQLNESS PROTOCOL POSTGRES
set search_path to 'test';

Affected Rows: 0

drop database test;

Affected Rows: 0

-- SQLNESS PROTOCOL POSTGRES
set search_path to 'public';

Affected Rows: 0

-- SQLNESS PROTOCOL POSTGRES
select current_schema();

+------------------+
| current_schema() |
+------------------+
| public |
+------------------+

-- make sure all the pg_catalog tables are only visible to postgres
select * from pg_catalog.pg_class;

Expand Down
11 changes: 11 additions & 0 deletions tests/cases/standalone/common/system/pg_catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ select current_schema();
-- SQLNESS PROTOCOL POSTGRES
show search_path;

-- set search_path for pg using schema for now FIXME when support real search_path
create database test;
-- SQLNESS PROTOCOL POSTGRES
set search_path to 'test';
drop database test;
-- SQLNESS PROTOCOL POSTGRES
set search_path to 'public';

-- SQLNESS PROTOCOL POSTGRES
select current_schema();

-- make sure all the pg_catalog tables are only visible to postgres
select * from pg_catalog.pg_class;
select * from pg_catalog.pg_namespace;
Expand Down
Loading