diff --git a/src/frontend/src/handler/variable.rs b/src/frontend/src/handler/variable.rs index 9060ff2209413..7d108ae35128e 100644 --- a/src/frontend/src/handler/variable.rs +++ b/src/frontend/src/handler/variable.rs @@ -13,7 +13,6 @@ // limitations under the License. use itertools::Itertools; -use pgwire::pg_field_descriptor::PgFieldDescriptor; use pgwire::pg_protocol::ParameterStatus; use pgwire::pg_response::{PgResponse, StatementType}; use pgwire::types::Row; @@ -25,6 +24,7 @@ use risingwave_sqlparser::ast::{Ident, SetTimeZoneValue, SetVariableValue, Value use super::RwPgResponse; use crate::handler::HandlerArgs; +use crate::utils::infer_stmt_row_desc::infer_show_variable; pub fn handle_set( handler_args: HandlerArgs, @@ -96,29 +96,22 @@ pub(super) async fn handle_show( ) -> Result { // TODO: Verify that the name used in `show` command is indeed always case-insensitive. let name = variable.iter().map(|e| e.real_value()).join(" "); - if name.eq_ignore_ascii_case("PARAMETERS") { - return handle_show_system_params(handler_args).await; - } - // Show session config. - let config_reader = handler_args.session.config(); - if name.eq_ignore_ascii_case("ALL") { - return handle_show_all(handler_args.clone()); - } - let row = Row::new(vec![Some(config_reader.get(&name)?.into())]); + let row_desc = infer_show_variable(&name); + let rows = if name.eq_ignore_ascii_case("PARAMETERS") { + handle_show_system_params(handler_args).await? + } else if name.eq_ignore_ascii_case("ALL") { + handle_show_all(handler_args.clone())? + } else { + let config_reader = handler_args.session.config(); + vec![Row::new(vec![Some(config_reader.get(&name)?.into())])] + }; Ok(PgResponse::builder(StatementType::SHOW_VARIABLE) - .values( - vec![row].into(), - vec![PgFieldDescriptor::new( - name.to_ascii_lowercase(), - DataType::Varchar.to_oid(), - DataType::Varchar.type_len(), - )], - ) + .values(rows.into(), row_desc) .into()) } -fn handle_show_all(handler_args: HandlerArgs) -> Result { +fn handle_show_all(handler_args: HandlerArgs) -> Result> { let config_reader = handler_args.session.config(); let all_variables = config_reader.get_all(); @@ -133,32 +126,10 @@ fn handle_show_all(handler_args: HandlerArgs) -> Result { ]) }) .collect_vec(); - - Ok(RwPgResponse::builder(StatementType::SHOW_VARIABLE) - .values( - rows.into(), - vec![ - PgFieldDescriptor::new( - "Name".to_string(), - DataType::Varchar.to_oid(), - DataType::Varchar.type_len(), - ), - PgFieldDescriptor::new( - "Setting".to_string(), - DataType::Varchar.to_oid(), - DataType::Varchar.type_len(), - ), - PgFieldDescriptor::new( - "Description".to_string(), - DataType::Varchar.to_oid(), - DataType::Varchar.type_len(), - ), - ], - ) - .into()) + Ok(rows) } -async fn handle_show_system_params(handler_args: HandlerArgs) -> Result { +async fn handle_show_system_params(handler_args: HandlerArgs) -> Result> { let params = handler_args .session .env() @@ -175,27 +146,5 @@ async fn handle_show_system_params(handler_args: HandlerArgs) -> Result, stmt: Statement) -> Result { let name = &variable[0].real_value().to_lowercase(); - if name.eq_ignore_ascii_case("ALL") { - Ok(vec![ - PgFieldDescriptor::new( - "Name".to_string(), - DataType::Varchar.to_oid(), - DataType::Varchar.type_len(), - ), - PgFieldDescriptor::new( - "Setting".to_string(), - DataType::Varchar.to_oid(), - DataType::Varchar.type_len(), - ), - PgFieldDescriptor::new( - "Description".to_string(), - DataType::Varchar.to_oid(), - DataType::Varchar.type_len(), - ), - ]) - } else { - Ok(vec![PgFieldDescriptor::new( - name.to_ascii_lowercase(), - DataType::Varchar.to_oid(), - DataType::Varchar.type_len(), - )]) - } + Ok(infer_show_variable(name)) } Statement::Describe { name: _ } => Ok(vec![ PgFieldDescriptor::new( diff --git a/src/frontend/src/utils/infer_stmt_row_desc.rs b/src/frontend/src/utils/infer_stmt_row_desc.rs index dbe8968f0a293..8ebb7ac5c7d7a 100644 --- a/src/frontend/src/utils/infer_stmt_row_desc.rs +++ b/src/frontend/src/utils/infer_stmt_row_desc.rs @@ -168,3 +168,49 @@ pub fn infer_show_object(objects: &ShowObject) -> Vec { )], } } + +pub fn infer_show_variable(name: &str) -> Vec { + if name.eq_ignore_ascii_case("ALL") { + vec![ + PgFieldDescriptor::new( + "Name".to_string(), + DataType::Varchar.to_oid(), + DataType::Varchar.type_len(), + ), + PgFieldDescriptor::new( + "Setting".to_string(), + DataType::Varchar.to_oid(), + DataType::Varchar.type_len(), + ), + PgFieldDescriptor::new( + "Description".to_string(), + DataType::Varchar.to_oid(), + DataType::Varchar.type_len(), + ), + ] + } else if name.eq_ignore_ascii_case("PARAMETERS") { + vec![ + PgFieldDescriptor::new( + "Name".to_string(), + DataType::Varchar.to_oid(), + DataType::Varchar.type_len(), + ), + PgFieldDescriptor::new( + "Value".to_string(), + DataType::Varchar.to_oid(), + DataType::Varchar.type_len(), + ), + PgFieldDescriptor::new( + "Mutable".to_string(), + DataType::Boolean.to_oid(), + DataType::Boolean.type_len(), + ), + ] + } else { + vec![PgFieldDescriptor::new( + name.to_ascii_lowercase(), + DataType::Varchar.to_oid(), + DataType::Varchar.type_len(), + )] + } +}