|
1 | 1 | use std::sync::Arc; |
2 | 2 |
|
3 | 3 | use async_trait::async_trait; |
4 | | -use datafusion::common::ParamValues; |
| 4 | +use datafusion::arrow::datatypes::{DataType, Field, Schema}; |
| 5 | +use datafusion::common::{ParamValues, ToDFSchema}; |
5 | 6 | use datafusion::logical_expr::LogicalPlan; |
6 | 7 | use datafusion::prelude::SessionContext; |
7 | 8 | use datafusion::sql::sqlparser::ast::Statement; |
8 | 9 | use log::{info, warn}; |
9 | | -use pgwire::api::results::{ |
10 | | - DataRowEncoder, DescribePortalResponse, DescribeResponse, DescribeStatementResponse, |
11 | | - FieldFormat, FieldInfo, QueryResponse, Response, Tag, |
12 | | -}; |
| 10 | +use pgwire::api::results::{DataRowEncoder, FieldFormat, FieldInfo, QueryResponse, Response, Tag}; |
13 | 11 | use pgwire::api::ClientInfo; |
14 | 12 | use pgwire::error::{PgWireError, PgWireResult}; |
15 | 13 | use postgres_types::Type; |
@@ -48,22 +46,66 @@ impl QueryHook for SetShowHook { |
48 | 46 |
|
49 | 47 | async fn handle_extended_parse_query( |
50 | 48 | &self, |
51 | | - statement: &Statement, |
52 | | - session_context: &SessionContext, |
53 | | - client: &(dyn ClientInfo + Send + Sync), |
| 49 | + stmt: &Statement, |
| 50 | + _session_context: &SessionContext, |
| 51 | + _client: &(dyn ClientInfo + Send + Sync), |
54 | 52 | ) -> Option<PgWireResult<LogicalPlan>> { |
55 | | - None |
| 53 | + let sql_lower = stmt.to_string().to_lowercase(); |
| 54 | + let sql_trimmed = sql_lower.trim(); |
| 55 | + |
| 56 | + if sql_trimmed.starts_with("show") { |
| 57 | + let show_schema = |
| 58 | + Arc::new(Schema::new(vec![Field::new("show", DataType::Utf8, false)])); |
| 59 | + let result = show_schema |
| 60 | + .to_dfschema() |
| 61 | + .map(|df_schema| { |
| 62 | + LogicalPlan::EmptyRelation(datafusion::logical_expr::EmptyRelation { |
| 63 | + produce_one_row: true, |
| 64 | + schema: Arc::new(df_schema), |
| 65 | + }) |
| 66 | + }) |
| 67 | + .map_err(|e| PgWireError::ApiError(Box::new(e))); |
| 68 | + Some(result) |
| 69 | + } else if sql_trimmed.starts_with("set") { |
| 70 | + let show_schema = Arc::new(Schema::new(Vec::<Field>::new())); |
| 71 | + let result = show_schema |
| 72 | + .to_dfschema() |
| 73 | + .map(|df_schema| { |
| 74 | + LogicalPlan::EmptyRelation(datafusion::logical_expr::EmptyRelation { |
| 75 | + produce_one_row: true, |
| 76 | + schema: Arc::new(df_schema), |
| 77 | + }) |
| 78 | + }) |
| 79 | + .map_err(|e| PgWireError::ApiError(Box::new(e))); |
| 80 | + Some(result) |
| 81 | + } else { |
| 82 | + None |
| 83 | + } |
56 | 84 | } |
57 | 85 |
|
58 | 86 | async fn handle_extended_query( |
59 | 87 | &self, |
60 | 88 | statement: &Statement, |
61 | | - logical_plan: &LogicalPlan, |
62 | | - params: &ParamValues, |
| 89 | + _logical_plan: &LogicalPlan, |
| 90 | + _params: &ParamValues, |
63 | 91 | session_context: &SessionContext, |
64 | 92 | client: &mut (dyn ClientInfo + Send + Sync), |
65 | 93 | ) -> Option<PgWireResult<Response>> { |
66 | | - None |
| 94 | + match statement { |
| 95 | + Statement::Set { .. } => { |
| 96 | + let query = statement.to_string(); |
| 97 | + let query_lower = query.to_lowercase(); |
| 98 | + |
| 99 | + try_respond_set_statements(client, &query_lower, session_context).await |
| 100 | + } |
| 101 | + Statement::ShowVariable { .. } | Statement::ShowStatus { .. } => { |
| 102 | + let query = statement.to_string(); |
| 103 | + let query_lower = query.to_lowercase(); |
| 104 | + |
| 105 | + try_respond_show_statements(client, &query_lower, session_context).await |
| 106 | + } |
| 107 | + _ => None, |
| 108 | + } |
67 | 109 | } |
68 | 110 | } |
69 | 111 |
|
|
0 commit comments