-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathtest_athena.py
More file actions
575 lines (475 loc) · 20.7 KB
/
test_athena.py
File metadata and controls
575 lines (475 loc) · 20.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
import typing as t
import pytest
from unittest.mock import Mock
from pytest_mock import MockerFixture
import pandas as pd # noqa: TID253
from sqlglot import exp, parse_one
import sqlmesh.core.dialect as d
from sqlmesh.core.engine_adapter import AthenaEngineAdapter
from sqlmesh.core.engine_adapter.shared import DataObject
from sqlmesh.core.model import load_sql_based_model
from sqlmesh.core.model.definition import SqlModel
from sqlmesh.utils.errors import SQLMeshError
from sqlmesh.core.table_diff import TableDiff
from tests.core.engine_adapter import to_sql_calls
pytestmark = [pytest.mark.athena, pytest.mark.engine]
@pytest.fixture
def adapter(make_mocked_engine_adapter: t.Callable) -> AthenaEngineAdapter:
return make_mocked_engine_adapter(AthenaEngineAdapter)
@pytest.fixture
def table_diff(adapter: AthenaEngineAdapter) -> TableDiff:
return TableDiff(
adapter=adapter,
source="source_table",
target="target_table",
on=["id"],
)
@pytest.mark.parametrize(
"config_s3_warehouse_location,table_properties,table,expected_location",
[
# No s3_warehouse_location in config
(None, None, exp.to_table("schema.table"), None),
(None, {}, exp.to_table("schema.table"), None),
(
None,
{"s3_base_location": exp.Literal.string("s3://some/location/")},
exp.to_table("schema.table"),
"s3://some/location/table/",
),
# Location set to bucket
("s3://bucket", None, exp.to_table("schema.table"), "s3://bucket/schema/table/"),
("s3://bucket", {}, exp.to_table("schema.table"), "s3://bucket/schema/table/"),
("s3://bucket", None, exp.to_table("schema.table"), "s3://bucket/schema/table/"),
(
"s3://bucket",
{"s3_base_location": exp.Literal.string("s3://some/location/")},
exp.to_table("schema.table"),
"s3://some/location/table/",
),
("s3://bucket", {}, exp.Table(db=exp.Identifier(this="test")), "s3://bucket/test/"),
# Location set to bucket with prefix
(
"s3://bucket/subpath/",
None,
exp.to_table("schema.table"),
"s3://bucket/subpath/schema/table/",
),
("s3://bucket/subpath/", None, exp.to_table("table"), "s3://bucket/subpath/table/"),
(
"s3://bucket/subpath/",
None,
exp.to_table("catalog.schema.table"),
"s3://bucket/subpath/catalog/schema/table/",
),
(
"s3://bucket/subpath/",
None,
exp.Table(db=exp.Identifier(this="test")),
"s3://bucket/subpath/test/",
),
],
)
def test_table_location(
adapter: AthenaEngineAdapter,
config_s3_warehouse_location: t.Optional[str],
table_properties: t.Optional[t.Dict[str, exp.Expression]],
table: exp.Table,
expected_location: t.Optional[str],
) -> None:
adapter.s3_warehouse_location = config_s3_warehouse_location
if expected_location is None:
with pytest.raises(SQLMeshError, match=r"Cannot figure out location for table.*"):
adapter._table_location_or_raise(table_properties, table)
else:
location = adapter._table_location_or_raise(
table_properties, table
).this.name # extract the unquoted location value from the LocationProperty
assert location == expected_location
if table_properties is not None:
# this get consumed by _table_location because we dont want it to end up in a TBLPROPERTIES clause
assert "s3_base_location" not in table_properties
def test_create_schema(adapter: AthenaEngineAdapter) -> None:
adapter.create_schema("test")
adapter.s3_warehouse_location = "s3://base"
adapter.create_schema("test")
assert to_sql_calls(adapter) == [
"CREATE SCHEMA IF NOT EXISTS `test`",
"CREATE SCHEMA IF NOT EXISTS `test` LOCATION 's3://base/test/'",
]
def test_create_table_hive(adapter: AthenaEngineAdapter) -> None:
expressions = d.parse(
"""
MODEL (
name test_table,
kind FULL,
partitioned_by (cola, colb),
storage_format parquet,
physical_properties (
s3_base_location = 's3://foo',
has_encrypted_data = 'true'
)
);
SELECT 1::timestamp AS cola, 2::varchar as colb, 'foo' as colc;
"""
)
model: SqlModel = t.cast(SqlModel, load_sql_based_model(expressions))
adapter.create_table(
model.name,
target_columns_to_types=model.columns_to_types_or_raise,
table_properties=model.physical_properties,
partitioned_by=model.partitioned_by,
storage_format=model.storage_format,
)
assert to_sql_calls(adapter) == [
"CREATE EXTERNAL TABLE IF NOT EXISTS `test_table` (`colc` STRING) PARTITIONED BY (`cola` TIMESTAMP, `colb` STRING) STORED AS PARQUET LOCATION 's3://foo/test_table/' TBLPROPERTIES ('has_encrypted_data'='true')"
]
def test_create_table_iceberg(adapter: AthenaEngineAdapter) -> None:
expressions = d.parse(
"""
MODEL (
name test_table,
kind FULL,
partitioned_by (colc, bucket(16, cola)),
table_format iceberg,
storage_format parquet,
physical_properties (
s3_base_location = 's3://foo'
)
);
SELECT 1::timestamp AS cola, 2::varchar as colb, 'foo' as colc;
"""
)
model: SqlModel = t.cast(SqlModel, load_sql_based_model(expressions))
adapter.create_table(
model.name,
target_columns_to_types=model.columns_to_types_or_raise,
table_properties=model.physical_properties,
partitioned_by=model.partitioned_by,
table_format=model.table_format,
storage_format=model.storage_format,
)
assert to_sql_calls(adapter) == [
"CREATE TABLE IF NOT EXISTS `test_table` (`cola` TIMESTAMP, `colb` STRING, `colc` STRING) PARTITIONED BY (`colc`, BUCKET(16, `cola`)) LOCATION 's3://foo/test_table/' TBLPROPERTIES ('table_type'='iceberg', 'format'='parquet')"
]
def test_create_table_no_location(adapter: AthenaEngineAdapter) -> None:
expressions = d.parse(
"""
MODEL (
name test_table,
kind FULL
);
SELECT a::int FROM foo;
"""
)
model: SqlModel = t.cast(SqlModel, load_sql_based_model(expressions))
with pytest.raises(SQLMeshError, match=r"Cannot figure out location.*"):
adapter.create_table(
model.name,
target_columns_to_types=model.columns_to_types_or_raise,
table_properties=model.physical_properties,
)
adapter.s3_warehouse_location = "s3://bucket/prefix"
adapter.create_table(
model.name,
target_columns_to_types=model.columns_to_types_or_raise,
table_properties=model.physical_properties,
)
assert to_sql_calls(adapter) == [
"CREATE EXTERNAL TABLE IF NOT EXISTS `test_table` (`a` INT) LOCATION 's3://bucket/prefix/test_table/'",
]
def test_ctas_hive(adapter: AthenaEngineAdapter):
adapter.s3_warehouse_location = "s3://bucket/prefix/"
adapter.ctas(
table_name="foo.bar",
target_columns_to_types={"a": exp.DataType.build("int")},
query_or_df=parse_one("select 1", into=exp.Select),
)
assert to_sql_calls(adapter) == [
'CREATE TABLE IF NOT EXISTS "foo"."bar" WITH (external_location=\'s3://bucket/prefix/foo/bar/\') AS SELECT CAST("a" AS INTEGER) AS "a" FROM (SELECT 1) AS "_subquery"'
]
def test_ctas_iceberg(adapter: AthenaEngineAdapter):
adapter.s3_warehouse_location = "s3://bucket/prefix/"
adapter.ctas(
table_name="foo.bar",
target_columns_to_types={"a": exp.DataType.build("int")},
query_or_df=parse_one("select 1", into=exp.Select),
table_format="iceberg",
)
assert to_sql_calls(adapter) == [
'CREATE TABLE IF NOT EXISTS "foo"."bar" WITH (table_type=\'iceberg\', location=\'s3://bucket/prefix/foo/bar/\', is_external=false) AS SELECT CAST("a" AS INTEGER) AS "a" FROM (SELECT 1) AS "_subquery"'
]
def test_ctas_iceberg_no_specific_location(adapter: AthenaEngineAdapter):
with pytest.raises(SQLMeshError, match=r"Cannot figure out location.*"):
adapter.ctas(
table_name="foo.bar",
target_columns_to_types={"a": exp.DataType.build("int")},
query_or_df=parse_one("select 1", into=exp.Select),
table_properties={"table_type": exp.Literal.string("iceberg")},
)
assert to_sql_calls(adapter) == []
def test_ctas_iceberg_partitioned(adapter: AthenaEngineAdapter):
expressions = d.parse(
"""
MODEL (
name test_table,
kind INCREMENTAL_BY_TIME_RANGE (
time_column business_date
),
table_format iceberg,
start '2025-01-15'
);
SELECT 1::timestamp AS business_date, 2::varchar as colb, 'foo' as colc;
"""
)
model: SqlModel = t.cast(SqlModel, load_sql_based_model(expressions))
adapter.s3_warehouse_location = "s3://bucket/prefix/"
adapter.ctas(
table_name=model.name,
target_columns_to_types=model.columns_to_types,
partitioned_by=model.partitioned_by,
query_or_df=model.ctas_query(),
table_format=model.table_format,
)
assert to_sql_calls(adapter) == [
"""CREATE TABLE IF NOT EXISTS "test_table" WITH (table_type='iceberg', partitioning=ARRAY['business_date'], location='s3://bucket/prefix/test_table/', is_external=false) AS SELECT CAST("business_date" AS TIMESTAMP) AS "business_date", CAST("colb" AS VARCHAR) AS "colb", CAST("colc" AS VARCHAR) AS "colc" FROM (SELECT CAST(1 AS TIMESTAMP) AS "business_date", CAST(2 AS VARCHAR) AS "colb", 'foo' AS "colc" LIMIT 0) AS "_subquery\""""
]
def test_replace_query(adapter: AthenaEngineAdapter, mocker: MockerFixture):
mocker.patch(
"sqlmesh.core.engine_adapter.athena.AthenaEngineAdapter.table_exists", return_value=True
)
mocker.patch(
"sqlmesh.core.engine_adapter.athena.AthenaEngineAdapter._query_table_type",
return_value="iceberg",
)
mocker.patch.object(
adapter,
"_get_data_objects",
return_value=[DataObject(schema="", name="test", type="table")],
)
adapter.replace_query(
table_name="test",
query_or_df=parse_one("select 1 as a", into=exp.Select),
target_columns_to_types={"a": exp.DataType.build("int")},
table_properties={},
)
assert to_sql_calls(adapter) == [
'DELETE FROM "test" WHERE TRUE',
'INSERT INTO "test" ("a") SELECT 1 AS "a"',
]
mocker.patch(
"sqlmesh.core.engine_adapter.athena.AthenaEngineAdapter.table_exists", return_value=False
)
mocker.patch.object(adapter, "_get_data_objects", return_value=[])
adapter.cursor.execute.reset_mock()
adapter._clear_data_object_cache()
adapter.s3_warehouse_location = "s3://foo"
adapter.replace_query(
table_name="test",
query_or_df=parse_one("select 1 as a", into=exp.Select),
target_columns_to_types={"a": exp.DataType.build("int")},
table_properties={},
)
# gets recreated as a Hive table because table_exists=False and nothing in the properties indicates it should be Iceberg
assert to_sql_calls(adapter) == [
'CREATE TABLE IF NOT EXISTS "test" WITH (external_location=\'s3://foo/test/\') AS SELECT CAST("a" AS INTEGER) AS "a" FROM (SELECT 1 AS "a") AS "_subquery"'
]
def test_columns(adapter: AthenaEngineAdapter, mocker: MockerFixture):
mock = mocker.patch(
"pandas.io.sql.read_sql_query",
return_value=pd.DataFrame(
data=[["col1", "int"], ["col2", "varchar"]], columns=["column_name", "data_type"]
),
)
assert adapter.columns("foo.bar") == {
"col1": exp.DataType.build("int"),
"col2": exp.DataType.build("varchar"),
}
assert (
mock.call_args_list[0][0][0]
== """SELECT "column_name", "data_type" FROM "information_schema"."columns" WHERE "table_schema" = 'foo' AND "table_name" = 'bar' ORDER BY "ordinal_position" NULLS FIRST"""
)
def test_truncate_table_iceberg(adapter: AthenaEngineAdapter, mocker: MockerFixture):
mocker.patch.object(
adapter,
"_query_table_type",
return_value="iceberg",
)
mocker.patch.multiple(
adapter, _clear_partition_data=mocker.DEFAULT, _clear_s3_location=mocker.DEFAULT
)
adapter._truncate_table(exp.to_table("foo.bar"))
assert to_sql_calls(adapter) == ['DELETE FROM "foo"."bar" WHERE TRUE']
t.cast(Mock, adapter._clear_partition_data).assert_not_called()
t.cast(Mock, adapter._clear_s3_location).assert_not_called()
def test_truncate_table_hive(adapter: AthenaEngineAdapter, mocker: MockerFixture):
mocker.patch.object(
adapter,
"_query_table_type",
return_value="hive",
)
mocker.patch.object(
adapter,
"_is_hive_partitioned_table",
return_value=False,
)
mocker.patch.object(adapter, "_query_table_s3_location", return_value="s3://foo/bar")
mocker.patch.multiple(
adapter, _clear_partition_data=mocker.DEFAULT, _clear_s3_location=mocker.DEFAULT
)
adapter._truncate_table(exp.to_table("foo.bar"))
assert to_sql_calls(adapter) == []
t.cast(Mock, adapter._clear_partition_data).assert_not_called()
t.cast(Mock, adapter._clear_s3_location).assert_called_with("s3://foo/bar")
def test_truncate_table_hive_partitioned(adapter: AthenaEngineAdapter, mocker: MockerFixture):
mocker.patch.object(
adapter,
"_query_table_type",
return_value="hive",
)
mocker.patch.object(
adapter,
"_is_hive_partitioned_table",
return_value=True,
)
mocker.patch.object(adapter, "_clear_partition_data")
mocker.patch.object(adapter, "_clear_s3_location")
adapter._truncate_table(exp.to_table("foo.bar"))
assert to_sql_calls(adapter) == []
t.cast(Mock, adapter._clear_partition_data).assert_called_with(
exp.to_table("foo.bar"), exp.true()
)
t.cast(Mock, adapter._clear_s3_location).assert_not_called()
def test_create_state_table(adapter: AthenaEngineAdapter):
adapter.s3_warehouse_location = "s3://base"
adapter.create_state_table("_snapshots", {"name": exp.DataType.build("varchar")})
assert to_sql_calls(adapter) == [
"CREATE TABLE IF NOT EXISTS `_snapshots` (`name` STRING) LOCATION 's3://base/_snapshots/' TBLPROPERTIES ('table_type'='iceberg')"
]
def test_drop_partitions_from_metastore_uses_batches(
adapter: AthenaEngineAdapter, mocker: MockerFixture
):
glue_client_mock = mocker.patch.object(AthenaEngineAdapter, "_glue_client", autospec=True)
glue_client_mock.batch_delete_partition.assert_not_called()
partition_values = []
for i in range(63):
partition_values.append([str(i)])
adapter._drop_partitions_from_metastore(
table=exp.table_("foo"), partition_values=partition_values
)
glue_client_mock.batch_delete_partition.assert_called()
# should have been called in batches of 25
calls = glue_client_mock.batch_delete_partition.call_args_list
assert len(calls) == 3
assert len(calls[0][1]["PartitionsToDelete"]) == 25
assert len(calls[1][1]["PartitionsToDelete"]) == 25
assert len(calls[2][1]["PartitionsToDelete"]) == 13
# first call 0-24
assert calls[0][1]["PartitionsToDelete"][0]["Values"][0] == "0"
assert calls[0][1]["PartitionsToDelete"][-1]["Values"][0] == "24"
# second call 25-49
assert calls[1][1]["PartitionsToDelete"][0]["Values"][0] == "25"
assert calls[1][1]["PartitionsToDelete"][-1]["Values"][0] == "49"
# third call 50-62
assert calls[2][1]["PartitionsToDelete"][0]["Values"][0] == "50"
assert calls[2][1]["PartitionsToDelete"][-1]["Values"][0] == "62"
def test_iceberg_partition_transforms(adapter: AthenaEngineAdapter):
expressions = d.parse(
"""
MODEL (
name test_table,
kind FULL,
table_format iceberg,
partitioned_by (month(business_date), bucket(4, colb), colc)
);
SELECT 1::timestamp AS business_date, 2::varchar as colb, 'foo' as colc;
"""
)
model: SqlModel = t.cast(SqlModel, load_sql_based_model(expressions))
assert model.partitioned_by == [
exp.Month(this=exp.column("business_date", quoted=True)),
exp.PartitionedByBucket(
this=exp.column("colb", quoted=True), expression=exp.Literal.number(4)
),
exp.column("colc", quoted=True),
]
adapter.s3_warehouse_location = "s3://bucket/prefix/"
adapter.create_table(
table_name=model.name,
target_columns_to_types=model.columns_to_types_or_raise,
partitioned_by=model.partitioned_by,
table_format=model.table_format,
)
adapter.ctas(
table_name=model.name,
target_columns_to_types=model.columns_to_types_or_raise,
partitioned_by=model.partitioned_by,
query_or_df=model.ctas_query(),
table_format=model.table_format,
)
assert to_sql_calls(adapter) == [
# Hive syntax - create table
"""CREATE TABLE IF NOT EXISTS `test_table` (`business_date` TIMESTAMP, `colb` STRING, `colc` STRING) PARTITIONED BY (MONTH(`business_date`), BUCKET(4, `colb`), `colc`) LOCATION 's3://bucket/prefix/test_table/' TBLPROPERTIES ('table_type'='iceberg')""",
# Trino syntax - CTAS
"""CREATE TABLE IF NOT EXISTS "test_table" WITH (table_type='iceberg', partitioning=ARRAY['MONTH(business_date)', 'BUCKET(colb, 4)', 'colc'], location='s3://bucket/prefix/test_table/', is_external=false) AS SELECT CAST("business_date" AS TIMESTAMP) AS "business_date", CAST("colb" AS VARCHAR) AS "colb", CAST("colc" AS VARCHAR) AS "colc" FROM (SELECT CAST(1 AS TIMESTAMP) AS "business_date", CAST(2 AS VARCHAR) AS "colb", 'foo' AS "colc" LIMIT 0) AS "_subquery\"""",
]
@pytest.mark.parametrize(
"source_format, target_format, expected_temp_format, expect_error",
[
("hive", "hive", None, False),
("iceberg", "hive", None, True), # Expect error for mismatched formats
("hive", "iceberg", None, True), # Expect error for mismatched formats
("iceberg", "iceberg", "iceberg", False),
(None, "iceberg", None, True), # Source doesn't exist or type unknown, target is iceberg
(
"iceberg",
None,
"iceberg",
True,
), # Target doesn't exist or type unknown, source is iceberg
(None, "hive", None, False), # Source doesn't exist or type unknown, target is hive
("hive", None, None, False), # Target doesn't exist or type unknown, source is hive
(None, None, None, False), # Both don't exist or types unknown
],
)
def test_table_diff_temp_table_format(
table_diff: TableDiff,
mocker: MockerFixture,
source_format: t.Optional[str],
target_format: t.Optional[str],
expected_temp_format: t.Optional[str],
expect_error: bool,
):
adapter = t.cast(AthenaEngineAdapter, table_diff.adapter)
# Mock _query_table_type to return specified formats
def mock_query_table_type(table_name: exp.Table) -> t.Optional[str]:
if table_name.name == "source_table":
return source_format
if table_name.name == "target_table":
return target_format
return "hive" # Default for other tables if any
mocker.patch.object(adapter, "_query_table_type", side_effect=mock_query_table_type)
# Mock temp_table to capture kwargs
mock_temp_table = mocker.patch.object(adapter, "temp_table", autospec=True)
mock_temp_table.return_value.__enter__.return_value = exp.to_table("diff_table")
# Mock fetchdf and other calls made within row_diff to avoid actual DB interaction
mocker.patch.object(adapter, "fetchdf", return_value=pd.DataFrame())
mocker.patch.object(adapter, "get_data_objects", return_value=[])
mocker.patch.object(adapter, "columns", return_value={"id": exp.DataType.build("int")})
if expect_error:
with pytest.raises(
SQLMeshError,
match="do not match for Athena. Diffing between different table formats is not supported.",
):
table_diff.row_diff()
mock_temp_table.assert_not_called() # temp_table should not be called if formats mismatch
return
try:
table_diff.row_diff()
except Exception:
pass # We only care about the temp_table call args for non-error cases
mock_temp_table.assert_called_once()
_, called_kwargs = mock_temp_table.call_args
if expected_temp_format:
assert called_kwargs.get("table_format") == expected_temp_format
else:
assert "table_format" not in called_kwargs