Skip to content

Commit b462435

Browse files
committed
fix: extended query for unsupported show statements
1 parent 726e7ed commit b462435

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

datafusion-postgres/src/handlers.rs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use crate::sql::{
99
RewriteArrayAnyAllOperation, SqlStatementRewriteRule,
1010
};
1111
use async_trait::async_trait;
12-
use datafusion::arrow::datatypes::DataType;
12+
use datafusion::arrow::datatypes::{DataType, Field, Schema};
13+
use datafusion::common::ToDFSchema;
14+
use datafusion::error::DataFusionError;
1315
use datafusion::logical_expr::LogicalPlan;
1416
use datafusion::prelude::*;
1517
use datafusion::sql::parser::Statement;
@@ -718,24 +720,15 @@ pub struct Parser {
718720
sql_rewrite_rules: Vec<Arc<dyn SqlStatementRewriteRule>>,
719721
}
720722

721-
#[async_trait]
722-
impl QueryParser for Parser {
723-
type Statement = (String, LogicalPlan);
724-
725-
async fn parse_sql<C>(
726-
&self,
727-
_client: &C,
728-
sql: &str,
729-
_types: &[Type],
730-
) -> PgWireResult<Self::Statement> {
731-
log::debug!("Received parse extended query: {sql}"); // Log for debugging
732-
723+
impl Parser {
724+
fn try_shortcut_parse_plan(&self, sql: &str) -> Result<Option<LogicalPlan>, DataFusionError> {
733725
// Check for transaction commands that shouldn't be parsed by DataFusion
734726
let sql_lower = sql.to_lowercase();
735727
let sql_trimmed = sql_lower.trim();
728+
736729
if matches!(
737730
sql_trimmed,
738-
"begin"
731+
"" | "begin"
739732
| "begin transaction"
740733
| "begin work"
741734
| "start transaction"
@@ -751,13 +744,50 @@ impl QueryParser for Parser {
751744
) {
752745
// Return a dummy plan for transaction commands - they'll be handled by transaction handler
753746
let dummy_schema = datafusion::common::DFSchema::empty();
754-
let dummy_plan = datafusion::logical_expr::LogicalPlan::EmptyRelation(
747+
return Ok(Some(LogicalPlan::EmptyRelation(
755748
datafusion::logical_expr::EmptyRelation {
756749
produce_one_row: false,
757-
schema: std::sync::Arc::new(dummy_schema),
750+
schema: Arc::new(dummy_schema),
758751
},
759-
);
760-
return Ok((sql.to_string(), dummy_plan));
752+
)));
753+
}
754+
755+
// show statement may not be supported by datafusion
756+
if sql_trimmed.starts_with("show") {
757+
// Return a dummy plan for transaction commands - they'll be handled by transaction handler
758+
let show_schema =
759+
Arc::new(Schema::new(vec![Field::new("show", DataType::Utf8, false)]));
760+
let df_schema = show_schema.to_dfschema()?;
761+
return Ok(Some(LogicalPlan::EmptyRelation(
762+
datafusion::logical_expr::EmptyRelation {
763+
produce_one_row: true,
764+
schema: Arc::new(df_schema),
765+
},
766+
)));
767+
}
768+
769+
Ok(None)
770+
}
771+
}
772+
773+
#[async_trait]
774+
impl QueryParser for Parser {
775+
type Statement = (String, LogicalPlan);
776+
777+
async fn parse_sql<C>(
778+
&self,
779+
_client: &C,
780+
sql: &str,
781+
_types: &[Type],
782+
) -> PgWireResult<Self::Statement> {
783+
log::debug!("Received parse extended query: {sql}"); // Log for debugging
784+
785+
// Check for transaction commands that shouldn't be parsed by DataFusion
786+
if let Some(plan) = self
787+
.try_shortcut_parse_plan(&sql)
788+
.map_err(|e| PgWireError::ApiError(Box::new(e)))?
789+
{
790+
return Ok((sql.to_string(), plan));
761791
}
762792

763793
let mut statements = parse(sql).map_err(|e| PgWireError::ApiError(Box::new(e)))?;

0 commit comments

Comments
 (0)