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

Storages: Fix comparing null in MinMaxIndex #9373

Merged
merged 10 commits into from
Aug 27, 2024

Conversation

JinheLin
Copy link
Contributor

@JinheLin JinheLin commented Aug 26, 2024

What problem does this PR solve?

Issue Number: close #9374

Problem Summary:

The filter of select * from test.t where not (a between NULL and '2024-08-25') will be transformed in TiFlash as not (a between NULL and '2024-08-25') => not (a >= NULL and a <= '2024-08-25') => a < NULL or a > '2024-08-25'.

The problem is how RSOperator and MinMaxIndex handle a < NULL.

As the code below, Less will be transformed into not GreaterEqual:

RSResults roughCheck(size_t start_pack, size_t pack_count, const RSCheckParam & param) override
{
auto results = minMaxCheckCmp<RoughCheck::CheckGreaterEqual>(start_pack, pack_count, param, attr, value);
std::transform(results.begin(), results.end(), results.begin(), [](RSResult result) { return !result; });
return results;
}

MinMaxIndex execute GreaterEqual and the result for comparing with NULL is RSResult::None.

RSResults MinMaxIndex::checkCmp(size_t start_pack, size_t pack_count, const Field & value, const DataTypePtr & type)
{
RSResults results(pack_count, RSResult::None);
if (value.isNull())
return results;

not RSResult::None equals to RSResult::All. And PR #9361 will skip filter if the filter result of a Block is RSResult::All.

The root cause is that the result of comparing any value with null is null, returning RSResult::None is incorrect.

What is changed and how it works?

In `MinMaxIndex`, if the input value is null, return `RSResult::NoneNull`.

Handling null input value, in checkCmp:

RSResults MinMaxIndex::checkCmp(size_t start_pack, size_t pack_count, const Field & value, const DataTypePtr & type)
{
RSResults results(pack_count, RSResult::None);
if (value.isNull())
return results;

Handling null input value, in checkIn:

// skip null value
if (v.isNull())
continue;

For some filters like checkCmp(null), the result is RSResult::NoneNull and the block can be skipped.
For some filters like not checkCmp(null), the result is RSResult::AllNull and the block will be read and filtered.

In fact, the result checkCmp(null) and not checkCmp(null) should always be null. Maybe we can extend RSResult to support this. However, comparing(>, <, >=, <=, ==, !=, in) with null is not a normal operation in MySQL. So just let it fallback to FilterTransforamAction if neccessary.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 26, 2024
@JinheLin JinheLin changed the title WIP: Storages: Fix comparing null in MinMaxIndex Storages: Fix comparing null in MinMaxIndex Aug 27, 2024
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 27, 2024
@@ -258,8 +258,6 @@ RSResults MinMaxIndex::checkNullableIn(
{
const auto & column_nullable = static_cast<const ColumnNullable &>(*minmaxes);
const auto & null_map = column_nullable.getNullMapColumn();

RSResults results(pack_count, RSResult::SomeNull);
Copy link
Contributor Author

@JinheLin JinheLin Aug 27, 2024

Choose a reason for hiding this comment

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

Just move some results to a smaller scope.

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Aug 27, 2024
dbms/src/Storages/DeltaMerge/Index/RoughCheck.h Outdated Show resolved Hide resolved
dbms/src/Storages/DeltaMerge/Index/MinMaxIndex.cpp Outdated Show resolved Hide resolved
Comment on lines +2684 to +2689
check(createGreater(attr("Int64"), null_value), RSResult::NoneNull);
check(createGreaterEqual(attr("Int64"), null_value), RSResult::NoneNull);
check(createLess(attr("Int64"), null_value), RSResult::AllNull);
check(createLessEqual(attr("Int64"), null_value), RSResult::AllNull);
check(createEqual(attr("Int64"), null_value), RSResult::NoneNull);
check(createNotEqual(attr("Int64"), null_value), RSResult::AllNull);
Copy link
Contributor

Choose a reason for hiding this comment

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

why "less"/"lessequal"/"noteuqal" return "AllNull" but others return "NoneNull"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because Less is transformed to not GreaterEqual in implementation...

GreaterEqual(NULL) will return RSResult::NoneNull as expected.

RSResults roughCheck(size_t start_pack, size_t pack_count, const RSCheckParam & param) override
{
auto results = minMaxCheckCmp<RoughCheck::CheckGreaterEqual>(start_pack, pack_count, param, attr, value);
std::transform(results.begin(), results.end(), results.begin(), [](RSResult result) { return !result; });
return results;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LessEqual is transformed to not Greater.
NotEqual is not Equal.

Copy link
Contributor

Choose a reason for hiding this comment

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

Better add some test case about this: https://dev.mysql.com/doc/refman/8.4/en/working-with-null.html

Two NULL values are regarded as equal in a GROUP BY.
When doing an ORDER BY, NULL values are presented first if you do ORDER BY ... ASC and last if you do ORDER BY ... DESC.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 27, 2024
Copy link
Contributor

@JaySon-Huang JaySon-Huang left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

ti-chi-bot bot commented Aug 27, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JaySon-Huang, Lloyd-Pottiger

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [JaySon-Huang,Lloyd-Pottiger]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Aug 27, 2024
Copy link
Contributor

ti-chi-bot bot commented Aug 27, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-08-27 04:02:50.05592476 +0000 UTC m=+842965.190374874: ☑️ agreed by Lloyd-Pottiger.
  • 2024-08-27 08:20:12.678406807 +0000 UTC m=+858407.812856930: ☑️ agreed by JaySon-Huang.

@ti-chi-bot ti-chi-bot bot merged commit 6664c0e into pingcap:master Aug 27, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect result is returned when comparing with null
3 participants