-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest_table_via_select.py
More file actions
753 lines (661 loc) · 23.7 KB
/
Copy pathtest_table_via_select.py
File metadata and controls
753 lines (661 loc) · 23.7 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
from sqlalchemy import bindparam
from sqlalchemy import Column
from sqlalchemy import CreateTableAs
from sqlalchemy import CreateView
from sqlalchemy import Integer
from sqlalchemy import literal
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import testing
from sqlalchemy import text
from sqlalchemy.schema import CreateTable
from sqlalchemy.schema import DropView
from sqlalchemy.sql import column
from sqlalchemy.sql import select
from sqlalchemy.sql import table
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_
from sqlalchemy.testing.assertions import AssertsCompiledSQL
from sqlalchemy.testing.assertions import expect_warnings
class TableViaSelectTest(fixtures.TestBase, AssertsCompiledSQL):
__dialect__ = "default"
@testing.fixture
def src_table(self):
return Table(
"src",
MetaData(),
Column("id", Integer),
Column("name", String(50)),
)
@testing.fixture
def src_two_tables(self):
a = table("a", column("id"), column("name"))
b = table("b", column("id"), column("status"))
return a, b
@testing.variation("type_", ["create_table_as", "create_view"])
def test_basic_element(self, src_table, type_: testing.Variation):
src = src_table
if type_.create_table_as:
stmt = CreateTableAs(
select(src.c.id, src.c.name),
"dst",
)
elif type_.create_view:
stmt = CreateView(
select(src.c.id, src.c.name),
"dst",
)
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f"dst AS SELECT src.id, src.name FROM src",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_schema_element_qualified(
self, src_table, type_: testing.Variation
):
src = src_table
if type_.create_table_as:
stmt = CreateTableAs(
select(src.c.id),
"dst",
schema="analytics",
)
elif type_.create_view:
stmt = CreateView(
select(src.c.id),
"dst",
schema="analytics",
)
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f"analytics.dst AS SELECT src.id FROM src",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_quoting(self, type_: testing.Variation):
src = Table(
"SourceTable",
MetaData(),
Column("Some Name", Integer),
Column("Other Col", String),
)
if type_.create_table_as:
stmt = CreateTableAs(
select(src),
"My Analytic Table",
schema="Analysis",
)
elif type_.create_view:
stmt = CreateView(
select(src),
"My Analytic View",
schema="Analysis",
)
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f'"Analysis"."My Analytic '
f'{"Table" if type_.create_table_as else "View"}" AS SELECT '
f'"SourceTable"."Some Name", "SourceTable"."Other Col" '
f'FROM "SourceTable"',
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_blank_schema_treated_as_none(
self, src_table, type_: testing.Variation
):
src = src_table
if type_.create_table_as:
stmt = CreateTableAs(select(src.c.id), "dst", schema="")
elif type_.create_view:
stmt = CreateView(select(src.c.id), "dst", schema="")
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f"dst AS SELECT src.id FROM src",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_binds_rendered_inline(self, src_table, type_: testing.Variation):
src = src_table
if type_.create_table_as:
stmt = CreateTableAs(
select(literal("x").label("tag")).select_from(src),
"dst",
)
elif type_.create_view:
stmt = CreateView(
select(literal("x").label("tag")).select_from(src),
"dst",
)
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f"dst AS SELECT 'x' AS tag FROM src",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_temporary_no_schema(self, src_table, type_: testing.Variation):
src = src_table
if type_.create_table_as:
stmt = CreateTableAs(
select(src.c.id, src.c.name),
"dst",
temporary=True,
)
elif type_.create_view:
stmt = CreateView(
select(src.c.id, src.c.name),
"dst",
temporary=True,
)
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE TEMPORARY {"TABLE" if type_.create_table_as else "VIEW"} '
f"dst AS SELECT src.id, src.name FROM src",
)
@testing.variation("temporary", [True, False])
@testing.variation("if_not_exists", [True, False])
def test_create_table_as_flags(
self,
src_table,
temporary: testing.Variation,
if_not_exists: testing.Variation,
):
src = src_table
stmt = CreateTableAs(
select(src.c.id),
"dst",
schema="sch",
temporary=bool(temporary),
if_not_exists=bool(if_not_exists),
)
self.assert_compile(
stmt,
f"""CREATE {
'TEMPORARY ' if temporary else ''
}TABLE {
'IF NOT EXISTS ' if if_not_exists else ''
}sch.dst AS SELECT src.id FROM src""",
)
def test_temporary_or_replace_create_view(self, src_table):
src = src_table
stmt = CreateView(
select(src.c.id),
"dst",
schema="sch",
temporary=True,
or_replace=True,
)
self.assert_compile(
stmt,
"CREATE OR REPLACE TEMPORARY VIEW sch.dst AS "
"SELECT src.id FROM src",
)
def test_or_replace(self, src_table):
src = src_table
stmt = CreateView(
select(src.c.id, src.c.name),
"dst",
or_replace=True,
)
self.assert_compile(
stmt,
"CREATE OR REPLACE VIEW dst AS SELECT src.id, src.name FROM src",
)
def test_join_with_binds_rendered_inline(self, src_two_tables):
a, b = src_two_tables
s = (
select(a.c.id, a.c.name)
.select_from(a.join(b, a.c.id == b.c.id))
.where(b.c.status == "active")
).into("dst")
# Ensure WHERE survives into CTAS and binds are rendered inline
self.assert_compile(
s,
"CREATE TABLE dst AS "
"SELECT a.id, a.name FROM a JOIN b ON a.id = b.id "
"WHERE b.status = 'active'",
)
def test_into_equivalent_to_element(self, src_table):
src = src_table
s = select(src.c.id).where(src.c.id == 2)
via_into = s.into("dst")
via_element = CreateTableAs(s, "dst")
self.assert_compile(
via_into,
"CREATE TABLE dst AS SELECT src.id FROM src WHERE src.id = 2",
)
self.assert_compile(
via_element,
"CREATE TABLE dst AS SELECT src.id FROM src WHERE src.id = 2",
)
def test_into_does_not_mutate_original_select(self, src_table):
src = src_table
s = select(src.c.id).where(src.c.id == 5)
# compile original SELECT
self.assert_compile(
s,
"SELECT src.id FROM src WHERE src.id = :id_1",
)
# build CTAS
_ = s.into("dst")
# original is still a SELECT
self.assert_compile(
s,
"SELECT src.id FROM src WHERE src.id = :id_1",
)
def test_into_with_schema_argument(self, src_table):
src = src_table
s = select(src.c.id).into("t", schema="analytics")
self.assert_compile(
s,
"CREATE TABLE analytics.t AS SELECT src.id FROM src",
)
@testing.variation("provide_metadata", [True, False])
@testing.variation("type_", ["create_table_as", "create_view"])
def test_generated_metadata_table_property(
self, src_table, provide_metadata, type_: testing.Variation
):
src = src_table
if provide_metadata:
metadata = MetaData()
else:
metadata = None
if type_.create_table_as:
stmt = CreateTableAs(
select(src.c.name.label("thename"), src.c.id),
"dst",
schema="sch",
metadata=metadata,
)
elif type_.create_view:
stmt = CreateView(
select(src.c.name.label("thename"), src.c.id),
"dst",
schema="sch",
metadata=metadata,
)
else:
type_.fail()
if metadata is not None:
is_(stmt.metadata, metadata)
assert isinstance(stmt.table, Table)
is_(stmt.table.metadata, stmt.metadata)
# this is validating the structure of the table but is not
# looking at CreateTable being the appropriate construct
self.assert_compile(
CreateTable(stmt.table),
"CREATE TABLE sch.dst (thename VARCHAR(50), id INTEGER)",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_labels_in_select_list_preserved(
self, src_table, type_: testing.Variation
):
src = src_table
if type_.create_table_as:
stmt = CreateTableAs(
select(
src.c.id.label("user_id"),
src.c.name.label("user_name"),
),
"dst",
)
elif type_.create_view:
stmt = CreateView(
select(
src.c.id.label("user_id"),
src.c.name.label("user_name"),
),
"dst",
)
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f"dst AS SELECT src.id AS user_id, src.name AS user_name FROM src",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_distinct_and_group_by_survive(
self, src_table, type_: testing.Variation
):
src = src_table
sel = select(src.c.name).distinct().group_by(src.c.name)
if type_.create_table_as:
stmt = CreateTableAs(sel, "dst")
elif type_.create_view:
stmt = CreateView(sel, "dst")
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f"dst AS SELECT DISTINCT src.name FROM src GROUP BY src.name",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_bindparam_no_value_raises(
self, src_table, type_: testing.Variation
):
src = src_table
sel = select(src.c.name).where(src.c.name == bindparam("x"))
if type_.create_table_as:
stmt = CreateTableAs(sel, "dst")
elif type_.create_view:
stmt = CreateView(sel, "dst")
else:
type_.fail()
with expect_warnings(
"Bound parameter 'x' rendering literal NULL in a SQL expression;"
):
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f"dst AS SELECT src.name FROM src WHERE src.name = NULL",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_union_all_with_binds_rendered_inline(
self, src_two_tables, type_: testing.Variation
):
a, b = src_two_tables
# Named binds so params are deterministic
s1 = select(a.c.id).where(a.c.id == bindparam("p_a", value=1))
s2 = select(b.c.id).where(b.c.id == bindparam("p_b", value=2))
u_all = s1.union_all(s2)
if type_.create_table_as:
stmt = CreateTableAs(u_all, "dst")
elif type_.create_view:
stmt = CreateView(u_all, "dst")
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} dst AS '
f"SELECT a.id FROM a WHERE a.id = 1 "
f"UNION ALL SELECT b.id FROM b WHERE b.id = 2",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_union_labels_follow_first_select(
self, src_two_tables, type_: testing.Variation
):
# Many engines take column names
# of a UNION from the first SELECT's labels.
a = table("a", column("val"))
b = table("b", column("val"))
s1 = select(a.c.val.label("first_name"))
s2 = select(b.c.val) # unlabeled second branch
u = s1.union(s2)
if type_.create_table_as:
stmt = CreateTableAs(u, "dst")
elif type_.create_view:
stmt = CreateView(u, "dst")
else:
type_.fail()
# We only assert what's stable across dialects:
# - first SELECT has the label
# - a UNION occurs
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} dst AS '
f"SELECT a.val AS first_name FROM a UNION SELECT b.val FROM b",
)
self.assert_compile(
select(stmt.table), "SELECT dst.first_name FROM dst"
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_union_all_with_inlined_literals_smoke(
self, src_two_tables, type_: testing.Variation
):
# Proves literal_binds=True behavior applies across branches.
a, b = src_two_tables
u = (
select(literal(1).label("x"))
.select_from(a)
.union_all(select(literal("b").label("x")).select_from(b))
)
if type_.create_table_as:
stmt = CreateTableAs(u, "dst")
elif type_.create_view:
stmt = CreateView(u, "dst")
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} dst AS '
f"SELECT 1 AS x FROM a UNION ALL SELECT 'b' AS x FROM b",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_select_shape_where_order_limit(
self, src_table, type_: testing.Variation
):
src = src_table
sel = (
select(src.c.id, src.c.name)
.where(src.c.id > literal(10))
.order_by(src.c.name)
.limit(5)
.offset(0)
)
if type_.create_table_as:
stmt = CreateTableAs(sel, "dst")
elif type_.create_view:
stmt = CreateView(sel, "dst")
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} dst AS '
f"SELECT src.id, src.name FROM src "
f"WHERE src.id > 10 ORDER BY src.name LIMIT 5 OFFSET 0",
)
@testing.variation("type_", ["create_table_as", "create_view"])
def test_cte_smoke(self, src_two_tables, type_: testing.Variation):
# Proves CTAS works with a WITH-CTE wrapper and labeled column.
a, _ = src_two_tables
cte = select(a.c.id.label("aid")).cte("u")
if type_.create_table_as:
stmt = CreateTableAs(select(cte.c.aid), "dst")
elif type_.create_view:
stmt = CreateView(select(cte.c.aid), "dst")
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} dst AS '
f"WITH u AS (SELECT a.id AS aid FROM a) SELECT u.aid FROM u",
)
# Verify the created table uses the label name for its column
from sqlalchemy.testing import eq_
eq_(list(stmt.table.c.keys()), ["aid"])
def test_materialized_view_basic(self, src_table):
src = src_table
stmt = CreateView(
select(src.c.id, src.c.name),
"dst",
materialized=True,
)
self.assert_compile(
stmt,
"CREATE MATERIALIZED VIEW dst AS SELECT src.id, src.name FROM src",
)
def test_materialized_view_with_schema(self, src_table):
src = src_table
stmt = CreateView(
select(src.c.id),
"dst",
schema="analytics",
materialized=True,
)
self.assert_compile(
stmt,
"CREATE MATERIALIZED VIEW analytics.dst AS SELECT src.id FROM src",
)
def test_materialized_view_or_replace(self, src_table):
src = src_table
stmt = CreateView(
select(src.c.id, src.c.name),
"dst",
materialized=True,
or_replace=True,
)
self.assert_compile(
stmt,
"CREATE OR REPLACE MATERIALIZED VIEW dst AS "
"SELECT src.id, src.name FROM src",
)
def test_materialized_view_temporary(self, src_table):
src = src_table
stmt = CreateView(
select(src.c.id, src.c.name),
"dst",
materialized=True,
temporary=True,
)
self.assert_compile(
stmt,
"CREATE TEMPORARY MATERIALIZED VIEW dst AS "
"SELECT src.id, src.name FROM src",
)
def test_materialized_view_all_flags(self, src_table):
src = src_table
stmt = CreateView(
select(src.c.id),
"dst",
schema="sch",
materialized=True,
temporary=True,
or_replace=True,
)
self.assert_compile(
stmt,
"CREATE OR REPLACE TEMPORARY MATERIALIZED VIEW "
"sch.dst AS SELECT src.id FROM src",
)
@testing.variation("type_", ["create_table_as", "create_view"])
@testing.variation("use_schema", [True, False])
def test_textual_select(
self, type_: testing.Variation, use_schema: testing.Variation
):
"""Test using text().columns() with CreateView and CreateTableAs.
This is likely how we will get alembic to autogenerate a CreateView()
construct since we dont want to rewrite a whole select() construct
in a migration file.
"""
textual = text(
"SELECT a, b, c FROM source_table WHERE x > 10"
).columns(
column("a", Integer),
column("b", String),
column("c", Integer),
)
schema = "analytics" if use_schema else None
if type_.create_table_as:
stmt = CreateTableAs(textual, "dst", schema=schema)
elif type_.create_view:
stmt = CreateView(textual, "dst", schema=schema)
else:
type_.fail()
self.assert_compile(
stmt,
f'CREATE {"TABLE" if type_.create_table_as else "VIEW"} '
f'{"analytics." if use_schema else ""}dst AS '
f"SELECT a, b, c FROM source_table WHERE x > 10",
)
# Verify the generated table has the correct columns
assert "a" in stmt.table.c
assert "b" in stmt.table.c
assert "c" in stmt.table.c
assert isinstance(stmt.table.c.a.type, Integer)
assert isinstance(stmt.table.c.b.type, String)
assert isinstance(stmt.table.c.c.type, Integer)
@testing.variation("temporary", [True, False])
@testing.variation("if_not_exists", [True, False])
def test_textual_select_with_flags_create_table_as(
self, temporary: testing.Variation, if_not_exists: testing.Variation
):
"""Test TextualSelect with flags for CREATE TABLE AS."""
textual = text("SELECT * FROM temp_data").columns(
column("x", Integer),
column("y", String),
)
stmt = CreateTableAs(
textual,
"snapshot",
temporary=bool(temporary),
if_not_exists=bool(if_not_exists),
)
self.assert_compile(
stmt,
f"""CREATE {
'TEMPORARY ' if temporary else ''
}TABLE {
'IF NOT EXISTS ' if if_not_exists else ''
}snapshot AS SELECT * FROM temp_data""",
)
@testing.variation("temporary", [True, False])
@testing.variation("or_replace", [True, False])
def test_textual_select_with_flags_create_view(
self, temporary: testing.Variation, or_replace: testing.Variation
):
"""Test TextualSelect with flags for CREATE VIEW."""
textual = text("SELECT * FROM temp_data").columns(
column("x", Integer),
column("y", String),
)
stmt = CreateView(
textual,
"snapshot_view",
temporary=bool(temporary),
or_replace=bool(or_replace),
)
self.assert_compile(
stmt,
f"""CREATE {
'OR REPLACE ' if or_replace else ''
}{
'TEMPORARY ' if temporary else ''
}VIEW snapshot_view AS SELECT * FROM temp_data""",
)
def test_drop_view_basic(self):
"""Test basic DROP VIEW compilation."""
src = table("src", column("id"), column("name"))
create_view = CreateView(select(src), "my_view")
view_table = create_view.table
drop_stmt = DropView(view_table)
self.assert_compile(drop_stmt, "DROP VIEW my_view")
def test_drop_view_materialized(self):
"""Test DROP MATERIALIZED VIEW compilation."""
src = table("src", column("id"), column("name"))
create_view = CreateView(select(src), "my_mat_view", materialized=True)
view_table = create_view.table
drop_stmt = DropView(view_table, materialized=True)
self.assert_compile(drop_stmt, "DROP MATERIALIZED VIEW my_mat_view")
def test_drop_view_from_create_view(self):
"""Test that CreateView automatically creates proper DropView."""
src = table("src", column("id"), column("name"))
# Regular view
create_view = CreateView(select(src), "regular_view")
drop_stmt = create_view.table._dropper_ddl
self.assert_compile(drop_stmt, "DROP VIEW regular_view")
def test_drop_materialized_view_from_create_view(self):
"""Test CreateView with materialized=True creates proper DropView."""
src = table("src", column("id"), column("name"))
# Materialized view
create_mat_view = CreateView(
select(src), "materialized_view", materialized=True
)
drop_stmt = create_mat_view.table._dropper_ddl
self.assert_compile(
drop_stmt, "DROP MATERIALIZED VIEW materialized_view"
)