-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathtest_duckdb.py
More file actions
138 lines (103 loc) · 4.94 KB
/
test_duckdb.py
File metadata and controls
138 lines (103 loc) · 4.94 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
import typing as t
import pandas as pd # noqa: TID253
import pytest
from pytest_mock.plugin import MockerFixture
from sqlglot import expressions as exp
from sqlglot import parse_one
from sqlmesh.core.engine_adapter import DuckDBEngineAdapter, EngineAdapter
from tests.core.engine_adapter import to_sql_calls
pytestmark = [pytest.mark.duckdb, pytest.mark.engine]
@pytest.fixture
def adapter(duck_conn):
duck_conn.execute("CREATE VIEW tbl AS SELECT 1 AS a")
return DuckDBEngineAdapter(lambda: duck_conn)
def test_create_view(adapter: EngineAdapter, duck_conn):
adapter.create_view("test_view", parse_one("SELECT a FROM tbl")) # type: ignore
adapter.create_view("test_view", parse_one("SELECT a FROM tbl")) # type: ignore
assert duck_conn.execute("SELECT * FROM test_view").fetchall() == [(1,)]
with pytest.raises(Exception):
adapter.create_view("test_view", parse_one("SELECT a FROM tbl"), replace=False) # type: ignore
def test_create_schema(adapter: EngineAdapter, duck_conn):
adapter.create_schema("test_schema")
assert duck_conn.execute(
"SELECT 1 FROM information_schema.schemata WHERE schema_name = 'test_schema'"
).fetchall() == [(1,)]
with pytest.raises(Exception):
adapter.create_schema("test_schema", ignore_if_exists=False, warn_on_error=False)
def test_table_exists(adapter: EngineAdapter, duck_conn):
assert not adapter.table_exists("test_table")
assert adapter.table_exists("tbl")
def test_create_table(adapter: EngineAdapter, duck_conn):
columns_to_types = {
"cola": exp.DataType.build("INT"),
"colb": exp.DataType.build("TEXT"),
}
expected_columns = [
("cola", "INTEGER", "YES", None, None, None),
("colb", "VARCHAR", "YES", None, None, None),
]
adapter.create_table("test_table", columns_to_types)
assert duck_conn.execute("DESCRIBE test_table").fetchall() == expected_columns
adapter.create_table(
"test_table2",
columns_to_types,
storage_format="ICEBERG",
partitioned_by=[exp.to_column("colb")],
)
assert duck_conn.execute("DESCRIBE test_table").fetchall() == expected_columns
def test_replace_query_pandas(adapter: EngineAdapter, duck_conn):
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
adapter.replace_query(
"test_table", df, {"a": exp.DataType.build("long"), "b": exp.DataType.build("long")}
)
pd.testing.assert_frame_equal(adapter.fetchdf("SELECT * FROM test_table"), df)
def test_set_current_catalog(make_mocked_engine_adapter: t.Callable, duck_conn):
adapter = make_mocked_engine_adapter(DuckDBEngineAdapter)
adapter.set_current_catalog("test_catalog")
assert to_sql_calls(adapter) == [
'USE "test_catalog"',
]
def test_temporary_table(make_mocked_engine_adapter: t.Callable, mocker: MockerFixture):
adapter = make_mocked_engine_adapter(DuckDBEngineAdapter)
mocker.patch.object(adapter, "get_current_catalog", return_value="test_catalog")
mocker.patch.object(adapter, "fetchone", return_value=("test_catalog",))
adapter.create_table(
"test_table",
{"a": exp.DataType.build("INT"), "b": exp.DataType.build("INT")},
table_properties={"creatable_type": exp.Column(this=exp.Identifier(this="Temporary"))},
)
assert to_sql_calls(adapter) == [
'CREATE TEMPORARY TABLE IF NOT EXISTS "test_table" ("a" INT, "b" INT)',
]
def test_create_catalog(make_mocked_engine_adapter: t.Callable) -> None:
adapter: DuckDBEngineAdapter = make_mocked_engine_adapter(DuckDBEngineAdapter)
adapter.create_catalog(exp.to_identifier("foo"))
assert to_sql_calls(adapter) == ["ATTACH IF NOT EXISTS 'foo.db' AS \"foo\""]
def test_drop_catalog(make_mocked_engine_adapter: t.Callable) -> None:
adapter: DuckDBEngineAdapter = make_mocked_engine_adapter(DuckDBEngineAdapter)
adapter.drop_catalog(exp.to_identifier("foo"))
assert to_sql_calls(adapter) == ['DETACH DATABASE IF EXISTS "foo"']
def test_ducklake_partitioning(adapter: EngineAdapter, duck_conn, tmp_path):
catalog = "a_ducklake_db"
duck_conn.install_extension("ducklake")
duck_conn.load_extension("ducklake")
duck_conn.execute(
f"ATTACH 'ducklake:{catalog}.ducklake' AS {catalog} (DATA_PATH '{tmp_path}');"
)
# no partitions on catalog creation
partition_info = duck_conn.execute(
f"SELECT * FROM __ducklake_metadata_{catalog}.main.ducklake_partition_info"
).fetchdf()
assert partition_info.empty
adapter.set_current_catalog(catalog)
adapter.create_schema("test_schema")
adapter.create_table(
"test_schema.test_table",
{"a": exp.DataType.build("INT"), "b": exp.DataType.build("INT")},
partitioned_by=[exp.to_column("a"), exp.to_column("b")],
)
# 1 partition after table creation
partition_info = duck_conn.execute(
f"SELECT * FROM __ducklake_metadata_{catalog}.main.ducklake_partition_info"
).fetchdf()
assert partition_info.shape[0] == 1