-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcolumns.py
More file actions
643 lines (565 loc) · 24.1 KB
/
columns.py
File metadata and controls
643 lines (565 loc) · 24.1 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
from testflows.core import *
from testflows.asserts import error
from helpers.common import getuid
from s3.tests.export_part.steps import *
from s3.tests.export_partition.steps import (
create_table_with_json_column,
create_table_with_json_column_with_hints,
create_table_with_nested_column,
create_table_with_complex_nested_column,
escape_json_for_sql,
)
from helpers.create import *
from helpers.queries import *
from s3.requirements.export_part import *
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_Alias("1.0"))
def alias_columns(self):
"""Test that ALIAS columns are materialized during export and exported correctly."""
with Given("I create a source table with ALIAS columns and empty S3 table"):
source_table = f"source_{getuid()}"
create_merge_tree_table(
table_name=source_table,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "arr", "type": "Array(UInt64)"},
{"name": "arr_1", "type": "UInt64", "alias": "arr[1]"},
],
partition_by="p",
stop_merges=True,
)
s3_table_name = create_s3_table(
table_name="s3",
create_new_bucket=True,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "arr", "type": "Array(UInt64)"},
{"name": "arr_1", "type": "UInt64"},
],
partition_by="p",
)
with And("I insert data and export parts"):
self.context.node.query(
f"INSERT INTO {source_table} (p, i, arr) VALUES (1, 100, [10, 20, 30]), (2, 200, [40, 50, 60])",
exitcode=0,
steps=True,
)
export_parts(source_table=source_table, destination_table=s3_table_name)
wait_for_all_exports_to_complete()
with Then("I verify ALIAS column values are computed correctly in source"):
source_data = self.context.node.query(
f"SELECT p, i, arr, arr_1 FROM {source_table} ORDER BY p",
exitcode=0,
steps=True,
).output.strip()
assert "1\t100\t[10,20,30]\t10" in source_data, error()
assert "2\t200\t[40,50,60]\t40" in source_data, error()
with And("I verify ALIAS column is exported as regular column"):
verify_column_in_destination(table_name=s3_table_name, column_name="arr_1")
verify_exported_data_matches_with_columns(
source_table=source_table,
destination_table=s3_table_name,
columns="p, i, arr, arr_1",
)
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_Materialized("1.0"))
def materialized_columns(self):
"""Test that MATERIALIZED columns are exported correctly with stored computed values."""
with Given("I create a source table with MATERIALIZED columns and empty S3 table"):
source_table = f"source_{getuid()}"
create_merge_tree_table(
table_name=source_table,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "tripled", "type": "UInt64", "materialized": "i * 3"},
],
partition_by="p",
stop_merges=True,
)
source_columns = get_columns_with_kind(table_name=source_table)
s3_columns = [
{"name": col["name"], "type": col["type"]}
for col in source_columns
if col.get("default_kind", "") != "Ephemeral"
]
s3_table_name = create_s3_table(
table_name="s3",
create_new_bucket=True,
columns=s3_columns,
partition_by="p",
)
with And("I insert data and export parts"):
self.context.node.query(
f"INSERT INTO {source_table} (p, i) VALUES (1, 10), (2, 20), (3, 30)",
exitcode=0,
steps=True,
)
export_parts(source_table=source_table, destination_table=s3_table_name)
wait_for_all_exports_to_complete()
with Then("I verify MATERIALIZED column values are stored correctly"):
source_data = self.context.node.query(
f"SELECT p, i, tripled FROM {source_table} ORDER BY p",
exitcode=0,
steps=True,
).output.strip()
assert "1\t10\t30" in source_data, error()
assert "2\t20\t60" in source_data, error()
assert "3\t30\t90" in source_data, error()
with And("I verify exported data matches source including MATERIALIZED columns"):
verify_exported_data_matches_with_columns(
source_table=source_table,
destination_table=s3_table_name,
columns="p, i, tripled",
)
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_Ephemeral("1.0"))
def ephemeral_columns(self):
"""Test that EPHEMERAL columns are ignored during export and not present in destination table schema."""
with Given("I create a source table with EPHEMERAL columns and empty S3 table"):
source_table = f"source_{getuid()}"
create_merge_tree_table(
table_name=source_table,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "unhexed", "type": "String", "ephemeral": ""},
{
"name": "hexed",
"type": "FixedString(4)",
"default": "unhex(unhexed)",
},
],
partition_by="p",
stop_merges=True,
)
s3_table_name = create_s3_table(
table_name="s3",
create_new_bucket=True,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "hexed", "type": "FixedString(4)"},
],
partition_by="p",
)
with And("I insert data and export parts"):
self.context.node.query(
f"INSERT INTO {source_table} (p, i, unhexed) VALUES (1, 100, '5a90b714'), (2, 200, 'deadbeef')",
exitcode=0,
steps=True,
)
export_parts(source_table=source_table, destination_table=s3_table_name)
wait_for_all_exports_to_complete()
with Then("I verify EPHEMERAL column is not in destination schema"):
verify_column_not_in_destination(
table_name=s3_table_name, column_name="unhexed"
)
with And("I verify exported data matches source (excluding EPHEMERAL columns)"):
verify_exported_data_matches_with_columns(
source_table=source_table,
destination_table=s3_table_name,
columns="p, i, hexed",
)
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_Default("1.0"))
def default_columns_with_default_values(self):
"""Test that DEFAULT columns are materialized during export when using default values."""
with Given("I create a source table with DEFAULT columns and empty S3 table"):
source_table = f"source_{getuid()}"
create_merge_tree_table(
table_name=source_table,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "status", "type": "String", "default": "'active'"},
{"name": "created_at", "type": "DateTime", "default": "now()"},
],
partition_by="p",
stop_merges=True,
)
source_columns = get_columns_with_kind(table_name=source_table)
s3_columns = [
{"name": col["name"], "type": col["type"]}
for col in source_columns
if col.get("default_kind", "") != "Ephemeral"
]
s3_table_name = create_s3_table(
table_name="s3",
create_new_bucket=True,
columns=s3_columns,
partition_by="p",
)
with And("I insert data without specifying DEFAULT columns and export"):
self.context.node.query(
f"INSERT INTO {source_table} (p, i) VALUES (1, 100), (2, 200)",
exitcode=0,
steps=True,
)
export_parts(source_table=source_table, destination_table=s3_table_name)
wait_for_all_exports_to_complete()
with Then("I verify DEFAULT values are used in source table"):
source_data = self.context.node.query(
f"SELECT p, i, status FROM {source_table} ORDER BY p",
exitcode=0,
steps=True,
).output.strip()
assert "1\t100\tactive" in source_data, error()
assert "2\t200\tactive" in source_data, error()
with And(
"I verify exported data matches source including materialized DEFAULT values"
):
source_matches_destination(
source_table=source_table,
destination_table=s3_table_name,
)
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_Default("1.0"))
def default_columns_with_explicit_values(self):
"""Test that DEFAULT columns are materialized during export when explicit values are provided."""
with Given("I create a source table with DEFAULT columns and empty S3 table"):
source_table = f"source_{getuid()}"
create_merge_tree_table(
table_name=source_table,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "status", "type": "String", "default": "'active'"},
],
partition_by="p",
stop_merges=True,
)
source_columns = get_columns_with_kind(table_name=source_table)
s3_columns = [
{"name": col["name"], "type": col["type"]}
for col in source_columns
if col.get("default_kind", "") != "Ephemeral"
]
s3_table_name = create_s3_table(
table_name="s3",
create_new_bucket=True,
columns=s3_columns,
partition_by="p",
)
with And("I insert data with explicit values and export"):
self.context.node.query(
f"INSERT INTO {source_table} (p, i, status) VALUES (1, 100, 'inactive'), (2, 200, 'pending')",
exitcode=0,
steps=True,
)
export_parts(source_table=source_table, destination_table=s3_table_name)
wait_for_all_exports_to_complete()
with Then("I verify explicit values are stored correctly"):
source_data = self.context.node.query(
f"SELECT p, i, status FROM {source_table} ORDER BY p",
exitcode=0,
steps=True,
).output.strip()
assert "1\t100\tinactive" in source_data, error()
assert "2\t200\tpending" in source_data, error()
with And("I verify exported data matches source with materialized explicit values"):
source_matches_destination(
source_table=source_table,
destination_table=s3_table_name,
)
@TestScenario
@Requirements(
RQ_ClickHouse_ExportPart_ColumnTypes_Alias("1.0"),
RQ_ClickHouse_ExportPart_ColumnTypes_Materialized("1.0"),
RQ_ClickHouse_ExportPart_ColumnTypes_Default("1.0"),
RQ_ClickHouse_ExportPart_ColumnTypes_Ephemeral("1.0"),
)
def mixed_columns(self):
"""Test that tables with ALIAS, MATERIALIZED, DEFAULT, and EPHEMERAL columns together export correctly."""
with Given("I create a source table with mixed column types and empty S3 table"):
source_table = f"source_{getuid()}"
create_merge_tree_table(
table_name=source_table,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "tag_input", "type": "String", "ephemeral": ""},
{"name": "doubled", "type": "UInt64", "alias": "i * 2"},
{"name": "tripled", "type": "UInt64", "materialized": "i * 3"},
{"name": "tag", "type": "String", "default": "upper(tag_input)"},
],
partition_by="p",
stop_merges=True,
)
s3_table_name = create_s3_table(
table_name="s3",
create_new_bucket=True,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "i", "type": "UInt64"},
{"name": "doubled", "type": "UInt64"},
{"name": "tripled", "type": "UInt64"},
{"name": "tag", "type": "String"},
],
partition_by="p",
)
with And("I insert data and export parts"):
self.context.node.query(
f"INSERT INTO {source_table} (p, i, tag_input) VALUES (1, 10, 'test1'), (2, 20, 'test2')",
exitcode=0,
steps=True,
)
export_parts(source_table=source_table, destination_table=s3_table_name)
wait_for_all_exports_to_complete()
with Then("I verify computed columns are correct in source"):
source_data = self.context.node.query(
f"SELECT p, i, doubled, tripled FROM {source_table} ORDER BY p",
exitcode=0,
steps=True,
).output.strip()
assert "1\t10\t20\t30" in source_data, error()
assert "2\t20\t40\t60" in source_data, error()
with And("I verify EPHEMERAL column is not in destination schema"):
verify_column_not_in_destination(
table_name=s3_table_name, column_name="tag_input"
)
with And(
"I verify ALIAS, MATERIALIZED, and DEFAULT columns are exported as regular columns"
):
verify_exported_data_matches_with_columns(
source_table=source_table,
destination_table=s3_table_name,
columns="p, i, doubled, tripled, tag",
)
@TestScenario
@Requirements(
RQ_ClickHouse_ExportPart_ColumnTypes_Alias("1.0"),
RQ_ClickHouse_ExportPart_ColumnTypes_Materialized("1.0"),
)
def complex_expressions(self):
"""Test that complex expressions in ALIAS and MATERIALIZED columns are materialized and exported correctly."""
with Given("I create a source table with complex expressions and empty S3 table"):
source_table = f"source_{getuid()}"
create_merge_tree_table(
table_name=source_table,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "id", "type": "UInt64"},
{"name": "name", "type": "String"},
{"name": "upper_name", "type": "String", "alias": "upper(name)"},
{
"name": "concat_result",
"type": "String",
"materialized": "concat(name, '-', toString(id))",
},
],
partition_by="p",
stop_merges=True,
)
s3_table_name = create_s3_table(
table_name="s3",
create_new_bucket=True,
columns=[
{"name": "p", "type": "UInt8"},
{"name": "id", "type": "UInt64"},
{"name": "name", "type": "String"},
{"name": "upper_name", "type": "String"},
{"name": "concat_result", "type": "String"},
],
partition_by="p",
)
with And("I insert data and export parts"):
self.context.node.query(
f"INSERT INTO {source_table} (p, id, name) VALUES (1, 100, 'Alice'), (2, 200, 'Bob')",
exitcode=0,
steps=True,
)
export_parts(source_table=source_table, destination_table=s3_table_name)
wait_for_all_exports_to_complete()
with Then("I verify computed columns are correct in source"):
source_data = self.context.node.query(
f"SELECT p, id, name, upper_name, concat_result FROM {source_table} ORDER BY p",
exitcode=0,
steps=True,
).output.strip()
assert "1\t100\tAlice\tALICE\tAlice-100" in source_data, error()
assert "2\t200\tBob\tBOB\tBob-200" in source_data, error()
with And(
"I verify exported data matches source including materialized ALIAS and MATERIALIZED columns"
):
verify_exported_data_matches_with_columns(
source_table=source_table,
destination_table=s3_table_name,
columns="p, id, name, upper_name, concat_result",
)
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_JSON("1.0"))
def json_columns(self):
"""Check that JSON columns are properly exported when exporting parts."""
with Given("I create a source table with JSON column and S3 destination table"):
table_name = f"mt_json_{getuid()}"
create_table_with_json_column(table_name=table_name)
s3_table_name = create_s3_table(
table_name="s3_json",
create_new_bucket=True,
columns=[
{"name": "id", "type": "UInt32"},
{"name": "json_data", "type": "JSON"},
],
partition_by="id",
)
json1_escaped = escape_json_for_sql({"a": {"b": 42}, "c": [1, 2, 3]})
json2_escaped = escape_json_for_sql({"d": "Hello", "e": 100})
insert_query = f"INSERT INTO {table_name} (id, json_data) VALUES (1, '{json1_escaped}'), (1, '{json2_escaped}')"
with By("inserting data into the source table"):
self.context.node.query(insert_query, use_file=True)
with And("exporting parts to the S3 table"):
export_parts(
source_table=table_name,
destination_table=s3_table_name,
)
with And("verifying JSON column data exported to S3 matches source"):
for retry in retries(timeout=35, delay=5):
with retry:
source_data = select_all_ordered(
table_name=table_name,
order_by="id",
)
destination_data = select_all_ordered(
table_name=s3_table_name,
order_by="id",
)
assert source_data == destination_data, error()
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_JSON("1.0"))
def json_columns_with_hints(self):
"""Check that JSON columns with type hints are properly exported when exporting parts."""
with Given(
"I create a source table with JSON column (with hints) and S3 destination table"
):
table_name = f"mt_json_hints_{getuid()}"
create_table_with_json_column_with_hints(table_name=table_name)
s3_table_name = create_s3_table(
table_name="s3_json_hints",
create_new_bucket=True,
columns=[
{"name": "id", "type": "UInt32"},
{"name": "json_data", "type": "JSON(a.b UInt32, a.c String)"},
],
partition_by="id",
)
json1_escaped = escape_json_for_sql({"a": {"b": 42, "c": "test"}})
json2_escaped = escape_json_for_sql({"a": {"b": 100, "c": "world"}})
insert_query = f"INSERT INTO {table_name} (id, json_data) VALUES (1, '{json1_escaped}'), (1, '{json2_escaped}')"
with By("inserting data into the source table"):
self.context.node.query(insert_query, use_file=True)
with And("exporting parts to the S3 table"):
export_parts(
source_table=table_name,
destination_table=s3_table_name,
)
with And("verifying JSON column data with hints exported to S3 matches source"):
for retry in retries(timeout=35, delay=5):
with retry:
source_data = select_all_ordered(
table_name=table_name,
order_by="id",
)
destination_data = select_all_ordered(
table_name=s3_table_name,
order_by="id",
)
assert source_data == destination_data, error()
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_Nested("1.0"))
def nested_columns(self):
"""Check that Nested columns are properly exported when exporting parts."""
with Given("I create a source table with Nested column and S3 destination table"):
table_name = f"mt_nested_{getuid()}"
create_table_with_nested_column(table_name=table_name)
s3_table_name = create_s3_table(
table_name="s3_nested",
create_new_bucket=True,
columns=[
{"name": "id", "type": "UInt32"},
{"name": "nested_data", "type": "Nested(key String, value UInt64)"},
],
partition_by="id",
)
insert_query = f"INSERT INTO {table_name} (id, nested_data.key, nested_data.value) VALUES (1, ['key1', 'key2'], [10, 20]), (1, ['key3'], [30])"
with By("inserting data into the source table"):
self.context.node.query(insert_query, use_file=True)
with And("exporting parts to the S3 table"):
export_parts(
source_table=table_name,
destination_table=s3_table_name,
)
with And("verifying Nested column data exported to S3 matches source"):
for retry in retries(timeout=35, delay=5):
with retry:
source_data = select_all_ordered(
table_name=table_name,
identifier="id, nested_data.key, nested_data.value",
order_by="id, nested_data.key",
)
destination_data = select_all_ordered(
table_name=s3_table_name,
identifier="id, nested_data.key, nested_data.value",
order_by="id, nested_data.key",
)
assert source_data == destination_data, error()
@TestScenario
@Requirements(RQ_ClickHouse_ExportPart_ColumnTypes_Nested("1.0"))
def complex_nested_columns(self):
"""Check that complex Nested columns (with arrays) are properly exported when exporting parts."""
with Given(
"I create a source table with complex Nested column and S3 destination table"
):
table_name = f"mt_nested_complex_{getuid()}"
create_table_with_complex_nested_column(table_name=table_name)
s3_table_name = create_s3_table(
table_name="s3_nested_complex",
create_new_bucket=True,
columns=[
{"name": "id", "type": "UInt32"},
{
"name": "nested_data",
"type": "Nested(name String, age UInt8, scores Array(UInt32))",
},
],
partition_by="id",
)
insert_query = f"INSERT INTO {table_name} (id, nested_data.name, nested_data.age, nested_data.scores) VALUES (1, ['Alice', 'Bob'], [25, 30], [[100, 90], [85, 95]]), (1, ['Charlie'], [35], [[80, 90, 100]])"
with By("inserting data into the source table"):
self.context.node.query(insert_query, use_file=True)
with And("exporting parts to the S3 table"):
export_parts(
source_table=table_name,
destination_table=s3_table_name,
)
with And("verifying complex Nested column data exported to S3 matches source"):
for retry in retries(timeout=35, delay=5):
with retry:
source_data = select_all_ordered(
table_name=table_name,
identifier="id, nested_data.name, nested_data.age, nested_data.scores",
order_by="id, nested_data.name",
)
destination_data = select_all_ordered(
table_name=s3_table_name,
identifier="id, nested_data.name, nested_data.age, nested_data.scores",
order_by="id, nested_data.name",
)
assert source_data == destination_data, error()
@TestFeature
@Name("columns")
def feature(self):
"""Check export part functionality with different column types: ALIAS, MATERIALIZED, EPHEMERAL, DEFAULT, JSON, and Nested."""
Scenario(run=alias_columns)
Scenario(run=materialized_columns)
Scenario(run=ephemeral_columns)
Scenario(run=default_columns_with_default_values)
Scenario(run=default_columns_with_explicit_values)
Scenario(run=mixed_columns)
Scenario(run=complex_expressions)
Scenario(run=json_columns)
Scenario(run=json_columns_with_hints)
Scenario(run=nested_columns)
Scenario(run=complex_nested_columns)