-
Notifications
You must be signed in to change notification settings - Fork 598
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
feat(frontend):change query_handle to return data stream #5556
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -12,10 +12,13 @@ | |||
// See the License for the specific language governing permissions and | ||||
// limitations under the License. | ||||
|
||||
use futures::stream::BoxStream; | ||||
use futures::StreamExt; | ||||
use pgwire::pg_field_descriptor::PgFieldDescriptor; | ||||
use pgwire::pg_response::{PgResponse, StatementType}; | ||||
use pgwire::pg_server::BoxedError; | ||||
use pgwire::types::Row; | ||||
use risingwave_common::error::Result; | ||||
use risingwave_common::error::{ErrorCode, Result, RwError}; | ||||
use risingwave_common::session_config::QueryMode; | ||||
use risingwave_sqlparser::ast::Statement; | ||||
use tracing::debug; | ||||
|
@@ -29,7 +32,7 @@ use crate::scheduler::{ | |||
}; | ||||
use crate::session::{OptimizerContext, SessionImpl}; | ||||
|
||||
pub type QueryResultSet = Vec<Row>; | ||||
pub type QueryResultSet = BoxStream<'static, std::result::Result<Row, BoxedError>>; | ||||
|
||||
pub async fn handle_query( | ||||
context: OptimizerContext, | ||||
|
@@ -55,7 +58,7 @@ pub async fn handle_query( | |||
}; | ||||
debug!("query_mode:{:?}", query_mode); | ||||
|
||||
let (rows, pg_descs) = match query_mode { | ||||
let (mut row_stream, pg_descs) = match query_mode { | ||||
QueryMode::Local => { | ||||
if stmt_type.is_dml() { | ||||
// DML do not support local mode yet. | ||||
|
@@ -69,10 +72,15 @@ pub async fn handle_query( | |||
}; | ||||
|
||||
let rows_count = match stmt_type { | ||||
StatementType::SELECT => rows.len() as i32, | ||||
StatementType::SELECT => 0_i32, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this is 0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for query statement, rows_count is meaningless because it's stream mode. We will calculate the row count when we receive the data.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better if we use |
||||
StatementType::INSERT | StatementType::DELETE | StatementType::UPDATE => { | ||||
let first_row = rows[0].values(); | ||||
let affected_rows_str = first_row[0] | ||||
// Get the row from the row_stream. | ||||
let first_row = row_stream | ||||
.next() | ||||
.await | ||||
.expect("compute node should return affected rows in output") | ||||
.map_err(|err| RwError::from(ErrorCode::InternalError(format!("{}", err))))?; | ||||
let affected_rows_str = first_row.values()[0] | ||||
.as_ref() | ||||
.expect("compute node should return affected rows in output"); | ||||
String::from_utf8(affected_rows_str.to_vec()) | ||||
|
@@ -88,7 +96,9 @@ pub async fn handle_query( | |||
flush_for_write(&session, stmt_type).await?; | ||||
} | ||||
|
||||
Ok(PgResponse::new(stmt_type, rows_count, rows, pg_descs)) | ||||
Ok(PgResponse::new_for_stream( | ||||
stmt_type, rows_count, row_stream, pg_descs, | ||||
)) | ||||
} | ||||
|
||||
fn to_statement_type(stmt: &Statement) -> StatementType { | ||||
|
@@ -195,7 +205,7 @@ async fn local_execute( | |||
// TODO: Passing sql here | ||||
let execution = | ||||
LocalQueryExecution::new(query, front_env.clone(), "", epoch, session.auth_context()); | ||||
let rsp = Ok((execution.collect_rows(format).await?, pg_descs)); | ||||
let rsp = Ok((execution.stream_rows(format), pg_descs)); | ||||
|
||||
// Release hummock snapshot for local execution. | ||||
hummock_snapshot_manager.release(epoch, &query_id).await; | ||||
|
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.
Can we use generic to avoid this
Box
? There could be millions of rows in the result set, and I'm not sure whether this impacts the performance a lot.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.
Indeed it may impact the performance. I try to use generic but it will make so many places full of generic param. I think it's not appropriate to use generic param( at least the way I use .
I think there are other ways to solve this problem:
BoxStream<'static, std::result::Result<Vec,BoxedError>>
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.
Could you elaborate this point? 👀
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.
Boxstream is Box, it's a dynamic bind. So I guess every time we call 'stream.next()' to get a row, it needs to call by some mechanism like vtable. Hence if there are millions of rows in the result set, we may need to access vtable millions of times.
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.
Oh yeees, Sorry I misread it as "may not impact" 😄😄😄
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. We may Type Alias Impl Trait here.
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.
I have used this way to decrease the time of calling stream.next() .
And I will create a new PR to create a new stream type to avoid Boxstream 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.
Please create an issue for that