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

fix(expr): fix a critical bug in case expression #13890

Merged
merged 3 commits into from
Dec 9, 2023
Merged

Conversation

wangrunji0408
Copy link
Contributor

@wangrunji0408 wangrunji0408 commented Dec 9, 2023

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

Sometimes the CASE expression fails to short circuit, causing the last matching value to be returned instead of the first one.

 dev=> create table t(x boolean, a int, y boolean, b int);
 CREATE_TABLE
 dev=> insert into t values (true, 1, true, 2);
 INSERT 0 1
 dev=> select *, case when x then a when y then b end from t;
  x | a | y | b | case 
 ---+---+---+---+------
- t | 1 | t | 2 |    2
+ t | 1 | t | 2 |    1

The reason is that some expressions ignore the visibility and return non-null values for invisible rows. Examples include literal expression and simd-optimized expressions such as =. The CASE expression doesn't mask invisible rows in the condition result, so later values may override earlier ones.

This bug seems to have been there from day 1 v0.1.15 (Jan 2023). It sounds very horrible. 🤯

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Comment on lines -68 to +67
let calc_then_vis = when.eval(&input).await?.as_bool().to_bitmap();
let input_vis = input.visibility().clone();
// note that evaluated result from when clause may contain bits that are not visible,
// so we need to mask it with input visibility.
let calc_then_vis = when.eval(&input).await?.as_bool().to_bitmap() & &input_vis;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The core fix.

Copy link
Member

@stdrc stdrc left a comment

Choose a reason for hiding this comment

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

LGTM! Thx for the quick fix!

Copy link

codecov bot commented Dec 9, 2023

Codecov Report

Attention: 28 lines in your changes are missing coverage. Please review.

Comparison is base (b87bcc3) 68.33% compared to head (9c64760) 68.27%.
Report is 2 commits behind head on main.

Files Patch % Lines
src/storage/hummock_sdk/src/compact.rs 0.00% 14 Missing ⚠️
src/meta/src/model/stream.rs 0.00% 10 Missing ⚠️
src/expr/impl/src/scalar/case.rs 89.47% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #13890      +/-   ##
==========================================
- Coverage   68.33%   68.27%   -0.06%     
==========================================
  Files        1528     1528              
  Lines      263343   263322      -21     
==========================================
- Hits       179958   179793     -165     
- Misses      83385    83529     +144     
Flag Coverage Δ
rust 68.27% <57.57%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wangrunji0408 wangrunji0408 added this pull request to the merge queue Dec 9, 2023
Merged via the queue into main with commit 5596207 Dec 9, 2023
34 of 35 checks passed
@wangrunji0408 wangrunji0408 deleted the wrj/fix-case branch December 9, 2023 10:45
github-actions bot pushed a commit that referenced this pull request Dec 9, 2023
Signed-off-by: Runji Wang <wangrunji0408@163.com>
@wangrunji0408
Copy link
Contributor Author

wangrunji0408 commented Dec 9, 2023

I confirmed that this bug was introduced during v0.1.14 to v0.1.15, by #6581.
And it seems that we have never tested cases with more than 1 when clause. 🥶

wangrunji0408 added a commit that referenced this pull request Dec 9, 2023
Signed-off-by: Runji Wang <wangrunji0408@163.com>
Co-authored-by: Runji Wang <wangrunji0408@163.com>
@xxchan
Copy link
Member

xxchan commented Dec 13, 2023

Is this considered a breaking change? 🥵 risingwavelabs/rfcs#24 (comment) @BugenZhao

@BugenZhao
Copy link
Member

Is this considered a breaking change? 🥵 risingwavelabs/rfcs#24 (comment) @BugenZhao

Strictly speaking, yes. 😕 We need to introduce a new function type with the fixed implementation, while keeping the buggy one as "backend only". But it's also okay for me to simply assume that no one persisted the result prior to this PR. 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/fix Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants