From e7ce6ac653464e0264330cb93c167f5f65b23366 Mon Sep 17 00:00:00 2001 From: yihong Date: Fri, 10 Jan 2025 12:04:05 +0800 Subject: [PATCH] feat: add show search_path for pg (#5328) Signed-off-by: yihong0618 --- src/frontend/src/instance.rs | 1 + src/operator/src/statement.rs | 1 + src/operator/src/statement/show.rs | 5 +++++ src/query/src/sql.rs | 14 ++++++++++++++ src/sql/src/parsers/show_parser.rs | 6 ++++-- src/sql/src/statements/show.rs | 10 ++++++++++ src/sql/src/statements/statement.rs | 7 +++++-- .../standalone/common/system/pg_catalog.result | 10 ++++++++++ .../cases/standalone/common/system/pg_catalog.sql | 4 ++++ 9 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/frontend/src/instance.rs b/src/frontend/src/instance.rs index 3f494763e446..ba187277e976 100644 --- a/src/frontend/src/instance.rs +++ b/src/frontend/src/instance.rs @@ -565,6 +565,7 @@ pub fn check_permission( validate_db_permission!(stmt, query_ctx); } Statement::ShowStatus(_stmt) => {} + Statement::ShowSearchPath(_stmt) => {} Statement::DescribeTable(stmt) => { validate_param(stmt.name(), query_ctx)?; } diff --git a/src/operator/src/statement.rs b/src/operator/src/statement.rs index c72a09d72b4e..2d4b2081bc8c 100644 --- a/src/operator/src/statement.rs +++ b/src/operator/src/statement.rs @@ -352,6 +352,7 @@ impl StatementExecutor { } Statement::ShowIndex(show_index) => self.show_index(show_index, query_ctx).await, Statement::ShowStatus(_) => self.show_status(query_ctx).await, + Statement::ShowSearchPath(_) => self.show_search_path(query_ctx).await, Statement::Use(db) => self.use_database(db, query_ctx).await, Statement::Admin(admin) => self.execute_admin_command(admin, query_ctx).await, } diff --git a/src/operator/src/statement/show.rs b/src/operator/src/statement/show.rs index fe91c71abe24..1d589f443479 100644 --- a/src/operator/src/statement/show.rs +++ b/src/operator/src/statement/show.rs @@ -289,6 +289,11 @@ impl StatementExecutor { .await .context(error::ExecuteStatementSnafu) } + pub async fn show_search_path(&self, query_ctx: QueryContextRef) -> Result { + query::sql::show_search_path(query_ctx) + .await + .context(error::ExecuteStatementSnafu) + } } pub(crate) fn create_partitions_stmt(partitions: Vec) -> Result> { diff --git a/src/query/src/sql.rs b/src/query/src/sql.rs index 269902453a79..3fe46b486db9 100644 --- a/src/query/src/sql.rs +++ b/src/query/src/sql.rs @@ -726,6 +726,20 @@ pub async fn show_status(_query_ctx: QueryContextRef) -> Result { Ok(Output::new_with_record_batches(records)) } +pub async fn show_search_path(_query_ctx: QueryContextRef) -> Result { + let schema = Arc::new(Schema::new(vec![ColumnSchema::new( + "search_path", + ConcreteDataType::string_datatype(), + false, + )])); + let records = RecordBatches::try_from_columns( + schema, + vec![Arc::new(StringVector::from(vec![_query_ctx.current_schema()])) as _], + ) + .context(error::CreateRecordBatchSnafu)?; + Ok(Output::new_with_record_batches(records)) +} + pub fn show_create_database(database_name: &str, options: OptionMap) -> Result { let stmt = CreateDatabase { name: ObjectName(vec![Ident { diff --git a/src/sql/src/parsers/show_parser.rs b/src/sql/src/parsers/show_parser.rs index fa31e813f3d6..fdefc74b6e79 100644 --- a/src/sql/src/parsers/show_parser.rs +++ b/src/sql/src/parsers/show_parser.rs @@ -22,8 +22,8 @@ use crate::error::{ use crate::parser::ParserContext; use crate::statements::show::{ ShowColumns, ShowCreateDatabase, ShowCreateFlow, ShowCreateTable, ShowCreateTableVariant, - ShowCreateView, ShowDatabases, ShowFlows, ShowIndex, ShowKind, ShowStatus, ShowTableStatus, - ShowTables, ShowVariables, ShowViews, + ShowCreateView, ShowDatabases, ShowFlows, ShowIndex, ShowKind, ShowSearchPath, ShowStatus, + ShowTableStatus, ShowTables, ShowVariables, ShowViews, }; use crate::statements::statement::Statement; @@ -107,6 +107,8 @@ impl ParserContext<'_> { Ok(Statement::ShowVariables(ShowVariables { variable })) } else if self.consume_token("STATUS") { Ok(Statement::ShowStatus(ShowStatus {})) + } else if self.consume_token("SEARCH_PATH") { + Ok(Statement::ShowSearchPath(ShowSearchPath {})) } else { self.unsupported(self.peek_token_as_string()) } diff --git a/src/sql/src/statements/show.rs b/src/sql/src/statements/show.rs index 92f13422e6ef..8f95dee7c610 100644 --- a/src/sql/src/statements/show.rs +++ b/src/sql/src/statements/show.rs @@ -289,6 +289,16 @@ impl Display for ShowStatus { } } +/// SQL structure for "SHOW SEARCH_PATH" postgres only +#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)] +pub struct ShowSearchPath {} + +impl Display for ShowSearchPath { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "SHOW SEARCH_PATH") + } +} + #[cfg(test)] mod tests { use std::assert_matches::assert_matches; diff --git a/src/sql/src/statements/statement.rs b/src/sql/src/statements/statement.rs index 2870f2b64a6a..a5e51764eed5 100644 --- a/src/sql/src/statements/statement.rs +++ b/src/sql/src/statements/statement.rs @@ -36,8 +36,8 @@ use crate::statements::query::Query; use crate::statements::set_variables::SetVariables; use crate::statements::show::{ ShowColumns, ShowCreateDatabase, ShowCreateFlow, ShowCreateTable, ShowCreateView, - ShowDatabases, ShowFlows, ShowIndex, ShowKind, ShowStatus, ShowTableStatus, ShowTables, - ShowVariables, ShowViews, + ShowDatabases, ShowFlows, ShowIndex, ShowKind, ShowSearchPath, ShowStatus, ShowTableStatus, + ShowTables, ShowVariables, ShowViews, }; use crate::statements::tql::Tql; use crate::statements::truncate::TruncateTable; @@ -102,6 +102,8 @@ pub enum Statement { ShowCreateView(ShowCreateView), // SHOW STATUS ShowStatus(ShowStatus), + // SHOW SEARCH_PATH + ShowSearchPath(ShowSearchPath), // SHOW VIEWS ShowViews(ShowViews), // DESCRIBE TABLE @@ -159,6 +161,7 @@ impl Display for Statement { Statement::ShowCreateView(s) => s.fmt(f), Statement::ShowViews(s) => s.fmt(f), Statement::ShowStatus(s) => s.fmt(f), + Statement::ShowSearchPath(s) => s.fmt(f), Statement::DescribeTable(s) => s.fmt(f), Statement::Explain(s) => s.fmt(f), Statement::Copy(s) => s.fmt(f), diff --git a/tests/cases/standalone/common/system/pg_catalog.result b/tests/cases/standalone/common/system/pg_catalog.result index 392ed7662a60..5cefa8c8b28c 100644 --- a/tests/cases/standalone/common/system/pg_catalog.result +++ b/tests/cases/standalone/common/system/pg_catalog.result @@ -23,6 +23,16 @@ select current_schema(); | public | +------------------+ +-- search_path for pg using schema for now FIXME when support real search_path +-- SQLNESS PROTOCOL POSTGRES +show search_path; + ++-------------+ +| search_path | ++-------------+ +| public | ++-------------+ + -- make sure all the pg_catalog tables are only visible to postgres select * from pg_catalog.pg_class; diff --git a/tests/cases/standalone/common/system/pg_catalog.sql b/tests/cases/standalone/common/system/pg_catalog.sql index 67a720a802af..03c4968d5dec 100644 --- a/tests/cases/standalone/common/system/pg_catalog.sql +++ b/tests/cases/standalone/common/system/pg_catalog.sql @@ -9,6 +9,10 @@ SELECT session_user is not null; -- SQLNESS PROTOCOL POSTGRES select current_schema(); +-- search_path for pg using schema for now FIXME when support real search_path +-- SQLNESS PROTOCOL POSTGRES +show search_path; + -- make sure all the pg_catalog tables are only visible to postgres select * from pg_catalog.pg_class; select * from pg_catalog.pg_namespace;