Skip to content

Commit

Permalink
fix(psql_conn): Add row desc with 'SHOW PARAMETERS' (#13099)
Browse files Browse the repository at this point in the history
Co-authored-by: xiangjinwu <[email protected]>
  • Loading branch information
xxhZs and xiangjinwu authored Oct 30, 2023
1 parent 7db34d5 commit 98edea6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 92 deletions.
81 changes: 15 additions & 66 deletions src/frontend/src/handler/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -96,29 +96,22 @@ pub(super) async fn handle_show(
) -> Result<RwPgResponse> {
// 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<RwPgResponse> {
fn handle_show_all(handler_args: HandlerArgs) -> Result<Vec<Row>> {
let config_reader = handler_args.session.config();

let all_variables = config_reader.get_all();
Expand All @@ -133,32 +126,10 @@ fn handle_show_all(handler_args: HandlerArgs) -> Result<RwPgResponse> {
])
})
.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<RwPgResponse> {
async fn handle_show_system_params(handler_args: HandlerArgs) -> Result<Vec<Row>> {
let params = handler_args
.session
.env()
Expand All @@ -175,27 +146,5 @@ async fn handle_show_system_params(handler_args: HandlerArgs) -> Result<RwPgResp
Row::new(vec![Some(k.into()), Some(v.into()), Some(is_mutable_bytes)])
})
.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(
"Value".to_string(),
DataType::Varchar.to_oid(),
DataType::Varchar.type_len(),
),
PgFieldDescriptor::new(
"Mutable".to_string(),
DataType::Boolean.to_oid(),
DataType::Boolean.type_len(),
),
],
)
.into())
Ok(rows)
}
28 changes: 2 additions & 26 deletions src/frontend/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ use crate::user::user_authentication::md5_hash_with_salt;
use crate::user::user_manager::UserInfoManager;
use crate::user::user_service::{UserInfoReader, UserInfoWriter, UserInfoWriterImpl};
use crate::user::UserId;
use crate::utils::infer_stmt_row_desc::infer_show_object;
use crate::utils::infer_stmt_row_desc::{infer_show_object, infer_show_variable};
use crate::{FrontendOpts, PgResponseStream};

pub(crate) mod transaction;
Expand Down Expand Up @@ -1102,31 +1102,7 @@ fn infer(bound: Option<BoundStatement>, stmt: Statement) -> Result<Vec<PgFieldDe
]),
Statement::ShowVariable { variable } => {
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(
Expand Down
46 changes: 46 additions & 0 deletions src/frontend/src/utils/infer_stmt_row_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,49 @@ pub fn infer_show_object(objects: &ShowObject) -> Vec<PgFieldDescriptor> {
)],
}
}

pub fn infer_show_variable(name: &str) -> Vec<PgFieldDescriptor> {
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(),
)]
}
}

0 comments on commit 98edea6

Please sign in to comment.