-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9261 from CurtizJ/improve-index-in
Improve performance of analysing index with IN
- Loading branch information
Showing
6 changed files
with
246 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
dbms/src/Interpreters/tests/gtest_merge_tree_set_index.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include <Interpreters/Set.h> | ||
#include <DataTypes/DataTypesNumber.h> | ||
#include <DataTypes/DataTypeString.h> | ||
#include <Storages/MergeTree/KeyCondition.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace DB; | ||
|
||
TEST(MergeTreeSetIndex, checkInRange_one) | ||
{ | ||
DataTypes types = {std::make_shared<const DataTypeInt64>()}; | ||
|
||
auto mut = types[0]->createColumn(); | ||
mut->insert(1); | ||
mut->insert(5); | ||
mut->insert(7); | ||
|
||
Columns columns = {std::move(mut)}; | ||
|
||
std::vector<MergeTreeSetIndex::KeyTuplePositionMapping> mapping = {{0, 0, {}}}; | ||
auto set = std::make_unique<MergeTreeSetIndex>(columns, std::move(mapping)); | ||
|
||
// Left and right bounded | ||
std::vector<Range> ranges = {Range(1, true, 4, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "(1, 4)"; | ||
|
||
ranges = {Range(2, true, 4, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, false) << "(2, 4)"; | ||
|
||
ranges = {Range(-1, true, 0, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, false) << "(-1, 0)"; | ||
|
||
ranges = {Range(-1, true, 10, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "(-1, 10)"; | ||
|
||
// Left bounded | ||
ranges = {Range::createLeftBounded(1, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "(1, +inf)"; | ||
|
||
ranges = {Range::createLeftBounded(-1, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "(-1, +inf)"; | ||
|
||
ranges = {Range::createLeftBounded(10, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, false) << "(10, +inf)"; | ||
|
||
// Right bounded | ||
ranges = {Range::createRightBounded(1, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "(-inf, 1)"; | ||
|
||
ranges = {Range::createRightBounded(-1, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, false) << "(-inf, -1)"; | ||
|
||
ranges = {Range::createRightBounded(10, true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "(-inf, 10)"; | ||
} | ||
|
||
TEST(MergeTreeSetIndex, checkInRange_tuple) | ||
{ | ||
DataTypes types = {std::make_shared<const DataTypeUInt64>(), std::make_shared<const DataTypeString>()}; | ||
|
||
Columns columns; | ||
{ | ||
auto values = {1, 1, 3, 3, 3, 10}; | ||
auto mut = types[0]->createColumn(); | ||
for (auto & val : values) | ||
mut->insert(val); | ||
columns.push_back(std::move(mut)); | ||
} | ||
|
||
{ | ||
auto values = {"a", "b", "a", "a", "b", "c"}; | ||
auto mut = types[1]->createColumn(); | ||
for (auto & val : values) | ||
mut->insert(val); | ||
columns.push_back(std::move(mut)); | ||
} | ||
|
||
std::vector<MergeTreeSetIndex::KeyTuplePositionMapping> mapping = {{0, 0, {}}, {1, 1, {}}}; | ||
auto set = std::make_unique<MergeTreeSetIndex>(columns, std::move(mapping)); | ||
|
||
std::vector<Range> ranges = {Range(1), Range("a", true, "c", true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "Range(1), Range('a', true, 'c', true)"; | ||
|
||
ranges = {Range(1, false, 3, false), Range()}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, false) << "Range(1, false, 3, false), Range()"; | ||
|
||
ranges = {Range(2, false, 5, false), Range()}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "Range(2, false, 5, false), Range()"; | ||
|
||
ranges = {Range(3), Range::createLeftBounded("a", true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "Range(3), Range::createLeftBounded('a', true)"; | ||
|
||
ranges = {Range(3), Range::createLeftBounded("f", true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, false) << "Range(3), Range::createLeftBounded('f', true)"; | ||
|
||
ranges = {Range(3), Range::createRightBounded("a", true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "Range(3), Range::createRightBounded('a', true)"; | ||
|
||
ranges = {Range(3), Range::createRightBounded("b", true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "Range(3), Range::createRightBounded('b', true)"; | ||
|
||
ranges = {Range(1), Range("b")}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "Range(1), Range('b')"; | ||
|
||
ranges = {Range(1), Range("c")}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, false) << "Range(1), Range('c')"; | ||
|
||
ranges = {Range(2, true, 3, true), Range()}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, true) << "Range(2, true, 3, true), Range('x', true, 'z', true)"; | ||
|
||
ranges = {Range(2), Range("a", true, "z", true)}; | ||
ASSERT_EQ(set->checkInRange(ranges, types).can_be_true, false) << "Range(2, true, 3, true), Range('c', true, 'z', true)"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<test> | ||
<type>loop</type> | ||
|
||
<create_query>CREATE TABLE test_in (`a` UInt32) ENGINE = MergeTree() ORDER BY a</create_query> | ||
<fill_query>INSERT INTO test_in SELECT number FROM numbers(500000000)</fill_query> | ||
|
||
<stop_conditions> | ||
<all_of> | ||
<total_time_ms>8000</total_time_ms> | ||
</all_of> | ||
<any_of> | ||
<min_time_not_changing_for_ms>7000</min_time_not_changing_for_ms> | ||
<total_time_ms>20000</total_time_ms> | ||
</any_of> | ||
</stop_conditions> | ||
|
||
|
||
<query>SELECT count() FROM test_in WHERE a IN (SELECT rand(1) FROM numbers(100000)) SETTINGS max_rows_to_read = 1, read_overflow_mode = 'break'</query> | ||
|
||
<drop_query>DROP TABLE IF EXISTS test_in</drop_query> | ||
</test> |