-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathtest_create_as_select.py
More file actions
696 lines (558 loc) · 18.5 KB
/
test_create_as_select.py
File metadata and controls
696 lines (558 loc) · 18.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
import pytest
from utils_pytest import *
COMPLEX_SELECT = """
SELECT i AS id,
'pg_lake' AS name,
array['abc','def']::text[],
array[(i::text, i, i::double precision)]::test_createas_schema_1.fully_qual_type[] as custom_arr,
array[(i::text, i, i::double precision)]::not_qual_type[] as custom_arr2,
array[(1, 1), (2, 2), (3, 3)]::map_type.key_int_val_int as map1,
array[('abc', 1), ('def', 2), ('ghi', 3)]::map_type.key_varchar_val_int as map2,
i::NUMERIC(20, 0) AS price,
i::REAL,
not_qual_fn(),
'2024-03-13 13:00:00'::TIMESTAMP,
'2024-03-13 14:00:00+03'::TIMESTAMPTZ as with_tz,
'14:00:00'::TIME as with_time,
'2022-10-6'::date as with_date,
12 as "int quoted",
ARRAY[i, i] as array_int,
1::bit,
'\\x0001'::bytea,
(-12)::oid,
'acd661ca-d18c-42e2-9c4e-61794318935e'::uuid,
'{"hello":"world"}'::json,
'{"hello":"world"}'::jsonb
FROM generate_series(1, 10) i
"""
def test_no_location(s3, pg_conn, extension):
error = run_command(
f"""
CREATE TABLE test_no_location USING iceberg
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert '"location" option is required for pg_lake_iceberg tables' in error
pg_conn.rollback()
def test_unsupported_url(s3, pg_conn, extension):
location = f"test_create_as_select/"
error = run_command(
f"""
CREATE TABLE test_unsupported_url USING iceberg
WITH (location = '{location}')
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert "only s3://, gs://, az://, azure://, abfss://, hf://, and r2:// URLs" in error
pg_conn.rollback()
def test_temp_table(s3, pg_conn, extension, with_default_location):
error = run_command(
f"""
CREATE TEMP TABLE test_temp_table USING iceberg
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert "temporary tables are not allowed" in error
pg_conn.rollback()
def test_matview(s3, pg_conn, extension, with_default_location):
error = run_command(
f"""
CREATE MATERIALIZED VIEW test_matview USING iceberg
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert "materialized views are not allowed" in error
pg_conn.rollback()
def test_with_tablespace(s3, pg_conn, extension, with_default_location):
error = run_command(
f"""
CREATE TABLE test_with_tablespace USING iceberg
TABLESPACE xx
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert "tablespace is not supported" in error
pg_conn.rollback()
def test_with_unsupported_option(s3, pg_conn, extension, with_default_location):
error = run_command(
f"""
CREATE TABLE test_with_unsupported_option USING iceberg
WITH (unsupported_option = 'value')
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert 'invalid option "unsupported_option"' in error
pg_conn.rollback()
def test_as_execute(s3, pg_conn, extension, with_default_location):
run_command(
f"""
PREPARE test_execute(int) AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
WHERE i > $1
""",
pg_conn,
)
error = run_command(
f"""
CREATE TABLE test_as_execute USING iceberg
AS EXECUTE test_execute(5)
""",
pg_conn,
raise_error=False,
)
assert "CREATE TABLE AS EXECUTE is not supported" in error
pg_conn.rollback()
def test_more_than_once_same_cols(s3, pg_conn, extension, with_default_location):
error = run_command(
f"""
CREATE TABLE test_more_than_once_same_cols USING iceberg
AS SELECT i AS id, i AS id
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert 'column "id" specified more than once' in error
pg_conn.rollback()
def test_unlogged_table(s3, pg_conn, extension, with_default_location):
error = run_command(
f"""
CREATE UNLOGGED TABLE test_unlogged_table USING iceberg
AS SELECT i AS id, i AS id
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert "unlogged tables are not allowed" in error
pg_conn.rollback()
def test_basic_iceberg_table(
s3, pg_conn, extension, create_heap_table, with_default_location
):
error = run_command(
f"""
CREATE TABLE test_iceberg_table USING iceberg
AS {COMPLEX_SELECT}
""",
pg_conn,
)
query = "SELECT * FROM test_iceberg_table ORDER BY id"
expected_expression = "ORDER BY"
assert_remote_query_contains_expression(query, expected_expression, pg_conn)
assert_query_results_on_tables(
query, pg_conn, ["test_iceberg_table"], ["heap_table"]
)
pg_conn.rollback()
def test_with_no_data(s3, pg_conn, extension, create_heap_table, with_default_location):
error = run_command(
f"""
CREATE TABLE test_iceberg_table_with_no_data USING iceberg
AS {COMPLEX_SELECT}
WITH NO DATA
""",
pg_conn,
raise_error=False,
)
query = "SELECT * FROM test_iceberg_table_with_no_data LIMIT 1"
result = run_query(query, pg_conn)
assert len(result) == 0
assert_remote_query_contains_expression(query, "LIMIT", pg_conn)
run_command("DROP TABLE test_iceberg_table_with_no_data", pg_conn)
def test_if_not_exists(
s3, pg_conn, extension, create_heap_table, with_default_location
):
run_command(
f"""
CREATE TABLE test_if_not_exists USING iceberg
AS {COMPLEX_SELECT}
""",
pg_conn,
)
run_command(
f"""
CREATE TABLE IF NOT EXISTS test_if_not_exists USING iceberg
AS {COMPLEX_SELECT}
""",
pg_conn,
)
query = "SELECT * FROM test_if_not_exists ORDER BY id"
expected_expression = "ORDER BY"
assert_remote_query_contains_expression(query, expected_expression, pg_conn)
assert_query_results_on_tables(
query, pg_conn, ["test_if_not_exists"], ["heap_table"]
)
run_command("DROP TABLE test_if_not_exists", pg_conn)
def test_already_exists(s3, pg_conn, extension, with_default_location):
run_command(
f"""
CREATE TABLE test_already_exists USING iceberg
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
)
error = run_command(
f"""
CREATE TABLE test_already_exists USING iceberg
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert "already exists" in error
pg_conn.rollback()
def test_select_from_table(
s3, pg_conn, extension, create_heap_table, with_default_location
):
run_command(
f"""
CREATE TABLE test_select_from_table USING iceberg
AS SELECT * FROM heap_table
""",
pg_conn,
)
query = "SELECT * FROM test_select_from_table ORDER BY id"
expected_expression = "ORDER BY"
assert_remote_query_contains_expression(query, expected_expression, pg_conn)
assert_query_results_on_tables(
query, pg_conn, ["test_select_from_table"], ["heap_table"]
)
run_command("DROP TABLE test_select_from_table", pg_conn)
def test_select_from_pg_lake_table(s3, pg_conn, extension, with_default_location):
run_command(
f"""
CREATE TABLE test_table USING iceberg
AS {COMPLEX_SELECT}
""",
pg_conn,
)
run_command(
f"""
CREATE TABLE test_select_from_pg_lake_table USING iceberg
AS SELECT * FROM test_table
""",
pg_conn,
)
query = "SELECT * FROM test_select_from_pg_lake_table ORDER BY id"
expected_expression = "ORDER BY"
assert_remote_query_contains_expression(query, expected_expression, pg_conn)
assert_query_results_on_tables(
query, pg_conn, ["test_select_from_pg_lake_table"], ["test_table"]
)
run_command("DROP TABLE test_table", pg_conn)
run_command("DROP TABLE test_select_from_pg_lake_table", pg_conn)
def test_as_table(s3, pg_conn, extension, create_heap_table, with_default_location):
run_command(
f"""
CREATE TABLE test_as_table USING iceberg
AS TABLE heap_table
""",
pg_conn,
)
query = "SELECT * FROM test_as_table ORDER BY id"
expected_expression = "ORDER BY"
assert_remote_query_contains_expression(query, expected_expression, pg_conn)
assert_query_results_on_tables(query, pg_conn, ["test_as_table"], ["heap_table"])
run_command("DROP TABLE test_as_table", pg_conn)
pg_conn.rollback()
def test_as_values(s3, pg_conn, extension, with_default_location):
run_command(
f"""
CREATE TABLE test_as_values USING iceberg
AS VALUES (1, 'pg_lake'), (2, 'lake')
""",
pg_conn,
)
query = "SELECT * FROM test_as_values LIMIT 4"
result = run_query(query, pg_conn)
assert len(result) == 2
assert_remote_query_contains_expression(query, "LIMIT", pg_conn)
run_command("DROP TABLE test_as_values", pg_conn)
def test_no_create_access(s3, extension, superuser_conn, with_default_location):
run_command("CREATE USER no_create_table_user", superuser_conn)
run_command("CREATE SCHEMA test_schema", superuser_conn)
run_command(
"REVOKE ALL ON SCHEMA test_schema FROM no_create_table_user", superuser_conn
)
run_command(
"GRANT USAGE ON SCHEMA test_schema TO no_create_table_user", superuser_conn
)
run_command(
"GRANT USAGE ON FOREIGN SERVER pg_lake_iceberg TO no_create_table_user",
superuser_conn,
)
run_command("GRANT lake_read TO no_create_table_user", superuser_conn)
run_command("SET ROLE no_create_table_user", superuser_conn)
error = run_command(
f"""
CREATE TABLE test_schema.test_no_create_access USING iceberg
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
superuser_conn,
raise_error=False,
)
assert "permission denied" in error
superuser_conn.rollback()
def test_with_create_access(s3, superuser_conn, extension):
location = f"s3://{TEST_BUCKET}/test_with_create_access/"
run_command("CREATE SCHEMA test_schema", superuser_conn)
run_command("CREATE USER with_create_table_user", superuser_conn)
run_command(
"GRANT ALL ON SCHEMA test_schema,lake_iceberg TO with_create_table_user",
superuser_conn,
)
run_command(
"GRANT ALL ON TABLE lake_iceberg.tables TO with_create_table_user",
superuser_conn,
)
run_command(
"GRANT USAGE ON FOREIGN SERVER pg_lake_iceberg TO with_create_table_user",
superuser_conn,
)
run_command("GRANT lake_read_write TO with_create_table_user", superuser_conn)
run_command("SET ROLE with_create_table_user", superuser_conn)
run_command(
f"""
CREATE TABLE test_schema.test_with_create_access USING iceberg
WITH (location = '{location}')
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
superuser_conn,
)
result = run_query(
"SELECT COUNT(*) = 10 FROM test_schema.test_with_create_access", superuser_conn
)
assert result[0][0] == True
run_command("RESET ROLE", superuser_conn)
run_command("DROP OWNED BY with_create_table_user", superuser_conn)
run_command("DROP USER with_create_table_user", superuser_conn)
run_command("DROP SCHEMA test_schema CASCADE", superuser_conn)
def test_unusual_table_name(s3, pg_conn, extension, with_default_location):
run_command(
f"""
CREATE TABLE "test_ 7=unuSual_table_name" USING iceberg
AS SELECT i AS id, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
)
result = run_query(
'SELECT COUNT(*) = 10 FROM "test_ 7=unuSual_table_name"', pg_conn
)
assert result[0][0] == True
run_command('DROP TABLE "test_ 7=unuSual_table_name"', pg_conn)
def test_reserved_column_names(s3, pg_conn, extension, with_default_location):
error = run_command(
f"""
CREATE TABLE reserved_column_names USING iceberg
AS SELECT i AS id, 'pg_lake' AS _pg_lake_filename
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert 'column name "_pg_lake_filename" is reserved' in error
pg_conn.rollback()
error = run_command(
f"""
CREATE TABLE reserved_column_names USING iceberg
AS SELECT i AS file_row_number, 'pg_lake' AS name
FROM generate_series(1, 10) i
""",
pg_conn,
raise_error=False,
)
assert 'column name "file_row_number" is reserved' in error
pg_conn.rollback()
def test_pg_lake_iceberg_access_method_unusable(
s3, pg_conn, extension, with_default_location
):
run_command(
f"""
CREATE TABLE test_table(id int)
""",
pg_conn,
)
error = run_command(
f"""
ALTER TABLE test_table SET ACCESS METHOD pg_lake_iceberg
""",
pg_conn,
raise_error=False,
)
assert "not supported" in error
pg_conn.rollback()
def test_default_table_access_method_pg_lake_iceberg(
s3, pg_conn, extension, create_heap_table, with_default_location
):
run_command(
f"""
SET default_table_access_method = 'pg_lake_iceberg'
""",
pg_conn,
)
run_command(
f"""
CREATE TABLE test_default_table_access_method
AS SELECT * FROM heap_table
""",
pg_conn,
)
query = "SELECT * FROM test_default_table_access_method ORDER BY id"
expected_expression = "ORDER BY"
assert_remote_query_contains_expression(query, expected_expression, pg_conn)
assert_query_results_on_tables(
query, pg_conn, ["test_default_table_access_method"], ["heap_table"]
)
run_command("RESET default_table_access_method", pg_conn)
run_command("DROP TABLE test_default_table_access_method", pg_conn)
pg_conn.commit()
def test_default_table_access_method_iceberg(
s3, pg_conn, extension, create_heap_table, with_default_location
):
run_command(
f"""
SET default_table_access_method = 'iceberg'
""",
pg_conn,
)
run_command(
f"""
CREATE TABLE test_default_table_access_method
AS SELECT * FROM heap_table
""",
pg_conn,
)
query = "SELECT * FROM test_default_table_access_method ORDER BY id"
expected_expression = "ORDER BY"
assert_remote_query_contains_expression(query, expected_expression, pg_conn)
assert_query_results_on_tables(
query, pg_conn, ["test_default_table_access_method"], ["heap_table"]
)
run_command("RESET default_table_access_method", pg_conn)
run_command("DROP TABLE test_default_table_access_method", pg_conn)
pg_conn.commit()
@pytest.mark.parametrize("access_method", ["iceberg", "heap"])
def test_ctas_domain_in_nested_types(
s3, pg_conn, extension, with_default_location, access_method
):
"""CREATE TABLE AS must handle domains inside composites, arrays of
composites, and domain-over-domain through the DESCRIBE path for
both iceberg and heap access methods.
"""
schema = "test_ctas_domain_nested"
run_command(
f"""
CREATE SCHEMA {schema};
SET search_path TO {schema};
CREATE DOMAIN d_int AS int;
CREATE DOMAIN d_text AS text;
CREATE DOMAIN dd_text AS d_text;
CREATE TYPE comp AS (a d_int, b dd_text);
""",
pg_conn,
)
using_clause = "USING iceberg" if access_method == "iceberg" else ""
run_command(
f"""
SET search_path TO {schema};
CREATE TABLE tgt {using_clause} AS
SELECT 1 AS id,
ROW(42, 'hello')::comp AS c,
ARRAY[ROW(1, 'a'), ROW(2, 'b')]::comp[] AS ca,
'world'::dd_text AS plain_d;
""",
pg_conn,
)
result = run_query(f"SELECT id, c, ca, plain_d FROM {schema}.tgt", pg_conn)
assert len(result) == 1
assert result[0][0] == 1
assert result[0][1] == "(42,hello)"
assert result[0][2] == '{"(1,a)","(2,b)"}'
assert result[0][3] == "world"
pg_conn.rollback()
# create the table on both Postgres
@pytest.fixture(scope="module")
def create_heap_table(pg_conn, s3, request, create_types):
run_command(
f"""
CREATE TABLE heap_table
AS {COMPLEX_SELECT}
""",
pg_conn,
)
pg_conn.commit()
yield
run_command("DROP TABLE heap_table", pg_conn)
pg_conn.commit()
@pytest.fixture(scope="module")
def create_types(pg_conn, s3, request):
run_command(
f"""
CREATE SCHEMA test_createas_schema_1;
""",
pg_conn,
)
run_command(
f"""
CREATE TYPE test_createas_schema_1.fully_qual_type AS (field1 text, field2 int, field3 double precision);
""",
pg_conn,
)
run_command(
f"""
CREATE SCHEMA test_createas_schema_2;
""",
pg_conn,
)
run_command(
f"""
SET search_path TO test_createas_schema_2, public;
""",
pg_conn,
)
run_command(
f"""
CREATE TYPE not_qual_type AS (field1 text, field2 int, field3 double precision);
""",
pg_conn,
)
run_command(
f"""
CREATE OR REPLACE FUNCTION not_qual_fn() RETURNS double precision AS $$
BEGIN
RETURN 1.0;
END;
$$ LANGUAGE plpgsql;
""",
pg_conn,
)
create_map_type("integer", "integer")
create_map_type("varchar", "integer")
pg_conn.commit()
yield
run_command("RESET search_path", pg_conn)
run_command("DROP SCHEMA test_createas_schema_1 CASCADE", pg_conn)
run_command("DROP SCHEMA test_createas_schema_2 CASCADE", pg_conn)
pg_conn.commit()