-
Notifications
You must be signed in to change notification settings - Fork 591
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
perf(expr): new interface for expression directly returning scalar #9049
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
76e8a24
add benchmark
BugenZhao ebd4670
add value and eval_new
BugenZhao 2f6964a
generate eval_new
BugenZhao cdeb820
clean up
BugenZhao c3900ab
use eval_new for literal
BugenZhao 649fe6e
refine docs
BugenZhao 1a26a7d
trigger CI
BugenZhao 8a85fab
simplify trait bound
BugenZhao 446b95a
Merge remote-tracking branch 'origin/main' into bz/scalar-expr
BugenZhao 5a25ac9
rename to eval_v2 and remove todo
BugenZhao 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 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
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 |
---|---|---|
|
@@ -64,9 +64,11 @@ pub(crate) mod data_types; | |
pub(crate) mod template; | ||
pub(crate) mod template_fast; | ||
pub mod test_utils; | ||
mod value; | ||
|
||
use std::sync::Arc; | ||
|
||
use futures_util::TryFutureExt; | ||
use risingwave_common::array::{ArrayRef, DataChunk}; | ||
use risingwave_common::row::{OwnedRow, Row}; | ||
use risingwave_common::types::{DataType, Datum}; | ||
|
@@ -76,9 +78,14 @@ pub use self::agg::AggKind; | |
pub use self::build::*; | ||
pub use self::expr_input_ref::InputRefExpression; | ||
pub use self::expr_literal::LiteralExpression; | ||
pub use self::value::{ValueImpl, ValueRef}; | ||
use super::{ExprError, Result}; | ||
|
||
/// Instance of an expression | ||
/// Interface of an expression. | ||
/// | ||
/// There're two functions to evaluate an expression: `eval` and `eval_v2`, exactly one of them | ||
/// should be implemented. Prefer calling and implementing `eval_v2` instead of `eval` if possible, | ||
/// to gain the performance benefit of scalar expression. | ||
#[async_trait::async_trait] | ||
pub trait Expression: std::fmt::Debug + Sync + Send { | ||
/// Get the return data type. | ||
|
@@ -94,14 +101,30 @@ pub trait Expression: std::fmt::Debug + Sync + Send { | |
Ok(res) | ||
} | ||
|
||
/// Evaluate the expression | ||
/// Evaluate the expression in vectorized execution. Returns an array. | ||
/// | ||
/// # Arguments | ||
/// The default implementation calls `eval_v2` and always converts the result to an array. | ||
async fn eval(&self, input: &DataChunk) -> Result<ArrayRef> { | ||
let value = self.eval_v2(input).await?; | ||
Ok(match value { | ||
ValueImpl::Array(array) => array, | ||
ValueImpl::Scalar { value, capacity } => { | ||
let mut builder = self.return_type().create_array_builder(capacity); | ||
builder.append_datum_n(capacity, value); | ||
builder.finish().into() | ||
} | ||
}) | ||
} | ||
|
||
/// Evaluate the expression in vectorized execution. Returns a value that can be either an | ||
/// array, or a scalar if all values in the array are the same. | ||
/// | ||
/// * `input` - input data of the Project Executor | ||
async fn eval(&self, input: &DataChunk) -> Result<ArrayRef>; | ||
/// The default implementation calls `eval` and puts the result into the `Array` variant. | ||
async fn eval_v2(&self, input: &DataChunk) -> Result<ValueImpl> { | ||
self.eval(input).map_ok(ValueImpl::Array).await | ||
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. So it stackoverflows if both not implemented? 😄 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. Yes. 🥵 Not sure if there's a way to avoid this. |
||
} | ||
|
||
/// Evaluate the expression in row-based execution. | ||
/// Evaluate the expression in row-based execution. Returns a nullable scalar. | ||
async fn eval_row(&self, input: &OwnedRow) -> Result<Datum>; | ||
|
||
/// Evaluate if the expression is constant. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2023 RisingWave 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 either::Either; | ||
use risingwave_common::array::*; | ||
use risingwave_common::for_all_variants; | ||
use risingwave_common::types::{Datum, Scalar}; | ||
|
||
/// The type-erased return value of an expression. | ||
/// | ||
/// It can be either an array, or a scalar if all values in the array are the same. | ||
#[derive(Debug, Clone)] | ||
pub enum ValueImpl { | ||
Array(ArrayRef), | ||
Scalar { value: Datum, capacity: usize }, | ||
} | ||
|
||
/// The generic reference type of [`ValueImpl`]. Used as the arguments of expressions. | ||
#[derive(Debug, Clone, Copy)] | ||
pub enum ValueRef<'a, A: Array> { | ||
Array(&'a A), | ||
Scalar { | ||
value: Option<<A as Array>::RefItem<'a>>, | ||
capacity: usize, | ||
}, | ||
} | ||
|
||
impl<'a, A: Array> ValueRef<'a, A> { | ||
/// Iterates over all scalars in this value. | ||
pub fn iter(self) -> impl Iterator<Item = Option<A::RefItem<'a>>> + 'a { | ||
match self { | ||
Self::Array(array) => Either::Left(array.iter()), | ||
Self::Scalar { value, capacity } => { | ||
Either::Right(std::iter::repeat(value).take(capacity)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
macro_rules! impl_convert { | ||
($( { $variant_name:ident, $suffix_name:ident, $array:ty, $builder:ty } ),*) => { | ||
$( | ||
paste::paste! { | ||
/// Converts a type-erased value to a reference of a specific array type. | ||
impl<'a> From<&'a ValueImpl> for ValueRef<'a, $array> { | ||
fn from(value: &'a ValueImpl) -> Self { | ||
match value { | ||
ValueImpl::Array(array) => { | ||
let array = array.[<as_ $suffix_name>](); | ||
ValueRef::Array(array) | ||
}, | ||
ValueImpl::Scalar { value, capacity } => { | ||
let value = value.as_ref().map(|v| v.[<as_ $suffix_name>]().as_scalar_ref()); | ||
ValueRef::Scalar { value, capacity: *capacity } | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
for_all_variants! { impl_convert } |
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.
Not sure if is there any general way to construct this test case. :(
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 guess not... Let's keep it manual construction :(