summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/suite/test_join02.py
blob: a691c499cf6f2a66de32dc97dbd96fc2fe6db1d8 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
#
# Public Domain 2014-2016 MongoDB, Inc.
# Public Domain 2008-2014 WiredTiger, Inc.
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import wiredtiger, wttest, suite_random
from wtscenario import check_scenarios, multiply_scenarios, number_scenarios

# test_join02.py
#    Join operations
# Join several indices together, trying all comparison combinations
class test_join02(wttest.WiredTigerTestCase):
    table_name1 = 'test_join02'
    nentries = 1000

    keyscen = [
        ('key-r', dict(keyformat='r')),
        ('key-S', dict(keyformat='S')),
        ('key-i', dict(keyformat='i')),
        ('key-iS', dict(keyformat='iS'))
    ]

    bloomscen = [
        ('bloom', dict(usebloom=True)),
        ('nobloom', dict(usebloom=False))
    ]

    scenarios = number_scenarios(multiply_scenarios('.', keyscen, bloomscen))

    # Start our range from 1, since WT record numbers start at 1,
    # it makes things work out nicer.
    def range(self):
        return range(1, self.nentries + 1)

    def gen_key(self, i):
        if self.keyformat == 'S':
            return [ 'key%06d' % i ]  # zero pad so it sorts expectedly
        elif self.keyformat == 'iS':
            return [ i, 'key%06d' % i ]
        else:
            return [ i ]

    def gen_values(self, i):
        s = str(i)
        x = 'x' * i
        rs = s[::-1]
        f = int(s[0:1])
        return [i, s, x, rs, f]

    def reinit_joinconfig(self):
        self.rand = suite_random.suite_random(self.seed)
        self.seed += 1

    def get_joinconfig(self):
        # When we're running the bloom scenario, make it so the
        # bloom filters are often shared. Make the number of
        # hashes and number of bits per item so they don't always
        # match up; WT should allow it.
        if self.usebloom:
            c = 10000 if (self.rand.rand32() % 3) != 0 else 100000
            k = 8 if (self.rand.rand32() % 10) != 0 else 10
            b = 16 if (self.rand.rand32() % 11) != 0 else 12
            return \
                ',strategy=bloom,count=' + str(c) + \
                ',bloom_bit_count=' + str(b) + \
                ',bloom_hash_count=' + str(k)
        else:
            return ''

    def do_join(self, jc, curleft, curright, choice, mbr):
        c0 = choice[0]
        if c0 == None:
            return mbr
        # The first join cannot use a bloom filter
        if jc.first_join:
            joinconfig = ''
            jc.first_join = False
        else:
            joinconfig = self.get_joinconfig()
        if c0 != None:
            #self.tty('join(jc, ' + curleft.name + ' ' + c0 +
            #         ' ' + str(curleft.low) + ')')
            curleft.reset()
            curleft.set_key(*curleft.low)
            self.assertEquals(0, curleft.search())
            self.session.join(jc, curleft, 'compare=' + c0 + joinconfig)
            if c0 == 'eq':
                mbr = mbr.intersection(curleft.eqmembers)
            elif c0 == 'ge':
                mbr = mbr.intersection(
                    set(curleft.eqmembers.union(curleft.gtmembers)))
            elif c0 == 'gt':
                mbr = mbr.intersection(curleft.gtmembers)
        c1 = choice[1] if len(choice) > 1 else None
        if c1 != None:
            #self.tty('join(jc, ' + curright.name + ' ' + c1 +
            #         ' ' + str(curright.high) + ')')
            curright.reset()
            curright.set_key(*curright.high)
            self.assertEquals(0, curright.search())
            self.session.join(jc, curright, 'compare=' + c1 + joinconfig)
            if c1 == 'le':
                mbr = mbr.intersection(
                    set(curright.eqmembers.union(curright.ltmembers)))
            elif c1 == 'lt':
                mbr = mbr.intersection(curright.ltmembers)
        return mbr

    def iterate(self, jc, mbr):
        #self.tty('iteration expects ' + str(len(mbr)) +
        #         ' entries: ' + str(mbr))
        while jc.next() == 0:
            keys = jc.get_keys()
            [v0,v1,v2,v3,v4] = jc.get_values()
            k0 = keys[0]
            k1 = keys[1] if len(keys) > 1 else None
            if self.keyformat == 'S':
                i = int(str(k0[3:]))
            elif self.keyformat == 'iS':
                i = k0
                self.assertEquals(i, int(str(k1[3:])))
            else:
                i = k0
            #self.tty('  iteration got key: ' + str(k0) + ',' + str(k1))
            #self.tty('  iteration got values: ' + str([v0,v1,v2,v3,v4]))
            #self.tty('  iteration expects values: ' + str(self.gen_values(i)))
            self.assertEquals(self.gen_values(i), [v0,v1,v2,v3,v4])
            if not i in mbr:
                self.tty('  result ' + str(i) + ' is not in: ' + str(mbr))
            self.assertTrue(i in mbr)
            mbr.remove(i)
        self.assertEquals(0, len(mbr))

    def mkmbr(self, expr):
        return frozenset([x for x in self.range() if expr(x)])

    def test_basic_join(self):
        self.seed = 1
        if self.keyformat == 'iS':
            keycols = 'k0,k1'
        else:
            keycols = 'k'
        self.session.create('table:join02', 'key_format=' + self.keyformat +
                            ',value_format=iSuSi,columns=(' + keycols +
                            ',v0,v1,v2,v3,v4)')
        self.session.create('index:join02:index0','columns=(v0)')
        self.session.create('index:join02:index1','columns=(v1)')
        self.session.create('index:join02:index2','columns=(v2)')
        self.session.create('index:join02:index3','columns=(v3)')
        self.session.create('index:join02:index4','columns=(v4)')
        c = self.session.open_cursor('table:join02', None, None)
        for i in self.range():
            c.set_key(*self.gen_key(i))
            c.set_value(*self.gen_values(i))
            c.insert()
        c.close()

        # Use the primary table in one of the joins.
        # Use various projections, which should not matter for ref cursors
        c0a = self.session.open_cursor('table:join02', None, None)
        c0b = self.session.open_cursor('table:join02(v4)', None, None)
        c1a = self.session.open_cursor('index:join02:index1(v0)', None, None)
        c1b = self.session.open_cursor('index:join02:index1', None, None)
        c2a = self.session.open_cursor('index:join02:index2', None, None)
        c2b = self.session.open_cursor('index:join02:index2', None, None)
        c3a = self.session.open_cursor('index:join02:index3(v4)', None, None)
        c3b = self.session.open_cursor('index:join02:index3(v0)', None, None)
        c4a = self.session.open_cursor('index:join02:index4(v1)', None, None)

        # Attach extra properties to each cursor.  For cursors that
        # may appear on the 'left' side of a range CA < x < CB,
        # we give a low value of the range, and calculate the members
        # of the set we expect to see for a 'gt' comparison, as well
        # as the 'eq' comparison.  For cursors that appear on the
        # 'right side of the range, we give a high value of the range,
        # and calculate membership sets for 'lt' and 'eq'.
        #
        # We've defined the low/high values so that there's a lot of
        # overlap between the values when we're doing ranges.
        c0a.name = 'c0a'
        c0b.name = 'c0b'
        if self.keyformat == 'i' or self.keyformat == 'r':
            c0a.low = [ 205 ]
            c0b.high = [ 990 ]
        elif self.keyformat == 'S':
            c0a.low = [ 'key000205' ]
            c0b.high = [ 'key000990' ]
        elif self.keyformat == 'iS':
            c0a.low = [ 205, 'key000205' ]
            c0b.high = [ 990, 'key000990' ]
        c0a.gtmembers = self.mkmbr(lambda x: x > 205)
        c0a.eqmembers = self.mkmbr(lambda x: x == 205)
        c0b.ltmembers = self.mkmbr(lambda x: x < 990)
        c0b.eqmembers = self.mkmbr(lambda x: x == 990)

        c1a.low = [ '150' ]
        c1a.gtmembers = self.mkmbr(lambda x: str(x) > '150')
        c1a.eqmembers = self.mkmbr(lambda x: str(x) == '150')
        c1a.name = 'c1a'
        c1b.high = [ '733' ]
        c1b.ltmembers = self.mkmbr(lambda x: str(x) < '733')
        c1b.eqmembers = self.mkmbr(lambda x: str(x) == '733')
        c1b.name = 'c1b'

        c2a.low = [ 'x' * 321 ]
        c2a.gtmembers = self.mkmbr(lambda x: x > 321)
        c2a.eqmembers = self.mkmbr(lambda x: x == 321)
        c2a.name = 'c2a'
        c2b.high = [ 'x' * 765 ]
        c2b.ltmembers = self.mkmbr(lambda x: x < 765)
        c2b.eqmembers = self.mkmbr(lambda x: x == 765)
        c2b.name = 'c2b'

        c3a.low = [ '432' ]
        c3a.gtmembers = self.mkmbr(lambda x: str(x)[::-1] > '432')
        c3a.eqmembers = self.mkmbr(lambda x: str(x)[::-1] == '432')
        c3a.name = 'c3a'
        c3b.high = [ '876' ]
        c3b.ltmembers = self.mkmbr(lambda x: str(x)[::-1] < '876')
        c3b.eqmembers = self.mkmbr(lambda x: str(x)[::-1] == '876')
        c3b.name = 'c3b'

        c4a.low = [ 4 ]
        c4a.gtmembers = self.mkmbr(lambda x: str(x)[0:1] > '4')
        c4a.eqmembers = self.mkmbr(lambda x: str(x)[0:1] == '4')
        c4a.name = 'c4a'

        choices = [[None], ['eq'], ['ge'], ['gt'], [None, 'le'], [None, 'lt'],
                   ['ge', 'le' ], ['ge', 'lt' ], ['gt', 'le' ], ['gt', 'lt' ]]
        smallchoices = [[None], ['eq'], ['ge'], ['gt', 'le' ]]
        for i0 in smallchoices:
            for i1 in choices:
                for i2 in smallchoices:
                    for i3 in smallchoices:
                        for i4 in [[None], ['eq'], ['ge'], ['gt']]:
                            if i0[0] == None and i1[0] == None and \
                               i2[0] == None and i3[0] == None and \
                               i4[0] == None:
                                continue
                            self.reinit_joinconfig()
                            #self.tty('Begin test: ' +
                            #         ','.join([str(i0),str(i1),str(i2),
                            #                   str(i3),str(i4)]))
                            jc = self.session.open_cursor('join:table:join02',
                                                          None, None)
                            jc.first_join = True
                            mbr = set(self.range())

                            # It shouldn't matter the order of the joins
                            mbr = self.do_join(jc, c3a, c3b, i3, mbr)
                            mbr = self.do_join(jc, c2a, c2b, i2, mbr)
                            mbr = self.do_join(jc, c4a, None, i4, mbr)
                            mbr = self.do_join(jc, c1a, c1b, i1, mbr)
                            mbr = self.do_join(jc, c0a, c0b, i0, mbr)
                            self.iterate(jc, mbr)
                            jc.close()
        c0a.close()
        c0b.close()
        c1a.close()
        c1b.close()
        c2a.close()
        c2b.close()
        c3a.close()
        c3b.close()
        c4a.close()

if __name__ == '__main__':
    wttest.run()