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

Ignore NULLs (if desired) while scanning keys during index navigation #8446

Merged
merged 2 commits into from
Feb 25, 2025

Conversation

dyemanov
Copy link
Member

See also #8291. This improvement completes the solution by extending the "ignore null" checks to the scan phase. The logic mostly matches btr.cpp (BTR_evaluate() and scan()).

With the same test case, results are:

before this PR:

SELECT count(*) from (SELECT ID FROM T WHERE ID < 3 ORDER BY ID);

Select Expression
    -> Aggregate
        -> Filter
            -> Table "T" Access By ID
                -> Index "IT" Range Scan (upper bound: 1/1)

                COUNT 
===================== 
                    1 

-- Fetches = 1775

after this PR:

-- Fetches = 7

// If we're walking in a descending index and we need to ignore NULLs
// then stop at the first NULL we see (only for single segment!)
if (descending && ignoreNulls && node.prefix == 0 &&
node.length >= 1 && node.data[0] == 255)
Copy link
Member

Choose a reason for hiding this comment

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

Accordingly to the compress():
// Further a NULL state is always returned as 1 byte 0xFF (descending index).
So, why check for >= 1, not == 1 ?
And, for consistency, please use 0xFF, not 255.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a copy-paste from btr.cpp :) where such a check is used twice. I don't mind changing it, but I'd suggest this to be done in btr.cpp too.

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'm also wondering whether we really should check for (node.prefix == 0 && node.length == 1 && node.data[0] == 0xFF) -- i.e. the first NULL encountered -- or better for (node.prefix + node.length == 1 && node.data[0] == 0xFF) -- i.e. any NULL encountered, as more reliable. Is it theoretically possible that we somehow jump to the bucket in the middle of the NULL duplicates chain and start scanning from there, thus skipping the first NULL node?

Copy link
Member

Choose a reason for hiding this comment

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

I'm also wondering whether we really should check for
(node.prefix == 0 && node.length == 1 && node.data[0] == 0xFF) -- i.e. the first NULL encountered -- or better
for
(node.prefix + node.length == 1 && node.data[0] == 0xFF) -- i.e. any NULL encountered, as more reliable.

Second way is not correct: when node.length == 0 we should not access node.data[0] at all.
Instead, we could look at key contents, if necessary.

Is it theoretically possible that we somehow jump to the bucket in the middle of the NULL duplicates chain and start scanning from there, thus skipping the first NULL node?

The position is based on the last key processed, so, we should be safe.

Copy link
Member

Choose a reason for hiding this comment

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

This is a copy-paste from btr.cpp :) where such a check is used twice. I don't mind changing it, but I'd suggest this to be done in btr.cpp too.

Ok, then, let it be this way

@dyemanov dyemanov merged commit 5767b9e into master Feb 25, 2025
47 of 48 checks passed
@dyemanov dyemanov deleted the work/index-navigation-skip-nulls branch February 25, 2025 07:25
@pavel-zotov
Copy link

QA note: ticket issue is covered by test for 8291 (it was enough to reduce MAX_ALLOWED_IDX_READS threshold, see notes there).

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

Successfully merging this pull request may close these issues.

3 participants