aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/base/ut/table_index_ut.cpp
blob: 932c43007c98a5370d8552a94969c0e7baf00d23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "table_index.h"
#include "table_vector_index.h"

#include <library/cpp/testing/unittest/registar.h>

namespace {

using namespace NKikimr::NTableIndex;

const TTableColumns Table{{"PK1", "PK2", "DATA1", "DATA2", "DATA3"}, {"PK2", "PK1"}};

Y_UNIT_TEST_SUITE (TableIndex) {
    Y_UNIT_TEST (CompatibleSecondaryIndex) {
        TString explain;
        auto type = NKikimrSchemeOp::EIndexType::EIndexTypeGlobal;

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"DATA1"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"DATA1", "DATA2"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"PK1", "PK2"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"DATA1"}, {"DATA3"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");

        {
            const TTableColumns Table2{{"PK", "DATA", NTableVectorKmeansTreeIndex::ParentColumn}, {"PK"}};

            UNIT_ASSERT(IsCompatibleIndex(type, Table2, {{NTableVectorKmeansTreeIndex::ParentColumn}, {}}, explain));
            UNIT_ASSERT_STRINGS_EQUAL(explain, "");

            UNIT_ASSERT(IsCompatibleIndex(type, Table2, {{"DATA"}, {NTableVectorKmeansTreeIndex::ParentColumn}}, explain));
            UNIT_ASSERT_STRINGS_EQUAL(explain, "");
        }
        {
            const TTableColumns Table3{{"PK", "DATA", NTableVectorKmeansTreeIndex::ParentColumn}, {NTableVectorKmeansTreeIndex::ParentColumn}};

            UNIT_ASSERT(IsCompatibleIndex(type, Table3, {{"DATA"}, {}}, explain));
            UNIT_ASSERT_STRINGS_EQUAL(explain, "");
        }

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"PK2"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");
    }

    Y_UNIT_TEST (NotCompatibleSecondaryIndex) {
        TString explain;
        auto type = NKikimrSchemeOp::EIndexType::EIndexTypeGlobal;

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"NOT EXIST"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index key columns should be in table columns, index key column NOT EXIST is missed");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{}, {"NOT EXIST"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index data columns should be in table columns, index data column NOT EXIST is missed");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"NOT EXIST"}, {"NOT EXIST"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index key columns should be in table columns, index key column NOT EXIST is missed");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"DATA1", "DATA1"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index key columns should be unique, for example DATA1");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"DATA1"}, {"PK2"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "the same column can't be used as key and data column for one index, for example PK2");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"DATA1"}, {"DATA1"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "the same column can't be used as key and data column for one index, for example DATA1");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"DATA1"}, {"DATA3", "DATA3"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index data columns should be unique, for example DATA3");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "should be at least single index key column");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"PK2", "PK1"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "index keys shouldn't be table keys");
    }

    Y_UNIT_TEST (CompatibleVectorIndex) {
        TString explain;
        auto type = NKikimrSchemeOp::EIndexType::EIndexTypeGlobalVectorKmeansTree;

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"DATA1"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"DATA1"}, {"DATA3"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"PK1"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");

        UNIT_ASSERT(IsCompatibleIndex(type, Table, {{"DATA1"}, {"DATA1"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "");
    }

    Y_UNIT_TEST (NotCompatibleVectorIndex) {
        TString explain;
        auto type = NKikimrSchemeOp::EIndexType::EIndexTypeGlobalVectorKmeansTree;

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"NOT EXIST"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index key columns should be in table columns, index key column NOT EXIST is missed");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{}, {"NOT EXIST"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index data columns should be in table columns, index data column NOT EXIST is missed");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"NOT EXIST"}, {"NOT EXIST"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index key columns should be in table columns, index key column NOT EXIST is missed");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"DATA1", "DATA1"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "all index key columns should be unique, for example DATA1");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"DATA1", "DATA2"}, {}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "only single key column is supported for vector index");

        UNIT_ASSERT(!IsCompatibleIndex(type, Table, {{"DATA1"}, {"PK2"}}, explain));
        UNIT_ASSERT_STRINGS_EQUAL(explain, "the same column can't be used as key and data column for one index, for example PK2");

        {
            const TTableColumns Table2{{"PK", "DATA", NTableVectorKmeansTreeIndex::ParentColumn}, {"PK"}};

            UNIT_ASSERT(!IsCompatibleIndex(type, Table2, {{NTableVectorKmeansTreeIndex::ParentColumn}, {}}, explain));
            UNIT_ASSERT_STRINGS_EQUAL(explain, TStringBuilder() << "index key column shouldn't have a reserved name: " << NTableVectorKmeansTreeIndex::ParentColumn);

            UNIT_ASSERT(!IsCompatibleIndex(type, Table2, {{"DATA"}, {NTableVectorKmeansTreeIndex::ParentColumn}}, explain));
            UNIT_ASSERT_STRINGS_EQUAL(explain, TStringBuilder() << "index data column shouldn't have a reserved name: " << NTableVectorKmeansTreeIndex::ParentColumn);
        }
        {
            const TTableColumns Table3{{"PK", "DATA", NTableVectorKmeansTreeIndex::ParentColumn}, {NTableVectorKmeansTreeIndex::ParentColumn}};

            UNIT_ASSERT(!IsCompatibleIndex(type, Table3, {{"DATA"}, {}}, explain));
            UNIT_ASSERT_STRINGS_EQUAL(explain, TStringBuilder() << "table key column shouldn't have a reserved name: " << NTableVectorKmeansTreeIndex::ParentColumn);
        }
    }
}

}