File size: 8,705 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from pathlib import Path

import numpy as np
import pytest
from numpy.testing import assert_array_equal

import tiledb


def test_schema_evolution(tmp_path):
    ctx = tiledb.default_ctx()
    se = tiledb.ArraySchemaEvolution(ctx)

    uri = str(tmp_path)

    attrs = [
        tiledb.Attr(name="a1", dtype=np.float64),
        tiledb.Attr(name="a2", dtype=np.int32),
    ]
    dims = [tiledb.Dim(domain=(0, 3), dtype=np.uint64)]
    domain = tiledb.Domain(*dims)
    schema = tiledb.ArraySchema(domain=domain, attrs=attrs, sparse=False)
    tiledb.Array.create(uri, schema)

    data1 = {
        "a1": np.arange(5, 9),
        "a2": np.random.randint(0, 1e7, size=4).astype(np.int32),
    }

    with tiledb.open(uri, "w") as A:
        A[:] = data1

    with tiledb.open(uri) as A:
        res = A[:]
        assert_array_equal(res["a1"], data1["a1"])
        assert_array_equal(res["a2"], data1["a2"])
        assert "a3" not in res.keys()

    newattr = tiledb.Attr("a3", dtype=np.int8)
    se.add_attribute(newattr)

    with pytest.raises(tiledb.TileDBError) as excinfo:
        se.add_attribute(newattr)
    assert "Input attribute name is already there" in str(excinfo.value)
    assert str(Path("tiledb") / "schema_evolution.cc") in str(excinfo.value)

    se.array_evolve(uri)

    data2 = {
        "a1": np.arange(5, 9),
        "a2": np.random.randint(0, 1e7, size=4).astype(np.int32),
        "a3": np.random.randint(0, 255, size=4).astype(np.int8),
    }

    with tiledb.open(uri, "w") as A:
        A[:] = data2

    def test_it():
        with tiledb.open(uri) as A:
            res = A[:]
            assert_array_equal(res["a1"], data2["a1"])
            assert_array_equal(res["a2"], data2["a2"])
            assert_array_equal(res["a3"], data2["a3"])

    test_it()
    tiledb.consolidate(uri)
    test_it()

    se = tiledb.ArraySchemaEvolution(ctx)
    se.drop_attribute("a1")
    se.array_evolve(uri)

    data3 = {
        "a2": np.random.randint(0, 1e7, size=4).astype(np.int32),
        "a3": np.random.randint(0, 255, size=4).astype(np.int8),
    }

    def test_it2():
        with tiledb.open(uri) as A:
            res = A[:]
            assert "a1" not in res.keys()
            assert_array_equal(res["a2"], data3["a2"])
            assert_array_equal(res["a3"], data3["a3"])

    with tiledb.open(uri, "w") as A:
        A[:] = data3

    test_it2()
    tiledb.consolidate(uri)
    test_it2()


def test_schema_evolution_timestamp(tmp_path):
    ctx = tiledb.default_ctx()
    se = tiledb.ArraySchemaEvolution(ctx)
    vfs = tiledb.VFS()

    uri = str(tmp_path)
    schema_uri = os.path.join(uri, "__schema")

    attrs = [tiledb.Attr(name="a1", dtype=np.float64)]
    domain = tiledb.Domain(tiledb.Dim(domain=(0, 3), dtype=np.uint64))
    schema = tiledb.ArraySchema(domain=domain, attrs=attrs, sparse=False)
    tiledb.Array.create(uri, schema)

    def get_schema_timestamps(schema_uri):
        schema_files = filter(lambda x: "__enumerations" not in x, vfs.ls(schema_uri))
        return [int(os.path.basename(file).split("_")[2]) for file in schema_files]

    assert 123456789 not in get_schema_timestamps(schema_uri)

    newattr = tiledb.Attr("a2", dtype=np.int8)
    se.timestamp(123456789)
    se.add_attribute(newattr)
    se.array_evolve(uri)

    assert 123456789 in get_schema_timestamps(schema_uri)


def test_schema_evolution_with_enmr(tmp_path):
    ctx = tiledb.default_ctx()
    se = tiledb.ArraySchemaEvolution(ctx)

    uri = str(tmp_path)

    attrs = [
        tiledb.Attr(name="a1", dtype=np.float64),
        tiledb.Attr(name="a2", dtype=np.int32),
    ]
    dims = [tiledb.Dim(domain=(0, 3), dtype=np.uint64)]
    domain = tiledb.Domain(*dims)
    schema = tiledb.ArraySchema(domain=domain, attrs=attrs, sparse=False)
    tiledb.Array.create(uri, schema)

    data1 = {
        "a1": np.arange(5, 9),
        "a2": np.random.randint(0, 1e7, size=4).astype(np.int32),
    }

    with tiledb.open(uri, "w") as A:
        A[:] = data1

    with tiledb.open(uri) as A:
        assert not A.schema.has_attr("a3")

    newattr = tiledb.Attr("a3", dtype=np.int8, enum_label="e3")
    se.add_attribute(newattr)

    with pytest.raises(tiledb.TileDBError) as excinfo:
        se.array_evolve(uri)
    assert " Attribute refers to an unknown enumeration" in str(excinfo.value)

    se.add_enumeration(tiledb.Enumeration("e3", True, np.arange(0, 8)))
    se.array_evolve(uri)

    se = tiledb.ArraySchemaEvolution(ctx)

    with tiledb.open(uri) as A:
        assert A.schema.has_attr("a3")
        assert A.attr("a3").enum_label == "e3"

    se.drop_enumeration("e3")

    with pytest.raises(tiledb.TileDBError) as excinfo:
        se.array_evolve(uri)
    assert "Unable to drop enumeration" in str(excinfo.value)

    se.drop_attribute("a3")
    se.array_evolve(uri)

    with tiledb.open(uri) as A:
        assert not A.schema.has_attr("a3")


@pytest.mark.parametrize(
    "type,data",
    (
        ("int", [0]),
        ("bool", [True, False]),
        ("str", ["abc", "defghi", "jk"]),
        ("bytes", [b"abc", b"defghi", b"jk"]),
    ),
)
def test_schema_evolution_extend_enmr(tmp_path, type, data):
    uri = str(tmp_path)
    enmr = tiledb.Enumeration("e", True, dtype=type)
    attrs = [tiledb.Attr(name="a", dtype=int, enum_label="e")]
    domain = tiledb.Domain(tiledb.Dim(domain=(0, 3), dtype=np.uint64))
    schema = tiledb.ArraySchema(domain=domain, attrs=attrs, enums=[enmr])
    tiledb.Array.create(uri, schema)

    with tiledb.open(uri) as A:
        assert A.schema.has_attr("a")
        assert A.attr("a").enum_label == "e"
        assert A.enum("e") == enmr

    se = tiledb.ArraySchemaEvolution()
    updated_enmr = enmr.extend(data)
    se.extend_enumeration(updated_enmr)
    se.array_evolve(uri)

    with tiledb.open(uri) as A:
        assert A.schema.has_attr("a")
        assert A.attr("a").enum_label == "e"
        assert A.enum("e") == updated_enmr


def test_schema_evolution_extend_check_bad_type():
    enmr = tiledb.Enumeration("e", True, dtype=str)
    with pytest.raises(tiledb.TileDBError):
        enmr.extend([1, 2, 3])
    with pytest.raises(tiledb.TileDBError):
        enmr.extend([True, False])
    enmr.extend(["a", "b"])

    enmr = tiledb.Enumeration("e", True, dtype=int)
    with pytest.raises(tiledb.TileDBError):
        enmr.extend(["a", "b"])
    with pytest.raises(tiledb.TileDBError):
        enmr.extend([True, False])
    enmr.extend([1, 2, 3])

    enmr = tiledb.Enumeration("e", True, dtype=bool)
    with pytest.raises(tiledb.TileDBError):
        enmr.extend(["a", "b"])
    with pytest.raises(tiledb.TileDBError):
        enmr.extend([1, 2, 3])
    enmr.extend([True, False])


@pytest.mark.skipif(
    tiledb.libtiledb.version() < (2, 27),
    reason="Dropping a fixed-sized attribute and adding it back"
    "as a var-sized attribute is not supported in TileDB < 2.27",
)
def test_schema_evolution_drop_fixed_attribute_and_add_back_as_var_sized(tmp_path):
    ctx = tiledb.default_ctx()
    uri = str(tmp_path)
    attrs = [
        tiledb.Attr(name="a", dtype=np.int32),
        tiledb.Attr(name="b", dtype=np.int32),
    ]
    dims = [tiledb.Dim(domain=(1, 10), dtype=np.int32)]
    domain = tiledb.Domain(*dims)
    schema = tiledb.ArraySchema(domain=domain, attrs=attrs, sparse=False)
    tiledb.Array.create(uri, schema)

    original_data = np.arange(1, 11)
    with tiledb.open(uri, "w") as A:
        A[:] = {"a": original_data, "b": original_data}

    se = tiledb.ArraySchemaEvolution(ctx)
    se.drop_attribute("a")
    se.array_evolve(uri)

    # check schema after dropping attribute
    with tiledb.open(uri) as A:
        assert not A.schema.has_attr("a")
        assert A.schema.attr("b").dtype == np.int32

    se = tiledb.ArraySchemaEvolution(ctx)
    newattr = tiledb.Attr("a", dtype="S", var=True)
    se.add_attribute(newattr)
    se.array_evolve(uri)

    # check schema and data after adding attribute back as a var-sized attribute
    with tiledb.open(uri) as A:
        assert A.schema.has_attr("a")
        assert A.schema.attr("a").dtype == "S"
        assert A.schema.attr("b").dtype == np.int32
        # check that each value == b'\x80' (empty byte)
        assert_array_equal(A[:]["a"], np.array([b"\x80" for _ in range(10)]))

    # add new data to the array
    new_data = np.array(
        ["tiledb-string-n.{}".format(i) for i in range(1, 11)], dtype="S"
    )
    with tiledb.open(uri, "w") as A:
        A[:] = {"a": new_data, "b": original_data}

    # check data for both attributes
    with tiledb.open(uri) as A:
        res = A[:]
        assert_array_equal(res["a"], new_data)
        assert_array_equal(res["b"], original_data)