forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest.py
More file actions
1037 lines (866 loc) · 36 KB
/
test.py
File metadata and controls
1037 lines (866 loc) · 36 KB
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
import time
import psycopg2
import pymysql.cursors
import pytest
import logging
from helpers.cluster import ClickHouseCluster
from helpers.test_tools import assert_eq_with_retry
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from multiprocessing.dummy import Pool
cluster = ClickHouseCluster(__file__)
node1 = cluster.add_instance(
"node1",
with_odbc_drivers=True,
with_mysql8=True,
with_postgres=True,
main_configs=["configs/openssl.xml", "configs/odbc_logging.xml"],
dictionaries=[
"configs/dictionaries/sqlite3_odbc_hashed_dictionary.xml",
"configs/dictionaries/sqlite3_odbc_cached_dictionary.xml",
"configs/dictionaries/postgres_odbc_hashed_dictionary.xml",
"configs/dictionaries/postgres_odbc_no_connection_pool_dictionary.xml",
],
)
drop_table_sql_template = """
DROP TABLE IF EXISTS `clickhouse`.`{}`
"""
create_table_sql_template = """
CREATE TABLE `clickhouse`.`{}` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`age` int NOT NULL default 0,
`money` int NOT NULL default 0,
`column_x` int default NULL,
PRIMARY KEY (`id`)) ENGINE=InnoDB;
"""
def skip_test_msan(instance):
if instance.is_built_with_memory_sanitizer():
pytest.skip("Memory Sanitizer cannot work with third-party shared libraries")
def get_mysql_conn():
errors = []
conn = None
for _ in range(15):
try:
if conn is None:
conn = pymysql.connect(
user="root",
password="clickhouse",
host=cluster.mysql8_ip,
port=cluster.mysql8_port,
)
else:
conn.ping(reconnect=True)
logging.debug(
f"MySQL Connection establised: {cluster.mysql8_ip}:{cluster.mysql8_port}"
)
return conn
except Exception as e:
errors += [str(e)]
time.sleep(1)
raise Exception("Connection not establised, {}".format(errors))
def create_mysql_db(conn, name):
with conn.cursor() as cursor:
cursor.execute("DROP DATABASE IF EXISTS {}".format(name))
cursor.execute("CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(name))
def create_mysql_table(conn, table_name):
with conn.cursor() as cursor:
cursor.execute(create_table_sql_template.format(table_name))
def drop_mysql_table(conn, table_name):
with conn.cursor() as cursor:
cursor.execute(drop_table_sql_template.format(table_name))
def get_postgres_conn(started_cluster):
conn_string = "host={} port={} user='postgres' password='mysecretpassword'".format(
started_cluster.postgres_ip, started_cluster.postgres_port
)
errors = []
for _ in range(15):
try:
conn = psycopg2.connect(conn_string)
logging.debug("Postgre Connection establised: {}".format(conn_string))
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
conn.autocommit = True
return conn
except Exception as e:
errors += [str(e)]
time.sleep(1)
raise Exception(
"Postgre connection not establised DSN={}, {}".format(conn_string, errors)
)
def create_postgres_db(conn, name):
cursor = conn.cursor()
cursor.execute("CREATE SCHEMA {}".format(name))
@pytest.fixture(scope="module")
def started_cluster():
try:
cluster.start()
sqlite_db = node1.odbc_drivers["SQLite3"]["Database"]
logging.debug(f"sqlite data received: {sqlite_db}")
node1.exec_in_container(
[
"sqlite3",
sqlite_db,
"CREATE TABLE t1(id INTEGER PRIMARY KEY ASC, x INTEGER, y, z);",
],
privileged=True,
user="root",
)
node1.exec_in_container(
[
"sqlite3",
sqlite_db,
"CREATE TABLE t2(id INTEGER PRIMARY KEY ASC, X INTEGER, Y, Z);",
],
privileged=True,
user="root",
)
node1.exec_in_container(
[
"sqlite3",
sqlite_db,
"CREATE TABLE t3(id INTEGER PRIMARY KEY ASC, X INTEGER, Y, Z);",
],
privileged=True,
user="root",
)
node1.exec_in_container(
[
"sqlite3",
sqlite_db,
"CREATE TABLE t4(id INTEGER PRIMARY KEY ASC, X INTEGER, Y, Z);",
],
privileged=True,
user="root",
)
node1.exec_in_container(
[
"sqlite3",
sqlite_db,
"CREATE TABLE t5(id INTEGER PRIMARY KEY ASC, X INTEGER, Y, Z);",
],
privileged=True,
user="root",
)
node1.exec_in_container(
[
"sqlite3",
sqlite_db,
"CREATE TABLE tf1(id INTEGER PRIMARY KEY ASC, x INTEGER, y, z);",
],
privileged=True,
user="root",
)
logging.debug("sqlite tables created")
mysql_conn = get_mysql_conn()
logging.debug("mysql connection received")
## create mysql db and table
create_mysql_db(mysql_conn, "clickhouse")
logging.debug("mysql database created")
postgres_conn = get_postgres_conn(cluster)
logging.debug("postgres connection received")
create_postgres_db(postgres_conn, "clickhouse")
logging.debug("postgres db created")
cursor = postgres_conn.cursor()
cursor.execute(
"create table if not exists clickhouse.test_table (id int primary key, column1 int not null, column2 varchar(40) not null)"
)
yield cluster
except Exception as ex:
logging.exception(ex)
raise ex
finally:
cluster.shutdown()
def test_mysql_simple_select_works(started_cluster):
skip_test_msan(node1)
mysql_setup = node1.odbc_drivers["MySQL"]
table_name = "test_insert_select"
conn = get_mysql_conn()
create_mysql_table(conn, table_name)
# Check that NULL-values are handled correctly by the ODBC-bridge
with conn.cursor() as cursor:
cursor.execute(
"INSERT INTO clickhouse.{} VALUES(50, 'null-guy', 127, 255, NULL), (100, 'non-null-guy', 127, 255, 511);".format(
table_name
)
)
conn.commit()
assert (
node1.query(
"SELECT column_x FROM odbc('DSN={}', '{}')".format(
mysql_setup["DSN"], table_name
),
settings={"external_table_functions_use_nulls": "1"},
)
== "\\N\n511\n"
)
assert (
node1.query(
"SELECT column_x FROM odbc('DSN={}', '{}')".format(
mysql_setup["DSN"], table_name
),
settings={"external_table_functions_use_nulls": "0"},
)
== "0\n511\n"
)
node1.query(
"""
CREATE TABLE {}(id UInt32, name String, age UInt32, money UInt32, column_x Nullable(UInt32)) ENGINE = MySQL('mysql80:3306', 'clickhouse', '{}', 'root', 'clickhouse');
""".format(
table_name, table_name
)
)
node1.query(
"INSERT INTO {}(id, name, money, column_x) select number, concat('name_', toString(number)), 3, NULL from numbers(49) ".format(
table_name
)
)
node1.query(
"INSERT INTO {}(id, name, money, column_x) select number, concat('name_', toString(number)), 3, 42 from numbers(51, 49) ".format(
table_name
)
)
assert (
node1.query(
"SELECT COUNT () FROM {} WHERE column_x IS NOT NULL".format(table_name)
)
== "50\n"
)
assert (
node1.query("SELECT COUNT () FROM {} WHERE column_x IS NULL".format(table_name))
== "50\n"
)
assert (
node1.query(
"SELECT count(*) FROM odbc('DSN={}', '{}')".format(
mysql_setup["DSN"], table_name
)
)
== "100\n"
)
# previously this test fails with segfault
# just to be sure :)
assert node1.query("select 1") == "1\n"
node1.query(f"DROP TABLE {table_name}")
drop_mysql_table(conn, table_name)
conn.close()
def test_table_function_odbc_with_named_collection(started_cluster):
skip_test_msan(node1)
mysql_setup = node1.odbc_drivers["MySQL"]
table_name = "test_mysql_with_named_collection"
conn = get_mysql_conn()
create_mysql_table(conn, table_name)
# Check that NULL-values are handled correctly by the ODBC-bridge
with conn.cursor() as cursor:
cursor.execute(
"INSERT INTO clickhouse.{} VALUES(50, 'name1', 127, 255, 512), (100, 'name2', 127, 255, 511);".format(
table_name
)
)
conn.commit()
node1.query(f"""
DROP NAMED COLLECTION IF EXISTS odbc_collection;
CREATE NAMED COLLECTION odbc_collection AS
connection_settings = 'DSN={mysql_setup["DSN"]}',
external_table = '{table_name}';
""")
assert node1.query("SELECT name FROM odbc(odbc_collection)") == "name1\nname2\n"
node1.query(f"DROP TABLE IF EXISTS {table_name}")
drop_mysql_table(conn, table_name)
conn.close()
def test_mysql_insert(started_cluster):
skip_test_msan(node1)
mysql_setup = node1.odbc_drivers["MySQL"]
table_name = "test_insert"
conn = get_mysql_conn()
create_mysql_table(conn, table_name)
odbc_args = "'DSN={}', '{}', '{}'".format(
mysql_setup["DSN"], mysql_setup["Database"], table_name
)
node1.query(
"create table mysql_insert (id Int64, name String, age UInt8, money Float, column_x Nullable(Int16)) Engine=ODBC({})".format(
odbc_args
)
)
node1.query(
"insert into mysql_insert values (1, 'test', 11, 111, 1111), (2, 'odbc', 22, 222, NULL)"
)
assert (
node1.query("select * from mysql_insert")
== "1\ttest\t11\t111\t1111\n2\todbc\t22\t222\t\\N\n"
)
node1.query(
"insert into table function odbc({}) values (3, 'insert', 33, 333, 3333)".format(
odbc_args
)
)
node1.query(
"insert into table function odbc({}) (id, name, age, money) select id*4, upper(name), age*4, money*4 from odbc({}) where id=1".format(
odbc_args, odbc_args
)
)
assert (
node1.query("select * from mysql_insert where id in (3, 4)")
== "3\tinsert\t33\t333\t3333\n4\tTEST\t44\t444\t\\N\n"
)
node1.query("DROP TABLE mysql_insert")
drop_mysql_table(conn, table_name)
def test_sqlite_simple_select_function_works(started_cluster):
skip_test_msan(node1)
sqlite_setup = node1.odbc_drivers["SQLite3"]
sqlite_db = sqlite_setup["Database"]
node1.exec_in_container(
["sqlite3", sqlite_db, "INSERT INTO t1 values(1, 1, 2, 3);"],
privileged=True,
user="root",
)
assert (
node1.query(
"select * from odbc('DSN={}', '{}')".format(sqlite_setup["DSN"], "t1")
)
== "1\t1\t2\t3\n"
)
assert (
node1.query(
"select y from odbc('DSN={}', '{}')".format(sqlite_setup["DSN"], "t1")
)
== "2\n"
)
assert (
node1.query(
"select z from odbc('DSN={}', '{}')".format(sqlite_setup["DSN"], "t1")
)
== "3\n"
)
assert (
node1.query(
"select x from odbc('DSN={}', '{}')".format(sqlite_setup["DSN"], "t1")
)
== "1\n"
)
assert (
node1.query(
"select x, y from odbc('DSN={}', '{}')".format(sqlite_setup["DSN"], "t1")
)
== "1\t2\n"
)
assert (
node1.query(
"select z, x, y from odbc('DSN={}', '{}')".format(sqlite_setup["DSN"], "t1")
)
== "3\t1\t2\n"
)
assert (
node1.query(
"select count(), sum(x) from odbc('DSN={}', '{}') group by x".format(
sqlite_setup["DSN"], "t1"
)
)
== "1\t1\n"
)
node1.exec_in_container(
["sqlite3", sqlite_db, "DELETE FROM t1;"],
privileged=True,
user="root",
)
def test_sqlite_table_function(started_cluster):
skip_test_msan(node1)
sqlite_setup = node1.odbc_drivers["SQLite3"]
sqlite_db = sqlite_setup["Database"]
node1.exec_in_container(
["sqlite3", sqlite_db, "INSERT INTO tf1 values(1, 1, 2, 3);"],
privileged=True,
user="root",
)
node1.query(
"create table odbc_tf as odbc('DSN={}', '{}')".format(
sqlite_setup["DSN"], "tf1"
)
)
assert node1.query("select * from odbc_tf") == "1\t1\t2\t3\n"
assert node1.query("select y from odbc_tf") == "2\n"
assert node1.query("select z from odbc_tf") == "3\n"
assert node1.query("select x from odbc_tf") == "1\n"
assert node1.query("select x, y from odbc_tf") == "1\t2\n"
assert node1.query("select z, x, y from odbc_tf") == "3\t1\t2\n"
assert node1.query("select count(), sum(x) from odbc_tf group by x") == "1\t1\n"
node1.query("DROP TABLE odbc_tf")
node1.exec_in_container(
["sqlite3", sqlite_db, "DELETE FROM tf1;"],
privileged=True,
user="root",
)
def test_sqlite_simple_select_storage_works(started_cluster):
skip_test_msan(node1)
sqlite_setup = node1.odbc_drivers["SQLite3"]
sqlite_db = sqlite_setup["Database"]
node1.exec_in_container(
["sqlite3", sqlite_db, "INSERT INTO t4 values(1, 1, 2, 3);"],
privileged=True,
user="root",
)
node1.query(
"create table SqliteODBC (x Int32, y String, z String) engine = ODBC('DSN={}', '', 't4')".format(
sqlite_setup["DSN"]
)
)
assert node1.query("select * from SqliteODBC") == "1\t2\t3\n"
assert node1.query("select y from SqliteODBC") == "2\n"
assert node1.query("select z from SqliteODBC") == "3\n"
assert node1.query("select x from SqliteODBC") == "1\n"
assert node1.query("select x, y from SqliteODBC") == "1\t2\n"
assert node1.query("select z, x, y from SqliteODBC") == "3\t1\t2\n"
assert node1.query("select count(), sum(x) from SqliteODBC group by x") == "1\t1\n"
node1.query("DROP TABLE SqliteODBC")
node1.exec_in_container(
["sqlite3", sqlite_db, "DELETE FROM t4;"],
privileged=True,
user="root",
)
def test_table_engine_odbc_named_collection(started_cluster):
skip_test_msan(node1)
sqlite_setup = node1.odbc_drivers["SQLite3"]
sqlite_db = sqlite_setup["Database"]
node1.exec_in_container(
["sqlite3", sqlite_db, "INSERT INTO t5 values(1, 1, 2, 3);"],
privileged=True,
user="root",
)
node1.query(f"""
DROP NAMED COLLECTION IF EXISTS engine_odbc_collection;
CREATE NAMED COLLECTION engine_odbc_collection AS
connection_settings = 'DSN={sqlite_setup["DSN"]}',
external_database = '',
external_table = 't5';
""")
node1.query("CREATE TABLE SqliteODBCNamedCol (x Int32, y String, z String) ENGINE = ODBC(engine_odbc_collection)")
assert node1.query("SELECT * FROM SqliteODBCNamedCol") == "1\t2\t3\n"
node1.query("DROP TABLE IF EXISTS SqliteODBCNamedCol")
node1.exec_in_container(
["sqlite3", sqlite_db, "DELETE FROM t5;"],
privileged=True,
user="root",
)
def test_sqlite_odbc_hashed_dictionary(started_cluster):
skip_test_msan(node1)
sqlite_db = node1.odbc_drivers["SQLite3"]["Database"]
node1.exec_in_container(
["sqlite3", sqlite_db, "INSERT INTO t2 values(1, 1, 2, 3);"],
privileged=True,
user="root",
)
node1.query("SYSTEM RELOAD DICTIONARY sqlite3_odbc_hashed")
first_update_time = node1.query(
"SELECT last_successful_update_time FROM system.dictionaries WHERE name = 'sqlite3_odbc_hashed'"
)
logging.debug(f"First update time {first_update_time}")
assert_eq_with_retry(
node1, "select dictGetUInt8('sqlite3_odbc_hashed', 'Z', toUInt64(1))", "3"
)
assert_eq_with_retry(
node1, "select dictGetUInt8('sqlite3_odbc_hashed', 'Z', toUInt64(200))", "1"
) # default
second_update_time = node1.query(
"SELECT last_successful_update_time FROM system.dictionaries WHERE name = 'sqlite3_odbc_hashed'"
)
# Reloaded with new data
logging.debug(f"Second update time {second_update_time}")
while first_update_time == second_update_time:
second_update_time = node1.query(
"SELECT last_successful_update_time FROM system.dictionaries WHERE name = 'sqlite3_odbc_hashed'"
)
logging.debug("Waiting dictionary to update for the second time")
time.sleep(0.1)
node1.exec_in_container(
["sqlite3", sqlite_db, "INSERT INTO t2 values(200, 200, 2, 7);"],
privileged=True,
user="root",
)
# No reload because of invalidate query
third_update_time = node1.query(
"SELECT last_successful_update_time FROM system.dictionaries WHERE name = 'sqlite3_odbc_hashed'"
)
logging.debug(f"Third update time {second_update_time}")
counter = 0
while third_update_time == second_update_time:
third_update_time = node1.query(
"SELECT last_successful_update_time FROM system.dictionaries WHERE name = 'sqlite3_odbc_hashed'"
)
time.sleep(0.1)
if counter > 50:
break
counter += 1
assert_eq_with_retry(
node1, "select dictGetUInt8('sqlite3_odbc_hashed', 'Z', toUInt64(1))", "3"
)
assert_eq_with_retry(
node1, "select dictGetUInt8('sqlite3_odbc_hashed', 'Z', toUInt64(200))", "1"
) # still default
node1.exec_in_container(
["sqlite3", sqlite_db, "REPLACE INTO t2 values(1, 1, 2, 5);"],
privileged=True,
user="root",
)
assert_eq_with_retry(
node1, "select dictGetUInt8('sqlite3_odbc_hashed', 'Z', toUInt64(1))", "5"
)
assert_eq_with_retry(
node1, "select dictGetUInt8('sqlite3_odbc_hashed', 'Z', toUInt64(200))", "7"
)
node1.exec_in_container(
["sqlite3", sqlite_db, "DELETE FROM t2;"],
privileged=True,
user="root",
)
def test_sqlite_odbc_cached_dictionary(started_cluster):
skip_test_msan(node1)
sqlite_db = node1.odbc_drivers["SQLite3"]["Database"]
node1.exec_in_container(
["sqlite3", sqlite_db, "INSERT INTO t3 values(1, 1, 2, 3);"],
privileged=True,
user="root",
)
assert (
node1.query("select dictGetUInt8('sqlite3_odbc_cached', 'Z', toUInt64(1))")
== "3\n"
)
# Allow insert
node1.exec_in_container(["chmod", "a+rw", "/tmp"], privileged=True, user="root")
node1.exec_in_container(["chmod", "a+rw", sqlite_db], privileged=True, user="root")
node1.query(
"insert into table function odbc('DSN={};ReadOnly=0', '', 't3') values (200, 200, 2, 7)".format(
node1.odbc_drivers["SQLite3"]["DSN"]
)
)
assert (
node1.query("select dictGetUInt8('sqlite3_odbc_cached', 'Z', toUInt64(200))")
== "7\n"
) # new value
node1.exec_in_container(
["sqlite3", sqlite_db, "REPLACE INTO t3 values(1, 1, 2, 12);"],
privileged=True,
user="root",
)
assert_eq_with_retry(
node1, "select dictGetUInt8('sqlite3_odbc_cached', 'Z', toUInt64(1))", "12"
)
node1.exec_in_container(
["sqlite3", sqlite_db, "DELETE FROM t3;"],
privileged=True,
user="root",
)
node1.query("SYSTEM RELOAD DICTIONARIES")
def test_postgres_odbc_hashed_dictionary_with_schema(started_cluster):
skip_test_msan(node1)
try:
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
cursor.execute(
"insert into clickhouse.test_table values(1, 1, 'hello'),(2, 2, 'world')"
)
node1.query("SYSTEM RELOAD DICTIONARY postgres_odbc_hashed")
node1.exec_in_container(
["ss", "-K", "dport", "postgresql"], privileged=True, user="root"
)
node1.query("SYSTEM RELOAD DICTIONARY postgres_odbc_hashed")
assert_eq_with_retry(
node1,
"select dictGetString('postgres_odbc_hashed', 'column2', toUInt64(1))",
"hello",
)
assert_eq_with_retry(
node1,
"select dictGetString('postgres_odbc_hashed', 'column2', toUInt64(2))",
"world",
)
finally:
cursor.execute("truncate table clickhouse.test_table")
def test_postgres_odbc_hashed_dictionary_no_tty_pipe_overflow(started_cluster):
skip_test_msan(node1)
try:
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
cursor.execute("insert into clickhouse.test_table values(3, 3, 'xxx')")
for i in range(100):
try:
node1.query("system reload dictionary postgres_odbc_hashed", timeout=15)
except Exception as ex:
assert False, "Exception occured -- odbc-bridge hangs: " + str(ex)
assert_eq_with_retry(
node1,
"select dictGetString('postgres_odbc_hashed', 'column2', toUInt64(3))",
"xxx",
)
finally:
cursor.execute("truncate table clickhouse.test_table")
def test_no_connection_pooling(started_cluster):
skip_test_msan(node1)
try:
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
cursor.execute(
"insert into clickhouse.test_table values(1, 1, 'hello'),(2, 2, 'world')"
)
node1.exec_in_container(
["ss", "-K", "dport", "5432"], privileged=True, user="root"
)
node1.query("SYSTEM RELOAD DICTIONARY postgres_odbc_nopool")
assert_eq_with_retry(
node1,
"select dictGetString('postgres_odbc_nopool', 'column2', toUInt64(1))",
"hello",
)
assert_eq_with_retry(
node1,
"select dictGetString('postgres_odbc_nopool', 'column2', toUInt64(2))",
"world",
)
# No open connections should be left because we don't use connection pooling.
assert "" == node1.exec_in_container(
["ss", "-H", "dport", "5432"], privileged=True, user="root"
)
finally:
cursor.execute("truncate table clickhouse.test_table")
def test_postgres_insert(started_cluster):
skip_test_msan(node1)
conn = get_postgres_conn(started_cluster)
# Also test with Servername containing '.' and '-' symbols (defined in
# postgres .yml file). This is needed to check parsing, validation and
# reconstruction of connection string.
try:
node1.query(
"create table pg_insert (id UInt64, column1 UInt8, column2 String) engine=ODBC('DSN=postgresql_odbc;Servername=postgre-sql.local', 'clickhouse', 'test_table')"
)
node1.query("insert into pg_insert values (1, 1, 'hello'), (2, 2, 'world')")
assert node1.query("select * from pg_insert") == "1\t1\thello\n2\t2\tworld\n"
node1.query(
"insert into table function odbc('DSN=postgresql_odbc', 'clickhouse', 'test_table') format CSV 3,3,test"
)
node1.query(
"insert into table function odbc('DSN=postgresql_odbc;Servername=postgre-sql.local', 'clickhouse', 'test_table')"
" select number, number, 's' || toString(number) from numbers (4, 7)"
)
assert (
node1.query("select sum(column1), count(column1) from pg_insert")
== "55\t10\n"
)
assert (
node1.query(
"select sum(n), count(n) from (select (*,).1 as n from (select * from odbc('DSN=postgresql_odbc', 'clickhouse', 'test_table')))"
)
== "55\t10\n"
)
finally:
node1.query("DROP TABLE IF EXISTS pg_insert")
conn.cursor().execute("truncate table clickhouse.test_table")
def test_odbc_postgres_date_data_type(started_cluster):
skip_test_msan(node1)
try:
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
cursor.execute(
"CREATE TABLE clickhouse.test_date (id integer, column1 integer, column2 date)"
)
cursor.execute("INSERT INTO clickhouse.test_date VALUES (1, 1, '2020-12-01')")
cursor.execute("INSERT INTO clickhouse.test_date VALUES (2, 2, '2020-12-02')")
cursor.execute("INSERT INTO clickhouse.test_date VALUES (3, 3, '2020-12-03')")
conn.commit()
node1.query(
"""
CREATE TABLE test_date (id UInt64, column1 UInt64, column2 Date)
ENGINE=ODBC('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_date')"""
)
expected = "1\t1\t2020-12-01\n2\t2\t2020-12-02\n3\t3\t2020-12-03\n"
result = node1.query("SELECT * FROM test_date")
assert result == expected
finally:
cursor.execute("DROP TABLE clickhouse.test_date")
node1.query("DROP TABLE IF EXISTS test_date")
def test_odbc_postgres_conversions(started_cluster):
skip_test_msan(node1)
try:
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
cursor.execute(
"""CREATE TABLE clickhouse.test_types (
a smallint, b integer, c bigint, d real, e double precision, f serial, g bigserial,
h timestamp)"""
)
node1.query(
"""
INSERT INTO TABLE FUNCTION
odbc('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_types')
VALUES (-32768, -2147483648, -9223372036854775808, 1.12345, 1.1234567890, 2147483647, 9223372036854775807, '2000-05-12 12:12:12')"""
)
result = node1.query(
"""
SELECT a, b, c, d, e, f, g, h
FROM odbc('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_types')
"""
)
assert (
result
== "-32768\t-2147483648\t-9223372036854775808\t1.12345\t1.123456789\t2147483647\t9223372036854775807\t2000-05-12 12:12:12\n"
)
cursor.execute("DROP TABLE IF EXISTS clickhouse.test_types")
cursor.execute(
"""CREATE TABLE clickhouse.test_types (column1 Timestamp, column2 Numeric)"""
)
node1.query(
"""
CREATE TABLE test_types (column1 DateTime64, column2 Decimal(5, 1))
ENGINE=ODBC('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_types')"""
)
node1.query(
"""INSERT INTO test_types
SELECT toDateTime64('2019-01-01 00:00:00', 3, 'Etc/UTC'), toDecimal32(1.1, 1)"""
)
expected = node1.query(
"SELECT toDateTime64('2019-01-01 00:00:00', 3, 'Etc/UTC'), toDecimal32(1.1, 1)"
)
result = node1.query("SELECT * FROM test_types")
assert result == expected
finally:
cursor.execute("DROP TABLE IF EXISTS clickhouse.test_types")
node1.query("DROP TABLE IF EXISTS test_types")
def test_odbc_cyrillic_with_varchar(started_cluster):
skip_test_msan(node1)
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS clickhouse.test_cyrillic")
cursor.execute("CREATE TABLE clickhouse.test_cyrillic (name varchar(11))")
node1.query(
"""
CREATE TABLE test_cyrillic (name String)
ENGINE = ODBC('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_cyrillic')"""
)
cursor.execute("INSERT INTO clickhouse.test_cyrillic VALUES ('A-nice-word')")
cursor.execute("INSERT INTO clickhouse.test_cyrillic VALUES ('Красивенько')")
result = node1.query(""" SELECT * FROM test_cyrillic ORDER BY name""")
assert result == "A-nice-word\nКрасивенько\n"
result = node1.query(
""" SELECT name FROM odbc('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_cyrillic') """
)
assert result == "A-nice-word\nКрасивенько\n"
node1.query("DROP TABLE test_cyrillic")
def test_many_connections(started_cluster):
skip_test_msan(node1)
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
cursor.execute("CREATE TABLE clickhouse.test_pg_table (key integer, value integer)")
node1.query(
"""
DROP TABLE IF EXISTS test_pg_table;
CREATE TABLE test_pg_table (key UInt32, value UInt32)
ENGINE = ODBC('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_pg_table')"""
)
node1.query("INSERT INTO test_pg_table SELECT number, number FROM numbers(10)")
query = "SELECT count() FROM ("
for i in range(24):
query += "SELECT key FROM {t} UNION ALL "
query += "SELECT key FROM {t})"
assert node1.query(query.format(t="test_pg_table")) == "250\n"
cursor.execute("DROP TABLE clickhouse.test_pg_table")
def test_concurrent_queries(started_cluster):
skip_test_msan(node1)
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
node1.query(
"""
DROP TABLE IF EXISTS test_pg_table;
CREATE TABLE test_pg_table (key UInt32, value UInt32)
ENGINE = ODBC('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_pg_table')"""
)
cursor.execute("DROP TABLE IF EXISTS clickhouse.test_pg_table")
cursor.execute("CREATE TABLE clickhouse.test_pg_table (key integer, value integer)")
def node_insert(_):
for i in range(5):
node1.query(
"INSERT INTO test_pg_table SELECT number, number FROM numbers(1000)",
user="default",
)
busy_pool = Pool(5)
p = busy_pool.map_async(node_insert, range(5))
p.wait()
assert_eq_with_retry(
node1, "SELECT count() FROM test_pg_table", str(5 * 5 * 1000), retry_count=100
)
def node_insert_select(_):
for i in range(5):
result = node1.query(
"INSERT INTO test_pg_table SELECT number, number FROM numbers(1000)",
user="default",
)
result = node1.query(
"SELECT * FROM test_pg_table LIMIT 100", user="default"
)
busy_pool = Pool(5)
p = busy_pool.map_async(node_insert_select, range(5))
p.wait()
assert_eq_with_retry(
node1,
"SELECT count() FROM test_pg_table",
str(5 * 5 * 1000 * 2),
retry_count=100,
)
node1.query("DROP TABLE test_pg_table;")
cursor.execute("DROP TABLE clickhouse.test_pg_table;")
def test_odbc_long_column_names(started_cluster):
skip_test_msan(node1)
conn = get_postgres_conn(started_cluster)
cursor = conn.cursor()
column_name = "column" * 8
create_table = "CREATE TABLE clickhouse.test_long_column_names ("
for i in range(1000):
if i != 0:
create_table += ", "
create_table += "{} integer".format(column_name + str(i))
create_table += ")"
cursor.execute(create_table)
insert = (
"INSERT INTO clickhouse.test_long_column_names SELECT i"
+ ", i" * 999
+ " FROM generate_series(0, 99) as t(i)"
)
cursor.execute(insert)
conn.commit()
create_table = "CREATE TABLE test_long_column_names ("
for i in range(1000):
if i != 0:
create_table += ", "
create_table += "{} UInt32".format(column_name + str(i))
create_table += ") ENGINE=ODBC('DSN=postgresql_odbc; Servername=postgre-sql.local', 'clickhouse', 'test_long_column_names')"
result = node1.query(create_table)
result = node1.query("SELECT * FROM test_long_column_names")
expected = node1.query("SELECT number" + ", number" * 999 + " FROM numbers(100)")
assert result == expected
cursor.execute("DROP TABLE IF EXISTS clickhouse.test_long_column_names")