mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 13:44:03 +00:00
feat(common-meta): add raw-series preflight and execution surface to Datanode trait
Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
@@ -18,10 +18,21 @@ use api::region::RegionResponse;
|
||||
use api::v1::flow::{DirtyWindowRequests, FlowRequest, FlowResponse};
|
||||
use api::v1::region::{InsertRequests, RegionRequest};
|
||||
pub use common_base::AffectedRows;
|
||||
use common_query::request::QueryRequest;
|
||||
use common_error::ext::{BoxedError, PlainError};
|
||||
use common_error::status_code::StatusCode;
|
||||
use common_query::error::Error as CommonQueryError;
|
||||
use common_query::request::{
|
||||
PreparedNamedRawSeries, QueryRequest, RAW_SERIES_REQUEST_EXTENSION_KEY, RawSeriesRequest,
|
||||
};
|
||||
use common_query::series_frame::{raw_series_frame_ordering, unpack_raw_series};
|
||||
use common_recordbatch::SendableRecordBatchStream;
|
||||
use common_recordbatch::adapter::DfRecordBatchStreamAdapter;
|
||||
use datatypes::series::SeriesOrdering;
|
||||
use datatypes::series::samples::SeriesSchema;
|
||||
use snafu::{ResultExt, location};
|
||||
use store_api::region_engine::{RawLabelSource, SendableRawSeriesStream};
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::error::{ExternalSnafu, Result};
|
||||
use crate::peer::Peer;
|
||||
|
||||
/// The trait for handling requests to datanode.
|
||||
@@ -32,6 +43,111 @@ pub trait Datanode: Send + Sync {
|
||||
|
||||
/// Handles query requests
|
||||
async fn handle_query(&self, request: QueryRequest) -> Result<SendableRecordBatchStream>;
|
||||
|
||||
/// Prepares a bound raw-series query without executing it.
|
||||
///
|
||||
/// Implementations that do not support preflight return `Ok(None)` without execution or a
|
||||
/// validation guarantee; failures other than unsupported preparation are returned as errors.
|
||||
async fn prepare_raw_series_query(
|
||||
&self,
|
||||
_request: QueryRequest,
|
||||
_binding: RawSeriesRequest,
|
||||
) -> Result<Option<SeriesOrdering>> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Non-consumingly checks whether a named raw-series preflight is supported.
|
||||
///
|
||||
/// Returns the approved binding and ordering on support, or `None` when the request is
|
||||
/// explicitly unsupported; failures that are not unsupported preparation are returned as
|
||||
/// errors. This point-in-time check does not reserve a plan, and execution revalidates
|
||||
/// support.
|
||||
async fn prepare_named_raw_series_query(
|
||||
&self,
|
||||
_request: QueryRequest,
|
||||
) -> Result<Option<PreparedNamedRawSeries>> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Handles a bound raw-series query and decodes its series frames.
|
||||
///
|
||||
/// The binding is an expectation supplied by the already-authorized internal planner; it is
|
||||
/// not evidence of provenance or an independent authorization grant.
|
||||
async fn handle_raw_series_query(
|
||||
&self,
|
||||
request: QueryRequest,
|
||||
binding: RawSeriesRequest,
|
||||
) -> Result<SendableRawSeriesStream> {
|
||||
let (request, expected_schema, label_source) = bind_raw_series_query(request, &binding)?;
|
||||
let stream = self.handle_query(request).await?;
|
||||
let ordering = raw_series_frame_ordering(stream.schema().arrow_schema().as_ref());
|
||||
let stream = Box::pin(DfRecordBatchStreamAdapter::new(stream));
|
||||
ordering
|
||||
.and_then(|ordering| unpack_raw_series(stream, expected_schema, label_source, ordering))
|
||||
.map_err(|error| {
|
||||
BoxedError::new(CommonQueryError::GeneralDataFusion {
|
||||
error,
|
||||
location: location!(),
|
||||
})
|
||||
})
|
||||
.context(ExternalSnafu)
|
||||
}
|
||||
}
|
||||
|
||||
/// Binds a raw-series request to the query context without executing the query.
|
||||
pub fn bind_raw_series_query(
|
||||
mut request: QueryRequest,
|
||||
binding: &RawSeriesRequest,
|
||||
) -> Result<(QueryRequest, Arc<SeriesSchema>, RawLabelSource)> {
|
||||
if request.region_id != binding.physical_metadata.region_id {
|
||||
return Err(BoxedError::new(PlainError::new(
|
||||
"raw-series request region does not match binding".to_string(),
|
||||
StatusCode::InvalidArguments,
|
||||
)))
|
||||
.context(ExternalSnafu);
|
||||
}
|
||||
let (expected_schema, label_source) = binding
|
||||
.bind(Arc::new(binding.physical_metadata.clone()))
|
||||
.map_err(|error| {
|
||||
BoxedError::new(CommonQueryError::GeneralDataFusion {
|
||||
error,
|
||||
location: location!(),
|
||||
})
|
||||
})
|
||||
.context(ExternalSnafu)?;
|
||||
if request
|
||||
.header
|
||||
.as_ref()
|
||||
.and_then(|header| header.query_context.as_ref())
|
||||
.is_some_and(|context| {
|
||||
context
|
||||
.extensions
|
||||
.contains_key(RAW_SERIES_REQUEST_EXTENSION_KEY)
|
||||
})
|
||||
{
|
||||
return Err(BoxedError::new(PlainError::new(
|
||||
"raw-series request marker already exists".to_string(),
|
||||
StatusCode::InvalidArguments,
|
||||
)))
|
||||
.context(ExternalSnafu);
|
||||
}
|
||||
let extension = binding
|
||||
.to_extension_value()
|
||||
.map_err(|error| {
|
||||
BoxedError::new(CommonQueryError::GeneralDataFusion {
|
||||
error,
|
||||
location: location!(),
|
||||
})
|
||||
})
|
||||
.context(ExternalSnafu)?;
|
||||
request
|
||||
.header
|
||||
.get_or_insert_default()
|
||||
.query_context
|
||||
.get_or_insert_default()
|
||||
.extensions
|
||||
.insert(RAW_SERIES_REQUEST_EXTENSION_KEY.to_string(), extension);
|
||||
Ok((request, expected_schema, label_source))
|
||||
}
|
||||
|
||||
impl dyn Datanode + '_ {
|
||||
|
||||
Reference in New Issue
Block a user