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

feat: Optimize CASE expression for "column or null" use case #11534

Merged
merged 5 commits into from
Jul 19, 2024

Conversation

andygrove
Copy link
Member

@andygrove andygrove commented Jul 18, 2024

Which issue does this PR close?

Closes #11484

Rationale for this change

Some TPC-DS queries include expressions such as case when (d_day_name='Sunday') then ss_sales_price else null end. The current implementation of CaseExpr is very expensive for an expression which is basically IF(expr, column, null). For this case we can take a fast path of just changing the null bitmask on the column.

case_when: column or null
                        time:   [842.29 ns 844.91 ns 847.49 ns]
                        change: [-92.101% -92.074% -92.046%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  1 (1.00%) low severe
  7 (7.00%) low mild
  2 (2.00%) high mild

What changes are included in this PR?

The existing evaluate method already had conditional logic for choosing between two execution methods based on context. This PR adds a third that specializes for the "column or null" use case.

The benchmarks were also improved.

Are these changes tested?

New test added for this optimization.

Are there any user-facing changes?

No, just faster queries in some cases.

@github-actions github-actions bot added the physical-expr Physical Expressions label Jul 18, 2024
/// END
CaseWithExpression,
/// This is a specialization for a specific use case where we can take a fast path
/// CASE WHEN condition THEN column [ELSE NULL] END
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm wondering why this special format of case when is not optimized to if/else during query optimization.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think DataFusion has an if/else expression. We have one in Comet that could be upstreamed.

EvalMethod::CaseWithExpression
} else if when_then_expr.len() == 1
&& when_then_expr[0].1.as_any().is::<Column>()
&& else_expr.is_none()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If else value is a null literal, is else_expr still none? Or is it a null literal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From SQL planning, it will be None, but it makes sense to add code here to normalize this for other use cases (other query engines that delegate to DataFusion). I have added this.

fn case_column_or_null(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
let when_expr = &self.when_then_expr[0].0;
let then_expr = &self.when_then_expr[0].1;
if let ColumnarValue::Array(bit_mask) = when_expr.evaluate(batch)? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is evaluated to Scalar, maybe we still can work on it? I.e., we can convert the scalar value to an boolean array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am planning on adding a specialization for scalar values as well to avoid converting scalars to arrays

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, nm, your question is different. Yes, we could implement special handling for CASE WHEN [true|false] THEN but I'm not sure that is a real-world use case that is worth optimizing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I forgot that in this optimization, the when expression could only be a Column.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @andygrove -- I think the performance improvement is pretty compelling and this PR does what it says.

I think it would be great to continue to optimize the CASE evaluation in follow on PRs. I bet if you filed some tickets others in the community would likely pitch in to help

ColumnarValue::Array(array) => {
Ok(ColumnarValue::Array(nullif(&array, &bit_mask)?))
}
ColumnarValue::Scalar(_) => Err(DataFusionError::Execution(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be better as an internal error

You could also use the macros like

internal_err!("expression did not evaluate to an array")

if when_then_expr.is_empty() {
exec_err!("There must be at least one WHEN clause")
} else {
let eval_method = if expr.is_some() {
EvalMethod::WithExpression
} else if when_then_expr.len() == 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this optimization and I think it would be valid for any CASE expression that had a NULL ELSE, not just column

So in other words, I think you could remove the when_then_expr[0].1.as_any().is::<Column>() check and this would still work fine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, it is only safe to use this approach for expressions that are infallible. One of the main reasons that regular CaseExpr is expensive is that we need to only evaluate the "true" expression on rows where the predicate has evaluated to true.

I do think that this could be extended beyond just Column expressions though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a comment in the code to explain this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more comments to explain the rationale and what is safe or not

}
} else {
Err(DataFusionError::Execution(
"predicate did not evaluate to an array".to_string(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @viirya I think it would be fairly straightforward here to handle ColumnarValue::Scalar s well. I don't think we need to do it in this PR

Given you say in https://github.com/apache/datafusion/pull/11534/files#r1683081792

I am planning on adding a specialization for scalar values as well to avoid converting scalars to arrays

I think we could definitely do it in a follow on PR

@@ -998,6 +1080,53 @@ mod tests {
Ok(())
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to make sure we had a slt level test to cover this as well,

Maybe in
https://github.com/apache/datafusion/blob/382bf4f3c7a730828684b9e4ce01369b89717e19/datafusion/sqllogictest/test_files/expr.slt

Or we could start adding a file just for CASE if we are about to spend a bunch of time optimizing it 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I willl add slt tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am planning on more CASE optimizations so will create a separate file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the slt test. This is my first time using slt and it is very cool

.downcast_ref::<BooleanArray>()
.expect("predicate should evaluate to a boolean array");
// invert the bitmask
let bit_mask = not(bit_mask)?;
Copy link
Contributor

@Dandandan Dandandan Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a further optimization I think the when predicate can be transformed to be not(condition) so it's possible to use bit_mask directly instead of not(bit_mask).
This makes it possible to optimize/simplify a condition like x=1 to x!=1 instead of not(x=1), saving the invert.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will look at this suggestion as a follow on. Great idea.

@github-actions github-actions bot added the sqllogictest SQL Logic Tests (.slt) label Jul 19, 2024
@andygrove andygrove merged commit 28fa74b into apache:main Jul 19, 2024
23 checks passed
@andygrove andygrove added the performance Make DataFusion faster label Jul 19, 2024
Lordworms pushed a commit to Lordworms/arrow-datafusion that referenced this pull request Jul 23, 2024
wiedld pushed a commit to influxdata/arrow-datafusion that referenced this pull request Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate performance Make DataFusion faster physical-expr Physical Expressions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize CASE WHEN for "expression or null" case
4 participants