forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest.py
More file actions
1034 lines (840 loc) · 39.5 KB
/
test.py
File metadata and controls
1034 lines (840 loc) · 39.5 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 glob
import json
import logging
import os
import random
import time
import uuid
from datetime import datetime, timedelta, time as dtime
import pyarrow as pa
import pytest
import requests
import urllib3
import pytz
from minio import Minio
from pyiceberg.catalog import load_catalog
from pyiceberg.partitioning import PartitionField, PartitionSpec, UNPARTITIONED_PARTITION_SPEC
from pyiceberg.schema import Schema
from pyiceberg.table.sorting import SortField, SortOrder
from pyiceberg.transforms import DayTransform, IdentityTransform
from pyiceberg.types import (
DoubleType,
LongType,
FloatType,
NestedField,
StringType,
StructType,
TimestampType,
TimestamptzType,
TimeType,
)
from pyiceberg.table.sorting import UNSORTED_SORT_ORDER
from helpers.cluster import ClickHouseCluster, ClickHouseInstance, is_arm
from helpers.config_cluster import minio_secret_key, minio_access_key
from helpers.s3_tools import get_file_contents, list_s3_objects, prepare_s3_bucket
from helpers.test_tools import TSV, csv_compare
from helpers.config_cluster import minio_secret_key
BASE_URL = "http://rest:8181/v1"
BASE_URL_LOCAL = "http://localhost:8182/v1"
BASE_URL_LOCAL_RAW = "http://localhost:8182"
CATALOG_NAME = "demo"
DEFAULT_SCHEMA = Schema(
NestedField(
field_id=1, name="datetime", field_type=TimestampType(), required=False
),
NestedField(field_id=2, name="symbol", field_type=StringType(), required=False),
NestedField(field_id=3, name="bid", field_type=DoubleType(), required=False),
NestedField(field_id=4, name="ask", field_type=DoubleType(), required=False),
NestedField(
field_id=5,
name="details",
field_type=StructType(
NestedField(
field_id=4,
name="created_by",
field_type=StringType(),
required=False,
),
),
required=False,
),
)
DEFAULT_CREATE_TABLE = "CREATE TABLE {}.`{}.{}`\\n(\\n `datetime` Nullable(DateTime64(6)),\\n `symbol` Nullable(String),\\n `bid` Nullable(Float64),\\n `ask` Nullable(Float64),\\n `details` Tuple(created_by Nullable(String))\\n)\\nENGINE = Iceberg(\\'http://minio:9000/warehouse-rest/data/\\', \\'minio\\', \\'[HIDDEN]\\')\n"
DEFAULT_PARTITION_SPEC = PartitionSpec(
PartitionField(
source_id=1, field_id=1000, transform=DayTransform(), name="datetime_day"
)
)
DEFAULT_SORT_ORDER = SortOrder(SortField(source_id=2, transform=IdentityTransform()))
AVAILABLE_ENGINES = ["DataLakeCatalog", "Iceberg"]
def list_namespaces():
response = requests.get(f"{BASE_URL_LOCAL}/namespaces")
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Failed to list namespaces: {response.status_code}")
def load_catalog_impl(started_cluster):
return load_catalog(
CATALOG_NAME,
**{
"uri": BASE_URL_LOCAL_RAW,
"type": "rest",
"s3.endpoint": f"http://{started_cluster.get_instance_ip('minio')}:9000",
"s3.access-key-id": minio_access_key,
"s3.secret-access-key": minio_secret_key,
},
)
def create_table(
catalog,
namespace,
table,
schema=DEFAULT_SCHEMA,
partition_spec=DEFAULT_PARTITION_SPEC,
sort_order=DEFAULT_SORT_ORDER,
):
return catalog.create_table(
identifier=f"{namespace}.{table}",
schema=schema,
location=f"s3://warehouse-rest/data",
partition_spec=partition_spec,
sort_order=sort_order,
)
def generate_record():
return {
"datetime": datetime.now(),
"symbol": str("kek"),
"bid": round(random.uniform(100, 200), 2),
"ask": round(random.uniform(200, 300), 2),
"details": {"created_by": "Alice Smith"},
}
def create_clickhouse_iceberg_database(
started_cluster, node, name, additional_settings={}, engine='DataLakeCatalog'
):
settings = {
"catalog_type": "rest",
"warehouse": "demo",
"storage_endpoint": "http://minio:9000/warehouse-rest",
}
settings.update(additional_settings)
node.query(
f"""
DROP DATABASE IF EXISTS {name};
SET allow_database_iceberg=true;
SET write_full_path_in_iceberg_metadata=1;
CREATE DATABASE {name} ENGINE = {engine}('{BASE_URL}', 'minio', '{minio_secret_key}')
SETTINGS {",".join((k+"="+repr(v) for k, v in settings.items()))}
"""
)
show_result = node.query(f"SHOW DATABASE {name}")
assert minio_secret_key not in show_result
assert "HIDDEN" in show_result
def create_clickhouse_iceberg_table(
started_cluster, node, database_name, table_name, schema, additional_settings={}
):
settings = {
"storage_catalog_type": "rest",
"storage_warehouse": "demo",
"object_storage_endpoint": "http://minio:9000/warehouse-rest",
"storage_region": "us-east-1",
"storage_catalog_url" : BASE_URL,
}
settings.update(additional_settings)
node.query(
f"""
SET allow_experimental_database_iceberg=true;
SET write_full_path_in_iceberg_metadata=1;
CREATE TABLE {CATALOG_NAME}.`{database_name}.{table_name}` {schema} ENGINE = IcebergS3('http://minio:9000/warehouse-rest/{table_name}/', '{minio_access_key}', '{minio_secret_key}')
SETTINGS {",".join((k+"="+repr(v) for k, v in settings.items()))}
"""
)
def drop_clickhouse_iceberg_table(
node, database_name, table_name
):
node.query(
f"""
DROP TABLE {CATALOG_NAME}.`{database_name}.{table_name}`
"""
)
@pytest.fixture(scope="module")
def started_cluster():
try:
cluster = ClickHouseCluster(__file__)
cluster.add_instance(
"node1",
main_configs=["configs/backups.xml", "configs/cluster.xml"],
user_configs=[],
stay_alive=True,
with_iceberg_catalog=True,
)
cluster.add_instance(
"node2",
main_configs=["configs/backups.xml", "configs/cluster.xml"],
user_configs=[],
stay_alive=True,
with_iceberg_catalog=True,
with_zookeeper=True,
)
logging.info("Starting cluster...")
cluster.start()
# TODO: properly wait for container
time.sleep(10)
yield cluster
finally:
cluster.shutdown()
@pytest.mark.parametrize("engine", AVAILABLE_ENGINES)
def test_list_tables(started_cluster, engine):
node = started_cluster.instances["node1"]
root_namespace = f"clickhouse_{uuid.uuid4()}"
namespace_1 = f"{root_namespace}.testA.A"
namespace_2 = f"{root_namespace}.testB.B"
namespace_1_tables = ["tableA", "tableB"]
namespace_2_tables = ["tableC", "tableD"]
catalog = load_catalog_impl(started_cluster)
for namespace in [namespace_1, namespace_2]:
catalog.create_namespace(namespace)
found = False
for namespace_list in list_namespaces()["namespaces"]:
if root_namespace == namespace_list[0]:
found = True
break
assert found
found = False
for namespace_list in catalog.list_namespaces():
if root_namespace == namespace_list[0]:
found = True
break
assert found
for namespace in [namespace_1, namespace_2]:
assert len(catalog.list_tables(namespace)) == 0
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME, engine=engine)
tables_list = ""
for table in namespace_1_tables:
create_table(catalog, namespace_1, table)
if len(tables_list) > 0:
tables_list += "\n"
tables_list += f"{namespace_1}.{table}"
for table in namespace_2_tables:
create_table(catalog, namespace_2, table)
if len(tables_list) > 0:
tables_list += "\n"
tables_list += f"{namespace_2}.{table}"
assert (
tables_list
== node.query(
f"SELECT name FROM system.tables WHERE database = '{CATALOG_NAME}' and name ILIKE '{root_namespace}%' ORDER BY name SETTINGS show_data_lake_catalogs_in_system_tables = true"
).strip()
)
node.restart_clickhouse()
assert (
tables_list
== node.query(
f"SELECT name FROM system.tables WHERE database = '{CATALOG_NAME}' and name ILIKE '{root_namespace}%' ORDER BY name SETTINGS show_data_lake_catalogs_in_system_tables = true"
).strip()
)
expected = DEFAULT_CREATE_TABLE.format(CATALOG_NAME, namespace_2, "tableC")
assert expected == node.query(
f"SHOW CREATE TABLE {CATALOG_NAME}.`{namespace_2}.tableC`"
)
@pytest.mark.parametrize("engine", AVAILABLE_ENGINES)
def test_many_namespaces(started_cluster, engine):
node = started_cluster.instances["node1"]
root_namespace_1 = f"A_{uuid.uuid4()}"
root_namespace_2 = f"B_{uuid.uuid4()}"
namespaces = [
f"{root_namespace_1}",
f"{root_namespace_1}.B.C",
f"{root_namespace_1}.B.C.D",
f"{root_namespace_1}.B.C.D.E",
f"{root_namespace_2}",
f"{root_namespace_2}.C",
f"{root_namespace_2}.CC",
]
tables = ["A", "B", "C"]
catalog = load_catalog_impl(started_cluster)
for namespace in namespaces:
catalog.create_namespace(namespace)
for table in tables:
create_table(catalog, namespace, table)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME, engine=engine)
for namespace in namespaces:
for table in tables:
table_name = f"{namespace}.{table}"
assert int(
node.query(
f"SELECT count() FROM system.tables WHERE database = '{CATALOG_NAME}' and name = '{table_name}' SETTINGS show_data_lake_catalogs_in_system_tables = true"
)
)
@pytest.mark.parametrize("engine", AVAILABLE_ENGINES)
def test_select(started_cluster, engine):
node = started_cluster.instances["node1"]
test_ref = f"test_list_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
namespace = f"{root_namespace}.A.B.C"
namespaces_to_create = [
root_namespace,
f"{root_namespace}.A",
f"{root_namespace}.A.B",
f"{root_namespace}.A.B.C",
]
catalog = load_catalog_impl(started_cluster)
for namespace in namespaces_to_create:
catalog.create_namespace(namespace)
assert len(catalog.list_tables(namespace)) == 0
table = create_table(catalog, namespace, table_name)
num_rows = 10
data = [generate_record() for _ in range(num_rows)]
df = pa.Table.from_pylist(data)
table.append(df)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME, engine=engine)
expected = DEFAULT_CREATE_TABLE.format(CATALOG_NAME, namespace, table_name)
assert expected == node.query(
f"SHOW CREATE TABLE {CATALOG_NAME}.`{namespace}.{table_name}`"
)
assert num_rows == int(
node.query(f"SELECT count() FROM {CATALOG_NAME}.`{namespace}.{table_name}`")
)
assert int(node.query(f"SELECT count() FROM system.iceberg_history WHERE table = '{namespace}.{table_name}' and database = '{CATALOG_NAME}'").strip()) == 1
@pytest.mark.parametrize("engine", AVAILABLE_ENGINES)
def test_hide_sensitive_info(started_cluster, engine):
node = started_cluster.instances["node1"]
test_ref = f"test_hide_sensitive_info_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
namespace = f"{root_namespace}.A"
catalog = load_catalog_impl(started_cluster)
catalog.create_namespace(namespace)
table = create_table(catalog, namespace, table_name)
create_clickhouse_iceberg_database(
started_cluster,
node,
CATALOG_NAME,
additional_settings={"catalog_credential": "SECRET_1"},
engine=engine,
)
assert "SECRET_1" not in node.query(f"SHOW CREATE DATABASE {CATALOG_NAME}")
create_clickhouse_iceberg_database(
started_cluster,
node,
CATALOG_NAME,
additional_settings={"auth_header": "SECRET_2"},
engine=engine,
)
assert "SECRET_2" not in node.query(f"SHOW CREATE DATABASE {CATALOG_NAME}")
@pytest.mark.parametrize("engine", AVAILABLE_ENGINES)
def test_tables_with_same_location(started_cluster, engine):
node = started_cluster.instances["node1"]
test_ref = f"test_tables_with_same_location_{uuid.uuid4()}"
namespace = f"{test_ref}_namespace"
catalog = load_catalog_impl(started_cluster)
table_name = f"{test_ref}_table"
table_name_2 = f"{test_ref}_table_2"
catalog.create_namespace(namespace)
table = create_table(catalog, namespace, table_name)
table_2 = create_table(catalog, namespace, table_name_2)
def record(key):
return {
"datetime": datetime.now(),
"symbol": str(key),
"bid": round(random.uniform(100, 200), 2),
"ask": round(random.uniform(200, 300), 2),
"details": {"created_by": "Alice Smith"},
}
data = [record('aaa') for _ in range(3)]
df = pa.Table.from_pylist(data)
table.append(df)
data = [record('bbb') for _ in range(3)]
df = pa.Table.from_pylist(data)
table_2.append(df)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME, engine=engine)
assert 'aaa\naaa\naaa' == node.query(f"SELECT symbol FROM {CATALOG_NAME}.`{namespace}.{table_name}`").strip()
assert 'bbb\nbbb\nbbb' == node.query(f"SELECT symbol FROM {CATALOG_NAME}.`{namespace}.{table_name_2}`").strip()
def test_backup_database(started_cluster):
node = started_cluster.instances["node1"]
create_clickhouse_iceberg_database(started_cluster, node, "backup_database")
backup_id = uuid.uuid4().hex
backup_name = f"File('/backups/test_backup_{backup_id}/')"
node.query(f"BACKUP DATABASE backup_database TO {backup_name}")
node.query("DROP DATABASE backup_database SYNC")
assert "backup_database" not in node.query("SHOW DATABASES")
node.query(f"RESTORE DATABASE backup_database FROM {backup_name}", settings={"allow_database_iceberg": 1})
assert (
node.query("SHOW CREATE DATABASE backup_database")
== "CREATE DATABASE backup_database\\nENGINE = DataLakeCatalog(\\'http://rest:8181/v1\\', \\'minio\\', \\'[HIDDEN]\\')\\nSETTINGS catalog_type = \\'rest\\', warehouse = \\'demo\\', storage_endpoint = \\'http://minio:9000/warehouse-rest\\'\n"
)
def test_non_existing_tables(started_cluster):
node = started_cluster.instances["node1"]
test_ref = f"test_list_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
namespace = f"{root_namespace}.A.B.C"
namespaces_to_create = [
root_namespace,
f"{root_namespace}.A",
f"{root_namespace}.A.B",
f"{root_namespace}.A.B.C",
]
catalog = load_catalog_impl(started_cluster)
for namespace in namespaces_to_create:
catalog.create_namespace(namespace)
assert len(catalog.list_tables(namespace)) == 0
table = create_table(catalog, namespace, table_name)
num_rows = 10
data = [generate_record() for _ in range(num_rows)]
df = pa.Table.from_pylist(data)
table.append(df)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
expected = DEFAULT_CREATE_TABLE.format(CATALOG_NAME, namespace, table_name)
assert expected == node.query(
f"SHOW CREATE TABLE {CATALOG_NAME}.`{namespace}.{table_name}`"
)
try:
node.query(
f"SHOW CREATE TABLE {CATALOG_NAME}.`{namespace}.qweqwe`"
)
except Exception as e:
assert "DB::Exception: Table" in str(e)
assert "doesn't exist" in str(e)
try:
node.query(
f"SHOW CREATE TABLE {CATALOG_NAME}.`qweqwe.qweqwe`"
)
except Exception as e:
assert "DB::Exception: Table" in str(e)
assert "doesn't exist" in str(e)
def test_timestamps(started_cluster):
node = started_cluster.instances["node1"]
test_ref = f"test_list_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
catalog = load_catalog_impl(started_cluster)
catalog.create_namespace(root_namespace)
schema = Schema(
NestedField(
field_id=1, name="timestamp", field_type=TimestampType(), required=False
),
NestedField(
field_id=2,
name="timestamptz",
field_type=TimestamptzType(),
required=False,
),
)
table = create_table(catalog, root_namespace, table_name, schema)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
data = [
{
"timestamp": datetime(2024, 1, 1, hour=12, minute=0, second=0, microsecond=0),
"timestamptz": datetime(
2024,
1,
1,
hour=12,
minute=0,
second=0,
microsecond=0,
tzinfo=pytz.timezone("UTC"),
)
}
]
df = pa.Table.from_pylist(data)
table.append(df)
assert node.query(f"SHOW CREATE TABLE {CATALOG_NAME}.`{root_namespace}.{table_name}`") == f"CREATE TABLE {CATALOG_NAME}.`{root_namespace}.{table_name}`\\n(\\n `timestamp` Nullable(DateTime64(6)),\\n `timestamptz` Nullable(DateTime64(6, \\'UTC\\'))\\n)\\nENGINE = Iceberg(\\'http://minio:9000/warehouse-rest/data/\\', \\'minio\\', \\'[HIDDEN]\\')\n"
assert node.query(f"SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`") == "2024-01-01 12:00:00.000000\t2024-01-01 12:00:00.000000\n"
# Berlin - UTC+1 at winter
# Istanbul - UTC+3 at winter
# 'UTC' is default value, responce is equal to query above
assert node.query(f"""
SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
SETTINGS iceberg_timezone_for_timestamptz='UTC'
""") == "2024-01-01 12:00:00.000000\t2024-01-01 12:00:00.000000\n"
# Timezone from setting
assert node.query(f"""
SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
SETTINGS iceberg_timezone_for_timestamptz='Europe/Berlin'
""") == "2024-01-01 12:00:00.000000\t2024-01-01 13:00:00.000000\n"
# Empty value means session timezone, by default it is 'UTC' too
assert node.query(f"""
SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
SETTINGS iceberg_timezone_for_timestamptz=''
""") == "2024-01-01 12:00:00.000000\t2024-01-01 12:00:00.000000\n"
# If session timezone is used, `timestamptz` does not changed, 'UTC' by default
assert node.query(f"""
SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
SETTINGS session_timezone='Asia/Istanbul'
""") == "2024-01-01 15:00:00.000000\t2024-01-01 12:00:00.000000\n"
# Setiing `iceberg_timezone_for_timestamptz` does not affect `timestamp` column
assert node.query(f"""
SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
SETTINGS session_timezone='Asia/Istanbul', iceberg_timezone_for_timestamptz='Europe/Berlin'
""") == "2024-01-01 15:00:00.000000\t2024-01-01 13:00:00.000000\n"
# Empty value, used non-default session timezone
assert node.query(f"""
SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
SETTINGS session_timezone='Asia/Istanbul', iceberg_timezone_for_timestamptz=''
""") == "2024-01-01 15:00:00.000000\t2024-01-01 15:00:00.000000\n"
# Invalid timezone
assert "Invalid time zone: Foo/Bar" in node.query_and_get_error(f"""
SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
SETTINGS iceberg_timezone_for_timestamptz='Foo/Bar'
""")
assert node.query(f"SHOW CREATE TABLE {CATALOG_NAME}.`{root_namespace}.{table_name}` SETTINGS iceberg_timezone_for_timestamptz='UTC'") == f"CREATE TABLE {CATALOG_NAME}.`{root_namespace}.{table_name}`\\n(\\n `timestamp` Nullable(DateTime64(6)),\\n `timestamptz` Nullable(DateTime64(6, \\'UTC\\'))\\n)\\nENGINE = Iceberg(\\'http://minio:9000/warehouse-rest/data/\\', \\'minio\\', \\'[HIDDEN]\\')\n"
assert node.query(f"SHOW CREATE TABLE {CATALOG_NAME}.`{root_namespace}.{table_name}` SETTINGS iceberg_timezone_for_timestamptz='Europe/Berlin'") == f"CREATE TABLE {CATALOG_NAME}.`{root_namespace}.{table_name}`\\n(\\n `timestamp` Nullable(DateTime64(6)),\\n `timestamptz` Nullable(DateTime64(6, \\'Europe/Berlin\\'))\\n)\\nENGINE = Iceberg(\\'http://minio:9000/warehouse-rest/data/\\', \\'minio\\', \\'[HIDDEN]\\')\n"
assert node.query(f"SELECT timezoneOf(timestamptz) FROM {CATALOG_NAME}.`{root_namespace}.{table_name}` LIMIT 1") == "UTC\n"
assert node.query(f"SELECT timezoneOf(timestamptz) FROM {CATALOG_NAME}.`{root_namespace}.{table_name}` LIMIT 1 SETTINGS iceberg_timezone_for_timestamptz='UTC'") == "UTC\n"
assert node.query(f"SELECT timezoneOf(timestamptz) FROM {CATALOG_NAME}.`{root_namespace}.{table_name}` LIMIT 1 SETTINGS iceberg_timezone_for_timestamptz='Europe/Berlin'") == "Europe/Berlin\n"
def test_insert(started_cluster):
node = started_cluster.instances["node1"]
test_ref = f"test_list_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
catalog = load_catalog_impl(started_cluster)
catalog.create_namespace(root_namespace)
create_table(catalog, root_namespace, table_name, DEFAULT_SCHEMA, PartitionSpec(), DEFAULT_SORT_ORDER)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
node.query(f"INSERT INTO {CATALOG_NAME}.`{root_namespace}.{table_name}` VALUES (NULL, 'AAPL', 193.24, 193.31, tuple('bot'));", settings={"allow_experimental_insert_into_iceberg": 1, 'write_full_path_in_iceberg_metadata': 1})
catalog.load_table(f"{root_namespace}.{table_name}")
assert node.query(f"SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`") == "\\N\tAAPL\t193.24\t193.31\t('bot')\n"
node.query(f"INSERT INTO {CATALOG_NAME}.`{root_namespace}.{table_name}` VALUES (NULL, 'Pavel Ivanov (pudge1000-7) pereezhai v amsterdam', 193.24, 193.31, tuple('bot'));", settings={"allow_experimental_insert_into_iceberg": 1, 'write_full_path_in_iceberg_metadata': 1})
assert node.query(f"SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}` ORDER BY ALL") == "\\N\tAAPL\t193.24\t193.31\t('bot')\n\\N\tPavel Ivanov (pudge1000-7) pereezhai v amsterdam\t193.24\t193.31\t('bot')\n"
def test_create(started_cluster):
node = started_cluster.instances["node1"]
test_ref = f"test_list_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
create_clickhouse_iceberg_table(started_cluster, node, root_namespace, table_name, "(x String)")
node.query(f"INSERT INTO {CATALOG_NAME}.`{root_namespace}.{table_name}` VALUES ('AAPL');", settings={"allow_experimental_insert_into_iceberg": 1, 'write_full_path_in_iceberg_metadata': 1})
assert node.query(f"SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`") == "AAPL\n"
def test_drop_table(started_cluster):
node = started_cluster.instances["node1"]
test_ref = f"test_list_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
catalog = load_catalog_impl(started_cluster)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
create_clickhouse_iceberg_table(started_cluster, node, root_namespace, table_name, "(x String)")
assert len(catalog.list_tables(root_namespace)) == 1
drop_clickhouse_iceberg_table(node, root_namespace, table_name)
assert len(catalog.list_tables(root_namespace)) == 0
def test_table_with_slash(started_cluster):
node = started_cluster.instances["node1"]
# pyiceberg at current moment (version 0.9.1) has a bug with table names with slashes
# see https://github.com/apache/iceberg-python/issues/2462
# so we need to encode it manually
table_raw_suffix = "table/foo"
table_encoded_suffix = "table%2Ffoo"
test_ref = f"test_list_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_{table_raw_suffix}"
table_encoded_name = f"{test_ref}_{table_encoded_suffix}"
root_namespace = f"{test_ref}_namespace"
catalog = load_catalog_impl(started_cluster)
catalog.create_namespace(root_namespace)
create_table(catalog, root_namespace, table_name, DEFAULT_SCHEMA, PartitionSpec(), DEFAULT_SORT_ORDER)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
node.query(f"INSERT INTO {CATALOG_NAME}.`{root_namespace}.{table_encoded_name}` VALUES (NULL, 'AAPL', 193.24, 193.31, tuple('bot'));", settings={"allow_experimental_insert_into_iceberg": 1, 'write_full_path_in_iceberg_metadata': 1})
assert node.query(f"SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_encoded_name}`") == "\\N\tAAPL\t193.24\t193.31\t('bot')\n"
def test_cluster_joins(started_cluster):
node = started_cluster.instances["node1"]
test_ref = f"test_join_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
table_name_2 = f"{test_ref}_table_2"
table_name_local = f"{test_ref}_table_local"
root_namespace = f"{test_ref}_namespace"
catalog = load_catalog_impl(started_cluster)
catalog.create_namespace(root_namespace)
schema = Schema(
NestedField(
field_id=1,
name="tag",
field_type=LongType(),
required=False
),
NestedField(
field_id=2,
name="name",
field_type=StringType(),
required=False,
),
)
table = create_table(catalog, root_namespace, table_name, schema,
partition_spec=UNPARTITIONED_PARTITION_SPEC, sort_order=UNSORTED_SORT_ORDER)
data = [{"tag": 1, "name": "John"}, {"tag": 2, "name": "Jack"}]
df = pa.Table.from_pylist(data)
table.append(df)
schema2 = Schema(
NestedField(
field_id=1,
name="id",
field_type=LongType(),
required=False
),
NestedField(
field_id=2,
name="second_name",
field_type=StringType(),
required=False,
),
)
table2 = create_table(catalog, root_namespace, table_name_2, schema2,
partition_spec=UNPARTITIONED_PARTITION_SPEC, sort_order=UNSORTED_SORT_ORDER)
data = [{"id": 1, "second_name": "Dow"}, {"id": 2, "second_name": "Sparrow"}]
df = pa.Table.from_pylist(data)
table2.append(df)
node.query(f"CREATE TABLE `{table_name_local}` (id Int64, second_name String) ENGINE = Memory()")
node.query(f"INSERT INTO `{table_name_local}` VALUES (1, 'Silver'), (2, 'Black')")
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
res = node.query(
f"""
SELECT t1.name,t2.second_name
FROM {CATALOG_NAME}.`{root_namespace}.{table_name}` AS t1
JOIN {CATALOG_NAME}.`{root_namespace}.{table_name_2}` AS t2
ON t1.tag=t2.id
WHERE t1.tag < 10 AND t2.id < 20
ORDER BY ALL
SETTINGS
object_storage_cluster='cluster_simple',
object_storage_cluster_join_mode='local'
"""
)
assert res == "Jack\tSparrow\nJohn\tDow\n"
res = node.query(
f"""
SELECT name
FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
WHERE tag in (
SELECT id
FROM {CATALOG_NAME}.`{root_namespace}.{table_name_2}`
)
ORDER BY ALL
SETTINGS
object_storage_cluster='cluster_simple',
object_storage_cluster_join_mode='local'
"""
)
assert res == "Jack\nJohn\n"
res = node.query(
f"""
SELECT t1.name,t2.second_name
FROM {CATALOG_NAME}.`{root_namespace}.{table_name}` AS t1
JOIN `{table_name_local}` AS t2
ON t1.tag=t2.id
WHERE t1.tag < 10 AND t2.id < 20
ORDER BY ALL
SETTINGS
object_storage_cluster='cluster_simple',
object_storage_cluster_join_mode='local'
"""
)
assert res == "Jack\tBlack\nJohn\tSilver\n"
res = node.query(
f"""
SELECT name
FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`
WHERE tag in (
SELECT id
FROM `{table_name_local}`
)
ORDER BY ALL
SETTINGS
object_storage_cluster='cluster_simple',
object_storage_cluster_join_mode='local'
"""
)
assert res == "Jack\nJohn\n"
res = node.query(
f"""
SELECT t1.name,t2.second_name
FROM {CATALOG_NAME}.`{root_namespace}.{table_name}` AS t1
CROSS JOIN `{table_name_local}` AS t2
WHERE t1.tag < 10 AND t2.id < 20
ORDER BY ALL
SETTINGS
object_storage_cluster='cluster_simple',
object_storage_cluster_join_mode='local'
"""
)
assert res == "Jack\tBlack\nJack\tSilver\nJohn\tBlack\nJohn\tSilver\n"
def test_gcs(started_cluster):
node = started_cluster.instances["node1"]
node.query("SYSTEM ENABLE FAILPOINT database_iceberg_gcs")
node.query(
f"""
DROP DATABASE IF EXISTS {CATALOG_NAME};
SET allow_database_iceberg = 1;
"""
)
with pytest.raises(Exception) as err:
node.query(
f"""
CREATE DATABASE {CATALOG_NAME}
ENGINE = DataLakeCatalog('http://rest:8181/v1', 'gcs', 'dummy')
SETTINGS
catalog_type = 'rest',
warehouse = 'demo',
"""
)
assert "Google cloud storage converts to S3" in str(err.value)
def test_namespace_filter(started_cluster):
node = started_cluster.instances["node1"]
# Use the same table name in all namespaces
table_name = f"table_{uuid.uuid4()}"
table2_name = f"table2_{uuid.uuid4()}"
namespace_prefix = f"namespace_{uuid.uuid4()}_"
catalog = load_catalog_impl(started_cluster)
def create_namespace(suffix):
namespace = f"{namespace_prefix}{suffix}"
catalog.create_namespace(namespace)
create_table(catalog, namespace, table_name, DEFAULT_SCHEMA, PartitionSpec(), DEFAULT_SORT_ORDER)
create_namespace("alpha");
create_namespace("alpha.a1");
create_namespace("alpha.a2");
create_namespace("bravo");
create_namespace("bravo.b1");
create_namespace("charlie");
create_namespace("charlie.c1");
create_namespace("delta");
create_namespace("delta.d1");
create_namespace("delta.d2");
create_namespace("echo");
create_namespace("echo.e1");
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME,
additional_settings={
"namespaces": f"{namespace_prefix}alpha,{namespace_prefix}alpha.a1,{namespace_prefix}bravo,{namespace_prefix}bravo.*,{namespace_prefix}charlie,{namespace_prefix}delta.d1,{namespace_prefix}echo.*"
})
assert node.query(f"SELECT name FROM system.tables WHERE database='{CATALOG_NAME}' ORDER BY name", settings={"show_data_lake_catalogs_in_system_tables": 1}) == TSV(
[
[f"{namespace_prefix}alpha.a1.{table_name}"],
[f"{namespace_prefix}alpha.{table_name}"],
[f"{namespace_prefix}bravo.b1.{table_name}"],
[f"{namespace_prefix}bravo.{table_name}"],
[f"{namespace_prefix}charlie.{table_name}"],
[f"{namespace_prefix}delta.d1.{table_name}"],
[f"{namespace_prefix}echo.e1.{table_name}"],
])
assert node.query(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}alpha.{table_name}`") == "0\n"
assert node.query(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}alpha.a1.{table_name}`") == "0\n"
assert "is filtered by `namespaces` database parameter." in node.query_and_get_error(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}alpha.a2.{table_name}`")
assert node.query(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}bravo.{table_name}`") == "0\n"
assert node.query(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}bravo.b1.{table_name}`") == "0\n"
assert node.query(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}charlie.{table_name}`") == "0\n"
assert "is filtered by `namespaces` database parameter." in node.query_and_get_error(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}charlie.c1.{table_name}`")
assert "is filtered by `namespaces` database parameter." in node.query_and_get_error(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}delta.{table_name}`")
assert node.query(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}delta.d1.{table_name}`") == "0\n"
assert "is filtered by `namespaces` database parameter." in node.query_and_get_error(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}delta.d2.{table_name}`")
assert "is filtered by `namespaces` database parameter." in node.query_and_get_error(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}echo.{table_name}`")
assert node.query(f"SELECT count() FROM {CATALOG_NAME}.`{namespace_prefix}echo.e1.{table_name}`") == "0\n"
node.query(f"CREATE TABLE {CATALOG_NAME}.`{namespace_prefix}alpha.{table2_name}` (x String) ENGINE = IcebergS3('http://minio:9000/warehouse-rest/{namespace_prefix}alpha/{table2_name}/', '{minio_access_key}', '{minio_secret_key}')")
node.query(f"CREATE TABLE {CATALOG_NAME}.`{namespace_prefix}alpha.a1.{table2_name}` (x String) ENGINE = IcebergS3('http://minio:9000/warehouse-rest/{namespace_prefix}alpha/a1/{table2_name}/', '{minio_access_key}', '{minio_secret_key}')")
assert "is filtered by `namespaces` database parameter." in node.query_and_get_error(f"CREATE TABLE {CATALOG_NAME}.`{namespace_prefix}alpha.a2.{table2_name}` (x String) ENGINE = IcebergS3('http://minio:9000/warehouse-rest/{namespace_prefix}alpha/a2/{table2_name}/', '{minio_access_key}', '{minio_secret_key}')")
node.query(f"DROP TABLE {CATALOG_NAME}.`{namespace_prefix}alpha.{table_name}`")
node.query(f"DROP TABLE {CATALOG_NAME}.`{namespace_prefix}alpha.a1.{table_name}`")
assert "is filtered by `namespaces` database parameter." in node.query_and_get_error(f"DROP TABLE {CATALOG_NAME}.`{namespace_prefix}alpha.a2.{table_name}`")
def test_cluster_select(started_cluster):
node1 = started_cluster.instances["node1"]
node2 = started_cluster.instances["node2"]
test_ref = f"test_list_tables_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
catalog = load_catalog_impl(started_cluster)
create_clickhouse_iceberg_database(started_cluster, node1, CATALOG_NAME)
create_clickhouse_iceberg_database(started_cluster, node2, CATALOG_NAME)
create_clickhouse_iceberg_table(started_cluster, node1, root_namespace, table_name, "(x String)")
node1.query(f"INSERT INTO {CATALOG_NAME}.`{root_namespace}.{table_name}` VALUES ('pablo');", settings={"allow_experimental_insert_into_iceberg": 1, 'write_full_path_in_iceberg_metadata': 1})
query_id = uuid.uuid4().hex
assert node1.query(f"SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}` SETTINGS parallel_replicas_for_cluster_engines=1, enable_parallel_replicas=2, cluster_for_parallel_replicas='cluster_simple'", query_id=query_id) == 'pablo\n'
node1.query("SYSTEM FLUSH LOGS system.query_log")
node2.query("SYSTEM FLUSH LOGS system.query_log")
assert node1.query(f"SELECT Settings['parallel_replicas_for_cluster_engines'] AS parallel_replicas_for_cluster_engines FROM system.query_log WHERE query_id = '{query_id}' LIMIT 1;") == '1\n'
for replica in [node1, node2]:
cluster_secondary_queries = (
replica.query(
f"""
SELECT query, type, is_initial_query, read_rows, read_bytes FROM system.query_log
WHERE
type = 'QueryStart' AND
positionCaseInsensitive(query, 's3Cluster') != 0 AND
position(query, 'system.query_log') = 0 AND
NOT is_initial_query AND
initial_query_id = '{query_id}'
"""
)
.strip()
.split("\n")
)
assert len(cluster_secondary_queries) == 1
assert node2.query(f"SELECT * FROM {CATALOG_NAME}.`{root_namespace}.{table_name}`", settings={"parallel_replicas_for_cluster_engines":1, 'enable_parallel_replicas': 2, 'cluster_for_parallel_replicas': 'cluster_simple', 'parallel_replicas_for_cluster_engines' : 1}) == 'pablo\n'
@pytest.mark.parametrize("storage_type", ["s3"])
def test_partitioning_by_time(started_cluster, storage_type):
node = started_cluster.instances["node1"]
test_ref = f"test_partitioning_by_time_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
namespace = f"{root_namespace}.A"
catalog = load_catalog_impl(started_cluster)
catalog.create_namespace(namespace)
schema = Schema(
NestedField(
field_id=1,
name="key",
field_type=TimeType(),
required=False
),
NestedField(
field_id=2,
name="value",
field_type=StringType(),
required=False,
),
)
partition_spec = PartitionSpec(
PartitionField(
source_id=1, field_id=1000, transform=IdentityTransform(), name="partition_key"
)
)
table = create_table(catalog, namespace, table_name, schema=schema, partition_spec=partition_spec)
data = [{"key": dtime(12,0,0), "value": "test"}]
df = pa.Table.from_pylist(data)
table.append(df)
create_clickhouse_iceberg_database(started_cluster, node, CATALOG_NAME)
assert node.query(f"SELECT * FROM {CATALOG_NAME}.`{namespace}.{table_name}`") == "12:00:00.000000\ttest\n"
@pytest.mark.parametrize("storage_type", ["s3"])
def test_partitioning_by_string(started_cluster, storage_type):
node = started_cluster.instances["node1"]
test_ref = f"test_partitioning_by_string_{uuid.uuid4()}"
table_name = f"{test_ref}_table"
root_namespace = f"{test_ref}_namespace"
namespace = f"{root_namespace}.A"
catalog = load_catalog_impl(started_cluster)
catalog.create_namespace(namespace)
schema = Schema(