Skip to content

Commit 848facf

Browse files
committed
Start with SET or SHOW
1 parent b322274 commit 848facf

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

datafusion-postgres/src/handlers.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use pgwire::api::stmt::StoredStatement;
1616
use pgwire::api::{ClientInfo, NoopErrorHandler, PgWireServerHandlers, Type};
1717
use pgwire::error::{PgWireError, PgWireResult};
1818

19-
// --- ADD THESE IMPORTS FOR MULTI-STATEMENT PARSING ---
19+
// --- Imports for multi-statement parsing ---
2020
use sqlparser::dialect::GenericDialect;
2121
use sqlparser::parser::Parser as SqlParser;
2222
// ------------------------------------------------------
@@ -115,31 +115,34 @@ impl SimpleQueryHandler for DfSessionService {
115115
{
116116
// 1) Parse the incoming query string into multiple statements using sqlparser.
117117
let dialect = GenericDialect {};
118-
let stmts = match SqlParser::parse_sql(&dialect, query) {
119-
Ok(s) => s,
120-
Err(e) => {
121-
return Err(PgWireError::ApiError(Box::new(e)));
122-
}
123-
};
118+
let stmts = SqlParser::parse_sql(&dialect, query)
119+
.map_err(|e| PgWireError::ApiError(Box::new(e)))?;
124120

125121
// 2) For each parsed statement, execute with DataFusion and collect results.
126122
let mut responses = Vec::with_capacity(stmts.len());
127123
for statement in stmts {
128-
// Convert the AST statement back to SQL text
129-
// (some statements might be empty if there's a trailing semicolon)
124+
// Convert the AST statement back to SQL text.
130125
let stmt_string = statement.to_string().trim().to_owned();
131126
if stmt_string.is_empty() {
132127
continue;
133128
}
134129

135-
// Execute the statement in DataFusion
130+
// Intercept configuration commands (e.g. SET, SHOW) that are unsupported.
131+
let stmt_upper = stmt_string.to_uppercase();
132+
if stmt_upper.starts_with("SET ") || stmt_upper.starts_with("SHOW ") {
133+
// Return an empty query response for these commands.
134+
responses.push(Response::EmptyQuery);
135+
continue;
136+
}
137+
138+
// Execute the statement in DataFusion.
136139
let df = self
137140
.session_context
138141
.sql(&stmt_string)
139142
.await
140143
.map_err(|e| PgWireError::ApiError(Box::new(e)))?;
141144

142-
// 3) Encode the DataFrame into a QueryResponse for the client
145+
// 3) Encode the DataFrame into a QueryResponse for the client.
143146
let resp = datatypes::encode_dataframe(df, &Format::UnifiedText).await?;
144147
responses.push(Response::Query(resp));
145148
}
@@ -242,7 +245,7 @@ impl ExtendedQueryHandler for DfSessionService {
242245
/// Helper to convert DataFusion’s parameter map into an ordered list.
243246
fn ordered_param_types(types: &HashMap<String, Option<DataType>>) -> Vec<Option<&DataType>> {
244247
// DataFusion stores parameters as a map keyed by "$1", "$2", etc.
245-
// We sort them in ascending order by key to match the expected param order.
248+
// We sort them in ascending order by key to match the expected parameter order.
246249
let mut types_vec = types.iter().collect::<Vec<_>>();
247250
types_vec.sort_by(|a, b| a.0.cmp(b.0));
248251
types_vec.into_iter().map(|pt| pt.1.as_ref()).collect()

0 commit comments

Comments
 (0)