-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10125 from zhang2014/refactor/aggregator
refactor(query): use accumulating to impl single state aggregator
- Loading branch information
Showing
8 changed files
with
317 additions
and
214 deletions.
There are no files selected for viewing
This file contains 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 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
132 changes: 132 additions & 0 deletions
132
src/query/pipeline/transforms/src/processors/transforms/transform_accumulating.rs
This file contains 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,132 @@ | ||
// Copyright 2023 Datafuse Labs. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use common_exception::Result; | ||
use common_expression::DataBlock; | ||
use common_pipeline_core::processors::port::InputPort; | ||
use common_pipeline_core::processors::port::OutputPort; | ||
use common_pipeline_core::processors::processor::Event; | ||
use common_pipeline_core::processors::Processor; | ||
|
||
pub trait AccumulatingTransform: Send { | ||
const NAME: &'static str; | ||
|
||
fn transform(&mut self, data: DataBlock) -> Result<Option<DataBlock>>; | ||
|
||
fn on_finish(&mut self, _output: bool) -> Result<Option<DataBlock>> { | ||
Ok(None) | ||
} | ||
} | ||
|
||
pub struct AccumulatingTransformer<T: AccumulatingTransform + 'static> { | ||
inner: T, | ||
input: Arc<InputPort>, | ||
output: Arc<OutputPort>, | ||
|
||
called_on_finish: bool, | ||
input_data: Option<DataBlock>, | ||
output_data: Option<DataBlock>, | ||
} | ||
|
||
impl<T: AccumulatingTransform + 'static> AccumulatingTransformer<T> { | ||
pub fn create(input: Arc<InputPort>, output: Arc<OutputPort>, inner: T) -> Box<dyn Processor> { | ||
Box::new(Self { | ||
inner, | ||
input, | ||
output, | ||
input_data: None, | ||
output_data: None, | ||
called_on_finish: false, | ||
}) | ||
} | ||
} | ||
|
||
impl<T: AccumulatingTransform + 'static> Drop for AccumulatingTransformer<T> { | ||
fn drop(&mut self) { | ||
if !self.called_on_finish { | ||
self.inner.on_finish(false).unwrap(); | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<T: AccumulatingTransform + 'static> Processor for AccumulatingTransformer<T> { | ||
fn name(&self) -> String { | ||
String::from(T::NAME) | ||
} | ||
|
||
fn as_any(&mut self) -> &mut dyn Any { | ||
self | ||
} | ||
|
||
fn event(&mut self) -> Result<Event> { | ||
if self.output.is_finished() { | ||
if !self.called_on_finish { | ||
return Ok(Event::Sync); | ||
} | ||
|
||
self.input.finish(); | ||
return Ok(Event::Finished); | ||
} | ||
|
||
if !self.output.can_push() { | ||
self.input.set_not_need_data(); | ||
return Ok(Event::NeedConsume); | ||
} | ||
|
||
if let Some(data_block) = self.output_data.take() { | ||
self.output.push_data(Ok(data_block)); | ||
return Ok(Event::NeedConsume); | ||
} | ||
|
||
if self.input_data.is_some() { | ||
return Ok(Event::Sync); | ||
} | ||
|
||
if self.input.has_data() { | ||
self.input_data = Some(self.input.pull_data().unwrap()?); | ||
return Ok(Event::Sync); | ||
} | ||
|
||
if self.input.is_finished() { | ||
return match !self.called_on_finish { | ||
true => Ok(Event::Sync), | ||
false => { | ||
self.output.finish(); | ||
Ok(Event::Finished) | ||
} | ||
}; | ||
} | ||
|
||
self.input.set_need_data(); | ||
Ok(Event::NeedData) | ||
} | ||
|
||
fn process(&mut self) -> Result<()> { | ||
if let Some(data_block) = self.input_data.take() { | ||
self.output_data = self.inner.transform(data_block)?; | ||
return Ok(()); | ||
} | ||
|
||
if !self.called_on_finish { | ||
self.called_on_finish = true; | ||
self.output_data = self.inner.on_finish(true)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
src/query/service/src/pipelines/processors/transforms/aggregator/aggregator_final.rs
This file was deleted.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.
72bde50
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.
Successfully deployed to the following URLs:
databend – ./
databend.vercel.app
databend-git-main-databend.vercel.app
databend-databend.vercel.app
databend.rs