Skip to content

Commit 46b3945

Browse files
authored
feat: output default values for show statements (#234)
* feat: output default values for show statements * refactor: improve code style, remove duplicates
1 parent d4f8e8f commit 46b3945

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

datafusion-postgres/src/hooks/set_show.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ use datafusion::logical_expr::LogicalPlan;
88
use datafusion::prelude::SessionContext;
99
use datafusion::sql::sqlparser::ast::{Expr, Set, Statement};
1010
use log::{info, warn};
11+
use pgwire::api::auth::DefaultServerParameterProvider;
1112
use pgwire::api::results::{DataRowEncoder, FieldFormat, FieldInfo, QueryResponse, Response, Tag};
1213
use pgwire::api::ClientInfo;
1314
use pgwire::error::{PgWireError, PgWireResult};
15+
use pgwire::types::format::FormatOptions;
1416
use postgres_types::Type;
1517

1618
use crate::client;
@@ -212,8 +214,7 @@ where
212214
// fallback to datafusion and ignore all errors
213215
if let Err(e) = execute_set_statement(session_context, statement.clone()).await {
214216
warn!(
215-
"SET statement {} is not supported by datafusion, error {e}, statement ignored",
216-
statement
217+
"SET statement {statement} is not supported by datafusion, error {e}, statement ignored",
217218
);
218219
}
219220

@@ -290,22 +291,32 @@ where
290291
["transaction", "isolation", "level"] => {
291292
Some(mock_show_response("transaction_isolation", "read_committed").map(Response::Query))
292293
}
293-
["bytea_output"]
294-
| ["datestyle"]
295-
| ["intervalstyle"]
296-
| ["application_name"]
297-
| ["extra_float_digits"]
298-
| ["search_path"] => {
294+
_ => {
299295
let val = client
300296
.metadata()
301297
.get(&variables[0])
302-
.map(|v| v.as_str())
303-
.unwrap_or("");
304-
Some(mock_show_response(&variables[0], val).map(Response::Query))
305-
}
306-
_ => {
307-
info!("Unsupported show statement: {}", statement);
308-
Some(mock_show_response("unsupported_show_statement", "").map(Response::Query))
298+
.map(|v| v.to_string())
299+
.or_else(|| match variables[0].as_str() {
300+
"bytea_output" => Some(FormatOptions::default().bytea_output),
301+
"datestyle" => Some(FormatOptions::default().date_style),
302+
"intervalstyle" => Some(FormatOptions::default().interval_style),
303+
"extra_float_digits" => {
304+
Some(FormatOptions::default().extra_float_digits.to_string())
305+
}
306+
"application_name" => Some(
307+
DefaultServerParameterProvider::default()
308+
.application_name
309+
.unwrap_or("".to_owned()),
310+
),
311+
"search_path" => Some(DefaultServerParameterProvider::default().search_path),
312+
_ => None,
313+
});
314+
if let Some(val) = val {
315+
Some(mock_show_response(&variables[0], &val).map(Response::Query))
316+
} else {
317+
info!("Unsupported show statement: {statement}");
318+
Some(mock_show_response("unsupported_show_statement", "").map(Response::Query))
319+
}
309320
}
310321
}
311322
}

0 commit comments

Comments
 (0)