forked from thepaul/cassandra-dtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeprecated_cql_tests.py
4525 lines (3572 loc) · 182 KB
/
deprecated_cql_tests.py
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
# coding: utf-8
import math
import random
import struct
import time
from collections import OrderedDict
from uuid import UUID
from cassandra import AlreadyExists, ConsistencyLevel, InvalidRequest
from cassandra.concurrent import execute_concurrent_with_args
from cassandra.protocol import (ConfigurationException, ProtocolException,
SyntaxException)
from cassandra.query import SimpleStatement
from cassandra.util import sortedset
from assertions import assert_all, assert_invalid, assert_none, assert_one
from dtest import Tester, canReuseCluster, freshCluster
from thrift_bindings.v22.ttypes import \
ConsistencyLevel as ThriftConsistencyLevel
from thrift_bindings.v22.ttypes import (CfDef, Column, ColumnOrSuperColumn,
Mutation)
from thrift_tests import get_thrift_client
from tools import require, rows_to_list, since
@since('1.0.x', max_version='2.0.x')
@canReuseCluster
class TestCQL(Tester):
def prepare(self, ordered=False, create_keyspace=True, use_cache=False, nodes=1, rf=1, protocol_version=None, **kwargs):
cluster = self.cluster
if (ordered):
cluster.set_partitioner("org.apache.cassandra.dht.ByteOrderedPartitioner")
if (use_cache):
cluster.set_configuration_options(values={'row_cache_size_in_mb': 100})
start_rpc = kwargs.pop('start_rpc', False)
if start_rpc:
cluster.set_configuration_options(values={'start_rpc': True})
if not cluster.nodelist():
cluster.populate(nodes).start()
node1 = cluster.nodelist()[0]
time.sleep(0.2)
session = self.patient_cql_connection(node1, protocol_version=protocol_version)
if create_keyspace:
if self._preserve_cluster:
session.execute("DROP KEYSPACE IF EXISTS ks")
self.create_ks(session, 'ks', rf)
return session
def static_cf_test(self):
"""
Test static CF syntax.
"""
session = self.prepare()
# Create
session.execute("""
CREATE TABLE users (
userid uuid PRIMARY KEY,
firstname text,
lastname text,
age int
);
""")
# Inserts
session.execute("INSERT INTO users (userid, firstname, lastname, age) VALUES (550e8400-e29b-41d4-a716-446655440000, 'Frodo', 'Baggins', 32)")
session.execute("UPDATE users SET firstname = 'Samwise', lastname = 'Gamgee', age = 33 WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479")
# Queries
res = session.execute("SELECT firstname, lastname FROM users WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
assert rows_to_list(res) == [['Frodo', 'Baggins']], res
res = session.execute("SELECT * FROM users WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
assert rows_to_list(res) == [[UUID('550e8400-e29b-41d4-a716-446655440000'), 32, 'Frodo', 'Baggins']], res
res = session.execute("SELECT * FROM users")
assert rows_to_list(res) == [
[UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479'), 33, 'Samwise', 'Gamgee'],
[UUID('550e8400-e29b-41d4-a716-446655440000'), 32, 'Frodo', 'Baggins'],
], res
# Test batch inserts
session.execute("""
BEGIN BATCH
INSERT INTO users (userid, age) VALUES (550e8400-e29b-41d4-a716-446655440000, 36)
UPDATE users SET age = 37 WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479
DELETE firstname, lastname FROM users WHERE userid = 550e8400-e29b-41d4-a716-446655440000
DELETE firstname, lastname FROM users WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479
APPLY BATCH
""")
res = session.execute("SELECT * FROM users")
assert rows_to_list(res) == [
[UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479'), 37, None, None],
[UUID('550e8400-e29b-41d4-a716-446655440000'), 36, None, None],
], res
def large_collection_errors(self):
"""
For large collections, make sure that we are printing warnings.
"""
# We only warn with protocol 2
session = self.prepare(protocol_version=2)
cluster = self.cluster
node1 = cluster.nodelist()[0]
self.ignore_log_patterns = ["Detected collection for table"]
session.execute("""
CREATE TABLE maps (
userid text PRIMARY KEY,
properties map<int, text>
);
""")
# Insert more than the max, which is 65535
for i in range(70000):
session.execute("UPDATE maps SET properties[%i] = 'x' WHERE userid = 'user'" % i)
# Query for the data and throw exception
session.execute("SELECT properties FROM maps WHERE userid = 'user'")
node1.watch_log_for("Detected collection for table ks.maps with 70000 elements, more than the 65535 limit. Only the first 65535 elements will be returned to the client. Please see http://cassandra.apache.org/doc/cql3/CQL.html#collections for more details.")
def noncomposite_static_cf_test(self):
"""
Test non-composite static CF syntax.
"""
session = self.prepare()
# Create
session.execute("""
CREATE TABLE users (
userid uuid PRIMARY KEY,
firstname text,
lastname text,
age int
) WITH COMPACT STORAGE;
""")
# Inserts
session.execute("INSERT INTO users (userid, firstname, lastname, age) VALUES (550e8400-e29b-41d4-a716-446655440000, 'Frodo', 'Baggins', 32)")
session.execute("UPDATE users SET firstname = 'Samwise', lastname = 'Gamgee', age = 33 WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479")
# Queries
res = session.execute("SELECT firstname, lastname FROM users WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
assert rows_to_list(res) == [['Frodo', 'Baggins']], res
res = session.execute("SELECT * FROM users WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
assert rows_to_list(res) == [[UUID('550e8400-e29b-41d4-a716-446655440000'), 32, 'Frodo', 'Baggins']], res
res = session.execute("SELECT * FROM users")
assert rows_to_list(res) == [
[UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479'), 33, 'Samwise', 'Gamgee'],
[UUID('550e8400-e29b-41d4-a716-446655440000'), 32, 'Frodo', 'Baggins'],
], res
# Test batch inserts
session.execute("""
BEGIN BATCH
INSERT INTO users (userid, age) VALUES (550e8400-e29b-41d4-a716-446655440000, 36)
UPDATE users SET age = 37 WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479
DELETE firstname, lastname FROM users WHERE userid = 550e8400-e29b-41d4-a716-446655440000
DELETE firstname, lastname FROM users WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479
APPLY BATCH
""")
res = session.execute("SELECT * FROM users")
assert rows_to_list(res) == [
[UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479'), 37, None, None],
[UUID('550e8400-e29b-41d4-a716-446655440000'), 36, None, None],
], res
def dynamic_cf_test(self):
"""
Test non-composite dynamic CF syntax.
"""
session = self.prepare()
session.execute("""
CREATE TABLE clicks (
userid uuid,
url text,
time bigint,
PRIMARY KEY (userid, url)
) WITH COMPACT STORAGE;
""")
# Inserts
session.execute("INSERT INTO clicks (userid, url, time) VALUES (550e8400-e29b-41d4-a716-446655440000, 'http://foo.bar', 42)")
session.execute("INSERT INTO clicks (userid, url, time) VALUES (550e8400-e29b-41d4-a716-446655440000, 'http://foo-2.bar', 24)")
session.execute("INSERT INTO clicks (userid, url, time) VALUES (550e8400-e29b-41d4-a716-446655440000, 'http://bar.bar', 128)")
session.execute("UPDATE clicks SET time = 24 WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 and url = 'http://bar.foo'")
session.execute("UPDATE clicks SET time = 12 WHERE userid IN (f47ac10b-58cc-4372-a567-0e02b2c3d479, 550e8400-e29b-41d4-a716-446655440000) and url = 'http://foo-3'")
# Queries
res = session.execute("SELECT url, time FROM clicks WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
assert rows_to_list(res) == [['http://bar.bar', 128], ['http://foo-2.bar', 24], ['http://foo-3', 12], ['http://foo.bar', 42]], res
res = session.execute("SELECT * FROM clicks WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479")
assert rows_to_list(res) == [
[UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479'), 'http://bar.foo', 24],
[UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479'), 'http://foo-3', 12]
], res
res = session.execute("SELECT time FROM clicks")
# Result from 'f47ac10b-58cc-4372-a567-0e02b2c3d479' are first
assert rows_to_list(res) == [[24], [12], [128], [24], [12], [42]], res
# Check we don't allow empty values for url since this is the full underlying cell name (#6152)
assert_invalid(session, "INSERT INTO clicks (userid, url, time) VALUES (810e8500-e29b-41d4-a716-446655440000, '', 42)")
def dense_cf_test(self):
"""
Test composite 'dense' CF syntax.
"""
session = self.prepare()
session.execute("""
CREATE TABLE connections (
userid uuid,
ip text,
port int,
time bigint,
PRIMARY KEY (userid, ip, port)
) WITH COMPACT STORAGE;
""")
# Inserts
session.execute("INSERT INTO connections (userid, ip, port, time) VALUES (550e8400-e29b-41d4-a716-446655440000, '192.168.0.1', 80, 42)")
session.execute("INSERT INTO connections (userid, ip, port, time) VALUES (550e8400-e29b-41d4-a716-446655440000, '192.168.0.2', 80, 24)")
session.execute("INSERT INTO connections (userid, ip, port, time) VALUES (550e8400-e29b-41d4-a716-446655440000, '192.168.0.2', 90, 42)")
session.execute("UPDATE connections SET time = 24 WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 AND ip = '192.168.0.2' AND port = 80")
# we don't have to include all of the clustering columns (see CASSANDRA-7990)
session.execute("INSERT INTO connections (userid, ip, time) VALUES (f47ac10b-58cc-4372-a567-0e02b2c3d479, '192.168.0.3', 42)")
session.execute("UPDATE connections SET time = 42 WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 AND ip = '192.168.0.4'")
# Queries
res = session.execute("SELECT ip, port, time FROM connections WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
assert rows_to_list(res) == [['192.168.0.1', 80, 42], ['192.168.0.2', 80, 24], ['192.168.0.2', 90, 42]], res
res = session.execute("SELECT ip, port, time FROM connections WHERE userid = 550e8400-e29b-41d4-a716-446655440000 and ip >= '192.168.0.2'")
assert rows_to_list(res) == [['192.168.0.2', 80, 24], ['192.168.0.2', 90, 42]], res
res = session.execute("SELECT ip, port, time FROM connections WHERE userid = 550e8400-e29b-41d4-a716-446655440000 and ip = '192.168.0.2'")
assert rows_to_list(res) == [['192.168.0.2', 80, 24], ['192.168.0.2', 90, 42]], res
res = session.execute("SELECT ip, port, time FROM connections WHERE userid = 550e8400-e29b-41d4-a716-446655440000 and ip > '192.168.0.2'")
assert rows_to_list(res) == [], res
res = session.execute("SELECT ip, port, time FROM connections WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 AND ip = '192.168.0.3'")
self.assertEqual([['192.168.0.3', None, 42]], rows_to_list(res))
res = session.execute("SELECT ip, port, time FROM connections WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 AND ip = '192.168.0.4'")
self.assertEqual([['192.168.0.4', None, 42]], rows_to_list(res))
# Deletion
session.execute("DELETE time FROM connections WHERE userid = 550e8400-e29b-41d4-a716-446655440000 AND ip = '192.168.0.2' AND port = 80")
res = session.execute("SELECT * FROM connections WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
assert len(res) == 2, res
session.execute("DELETE FROM connections WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
res = session.execute("SELECT * FROM connections WHERE userid = 550e8400-e29b-41d4-a716-446655440000")
assert len(res) == 0, res
session.execute("DELETE FROM connections WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 AND ip = '192.168.0.3'")
res = session.execute("SELECT * FROM connections WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 AND ip = '192.168.0.3'")
self.assertEqual([], res)
def sparse_cf_test(self):
"""
Test composite 'sparse' CF syntax.
"""
session = self.prepare()
session.execute("""
CREATE TABLE timeline (
userid uuid,
posted_month int,
posted_day int,
body text,
posted_by text,
PRIMARY KEY (userid, posted_month, posted_day)
);
""")
# Inserts
session.execute("INSERT INTO timeline (userid, posted_month, posted_day, body, posted_by) VALUES (550e8400-e29b-41d4-a716-446655440000, 1, 12, 'Something else', 'Frodo Baggins')")
session.execute("INSERT INTO timeline (userid, posted_month, posted_day, body, posted_by) VALUES (550e8400-e29b-41d4-a716-446655440000, 1, 24, 'Something something', 'Frodo Baggins')")
session.execute("UPDATE timeline SET body = 'Yo Froddo', posted_by = 'Samwise Gamgee' WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 AND posted_month = 1 AND posted_day = 3")
session.execute("UPDATE timeline SET body = 'Yet one more message' WHERE userid = 550e8400-e29b-41d4-a716-446655440000 AND posted_month = 1 and posted_day = 30")
# Queries
res = session.execute("SELECT body, posted_by FROM timeline WHERE userid = 550e8400-e29b-41d4-a716-446655440000 AND posted_month = 1 AND posted_day = 24")
assert rows_to_list(res) == [['Something something', 'Frodo Baggins']], res
res = session.execute("SELECT posted_day, body, posted_by FROM timeline WHERE userid = 550e8400-e29b-41d4-a716-446655440000 AND posted_month = 1 AND posted_day > 12")
assert rows_to_list(res) == [
[24, 'Something something', 'Frodo Baggins'],
[30, 'Yet one more message', None]
], res
res = session.execute("SELECT posted_day, body, posted_by FROM timeline WHERE userid = 550e8400-e29b-41d4-a716-446655440000 AND posted_month = 1")
assert rows_to_list(res) == [
[12, 'Something else', 'Frodo Baggins'],
[24, 'Something something', 'Frodo Baggins'],
[30, 'Yet one more message', None]
], res
def create_invalid_test(self):
"""
Check invalid CREATE TABLE requests.
"""
session = self.prepare()
assert_invalid(session, "CREATE TABLE test ()", expected=SyntaxException)
if self.cluster.version() < "1.2":
assert_invalid(session, "CREATE TABLE test (key text PRIMARY KEY)")
assert_invalid(session, "CREATE TABLE test (c1 text, c2 text, c3 text)")
assert_invalid(session, "CREATE TABLE test (key1 text PRIMARY KEY, key2 text PRIMARY KEY)")
assert_invalid(session, "CREATE TABLE test (key text PRIMARY KEY, key int)")
assert_invalid(session, "CREATE TABLE test (key text PRIMARY KEY, c int, c text)")
assert_invalid(session, "CREATE TABLE test (key text, key2 text, c int, d text, PRIMARY KEY (key, key2)) WITH COMPACT STORAGE")
@freshCluster()
def limit_ranges_test(self):
"""
Validate LIMIT option for 'range queries' in SELECT statements.
"""
session = self.prepare(ordered=True)
session.execute("""
CREATE TABLE clicks (
userid int,
url text,
time bigint,
PRIMARY KEY (userid, url)
) WITH COMPACT STORAGE;
""")
# Inserts
for id in xrange(0, 100):
for tld in ['com', 'org', 'net']:
session.execute("INSERT INTO clicks (userid, url, time) VALUES (%i, 'http://foo.%s', 42)" % (id, tld))
# Queries
res = session.execute("SELECT * FROM clicks WHERE token(userid) >= token(2) LIMIT 1")
assert rows_to_list(res) == [[2, 'http://foo.com', 42]], res
res = session.execute("SELECT * FROM clicks WHERE token(userid) > token(2) LIMIT 1")
assert rows_to_list(res) == [[3, 'http://foo.com', 42]], res
def limit_multiget_test(self):
"""
Validate LIMIT option for 'multiget' in SELECT statements.
"""
session = self.prepare()
session.execute("""
CREATE TABLE clicks (
userid int,
url text,
time bigint,
PRIMARY KEY (userid, url)
) WITH COMPACT STORAGE;
""")
# Inserts
for id in xrange(0, 100):
for tld in ['com', 'org', 'net']:
session.execute("INSERT INTO clicks (userid, url, time) VALUES (%i, 'http://foo.%s', 42)" % (id, tld))
# Check that we do limit the output to 1 *and* that we respect query
# order of keys (even though 48 is after 2)
res = session.execute("SELECT * FROM clicks WHERE userid IN (48, 2) LIMIT 1")
if self.cluster.version() >= '2.2':
assert rows_to_list(res) == [[2, 'http://foo.com', 42]], res
else:
assert rows_to_list(res) == [[48, 'http://foo.com', 42]], res
def tuple_query_mixed_order_columns_prepare(self, session, *col_order):
session.execute("""
create table foo (a int, b int, c int, d int , e int, PRIMARY KEY (a, b, c, d, e) )
WITH CLUSTERING ORDER BY (b {0}, c {1}, d {2}, e {3});
""".format(col_order[0], col_order[1], col_order[2], col_order[3]))
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 2, 0, 0, 0);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 1, 0, 0, 0);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 0, 0, 0, 0);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 0, 1, 2, -1);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 0, 1, 1, -1);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 0, 1, 1, 0);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 0, 1, 1, 1);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 0, 1, 0, 2);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 0, 2, 1, -3);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, 0, 2, 0, 3);""")
session.execute("""INSERT INTO foo (a, b, c, d, e) VALUES (0, -1, 2, 2, 2);""")
@require("7281")
def tuple_query_mixed_order_columns_test(self):
"""
@jira_ticket CASSANDRA-7281
Regression test for broken SELECT statements on tuple relations with
mixed ASC/DESC clustering order.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'DESC', 'ASC', 'DESC', 'ASC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) > (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, 2, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 2, -1],
[0, 0, 1, 1, 1], [0, 0, 2, 1, -3], [0, 0, 2, 0, 3]], res
@require("7281")
def tuple_query_mixed_order_columns_test2(self):
"""
@jira_ticket CASSANDRA-7281
Regression test for broken SELECT statements on tuple relations with
mixed ASC/DESC clustering order.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'DESC', 'DESC', 'DESC', 'ASC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) > (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, 2, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 2, 1, -3],
[0, 0, 2, 0, 3], [0, 0, 1, 2, -1], [0, 0, 1, 1, 1]], res
@require("7281")
def tuple_query_mixed_order_columns_test3(self):
"""
@jira_ticket CASSANDRA-7281
Regression test for broken SELECT statements on tuple relations with
mixed ASC/DESC clustering order.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'ASC', 'DESC', 'DESC', 'ASC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) > (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, 0, 2, 1, -3], [0, 0, 2, 0, 3], [0, 0, 1, 2, -1],
[0, 0, 1, 1, 1], [0, 1, 0, 0, 0], [0, 2, 0, 0, 0]], res
@require("7281")
def tuple_query_mixed_order_columns_test4(self):
"""
@jira_ticket CASSANDRA-7281
Regression test for broken SELECT statements on tuple relations with
mixed ASC/DESC clustering order.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'DESC', 'ASC', 'ASC', 'DESC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) > (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, 2, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 1, 1],
[0, 0, 1, 2, -1], [0, 0, 2, 0, 3], [0, 0, 2, 1, -3]], res
@require("7281")
def tuple_query_mixed_order_columns_test5(self):
"""
@jira_ticket CASSANDRA-7281
Test that tuple relations with non-mixed ASC/DESC order still works.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'DESC', 'DESC', 'DESC', 'DESC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) > (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, 2, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 2, 1, -3],
[0, 0, 2, 0, 3], [0, 0, 1, 2, -1], [0, 0, 1, 1, 1]], res
@require("7281")
def tuple_query_mixed_order_columns_test6(self):
"""CASSANDRA-7281: SELECT on tuple relations are broken for mixed ASC/DESC clustering order
Test that non mixed columns are still working.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'ASC', 'ASC', 'ASC', 'ASC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) > (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, 0, 1, 1, 1], [0, 0, 1, 2, -1], [0, 0, 2, 0, 3],
[0, 0, 2, 1, -3], [0, 1, 0, 0, 0], [0, 2, 0, 0, 0]], res
@require("7281")
def tuple_query_mixed_order_columns_test7(self):
"""
@jira_ticket CASSANDRA-7281
Test that tuple relations with non-mixed ASC/DESC order still works.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'DESC', 'ASC', 'DESC', 'ASC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) <= (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, 0, 0, 0, 0], [0, 0, 1, 1, -1], [0, 0, 1, 1, 0],
[0, 0, 1, 0, 2], [0, -1, 2, 2, 2]], res
@require("7281")
def tuple_query_mixed_order_columns_test8(self):
"""
@jira_ticket CASSANDRA-7281
Test that tuple relations with non-mixed ASC/DESC order still works.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'ASC', 'DESC', 'DESC', 'ASC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) <= (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, -1, 2, 2, 2], [0, 0, 1, 1, -1], [0, 0, 1, 1, 0],
[0, 0, 1, 0, 2], [0, 0, 0, 0, 0]], res
@require("7281")
def tuple_query_mixed_order_columns_test9(self):
"""
@jira_ticket CASSANDRA-7281
Test that tuple relations with non-mixed ASC/DESC order still works.
"""
session = self.prepare()
self.tuple_query_mixed_order_columns_prepare(session, 'DESC', 'ASC', 'DESC', 'DESC')
res = session.execute("SELECT * FROM foo WHERE a=0 AND (b, c, d, e) <= (0, 1, 1, 0);")
assert rows_to_list(res) == [[0, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 0, 1, 1, -1],
[0, 0, 1, 0, 2], [0, -1, 2, 2, 2]], res
def simple_tuple_query_test(self):
"""
@jira_ticket CASSANDRA-8613
"""
session = self.prepare()
session.execute("create table bard (a int, b int, c int, d int , e int, PRIMARY KEY (a, b, c, d, e))")
session.execute("""INSERT INTO bard (a, b, c, d, e) VALUES (0, 2, 0, 0, 0);""")
session.execute("""INSERT INTO bard (a, b, c, d, e) VALUES (0, 1, 0, 0, 0);""")
session.execute("""INSERT INTO bard (a, b, c, d, e) VALUES (0, 0, 0, 0, 0);""")
session.execute("""INSERT INTO bard (a, b, c, d, e) VALUES (0, 0, 1, 1, 1);""")
session.execute("""INSERT INTO bard (a, b, c, d, e) VALUES (0, 0, 2, 2, 2);""")
session.execute("""INSERT INTO bard (a, b, c, d, e) VALUES (0, 0, 3, 3, 3);""")
session.execute("""INSERT INTO bard (a, b, c, d, e) VALUES (0, 0, 1, 1, 1);""")
res = session.execute("SELECT * FROM bard WHERE b=0 AND (c, d, e) > (1, 1, 1) ALLOW FILTERING;")
assert rows_to_list(res) == [[0, 0, 2, 2, 2], [0, 0, 3, 3, 3]]
def limit_sparse_test(self):
"""
Validate LIMIT option for sparse table in SELECT statements.
"""
session = self.prepare()
session.execute("""
CREATE TABLE clicks (
userid int,
url text,
day int,
month text,
year int,
PRIMARY KEY (userid, url)
);
""")
# Inserts
for id in xrange(0, 100):
for tld in ['com', 'org', 'net']:
session.execute("INSERT INTO clicks (userid, url, day, month, year) VALUES (%i, 'http://foo.%s', 1, 'jan', 2012)" % (id, tld))
# Queries
# Check we do get as many rows as requested
res = session.execute("SELECT * FROM clicks LIMIT 4")
assert len(res) == 4, res
def counters_test(self):
"""
Validate counter support.
"""
session = self.prepare()
session.execute("""
CREATE TABLE clicks (
userid int,
url text,
total counter,
PRIMARY KEY (userid, url)
) WITH COMPACT STORAGE;
""")
session.execute("UPDATE clicks SET total = total + 1 WHERE userid = 1 AND url = 'http://foo.com'")
res = session.execute("SELECT total FROM clicks WHERE userid = 1 AND url = 'http://foo.com'")
assert rows_to_list(res) == [[1]], res
session.execute("UPDATE clicks SET total = total - 4 WHERE userid = 1 AND url = 'http://foo.com'")
res = session.execute("SELECT total FROM clicks WHERE userid = 1 AND url = 'http://foo.com'")
assert rows_to_list(res) == [[-3]], res
session.execute("UPDATE clicks SET total = total+1 WHERE userid = 1 AND url = 'http://foo.com'")
res = session.execute("SELECT total FROM clicks WHERE userid = 1 AND url = 'http://foo.com'")
assert rows_to_list(res) == [[-2]], res
session.execute("UPDATE clicks SET total = total -2 WHERE userid = 1 AND url = 'http://foo.com'")
res = session.execute("SELECT total FROM clicks WHERE userid = 1 AND url = 'http://foo.com'")
assert rows_to_list(res) == [[-4]], res
def indexed_with_eq_test(self):
""" Check that you can query for an indexed column even with a key EQ clause """
session = self.prepare()
# Create
session.execute("""
CREATE TABLE users (
userid uuid PRIMARY KEY,
firstname text,
lastname text,
age int
);
""")
session.execute("CREATE INDEX byAge ON users(age)")
# Inserts
session.execute("INSERT INTO users (userid, firstname, lastname, age) VALUES (550e8400-e29b-41d4-a716-446655440000, 'Frodo', 'Baggins', 32)")
session.execute("UPDATE users SET firstname = 'Samwise', lastname = 'Gamgee', age = 33 WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479")
# Queries
res = session.execute("SELECT firstname FROM users WHERE userid = 550e8400-e29b-41d4-a716-446655440000 AND age = 33")
assert rows_to_list(res) == [], res
res = session.execute("SELECT firstname FROM users WHERE userid = f47ac10b-58cc-4372-a567-0e02b2c3d479 AND age = 33")
assert rows_to_list(res) == [['Samwise']], res
def select_key_in_test(self):
"""
Query for KEY IN (...).
"""
session = self.prepare()
# Create
session.execute("""
CREATE TABLE users (
userid uuid PRIMARY KEY,
firstname text,
lastname text,
age int
);
""")
# Inserts
session.execute("""
INSERT INTO users (userid, firstname, lastname, age)
VALUES (550e8400-e29b-41d4-a716-446655440000, 'Frodo', 'Baggins', 32)
""")
session.execute("""
INSERT INTO users (userid, firstname, lastname, age)
VALUES (f47ac10b-58cc-4372-a567-0e02b2c3d479, 'Samwise', 'Gamgee', 33)
""")
# Select
res = session.execute("""
SELECT firstname, lastname FROM users
WHERE userid IN (550e8400-e29b-41d4-a716-446655440000, f47ac10b-58cc-4372-a567-0e02b2c3d479)
""")
assert len(res) == 2, res
def exclusive_slice_test(self):
"""
Test SELECT respects inclusive and exclusive bounds.
"""
session = self.prepare()
session.execute("""
CREATE TABLE test (
k int,
c int,
v int,
PRIMARY KEY (k, c)
) WITH COMPACT STORAGE;
""")
# Inserts
for x in range(0, 10):
session.execute("INSERT INTO test (k, c, v) VALUES (0, %i, %i)" % (x, x))
# Queries
res = session.execute("SELECT v FROM test WHERE k = 0")
assert len(res) == 10, res
res = session.execute("SELECT v FROM test WHERE k = 0 AND c >= 2 AND c <= 6")
assert len(res) == 5 and res[0][0] == 2 and res[len(res) - 1][0] == 6, res
res = session.execute("SELECT v FROM test WHERE k = 0 AND c > 2 AND c <= 6")
assert len(res) == 4 and res[0][0] == 3 and res[len(res) - 1][0] == 6, res
res = session.execute("SELECT v FROM test WHERE k = 0 AND c >= 2 AND c < 6")
assert len(res) == 4 and res[0][0] == 2 and res[len(res) - 1][0] == 5, res
res = session.execute("SELECT v FROM test WHERE k = 0 AND c > 2 AND c < 6")
assert len(res) == 3 and res[0][0] == 3 and res[len(res) - 1][0] == 5, res
# With LIMIT
res = session.execute("SELECT v FROM test WHERE k = 0 AND c > 2 AND c <= 6 LIMIT 2")
assert len(res) == 2 and res[0][0] == 3 and res[len(res) - 1][0] == 4, res
res = session.execute("SELECT v FROM test WHERE k = 0 AND c >= 2 AND c < 6 ORDER BY c DESC LIMIT 2")
assert len(res) == 2 and res[0][0] == 5 and res[len(res) - 1][0] == 4, res
def in_clause_wide_rows_test(self):
""" Check IN support for 'wide rows' in SELECT statement """
session = self.prepare()
session.execute("""
CREATE TABLE test1 (
k int,
c int,
v int,
PRIMARY KEY (k, c)
) WITH COMPACT STORAGE;
""")
# Inserts
for x in range(0, 10):
session.execute("INSERT INTO test1 (k, c, v) VALUES (0, %i, %i)" % (x, x))
res = session.execute("SELECT v FROM test1 WHERE k = 0 AND c IN (5, 2, 8)")
if self.cluster.version() <= "1.2":
assert rows_to_list(res) == [[5], [2], [8]], res
else:
assert rows_to_list(res) == [[2], [5], [8]], res
# composites
session.execute("""
CREATE TABLE test2 (
k int,
c1 int,
c2 int,
v int,
PRIMARY KEY (k, c1, c2)
) WITH COMPACT STORAGE;
""")
# Inserts
for x in range(0, 10):
session.execute("INSERT INTO test2 (k, c1, c2, v) VALUES (0, 0, %i, %i)" % (x, x))
# Check first we don't allow IN everywhere
if self.cluster.version() >= '2.2':
assert_none(session, "SELECT v FROM test2 WHERE k = 0 AND c1 IN (5, 2, 8) AND c2 = 3")
else:
assert_invalid(session, "SELECT v FROM test2 WHERE k = 0 AND c1 IN (5, 2, 8) AND c2 = 3")
res = session.execute("SELECT v FROM test2 WHERE k = 0 AND c1 = 0 AND c2 IN (5, 2, 8)")
assert rows_to_list(res) == [[2], [5], [8]], res
def order_by_test(self):
""" Check ORDER BY support in SELECT statement """
session = self.prepare()
session.execute("""
CREATE TABLE test1 (
k int,
c int,
v int,
PRIMARY KEY (k, c)
) WITH COMPACT STORAGE;
""")
# Inserts
for x in range(0, 10):
session.execute("INSERT INTO test1 (k, c, v) VALUES (0, %i, %i)" % (x, x))
res = session.execute("SELECT v FROM test1 WHERE k = 0 ORDER BY c DESC")
assert rows_to_list(res) == [[x] for x in range(9, -1, -1)], res
# composites
session.execute("""
CREATE TABLE test2 (
k int,
c1 int,
c2 int,
v int,
PRIMARY KEY (k, c1, c2)
);
""")
# Inserts
for x in range(0, 4):
for y in range(0, 2):
session.execute("INSERT INTO test2 (k, c1, c2, v) VALUES (0, %i, %i, %i)" % (x, y, x * 2 + y))
# Check first we don't always ORDER BY
assert_invalid(session, "SELECT v FROM test2 WHERE k = 0 ORDER BY c DESC")
assert_invalid(session, "SELECT v FROM test2 WHERE k = 0 ORDER BY c2 DESC")
assert_invalid(session, "SELECT v FROM test2 WHERE k = 0 ORDER BY k DESC")
res = session.execute("SELECT v FROM test2 WHERE k = 0 ORDER BY c1 DESC")
assert rows_to_list(res) == [[x] for x in range(7, -1, -1)], res
res = session.execute("SELECT v FROM test2 WHERE k = 0 ORDER BY c1")
assert rows_to_list(res) == [[x] for x in range(0, 8)], res
def more_order_by_test(self):
""" More ORDER BY checks (#4160) """
session = self.prepare()
session.execute("""
CREATE COLUMNFAMILY Test (
row text,
number int,
string text,
PRIMARY KEY (row, number)
) WITH COMPACT STORAGE
""")
session.execute("INSERT INTO Test (row, number, string) VALUES ('row', 1, 'one');")
session.execute("INSERT INTO Test (row, number, string) VALUES ('row', 2, 'two');")
session.execute("INSERT INTO Test (row, number, string) VALUES ('row', 3, 'three');")
session.execute("INSERT INTO Test (row, number, string) VALUES ('row', 4, 'four');")
res = session.execute("SELECT number FROM Test WHERE row='row' AND number < 3 ORDER BY number ASC;")
assert rows_to_list(res) == [[1], [2]], res
res = session.execute("SELECT number FROM Test WHERE row='row' AND number >= 3 ORDER BY number ASC;")
assert rows_to_list(res) == [[3], [4]], res
res = session.execute("SELECT number FROM Test WHERE row='row' AND number < 3 ORDER BY number DESC;")
assert rows_to_list(res) == [[2], [1]], res
res = session.execute("SELECT number FROM Test WHERE row='row' AND number >= 3 ORDER BY number DESC;")
assert rows_to_list(res) == [[4], [3]], res
res = session.execute("SELECT number FROM Test WHERE row='row' AND number > 3 ORDER BY number DESC;")
assert rows_to_list(res) == [[4]], res
res = session.execute("SELECT number FROM Test WHERE row='row' AND number <= 3 ORDER BY number DESC;")
assert rows_to_list(res) == [[3], [2], [1]], res
def order_by_validation_test(self):
""" Check we don't allow order by on row key (#4246) """
session = self.prepare()
session.execute("""
CREATE TABLE test (
k1 int,
k2 int,
v int,
PRIMARY KEY (k1, k2)
)
""")
q = "INSERT INTO test (k1, k2, v) VALUES (%d, %d, %d)"
session.execute(q % (0, 0, 0))
session.execute(q % (1, 1, 1))
session.execute(q % (2, 2, 2))
assert_invalid(session, "SELECT * FROM test ORDER BY k2")
def order_by_with_in_test(self):
""" Check that order-by works with IN (#4327) """
session = self.prepare()
session.default_fetch_size = None
session.execute("""
CREATE TABLE test(
my_id varchar,
col1 int,
value varchar,
PRIMARY KEY (my_id, col1)
)
""")
session.execute("INSERT INTO test(my_id, col1, value) VALUES ( 'key1', 1, 'a')")
session.execute("INSERT INTO test(my_id, col1, value) VALUES ( 'key2', 3, 'c')")
session.execute("INSERT INTO test(my_id, col1, value) VALUES ( 'key3', 2, 'b')")
session.execute("INSERT INTO test(my_id, col1, value) VALUES ( 'key4', 4, 'd')")
query = SimpleStatement("SELECT col1 FROM test WHERE my_id in('key1', 'key2', 'key3') ORDER BY col1")
res = session.execute(query)
assert rows_to_list(res) == [[1], [2], [3]], res
query = SimpleStatement("SELECT col1, my_id FROM test WHERE my_id in('key1', 'key2', 'key3') ORDER BY col1")
res = session.execute(query)
assert rows_to_list(res) == [[1, 'key1'], [2, 'key3'], [3, 'key2']], res
query = SimpleStatement("SELECT my_id, col1 FROM test WHERE my_id in('key1', 'key2', 'key3') ORDER BY col1")
res = session.execute(query)
assert rows_to_list(res) == [['key1', 1], ['key3', 2], ['key2', 3]], res
def reversed_comparator_test(self):
session = self.prepare()
session.execute("""
CREATE TABLE test (
k int,
c int,
v int,
PRIMARY KEY (k, c)
) WITH CLUSTERING ORDER BY (c DESC);
""")
# Inserts
for x in range(0, 10):
session.execute("INSERT INTO test (k, c, v) VALUES (0, %i, %i)" % (x, x))
res = session.execute("SELECT c, v FROM test WHERE k = 0 ORDER BY c ASC")
assert rows_to_list(res) == [[x, x] for x in range(0, 10)], res
res = session.execute("SELECT c, v FROM test WHERE k = 0 ORDER BY c DESC")
assert rows_to_list(res) == [[x, x] for x in range(9, -1, -1)], res
session.execute("""
CREATE TABLE test2 (
k int,
c1 int,
c2 int,
v text,
PRIMARY KEY (k, c1, c2)
) WITH CLUSTERING ORDER BY (c1 ASC, c2 DESC);
""")
# Inserts
for x in range(0, 10):
for y in range(0, 10):
session.execute("INSERT INTO test2 (k, c1, c2, v) VALUES (0, %i, %i, '%i%i')" % (x, y, x, y))
assert_invalid(session, "SELECT c1, c2, v FROM test2 WHERE k = 0 ORDER BY c1 ASC, c2 ASC")
assert_invalid(session, "SELECT c1, c2, v FROM test2 WHERE k = 0 ORDER BY c1 DESC, c2 DESC")
res = session.execute("SELECT c1, c2, v FROM test2 WHERE k = 0 ORDER BY c1 ASC")
assert rows_to_list(res) == [[x, y, '%i%i' % (x, y)] for x in range(0, 10) for y in range(9, -1, -1)], res
res = session.execute("SELECT c1, c2, v FROM test2 WHERE k = 0 ORDER BY c1 ASC, c2 DESC")
assert rows_to_list(res) == [[x, y, '%i%i' % (x, y)] for x in range(0, 10) for y in range(9, -1, -1)], res
res = session.execute("SELECT c1, c2, v FROM test2 WHERE k = 0 ORDER BY c1 DESC, c2 ASC")
assert rows_to_list(res) == [[x, y, '%i%i' % (x, y)] for x in range(9, -1, -1) for y in range(0, 10)], res
assert_invalid(session, "SELECT c1, c2, v FROM test2 WHERE k = 0 ORDER BY c2 DESC, c1 ASC")
def invalid_old_property_test(self):
""" Check obsolete properties from CQL2 are rejected """
session = self.prepare()
assert_invalid(session, "CREATE TABLE test (foo text PRIMARY KEY, c int) WITH default_validation=timestamp", expected=SyntaxException)
session.execute("CREATE TABLE test (foo text PRIMARY KEY, c int)")
assert_invalid(session, "ALTER TABLE test WITH default_validation=int;", expected=SyntaxException)
def null_support_test(self):
""" Test support for nulls """
session = self.prepare()
session.execute("""
CREATE TABLE test (
k int,
c int,
v1 int,
v2 set<text>,
PRIMARY KEY (k, c)
);
""")
# Inserts
session.execute("INSERT INTO test (k, c, v1, v2) VALUES (0, 0, null, {'1', '2'})")
session.execute("INSERT INTO test (k, c, v1) VALUES (0, 1, 1)")
res = session.execute("SELECT * FROM test")
assert rows_to_list(res) == [[0, 0, None, set(['1', '2'])], [0, 1, 1, None]], res
session.execute("INSERT INTO test (k, c, v1) VALUES (0, 1, null)")
session.execute("INSERT INTO test (k, c, v2) VALUES (0, 0, null)")
res = session.execute("SELECT * FROM test")
assert rows_to_list(res) == [[0, 0, None, None], [0, 1, None, None]], res
assert_invalid(session, "INSERT INTO test (k, c, v2) VALUES (0, 2, {1, null})")
assert_invalid(session, "SELECT * FROM test WHERE k = null")
assert_invalid(session, "INSERT INTO test (k, c, v2) VALUES (0, 0, { 'foo', 'bar', null })")
def nameless_index_test(self):
""" Test CREATE INDEX without name and validate the index can be dropped """
session = self.prepare()
session.execute("""
CREATE TABLE users (
id text PRIMARY KEY,
birth_year int,
)
""")