Skip to content
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

refactor(expr): refactor constant lookup optimization for case-when expression #14884

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions src/expr/impl/src/scalar/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,35 @@ impl ConstantLookupExpression {
operand,
}
}

/// Evaluate the fallback arm with the given input
async fn eval_fallback(&self, input: &OwnedRow) -> Result<Datum> {
let Some(ref fallback) = self.fallback else {
return Ok(None);
};
let Ok(res) = fallback.eval_row(input).await else {
bail!("failed to evaluate the input for fallback arm");
};
Ok(res)
}

/// The actual lookup & evaluation logic
/// used in both `eval_row` & `eval`
async fn lookup(&self, datum: Datum, input: &OwnedRow) -> Result<Datum> {
if datum.is_none() {
return self.eval_fallback(input).await;
}

if let Some(expr) = self.arms.get(datum.as_ref().unwrap()) {
let Ok(res) = expr.eval_row(input).await else {
bail!("failed to evaluate the input for normal arm");
};
Ok(res)
} else {
// Fallback arm goes here
self.eval_fallback(input).await
}
}
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -162,16 +191,11 @@ impl Expression for ConstantLookupExpression {
// rather than from `eval_result`
let owned_row = row.into_owned_row();

if let Some(expr) = self.arms.get(datum.as_ref().unwrap()) {
builder.append(expr.eval_row(&owned_row).await.unwrap().as_ref());
// Lookup and evaluate the current input datum
if let Ok(datum) = self.lookup(datum, &owned_row).await {
builder.append(datum.as_ref());
} else {
// Otherwise this should goes to the fallback arm
// The fallback arm should also be const
if let Some(ref fallback) = self.fallback {
builder.append(fallback.eval_row(&owned_row).await.unwrap().as_ref());
} else {
builder.append_null();
}
bail!("failed to lookup and evaluate the expression in `eval`");
}
}

Expand All @@ -180,15 +204,7 @@ impl Expression for ConstantLookupExpression {

async fn eval_row(&self, input: &OwnedRow) -> Result<Datum> {
let datum = self.operand.eval_row(input).await?;

if let Some(expr) = self.arms.get(datum.as_ref().unwrap()) {
expr.eval_row(input).await
} else {
let Some(ref expr) = self.fallback else {
return Ok(None);
};
expr.eval_row(input).await
}
self.lookup(datum, input).await
}
}

Expand Down
Loading