-
Notifications
You must be signed in to change notification settings - Fork 592
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(expr): expression node level non-strict evaluation #12461
Merged
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8479c9d
non strict expr wrapper
BugenZhao b19e668
build non strict
BugenZhao 91869b3
refactor executors
BugenZhao 8676ca2
refactor with build trait
BugenZhao f6c468b
clippy
BugenZhao 96600ff
refine docs
BugenZhao 5402efb
format lines
BugenZhao 00a22aa
add more docs
BugenZhao a16c575
add build func non strict
BugenZhao 351e3cc
use `build_func_non_strict`
BugenZhao f306ce9
do not wrap child
BugenZhao 64602ac
Merge remote-tracking branch 'origin/main' into bz/non-strict-expr-wr…
BugenZhao 1b8cdc3
refine e2e tests
BugenZhao fca876b
Merge remote-tracking branch 'origin/main' into bz/non-strict-expr-wr…
BugenZhao d087627
fix hakari
BugenZhao c15446c
fix e2e test
BugenZhao e108c19
Merge remote-tracking branch 'origin/main' into bz/non-strict-expr-wr…
BugenZhao 52300b1
Merge remote-tracking branch 'origin/main' into bz/non-strict-expr-wr…
BugenZhao 03cff53
minor refactor
BugenZhao b671946
remove needless ctxs
BugenZhao 921eefc
cleanup identities
BugenZhao a597f2c
fix clippy
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
|
@@ -27,57 +27,160 @@ use super::expr_in::InExpression; | |
use super::expr_some_all::SomeAllExpression; | ||
use super::expr_udf::UdfExpression; | ||
use super::expr_vnode::VnodeExpression; | ||
use super::wrapper::Checked; | ||
use crate::expr::{ | ||
BoxedExpression, Expression, InputRefExpression, LiteralExpression, TryFromExprNodeBoxed, | ||
}; | ||
use super::wrapper::{Checked, EvalErrorReport, NonStrict}; | ||
use crate::expr::{BoxedExpression, Expression, InputRefExpression, LiteralExpression}; | ||
use crate::sig::func::FUNC_SIG_MAP; | ||
use crate::sig::FuncSigDebug; | ||
use crate::{bail, ExprError, Result}; | ||
|
||
/// Build an expression from protobuf. | ||
fn build_from_prost_inner(prost: &ExprNode) -> Result<BoxedExpression> { | ||
use PbType as E; | ||
|
||
let func_call = match prost.get_rex_node()? { | ||
RexNode::InputRef(_) => return InputRefExpression::try_from_boxed(prost), | ||
RexNode::Constant(_) => return LiteralExpression::try_from_boxed(prost), | ||
RexNode::Udf(_) => return UdfExpression::try_from_boxed(prost), | ||
RexNode::FuncCall(func_call) => func_call, | ||
RexNode::Now(_) => unreachable!("now should not be built at backend"), | ||
}; | ||
|
||
let func_type = prost.function_type(); | ||
|
||
match func_type { | ||
// Dedicated types | ||
E::All | E::Some => SomeAllExpression::try_from_boxed(prost), | ||
E::In => InExpression::try_from_boxed(prost), | ||
E::Case => CaseExpression::try_from_boxed(prost), | ||
E::Coalesce => CoalesceExpression::try_from_boxed(prost), | ||
E::Field => FieldExpression::try_from_boxed(prost), | ||
E::Vnode => VnodeExpression::try_from_boxed(prost), | ||
|
||
_ => { | ||
let ret_type = DataType::from(prost.get_return_type().unwrap()); | ||
let children = func_call | ||
.get_children() | ||
.iter() | ||
.map(build_from_prost) | ||
.try_collect()?; | ||
|
||
build_func(func_type, ret_type, children) | ||
pub fn build_from_prost(prost: &ExprNode) -> Result<BoxedExpression> { | ||
ExprBuilder::new_strict().build(prost) | ||
} | ||
|
||
/// Build an expression from protobuf in non-strict mode. | ||
pub fn build_non_strict_from_prost( | ||
prost: &ExprNode, | ||
error_report: impl EvalErrorReport + 'static, | ||
) -> Result<BoxedExpression> { | ||
ExprBuilder::new_non_strict(error_report).build(prost) | ||
} | ||
|
||
/// Build an expression from protobuf with possibly some wrappers attached to each node. | ||
struct ExprBuilder<R> { | ||
/// The error reporting for non-strict mode. | ||
/// | ||
/// If set, each expression node will be wrapped with a [`NonStrict`] node that reports | ||
/// errors to this error reporting. | ||
error_report: Option<R>, | ||
} | ||
|
||
impl ExprBuilder<!> { | ||
/// Create a new builder in strict mode. | ||
fn new_strict() -> Self { | ||
Self { error_report: None } | ||
} | ||
} | ||
|
||
impl<R> ExprBuilder<R> | ||
where | ||
R: EvalErrorReport + 'static, | ||
{ | ||
/// Create a new builder in non-strict mode with the given error reporting. | ||
fn new_non_strict(error_report: R) -> Self { | ||
Self { | ||
error_report: Some(error_report), | ||
} | ||
} | ||
|
||
/// Attach wrappers to an expression. | ||
#[expect(clippy::let_and_return)] | ||
fn wrap(&self, expr: impl Expression + 'static) -> BoxedExpression { | ||
let checked = Checked(expr); | ||
|
||
let may_non_strict = if let Some(error_report) = &self.error_report { | ||
NonStrict::new(checked, error_report.clone()).boxed() | ||
} else { | ||
checked.boxed() | ||
}; | ||
|
||
may_non_strict | ||
} | ||
|
||
/// Build an expression with `build_inner` and attach some wrappers. | ||
fn build(&self, prost: &ExprNode) -> Result<BoxedExpression> { | ||
let expr = self.build_inner(prost)?; | ||
Ok(self.wrap(expr)) | ||
} | ||
|
||
/// Build an expression from protobuf. | ||
fn build_inner(&self, prost: &ExprNode) -> Result<BoxedExpression> { | ||
use PbType as E; | ||
|
||
let build_child = |prost: &'_ ExprNode| self.build(prost); | ||
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. Interesting |
||
|
||
match prost.get_rex_node()? { | ||
RexNode::InputRef(_) => InputRefExpression::build_boxed(prost, build_child), | ||
RexNode::Constant(_) => LiteralExpression::build_boxed(prost, build_child), | ||
RexNode::Udf(_) => UdfExpression::build_boxed(prost, build_child), | ||
|
||
RexNode::FuncCall(_) => match prost.function_type() { | ||
// Dedicated types | ||
E::All | E::Some => SomeAllExpression::build_boxed(prost, build_child), | ||
E::In => InExpression::build_boxed(prost, build_child), | ||
E::Case => CaseExpression::build_boxed(prost, build_child), | ||
E::Coalesce => CoalesceExpression::build_boxed(prost, build_child), | ||
E::Field => FieldExpression::build_boxed(prost, build_child), | ||
E::Vnode => VnodeExpression::build_boxed(prost, build_child), | ||
|
||
// General types, lookup in the function signature map | ||
_ => FuncCallBuilder::build_boxed(prost, build_child), | ||
}, | ||
|
||
RexNode::Now(_) => unreachable!("now should not be built at backend"), | ||
} | ||
} | ||
} | ||
|
||
/// Build an expression from protobuf with wrappers. | ||
pub fn build_from_prost(prost: &ExprNode) -> Result<BoxedExpression> { | ||
let expr = build_from_prost_inner(prost)?; | ||
/// Manually build the expression `Self` from protobuf. | ||
pub(crate) trait Build: Expression + Sized { | ||
/// Build the expression `Self` from protobuf. | ||
/// | ||
/// To build children, call `build_child` on each child instead of [`build_from_prost`]. | ||
fn build( | ||
prost: &ExprNode, | ||
build_child: impl Fn(&ExprNode) -> Result<BoxedExpression>, | ||
) -> Result<Self>; | ||
|
||
/// Build the expression `Self` from protobuf for test, where each child is built with | ||
/// [`build_from_prost`]. | ||
fn build_for_test(prost: &ExprNode) -> Result<Self> { | ||
Self::build(prost, build_from_prost) | ||
} | ||
} | ||
|
||
/// Manually build a boxed expression from protobuf. | ||
pub(crate) trait BuildBoxed: 'static { | ||
/// Build a boxed expression from protobuf. | ||
fn build_boxed( | ||
prost: &ExprNode, | ||
build_child: impl Fn(&ExprNode) -> Result<BoxedExpression>, | ||
) -> Result<BoxedExpression>; | ||
} | ||
|
||
/// Implement [`BuildBoxed`] for all expressions that implement [`Build`]. | ||
impl<E: Build + 'static> BuildBoxed for E { | ||
fn build_boxed( | ||
prost: &ExprNode, | ||
build_child: impl Fn(&ExprNode) -> Result<BoxedExpression>, | ||
) -> Result<BoxedExpression> { | ||
Self::build(prost, build_child).map(Expression::boxed) | ||
} | ||
} | ||
|
||
let checked = Checked(expr); | ||
/// Build a function call expression from protobuf with [`build_func`]. | ||
struct FuncCallBuilder; | ||
|
||
Ok(checked.boxed()) | ||
impl BuildBoxed for FuncCallBuilder { | ||
fn build_boxed( | ||
prost: &ExprNode, | ||
build_child: impl Fn(&ExprNode) -> Result<BoxedExpression>, | ||
) -> Result<BoxedExpression> { | ||
let func_type = prost.function_type(); | ||
let ret_type = DataType::from(prost.get_return_type().unwrap()); | ||
let func_call = prost | ||
.get_rex_node()? | ||
.as_func_call() | ||
.expect("not a func call"); | ||
|
||
let children = func_call | ||
.get_children() | ||
.iter() | ||
.map(build_child) | ||
.try_collect()?; | ||
|
||
build_func(func_type, ret_type, children) | ||
} | ||
} | ||
|
||
/// Build an expression in `FuncCall` variant. | ||
|
@@ -111,9 +214,26 @@ pub fn build_func( | |
} | ||
)) | ||
})?; | ||
|
||
(desc.build)(ret_type, children) | ||
} | ||
|
||
/// Build an expression in `FuncCall` variant in non-strict mode. | ||
/// | ||
/// Note: This is a workaround, and only the root node are wrappedin non-strict mode. | ||
/// Prefer [`build_non_strict_from_prost`] if possible. | ||
pub fn build_func_non_strict( | ||
func: PbType, | ||
ret_type: DataType, | ||
children: Vec<BoxedExpression>, | ||
error_report: impl EvalErrorReport + 'static, | ||
) -> Result<BoxedExpression> { | ||
let expr = build_func(func, ret_type, children)?; | ||
let wrapped = ExprBuilder::new_non_strict(error_report).wrap(expr); | ||
|
||
Ok(wrapped) | ||
} | ||
|
||
pub(super) fn get_children_and_return_type(prost: &ExprNode) -> Result<(&[ExprNode], DataType)> { | ||
let ret_type = DataType::from(prost.get_return_type().unwrap()); | ||
if let RexNode::FuncCall(func_call) = prost.get_rex_node().unwrap() { | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
What does
impl ExprBuilder<!>
mean?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.
We need to specify the generic type
R: EvalErrorReport
even if we useNone
forerror_report
. However, since the concrete type does not matter in this case, we can simply use the "never type"!
.