-
-
Notifications
You must be signed in to change notification settings - Fork 87
Handle empty extended queries #453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+201
−21
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| use std::fmt::Debug; | ||
| use std::net::{IpAddr, Ipv4Addr, SocketAddr}; | ||
| use std::sync::Arc; | ||
|
|
||
| use async_trait::async_trait; | ||
| use bytes::BytesMut; | ||
| use futures::Sink; | ||
| use pgwire::api::portal::Portal; | ||
| use pgwire::api::query::ExtendedQueryHandler; | ||
| use pgwire::api::results::{DescribePortalResponse, DescribeStatementResponse, Response}; | ||
| use pgwire::api::stmt::{NoopQueryParser, StoredStatement}; | ||
| use pgwire::api::{ClientInfo, DefaultClient, PgWireConnectionState}; | ||
| use pgwire::error::{PgWireError, PgWireResult}; | ||
| use pgwire::messages::extendedquery::{ | ||
| Bind, Describe, Execute, Parse, Sync as PgSync, TARGET_TYPE_BYTE_PORTAL, | ||
| TARGET_TYPE_BYTE_STATEMENT, | ||
| }; | ||
| use pgwire::messages::{DecodeContext, PgWireBackendMessage}; | ||
| use pgwire::tokio::server::PgWireMessageServerCodec; | ||
| use tokio::io::{AsyncReadExt, duplex}; | ||
| use tokio_util::codec::Framed; | ||
|
|
||
| struct TestExtendedQueryHandler; | ||
|
|
||
| #[async_trait] | ||
| impl ExtendedQueryHandler for TestExtendedQueryHandler { | ||
| type Statement = String; | ||
| type QueryParser = NoopQueryParser; | ||
|
|
||
| fn query_parser(&self) -> Arc<Self::QueryParser> { | ||
| Arc::new(NoopQueryParser) | ||
| } | ||
|
|
||
| async fn do_describe_statement<C>( | ||
| &self, | ||
| _client: &mut C, | ||
| _target: &StoredStatement<Self::Statement>, | ||
| ) -> PgWireResult<DescribeStatementResponse> | ||
| where | ||
| C: ClientInfo + Unpin + Send + Sync, | ||
| { | ||
| panic!("empty query reached statement description") | ||
| } | ||
|
|
||
| async fn do_describe_portal<C>( | ||
| &self, | ||
| _client: &mut C, | ||
| _target: &Portal<Self::Statement>, | ||
| ) -> PgWireResult<DescribePortalResponse> | ||
| where | ||
| C: ClientInfo + Unpin + Send + Sync, | ||
| { | ||
| panic!("empty query reached portal description") | ||
| } | ||
|
|
||
| async fn do_query<C>( | ||
| &self, | ||
| _client: &mut C, | ||
| _portal: &Portal<Self::Statement>, | ||
| _max_rows: usize, | ||
| ) -> PgWireResult<Response> | ||
| where | ||
| C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync, | ||
| C::Error: Debug, | ||
| PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>, | ||
| { | ||
| panic!("empty query reached query execution") | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn empty_extended_query() { | ||
| let address = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 5432); | ||
| let mut client_info = DefaultClient::<String>::new(address, false); | ||
| client_info.set_state(PgWireConnectionState::ReadyForQuery); | ||
| let codec = PgWireMessageServerCodec::new(client_info); | ||
| let (server_stream, mut client_stream) = duplex(1024); | ||
| let mut server = Framed::new(server_stream, codec); | ||
| let handler = TestExtendedQueryHandler; | ||
|
|
||
| handler | ||
| .on_parse(&mut server, Parse::new(None, String::new(), vec![])) | ||
| .await | ||
| .unwrap(); | ||
| handler | ||
| .on_describe(&mut server, Describe::new(TARGET_TYPE_BYTE_STATEMENT, None)) | ||
| .await | ||
| .unwrap(); | ||
| handler | ||
| .on_bind(&mut server, Bind::new(None, None, vec![], vec![], vec![])) | ||
| .await | ||
| .unwrap(); | ||
| handler | ||
| .on_describe(&mut server, Describe::new(TARGET_TYPE_BYTE_PORTAL, None)) | ||
| .await | ||
| .unwrap(); | ||
| handler | ||
| .on_execute(&mut server, Execute::new(None, 0)) | ||
| .await | ||
| .unwrap(); | ||
| handler.on_sync(&mut server, PgSync::new()).await.unwrap(); | ||
|
|
||
| let mut buffer = BytesMut::new(); | ||
| let mut messages = Vec::new(); | ||
| while messages.len() < 7 { | ||
| assert_ne!(client_stream.read_buf(&mut buffer).await.unwrap(), 0); | ||
| while let Some(message) = | ||
| PgWireBackendMessage::decode(&mut buffer, &DecodeContext::default()).unwrap() | ||
| { | ||
| messages.push(message); | ||
| } | ||
| } | ||
|
|
||
| assert_eq!(messages.len(), 7); | ||
| assert!(matches!( | ||
| messages[0], | ||
| PgWireBackendMessage::ParseComplete(_) | ||
| )); | ||
| match &messages[1] { | ||
| PgWireBackendMessage::ParameterDescription(description) => { | ||
| assert!(description.types.is_empty()); | ||
| } | ||
| message => panic!("unexpected message: {message:?}"), | ||
| } | ||
| assert!(matches!(messages[2], PgWireBackendMessage::NoData(_))); | ||
| assert!(matches!(messages[3], PgWireBackendMessage::BindComplete(_))); | ||
| assert!(matches!(messages[4], PgWireBackendMessage::NoData(_))); | ||
| assert!(matches!( | ||
| messages[5], | ||
| PgWireBackendMessage::EmptyQueryResponse(_) | ||
| )); | ||
| assert!(matches!( | ||
| messages[6], | ||
| PgWireBackendMessage::ReadyForQuery(_) | ||
| )); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for this api implementation, we will still need to check portal.statement.statement.is_none() or use a hard
unwrapjust like the examples. I wonder if we can avoid theOption<>in statement, and use aboolfield in our Statement struct to indicate if a statement is empty.WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bool would still need a value for S when parsing is skipped, which means parsing the empty query again or requiring S: Default. I kept the Option and handled None directly in the examples. Does that work for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes but without the bool, even if we can return
EmptyQueryin our default handler implementation, the user will still need to deal with theOptionand have duplicatedEmptyQuerylogic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A possible new idea is to update our PortalStore API to store empty query specifically. A breaking change at PortalStore is fine because it's rarely directly used in user code.