File size: 46,356 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 | import string
import numpy as np
import pytest
from numpy.testing import assert_array_equal
import tiledb
from .common import DiskTestCase, has_pandas, rand_utf8
class QueryConditionTest(DiskTestCase):
def filter_dense(self, data, mask):
if isinstance(mask, np.ndarray):
mask = mask[0]
if isinstance(mask, float):
return data[np.invert(np.isnan(data))]
if isinstance(mask, np.timedelta64):
return data[np.invert(np.isnat(data))]
if isinstance(mask, (str, bytes)):
return data[np.invert(data == mask)]
return data[data != mask]
def create_input_array_UIDSA(self, sparse):
path = self.path("input_array_UIDSA")
dom = tiledb.Domain(
tiledb.Dim(name="d", domain=(1, 10), tile=1, dtype=np.uint32)
)
attrs = [
tiledb.Attr(name="U", dtype=np.uint32),
tiledb.Attr(name="I", dtype=np.int64),
tiledb.Attr(name="D", dtype=np.float64),
tiledb.Attr(name="S", dtype=np.dtype("|S1"), var=False),
tiledb.Attr(name="A", dtype="ascii", var=True),
tiledb.Attr(name="UTF", dtype=np.dtype("U"), var=True),
]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=sparse)
tiledb.Array.create(path, schema)
with tiledb.open(path, "w") as arr:
data = {
"U": np.random.randint(1, 10, 10),
"I": np.random.randint(-5, 5, 10),
"D": np.random.rand(10),
"S": np.array(list(string.ascii_lowercase[:10]), dtype="|S1"),
"A": np.array(
list(string.ascii_lowercase[i] * (i + 1) for i in range(10)),
dtype="|S",
),
"UTF": np.array(
["$", "£$", "€ह£$", "한ह£", "£$𐍈"]
+ [rand_utf8(np.random.randint(1, 100)) for _ in range(5)],
dtype="|U0",
),
}
if sparse:
arr[np.arange(1, 11)] = data
else:
arr[:] = data
return path
def setUp(self):
super().setUp()
if not tiledb.libtiledb.version() >= (2, 2, 3):
pytest.skip("Only run QueryCondition test with TileDB>=2.2.3")
@pytest.mark.parametrize("sparse", [True, False])
def test_errors(self, sparse):
uri = self.create_input_array_UIDSA(sparse)
with self.assertRaises(tiledb.TileDBError):
with tiledb.open(uri) as A:
A.query(cond="1.324 < 1")[:]
with self.assertRaises(tiledb.TileDBError):
with tiledb.open(uri) as A:
A.query(cond="foo >= bar")[:]
with self.assertRaises(tiledb.TileDBError):
with tiledb.open(uri) as A:
A.query(cond="'foo' == 'bar'")[:]
with self.assertRaises(tiledb.TileDBError):
with tiledb.open(uri) as A:
A.query(cond="U < 10000000000000000000000.0", attrs=["U"])[:]
with self.assertRaises(tiledb.TileDBError):
with tiledb.open(uri) as A:
A.query(cond="D", attrs=["D"])[:]
with self.assertRaises(tiledb.TileDBError):
with tiledb.open(uri) as A:
A.query(cond="D,", attrs=["D"])[:]
with self.assertRaises(tiledb.TileDBError):
with tiledb.open(uri) as A:
A.query(cond="D > ", attrs=["D"])[:]
def test_qc_dense(self):
path = self.path("test_qc_dense")
dom = tiledb.Domain(
tiledb.Dim(name="d", domain=(1, 10), tile=1, dtype=np.uint8)
)
attrs = [tiledb.Attr(name="a", dtype=np.uint8)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=False)
tiledb.Array.create(path, schema)
with tiledb.open(path) as A:
A.query(cond="a < 5")
def test_unsigned_sparse(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
A.query(cond="U < 5", attrs=["U"])[:]
result = A.query(cond="U < 5", attrs=["U"])[:]
assert all(result["U"] < 5)
def test_unsigned_dense(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=False)) as A:
mask = A.attr("U").fill
A.query(cond="U < 5", attrs=["U"])[:]
result = A.query(cond="U < 5", attrs=["U"])[:]
assert all(self.filter_dense(result["U"], mask) < 5)
def test_signed_sparse(self):
uri = self.create_input_array_UIDSA(sparse=True)
with tiledb.open(uri) as A:
result = A.query(cond="I < 1", attrs=["I"])[:]
assert all(result["I"] < 1)
result = A.query(cond="I < +1", attrs=["I"])[:]
assert all(result["I"] < +1)
result = A.query(cond="I < ---1", attrs=["I"])[:]
assert all(result["I"] < ---1)
result = A.query(cond="-5 < I < 5", attrs=["I"])[:]
assert all(-5 < result["I"])
assert all(result["I"] < 5)
def test_signed_dense(self):
uri = self.create_input_array_UIDSA(sparse=False)
with tiledb.open(uri) as A:
mask = A.attr("I").fill
result = A.query(cond="I < 1", attrs=["I"])[:]
assert all(self.filter_dense(result["I"], mask) < 1)
result = A.query(cond="I < +1", attrs=["I"])[:]
assert all(self.filter_dense(result["I"], mask) < +1)
result = A.query(cond="I < ---1", attrs=["I"])[:]
assert all(self.filter_dense(result["I"], mask) < ---1)
result = A.query(cond="-5 < I < 5", attrs=["I"])[:]
assert all(-5 < self.filter_dense(result["I"], mask))
assert all(self.filter_dense(result["I"], mask) < 5)
def test_floats_sparse(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
result = A.query(cond="D > 5.0", attrs=["D"])[:]
assert all(result["D"] > 5.0)
result = A.query(cond="(D > 0.7) & (D < 3.5)", attrs=["D"])[:]
assert all((result["D"] > 0.7) & (result["D"] < 3.5))
result = A.query(cond="0.2 < D < 0.75", attrs=["D", "D"])[:]
assert all(0.2 < result["D"])
assert all(result["D"] < 0.75)
def test_floats_dense(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=False)) as A:
mask = A.attr("D").fill
result = A.query(cond="D > 5.0", attrs=["D"])[:]
assert all(self.filter_dense(result["D"], mask) > 5.0)
result = A.query(cond="(D > 0.7) & (D < 3.5)", attrs=["D"])[:]
assert all(self.filter_dense(result["D"], mask) > 0.7)
assert all(self.filter_dense(result["D"], mask) < 3.5)
result = A.query(cond="0.2 < D < 0.75", attrs=["D", "D"])[:]
assert all(0.2 < self.filter_dense(result["D"], mask))
assert all(self.filter_dense(result["D"], mask) < 0.75)
def test_string_sparse(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
with self.assertRaises(tiledb.TileDBError) as exc_info:
A.query(cond="S == c", attrs=["S"])[:]
assert (
"right-hand sides must be constant expressions, not variables -- did you mean to quote the right-hand side as a string?"
in str(exc_info.value)
)
result = A.query(cond="S == 'c'", attrs=["S"])[:]
assert len(result["S"]) == 1
assert result["S"][0] == b"c"
result = A.query(cond="A == 'a'", attrs=["A"])[:]
assert len(result["A"]) == 1
assert result["A"][0] == b"a"
if tiledb.libtiledb.version() > (2, 14):
for t in A.query(attrs=["UTF"])[:]["UTF"]:
cond = f"""UTF == '{t}'"""
result = A.query(cond=cond, attrs=["UTF"])[:]
assert result["UTF"] == t
def test_string_dense(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=False)) as A:
with self.assertRaises(tiledb.TileDBError) as exc_info:
A.query(cond="S == ccc", attrs=["S"])[:]
assert (
"right-hand sides must be constant expressions, not variables -- did you mean to quote the right-hand side as a string?"
in str(exc_info.value)
)
result = A.query(cond="S == 'ccc'", attrs=["S"])[:]
assert all(self.filter_dense(result["S"], A.attr("S").fill) == b"c")
result = A.query(cond="A == 'ccc'", attrs=["A"])[:]
assert all(self.filter_dense(result["A"], A.attr("A").fill) == b"ccc")
if tiledb.libtiledb.version() > (2, 14):
for t in A.query(attrs=["UTF"])[:]["UTF"]:
cond = f"""UTF == '{t}'"""
result = A.query(cond=cond, attrs=["UTF"])[:]
assert all(
self.filter_dense(result["UTF"], A.attr("UTF").fill) == t
)
def test_combined_types_sparse(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
qc = "(I > 0) & ((-3 < D) & (D < 3.0))"
result = A.query(cond=qc, attrs=["I", "D"])[:]
assert all((result["I"] > 0) & ((-3 < result["D"]) & (result["D"] < 3.0)))
qc = "U >= 3 and 0.7 < D"
result = A.query(cond=qc, attrs=["U", "D"])[:]
assert all(result["U"] >= 3) & all(0.7 < result["D"])
qc = "(0.2 < D and D < 0.75) and (-5 < I < 5)"
result = A.query(cond=qc, attrs=["D", "I"])[:]
assert all((0.2 < result["D"]) & (result["D"] < 0.75))
assert all((-5 < result["I"]) & (result["I"] < 5))
qc = "(-5 < I <= -1) and (0.2 < D < 0.75)"
result = A.query(cond=qc, attrs=["D", "I"])[:]
assert all((0.2 < result["D"]) & (result["D"] < 0.75))
assert all((-5 < result["I"]) & (result["I"] <= -1))
qc = "(0.2 < D < 0.75) and (-5 < I < 5)"
result = A.query(cond=qc, attrs=["D", "I"])[:]
assert all((0.2 < result["D"]) & (result["D"] < 0.75))
assert all((-5 < result["I"]) & (result["I"] < 5))
def test_combined_types_dense(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=False)) as A:
mask_U = A.attr("U").fill
mask_I = A.attr("I").fill
mask_D = A.attr("D").fill
qc = "(I > 0) & ((-3 < D) & (D < 3.0))"
result = A.query(cond=qc, attrs=["I", "D"])[:]
res_I = self.filter_dense(result["I"], mask_I)
res_D = self.filter_dense(result["D"], mask_D)
assert all(res_I > 0) & all(-3 < res_D) & all(res_D < 3.0)
qc = "U >= 3 and 0.7 < D"
result = A.query(cond=qc, attrs=["U", "D"])[:]
res_U = self.filter_dense(result["U"], mask_U)
res_D = self.filter_dense(result["D"], mask_D)
assert all(res_U >= 3) & all(0.7 < res_D)
qc = "(0.2 < D and D < 0.75) and (-5 < I < 5)"
result = A.query(cond=qc, attrs=["D", "I"])[:]
res_D = self.filter_dense(result["D"], mask_D)
res_I = self.filter_dense(result["I"], mask_I)
assert all((0.2 < res_D) & (res_D < 0.75))
assert all((-5 < res_I) & (res_I < 5))
qc = "(-5 < I <= -1) and (0.2 < D < 0.75)"
result = A.query(cond=qc, attrs=["D", "I"])[:]
res_D = self.filter_dense(result["D"], mask_D)
res_I = self.filter_dense(result["I"], mask_I)
assert all((0.2 < res_D) & (res_D < 0.75))
assert all((-5 < res_I) & (res_I <= -1))
qc = "(0.2 < D < 0.75) and (-5 < I < 5)"
result = A.query(cond=qc, attrs=["D", "I"])[:]
res_D = self.filter_dense(result["D"], mask_D)
res_I = self.filter_dense(result["I"], mask_I)
assert all((0.2 < res_D) & (res_D < 0.75))
assert all((-5 < res_I) & (res_I < 5))
def test_check_attrs_sparse(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
result = A.query(cond="U < 0.1", attrs=["U"])[:]
assert all(result["U"] < 0.1)
result = A.query(cond="U < 1.0", attrs=["U"])[:]
assert all(result["U"] < 1.0)
with self.assertRaises(tiledb.TileDBError):
A.query(cond="U < '1'", attrs=["U"])[:]
with self.assertRaises(tiledb.TileDBError):
A.query(cond="U < 'one'", attrs=["U"])[:]
def test_check_attrs_dense(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=False)) as A:
mask = A.attr("U").fill
result = A.query(cond="U < 0.1", attrs=["U"])[:]
assert all(self.filter_dense(result["U"], mask) < 0.1)
result = A.query(cond="U < 1.0", attrs=["U"])[:]
assert all(self.filter_dense(result["U"], mask) < 1.0)
with self.assertRaises(tiledb.TileDBError):
A.query(cond="U < '1'", attrs=["U"])[:]
with self.assertRaises(tiledb.TileDBError):
A.query(cond="U < 'one'", attrs=["U"])[:]
def test_attr_and_val_casting_num(self):
path = self.path("test_attr_and_val_casting_num")
dom = tiledb.Domain(
tiledb.Dim(name="dim", domain=(1, 10), tile=1, dtype=np.uint32)
)
attrs = [
tiledb.Attr(name="64-bit integer", dtype=np.int64),
tiledb.Attr(name="double", dtype=np.float64),
]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
with tiledb.open(path, "w") as arr:
arr[np.arange(1, 11)] = {
"64-bit integer": np.random.randint(-5, 5, 10),
"double": np.random.rand(10),
}
with tiledb.open(path) as arr:
result = arr.query(cond="attr('64-bit integer') <= val(0)")[:]
assert all(result["64-bit integer"] <= 0)
result = arr.query(cond="attr('64-bit integer') <= 0")[:]
assert all(result["64-bit integer"] <= 0)
result = arr.query(cond="double <= 0.5")[:]
assert all(result["double"] <= 0.5)
result = arr.query(cond="attr('double') <= 0.5")[:]
assert all(result["double"] <= 0.5)
result = arr.query(cond="double <= val(0.5)")[:]
assert all(result["double"] <= 0.5)
result = arr.query(cond="attr('double') <= val(0.5)")[:]
assert all(result["double"] <= 0.5)
def test_casting_str(self):
path = self.path("test_attr_and_val_casting_str")
dom = tiledb.Domain(tiledb.Dim(name="dim with spaces", dtype="ascii"))
attrs = [tiledb.Attr(name="attr with spaces", dtype="ascii", var=True)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
A = np.array(
[
"value with spaces",
"nospaces",
"value with spaces",
"another value",
"",
]
)
with tiledb.open(path, "w") as arr:
arr[["a", "b", "c", "d", "e"]] = {"attr with spaces": A}
with tiledb.open(path) as arr:
qc = "attr('attr with spaces') == 'value with spaces'"
result = arr.query(cond=qc)[:]
assert list(result["dim with spaces"]) == [b"a", b"c"]
with pytest.raises(tiledb.TileDBError) as exc_info:
result = arr.query(cond="dim('attr with spaces') == 'd'")[:]
assert "is not a dimension" in str(exc_info.value)
qc = "attr('attr with spaces') == val('value with spaces')"
result = arr.query(cond=qc)[:]
assert list(result["dim with spaces"]) == [b"a", b"c"]
with pytest.raises(tiledb.TileDBError) as exc_info:
result = arr.query(cond="attr('dim with spaces') == 'd'")[:]
assert "is not an attribute" in str(exc_info.value)
result = arr.query(cond="dim('dim with spaces') == 'd'")[:]
assert list(result["dim with spaces"]) == [b"d"]
@pytest.mark.skipif(
tiledb.libtiledb.version() < (2, 7, 0),
reason="var-length np.bytes_ query condition support introduced in 2.7.0",
)
def test_var_length_str(self):
path = self.path("test_var_length_str")
dom = tiledb.Domain(tiledb.Dim(name="d", domain=(0, 4)))
attrs = [
tiledb.Attr(name="ascii", dtype="ascii", var=True),
tiledb.Attr(name="bytes", dtype=np.bytes_, var=True),
]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
def create_array(func):
return np.array([func[i - 1] * i for i in range(1, 6)], dtype=np.bytes_)
ascii_data = create_array(string.ascii_lowercase)
bytes_data = create_array(string.ascii_uppercase)
with tiledb.open(path, "w") as arr:
arr[np.arange(5)] = {"ascii": ascii_data, "bytes": bytes_data}
with tiledb.open(path, "r") as arr:
for s in ascii_data:
result = arr.query(cond=f"ascii == '{s.decode()}'")[:]
assert result["ascii"][0] == s
for s in bytes_data:
result = arr.query(cond=f"bytes == '{s.decode()}'")[:]
assert result["bytes"][0] == s
@pytest.mark.skipif(
tiledb.libtiledb.version() < (2, 10, 0),
reason="OR query condition operator introduced in libtiledb 2.10",
)
def test_or_sparse(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
result = A.query(cond="(D < 0.25) | (D > 0.75)", attrs=["D"])[:]
assert all((result["D"] < 0.25) | (result["D"] > 0.75))
result = A.query(cond="(D < 0.25) or (D > 0.75)", attrs=["D"])[:]
assert all((result["D"] < 0.25) | (result["D"] > 0.75))
@pytest.mark.skipif(
tiledb.libtiledb.version() < (2, 10, 0),
reason="OR query condition operator introduced in libtiledb 2.10",
)
def test_or_dense(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=False)) as A:
mask = A.attr("D").fill
result = A.query(cond="(D < 0.25) | (D > 0.75)", attrs=["D"])[:]
res = self.filter_dense(result["D"], mask)
assert all((res < 0.25) | (res > 0.75))
result = A.query(cond="(D < 0.25) or (D > 0.75)", attrs=["D"])[:]
res = self.filter_dense(result["D"], mask)
assert all((res < 0.25) | (res > 0.75))
@pytest.mark.skipif(
tiledb.libtiledb.version() < (2, 10, 0),
reason="OR query condition operator and bool type introduced in libtiledb 2.10",
)
def test_01(self):
path = self.path("test_01")
dom = tiledb.Domain(tiledb.Dim(domain=(1, 10), tile=1, dtype=np.uint32))
attrs = [
tiledb.Attr(name="a", dtype=np.uint8),
tiledb.Attr(name="b", dtype=np.uint8),
tiledb.Attr(name="c", dtype=np.uint8),
]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
with tiledb.open(path, "w") as arr:
arr[np.arange(1, 11)] = {
"a": np.random.randint(0, high=2, size=10),
"b": np.random.randint(0, high=2, size=10),
"c": np.random.randint(0, high=2, size=10),
}
with tiledb.open(path) as A:
result = A.query(cond="a == 1 and b == 1 and c == 1")[:]
assert all(result["a"] & result["b"] & result["c"])
result = A.query(cond="a == 1 and b == 1 or c == 1")[:]
assert all(result["a"] & result["b"] | result["c"])
result = A.query(cond="a == 1 or b == 1 and c == 1")[:]
assert all(result["a"] | result["b"] & result["c"])
result = A.query(cond="a == 1 or b == 1 or c == 1")[:]
assert all(result["a"] | result["b"] | result["c"])
result = A.query(cond="(a == 1 and b == 1) or c == 1")[:]
assert all(result["a"] & result["b"] | result["c"])
result = A.query(cond="a == 1 and (b == 1 or c == 1)")[:]
assert all(result["a"] & (result["b"] | result["c"]))
result = A.query(cond="(a == 1 or b == 1) and c == 1")[:]
assert all((result["a"] | result["b"]) & result["c"])
result = A.query(cond="a == 1 or (b == 1 and c == 1)")[:]
assert all(result["a"] | result["b"] & result["c"])
def test_in_operator_sparse(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
result = A.query(cond="U in [1, 2, 3]", attrs=["U"])[:]
for val in result["U"]:
assert val in [1, 2, 3]
result = A.query(cond="S in ['a', 'e', 'i', 'o', 'u']", attrs=["S"])[:]
for val in result["S"]:
assert val in [b"a", b"e", b"i", b"o", b"u"]
qc = "S in ['a', 'e', 'i', 'o', 'u'] and U in [5, 6, 7]"
result = A.query(cond=qc)[:]
for val in result["U"]:
assert val in [5, 6, 7]
for val in result["S"]:
assert val in [b"a", b"e", b"i", b"o", b"u"]
result = A.query(cond="U in [8]")[:]
for val in result["U"]:
assert val == 8
result = A.query(cond="S in ['8']")[:]
assert len(result["S"]) == 0
result = A.query(cond="U not in [5, 6, 7]")[:]
for val in result["U"]:
assert val not in [5, 6, 7]
with pytest.raises(tiledb.TileDBError) as exc_info:
A.query(cond="U not in []")[:]
assert "At least one value must be provided to the set membership" in str(
exc_info.value
)
def test_in_operator_dense(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=False)) as A:
U_mask = A.attr("U").fill
S_mask = A.attr("S").fill
result = A.query(cond="U in [1, 2, 3]", attrs=["U"])[:]
for val in self.filter_dense(result["U"], U_mask):
assert val in [1, 2, 3]
result = A.query(cond="S in ['a', 'e', 'i', 'o', 'u']", attrs=["S"])[:]
for val in self.filter_dense(result["S"], S_mask):
assert val in [b"a", b"e", b"i", b"o", b"u"]
qc = "S in ['a', 'e', 'i', 'o', 'u'] and U in [5, 6, 7]"
result = A.query(cond=qc)[:]
for val in self.filter_dense(result["U"], U_mask):
assert val in [5, 6, 7]
for val in self.filter_dense(result["S"], S_mask):
assert val in [b"a", b"e", b"i", b"o", b"u"]
result = A.query(cond="U in [8]")[:]
for val in self.filter_dense(result["U"], U_mask):
assert val == 8
result = A.query(cond="S in ['8']")[:]
assert len(self.filter_dense(result["S"], S_mask)) == 0
result = A.query(cond="U not in [5, 6, 7]")[:]
for val in self.filter_dense(result["U"], U_mask):
assert val not in [5, 6, 7]
with pytest.raises(tiledb.TileDBError) as exc_info:
A.query(cond="U not in []")[:]
assert "At least one value must be provided to the set membership" in str(
exc_info.value
)
@pytest.mark.parametrize(
"expression_and_message",
[
["foo is True", "the `is` operator is not supported"],
["foo is not True", "the `is not` operator is not supported"],
[
"foo &&& bar",
"Could not parse the given QueryCondition statement: foo &&& bar",
],
],
)
@pytest.mark.parametrize("sparse", [True, False])
def test_not_supported_operators(self, expression_and_message, sparse):
with tiledb.open(self.create_input_array_UIDSA(sparse=sparse)) as A:
expression, message = expression_and_message
with self.assertRaisesRegex(tiledb.TileDBError, message):
A.query(cond=expression)[:]
@pytest.mark.skipif(not has_pandas(), reason="pandas>=1.0,<3.0 not installed")
def test_dense_datetime(self):
import pandas as pd
uri = self.path("query-filter-dense-datetime.tdb")
data = pd.DataFrame(
np.random.randint(438923600, 243892360000, 20, dtype=np.int64),
columns=["dates"],
)
tiledb.from_pandas(
uri,
data,
column_types={"dates": "datetime64[ns]"},
)
with tiledb.open(uri) as A:
idx = 5
dt_mask = A.attr("dates").fill
search_date = data["dates"][idx]
result = A.query(cond=f"dates == {search_date}").df[:]
assert all(self.filter_dense(result["dates"], dt_mask) == A[idx]["dates"])
def test_array_with_bool_but_unused(self):
path = self.path("test_array_with_bool_but_unused")
dom = tiledb.Domain(
tiledb.Dim(name="d", domain=(1, 3), tile=1, dtype=np.uint32)
)
attrs = [
tiledb.Attr(name="myint", dtype=int),
tiledb.Attr(name="mystr", dtype=str),
tiledb.Attr(name="mybool", dtype=bool),
]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
data = {
"myint": np.asarray([10, 20, 30]),
"mystr": np.asarray(["apple", "ball", "cat"]),
"mybool": np.asarray([True, False, True]),
}
with tiledb.open(path, "w") as A:
A[np.arange(1, 4)] = data
with tiledb.open(path) as A:
result = A.query(cond="myint > 10", attrs=["myint"])[:]
assert all(result["myint"] > 10)
def test_do_not_return_queried_attr(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
qc = "U < 3"
i_result = A.query(cond=qc, attrs=["I", "U"])[:]
assert "I" in i_result.keys()
assert "U" in i_result.keys()
assert all(i_result["U"] < 5)
u_result = A.query(cond=qc, attrs=["I"])[:]
assert "I" in u_result.keys()
assert "U" not in u_result.keys()
assert_array_equal(i_result["I"], u_result["I"])
def test_deprecate_attr_cond(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
qc = "U < 3"
A.query(cond=qc)
A.query(cond=qc).cond
A.subarray(1, cond=qc)
def test_on_dense_dimensions(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=False)) as A:
with pytest.raises(tiledb.TileDBError) as excinfo:
A.query(cond="2 <= d < 6")[:]
assert (
"Cannot apply query condition to dimensions on dense arrays"
) in str(excinfo.value)
def test_on_sparse_dimensions(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
result = A.query(cond="2 <= d < 6")[:]
assert_array_equal(result["d"], A[2:6]["d"])
def test_overlapping(self):
path = self.path("test_overlapping")
dom = tiledb.Domain(tiledb.Dim(name="dim", domain=(0, 10), dtype=np.uint32))
attrs = [tiledb.Attr(name="data", dtype=np.uint32)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
with tiledb.open(path, "w") as A:
A[np.arange(11)] = np.arange(11)
with tiledb.open(path, "r") as A:
result = A.query(cond="2 <= dim < 7 and 5 <= dim < 9")[:]
assert_array_equal(result["dim"], A[5:7]["dim"])
result = A.query(cond="2 <= dim < 6 or 5 <= dim < 9")[:]
assert_array_equal(result["dim"], A[2:9]["dim"])
result = A.query(cond="2 <= data < 7 and 5 <= data < 9")[:]
assert_array_equal(result["data"], A[5:7]["data"])
result = A.query(cond="2 <= data < 6 or 5 <= data < 9")[:]
assert_array_equal(result["data"], A[2:9]["data"])
def test_with_whitespace(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
result = A.query(cond=" d < 6")[:]
assert_array_equal(result["d"], A[:6]["d"])
result = A.query(cond=" ( d < 6) ")[:]
assert_array_equal(result["d"], A[:6]["d"])
result = A.query(cond=" ( \n d \n\t< 6) ")[:]
assert_array_equal(result["d"], A[:6]["d"])
qc = """
U < 5
or
I >= 5
"""
result = A.query(cond=qc)[:]
assert all((result["U"] < 5) | (result["U"] > 5))
qc = """
A == ' a'
"""
result = A.query(cond=qc)[:]
# ensures that ' a' does not match 'a'
assert len(result["A"]) == 0
def test_attribute_with_dot(self):
path = self.path("test_with_dot")
dom = tiledb.Domain(tiledb.Dim(name="dim", domain=(0, 10), dtype=np.uint32))
attrs = [
tiledb.Attr(name="attr.one", dtype=np.uint32),
tiledb.Attr(name="attr.two", dtype=np.uint32),
]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
with tiledb.open(path, "w") as A:
A[np.arange(11)] = {"attr.one": np.arange(11), "attr.two": np.arange(11)}
with tiledb.open(path, "r") as A:
with pytest.raises(tiledb.TileDBError) as exc_info:
A.query(cond="attr.one < 6")[:]
assert (
"TileDBError: Unhandled dot operator in Attribute(value=Name(id='attr', ctx=Load()), attr='one', ctx=Load()) -- if your attribute name has a dot in it, e.g. `orig.ident`, please wrap it with `attr(\"...\")`, e.g. `attr(\"orig.ident\")`"
in str(exc_info.value)
)
with pytest.raises(tiledb.TileDBError) as exc_info:
A.query(cond="attr.two >= 6")[:]
assert (
"TileDBError: Unhandled dot operator in Attribute(value=Name(id='attr', ctx=Load()), attr='two', ctx=Load()) -- if your attribute name has a dot in it, e.g. `orig.ident`, please wrap it with `attr(\"...\")`, e.g. `attr(\"orig.ident\")`"
in str(exc_info.value)
)
# now test with the correct syntax
result = A.query(cond='attr("attr.one") < 6')[:]
assert_array_equal(result["attr.one"], A[:6]["attr.one"])
result = A.query(cond='attr("attr.two") >= 6')[:]
assert_array_equal(result["attr.two"], A[6:]["attr.two"])
@pytest.mark.skipif(not has_pandas(), reason="pandas>=1.0,<3.0 not installed")
def test_do_not_return_attrs(self):
with tiledb.open(self.create_input_array_UIDSA(sparse=True)) as A:
cond = None
assert "D" in A.query(cond=cond, attrs=None)[:]
assert "D" not in A.query(cond=cond, attrs=[])[:]
assert "D" in A.query(cond=cond, attrs=None).df[:]
assert "D" not in A.query(cond=cond, attrs=[]).df[:]
assert "D" in A.query(cond=cond, attrs=None).multi_index[:]
assert "D" not in A.query(cond=cond, attrs=[]).multi_index[:]
cond = "D > 100"
assert "D" in A.query(cond=cond, attrs=None)[:]
assert "D" not in A.query(cond=cond, attrs=[])[:]
assert "D" in A.query(cond=cond, attrs=None).df[:]
assert "D" not in A.query(cond=cond, attrs=[]).df[:]
assert "D" in A.query(cond=cond, attrs=None).multi_index[:]
assert "D" not in A.query(cond=cond, attrs=[]).multi_index[:]
def test_boolean_sparse(self):
path = self.path("test_boolean_sparse")
dom = tiledb.Domain(tiledb.Dim(domain=(1, 10), tile=1, dtype=np.uint32))
attrs = [
tiledb.Attr(name="a", dtype=np.bool_),
tiledb.Attr(name="b", dtype=np.bool_),
tiledb.Attr(name="c", dtype=np.bool_),
]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
with tiledb.open(path, "w") as arr:
arr[np.arange(1, 11)] = {
"a": np.random.randint(0, high=2, size=10),
"b": np.random.randint(0, high=2, size=10),
"c": np.random.randint(0, high=2, size=10),
}
with tiledb.open(path) as A:
result = A.query(cond="a == True")[:]
assert all(result["a"])
result = A.query(cond="a == False")[:]
assert all(~result["a"])
result = A.query(cond="a == True and b == True")[:]
assert all(result["a"])
assert all(result["b"])
result = A.query(cond="a == False and c == True")[:]
assert all(~result["a"])
assert all(result["c"])
def test_boolean_dense(self):
path = self.path("test_boolean_dense")
dom = tiledb.Domain(tiledb.Dim(domain=(1, 10), tile=1, dtype=np.uint32))
attrs = [
tiledb.Attr(name="a", dtype=np.bool_),
tiledb.Attr(name="b", dtype=np.bool_),
tiledb.Attr(name="c", dtype=np.bool_),
]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=False)
tiledb.Array.create(path, schema)
with tiledb.open(path, "w") as arr:
arr[:] = {
"a": np.random.randint(0, high=2, size=10),
"b": np.random.randint(0, high=2, size=10),
"c": np.random.randint(0, high=2, size=10),
}
with tiledb.open(path) as A:
mask = A.attr("a").fill
result = A.query(cond="a == True")[:]
assert all(self.filter_dense(result["a"], mask))
result = A.query(cond="a == True and b == True")[:]
assert all(self.filter_dense(result["a"], mask))
assert all(self.filter_dense(result["b"], mask))
def test_qc_enumeration(self):
uri = self.path("test_qc_enumeration")
dom = tiledb.Domain(tiledb.Dim(domain=(1, 8), tile=1))
enum1 = tiledb.Enumeration("enmr1", True, [0, 1, 2])
enum2 = tiledb.Enumeration("enmr2", True, ["a", "bb", "ccc"])
attr1 = tiledb.Attr("attr1", dtype=np.int32, enum_label="enmr1")
attr2 = tiledb.Attr("attr2", dtype=np.int32, enum_label="enmr2")
schema = tiledb.ArraySchema(
domain=dom, attrs=(attr1, attr2), enums=(enum1, enum2)
)
tiledb.Array.create(uri, schema)
data1 = np.random.randint(0, 3, 8)
data2 = np.random.randint(0, 3, 8)
with tiledb.open(uri, "w") as A:
A[:] = {"attr1": data1, "attr2": data2}
with tiledb.open(uri, "r") as A:
mask = A.attr("attr1").fill
result = A.query(cond="attr1 < 2", attrs=["attr1"])[:]
assert all(self.filter_dense(result["attr1"], mask) < 2)
result = A.query(cond="attr1 <= 2", attrs=["attr1"])[:]
assert all(self.filter_dense(result["attr1"], mask) <= 2)
result = A.query(cond="attr1 > 0", attrs=["attr1"])[:]
assert all(self.filter_dense(result["attr1"], mask) > 0)
result = A.query(cond="attr1 != 1", attrs=["attr1"])[:]
assert all(self.filter_dense(result["attr1"], mask) != 1)
mask = A.attr("attr2").fill
result = A.query(cond="attr2 == 'bb'", attrs=["attr2"])[:]
assert all(
self.filter_dense(result["attr2"], mask)
== list(enum2.values()).index("bb")
)
mask = A.attr("attr2").fill
result = A.query(cond="attr2 < 'ccc'", attrs=["attr2"])[:]
assert list(enum2.values()).index("ccc") not in self.filter_dense(
result["attr2"], mask
)
result = A.query(cond="attr2 == 'b'", attrs=["attr2"])[:]
assert all(self.filter_dense(result["attr2"], mask) == [])
result = A.query(cond="attr2 in ['b']", attrs=["attr2"])[:]
assert all(self.filter_dense(result["attr2"], mask) == [])
result = A.query(cond="attr2 not in ['b']", attrs=["attr2"])[:]
assert len(result["attr2"]) == len(data2)
result = A.query(cond="attr2 not in ['b', 'ccc']", attrs=["attr2"])[:]
assert list(enum2.values()).index("ccc") not in self.filter_dense(
result["attr2"], mask
)
result = A.query(
cond="attr1 < 2 and attr2 == 'bb'", attrs=["attr1", "attr2"]
)[:]
assert all(self.filter_dense(result["attr1"], mask) < 2) and all(
self.filter_dense(result["attr2"], mask)
== list(enum2.values()).index("bb")
)
result = A.query(cond="attr1 == 2", attrs=["attr1"])[:]
assert all(self.filter_dense(result["attr1"], mask) == 2)
result = A.query(
cond="attr1 == 0 or attr2 == 'ccc'", attrs=["attr1", "attr2"]
)[:]
assert any(self.filter_dense(result["attr1"], mask) == 0) or any(
self.filter_dense(result["attr2"], mask)
== list(enum2.values()).index("ccc")
)
def test_boolean_insert(self):
path = self.path("test_boolean_insert")
attr = tiledb.Attr("a", dtype=np.bool_, var=False)
dom = tiledb.Domain(tiledb.Dim(domain=(1, 10), tile=1, dtype=np.uint32))
schema = tiledb.ArraySchema(domain=dom, sparse=True, attrs=[attr])
tiledb.Array.create(path, schema)
a = np.array(
list(
[
np.array([True], dtype=np.bool_),
np.array([True], dtype=np.bool_),
np.array([True], dtype=np.bool_),
np.array([True], dtype=np.bool_),
]
),
dtype=object,
)
with tiledb.open(path, "w") as A:
A[range(1, len(a) + 1)] = {"a": a}
with tiledb.open(path, "r") as A:
for k in A[:]["a"]:
assert k == True # noqa: E712
def test_qc_dense_empty(self):
path = self.path("test_qc_dense_empty")
dom = tiledb.Domain(tiledb.Dim(name="d", domain=(1, 1), tile=1, dtype=np.uint8))
attrs = [tiledb.Attr(name="a", dtype=np.uint8)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=False)
tiledb.Array.create(path, schema)
with tiledb.open(path, mode="w") as A:
A[:] = np.arange(1)
with tiledb.open(path) as A:
assert_array_equal(A.query(cond="")[:]["a"], [0])
def test_qc_sparse_empty(self):
path = self.path("test_qc_sparse_empty")
dom = tiledb.Domain(
tiledb.Dim(name="d", domain=(1, 10), tile=1, dtype=np.uint8)
)
attrs = [tiledb.Attr(name="a", dtype=np.uint8)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
with tiledb.open(path, mode="w") as A:
A[1] = {"a": np.arange(1)}
with tiledb.open(path) as A:
assert_array_equal(A.query(cond="")[:]["a"], [0])
class QueryDeleteTest(DiskTestCase):
def test_basic_sparse(self):
path = self.path("test_basic_sparse")
dom = tiledb.Domain(tiledb.Dim(domain=(1, 10), tile=1, dtype=np.uint32))
attrs = [tiledb.Attr("ints", dtype=np.uint32)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
data = np.random.randint(1, 10, 10)
qc = "ints < 5"
with tiledb.open(path, "w") as A:
A[np.arange(1, 11)] = data
with pytest.raises(
tiledb.TileDBError,
match="SparseArray must be opened in read or delete mode",
):
A.query(cond=qc).submit()
with tiledb.open(path, "r") as A:
assert_array_equal(data, A[:]["ints"])
with tiledb.open(path, "d") as A:
with pytest.raises(
tiledb.TileDBError,
match="Cannot initialize deletes; One condition is needed",
):
A.query().submit()
A.query(cond=qc).submit()
with tiledb.open(path, "r") as A:
assert all(A[:]["ints"] >= 5)
def test_basic_dense(self):
path = self.path("test_basic_dense")
dom = tiledb.Domain(tiledb.Dim(domain=(1, 10), tile=1))
attrs = [tiledb.Attr("ints", dtype=np.uint8)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=False)
tiledb.Array.create(path, schema)
with tiledb.open(path, "d") as A:
with pytest.raises(
tiledb.TileDBError,
match="DenseArray must be opened in read mode",
):
A.query()
@pytest.mark.parametrize("use_timestamps", [True, False])
def test_with_fragments(self, use_timestamps):
path = self.path("test_with_fragments")
dom = tiledb.Domain(tiledb.Dim(domain=(1, 3), tile=1))
attrs = [tiledb.Attr("ints", dtype=np.uint8)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
if use_timestamps:
with tiledb.open(path, "w", timestamp=1) as A:
A[1] = 1
with tiledb.open(path, "w", timestamp=2) as A:
A[2] = 2
with tiledb.open(path, "w", timestamp=3) as A:
A[3] = 3
else:
with tiledb.open(path, "w") as A:
A[1] = 1
A[2] = 2
A[3] = 3
with tiledb.open(path, "r") as A:
assert_array_equal([1, 2, 3], A[:]["ints"])
timestamps = [t[0] for t in tiledb.array_fragments(path).timestamp_range]
with tiledb.open(path, "d", timestamp=timestamps[2]) as A:
A.query(cond="ints == 1").submit()
with tiledb.open(path, "r", timestamp=timestamps[0]) as A:
assert_array_equal([1], A[:]["ints"])
with tiledb.open(path, "r", timestamp=timestamps[1]) as A:
assert_array_equal([1, 2], A[:]["ints"])
with tiledb.open(path, "r", timestamp=timestamps[2]) as A:
assert_array_equal([2, 3], A[:]["ints"])
assert len(tiledb.array_fragments(path)) == 3
tiledb.consolidate(path)
tiledb.vacuum(path)
assert len(tiledb.array_fragments(path)) == 1
with tiledb.open(path, "r") as A:
assert A.nonempty_domain() == ((1, 3),)
assert_array_equal([2, 3], A[:]["ints"])
@pytest.mark.parametrize("use_timestamps", [True, False])
def test_purge_deleted_cells(self, use_timestamps):
path = self.path("test_with_fragments")
dom = tiledb.Domain(tiledb.Dim(domain=(1, 3), tile=1))
attrs = [tiledb.Attr("ints", dtype=np.uint8)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)
if use_timestamps:
with tiledb.open(path, "w", timestamp=1) as A:
A[1] = 1
with tiledb.open(path, "w", timestamp=2) as A:
A[2] = 2
with tiledb.open(path, "w", timestamp=3) as A:
A[3] = 3
else:
with tiledb.open(path, "w") as A:
A[1] = 1
A[2] = 2
A[3] = 3
with tiledb.open(path, "r") as A:
assert_array_equal([1, 2, 3], A[:]["ints"])
timestamps = [t[0] for t in tiledb.array_fragments(path).timestamp_range]
with tiledb.open(path, "d", timestamp=timestamps[2]) as A:
A.query(cond="ints == 1").submit()
with tiledb.open(path, "r", timestamp=timestamps[0]) as A:
assert_array_equal([1], A[:]["ints"])
with tiledb.open(path, "r", timestamp=timestamps[1]) as A:
assert_array_equal([1, 2], A[:]["ints"])
with tiledb.open(path, "r", timestamp=timestamps[2]) as A:
assert_array_equal([2, 3], A[:]["ints"])
cfg = tiledb.Config({"sm.consolidation.purge_deleted_cells": "true"})
with tiledb.scope_ctx(cfg):
tiledb.consolidate(path)
tiledb.vacuum(path)
with tiledb.open(path, "r") as A:
assert A.nonempty_domain() == ((2, 3),)
assert_array_equal([2, 3], A[:]["ints"])
def test_delete_with_string_dimension(self):
path = self.path("test_delete_with_string_dimension")
schema = tiledb.ArraySchema(
domain=tiledb.Domain(tiledb.Dim(name="d", dtype="|S0", var=True)),
attrs=[tiledb.Attr(name="a", dtype="uint32")],
sparse=True,
)
tiledb.Array.create(path, schema)
with tiledb.open(path, "w") as A:
A[["a", "b", "c"]] = [10, 20, 30]
with tiledb.open(path, "d") as A:
A.query(cond="a == 20").submit()
with tiledb.open(path, "r") as A:
assert_array_equal(A[:]["d"], [b"a", b"c"])
assert_array_equal(A[:]["a"], [10, 30])
with tiledb.open(path, "d") as A:
A.query(cond="d == 'a'").submit()
with tiledb.open(path, "r") as A:
assert_array_equal(A[:]["d"], [b"c"])
assert_array_equal(A[:]["a"], [30])
|