-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathpostgres.py
More file actions
127 lines (117 loc) · 4.52 KB
/
postgres.py
File metadata and controls
127 lines (117 loc) · 4.52 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
from __future__ import annotations
import logging
import typing as t
from functools import partial
from sqlglot import exp
from sqlmesh.core.engine_adapter.base_postgres import BasePostgresEngineAdapter
from sqlmesh.core.engine_adapter.mixins import (
GetCurrentCatalogFromFunctionMixin,
PandasNativeFetchDFSupportMixin,
RowDiffMixin,
logical_merge,
)
from sqlmesh.core.engine_adapter.shared import set_catalog
from sqlmesh.core.schema_diff import SchemaDiffer
if t.TYPE_CHECKING:
from sqlmesh.core._typing import TableName
from sqlmesh.core.engine_adapter._typing import DF, QueryOrDF
logger = logging.getLogger(__name__)
@set_catalog()
class PostgresEngineAdapter(
BasePostgresEngineAdapter,
PandasNativeFetchDFSupportMixin,
GetCurrentCatalogFromFunctionMixin,
RowDiffMixin,
):
DIALECT = "postgres"
SUPPORTS_INDEXES = True
HAS_VIEW_BINDING = True
CURRENT_CATALOG_EXPRESSION = exp.column("current_catalog")
SUPPORTS_REPLACE_TABLE = False
MAX_IDENTIFIER_LENGTH = 63
SCHEMA_DIFFER = SchemaDiffer(
parameterized_type_defaults={
# DECIMAL without precision is "up to 131072 digits before the decimal point; up to 16383 digits after the decimal point"
exp.DataType.build("DECIMAL", dialect=DIALECT).this: [(131072 + 16383, 16383), (0,)],
exp.DataType.build("CHAR", dialect=DIALECT).this: [(1,)],
exp.DataType.build("TIME", dialect=DIALECT).this: [(6,)],
exp.DataType.build("TIMESTAMP", dialect=DIALECT).this: [(6,)],
},
types_with_unlimited_length={
# all can ALTER to `TEXT`
exp.DataType.build("TEXT", dialect=DIALECT).this: {
exp.DataType.build("VARCHAR", dialect=DIALECT).this,
exp.DataType.build("CHAR", dialect=DIALECT).this,
exp.DataType.build("BPCHAR", dialect=DIALECT).this,
},
# all can ALTER to unparameterized `VARCHAR`
exp.DataType.build("VARCHAR", dialect=DIALECT).this: {
exp.DataType.build("VARCHAR", dialect=DIALECT).this,
exp.DataType.build("CHAR", dialect=DIALECT).this,
exp.DataType.build("BPCHAR", dialect=DIALECT).this,
exp.DataType.build("TEXT", dialect=DIALECT).this,
},
# parameterized `BPCHAR(n)` can ALTER to unparameterized `BPCHAR`
exp.DataType.build("BPCHAR", dialect=DIALECT).this: {
exp.DataType.build("BPCHAR", dialect=DIALECT).this
},
},
drop_cascade=True,
)
def _fetch_native_df(
self, query: t.Union[exp.Expression, str], quote_identifiers: bool = False
) -> DF:
"""
`read_sql_query` when using psycopg will result on a hanging transaction that must be committed
https://github.com/pandas-dev/pandas/pull/42277
"""
df = super()._fetch_native_df(query, quote_identifiers)
if not self._connection_pool.is_transaction_active:
self._connection_pool.commit()
return df
def create_table_like(
self,
target_table_name: TableName,
source_table_name: TableName,
exists: bool = True,
**kwargs: t.Any,
) -> None:
self.execute(
exp.Create(
this=exp.Schema(
this=exp.to_table(target_table_name),
expressions=[
exp.LikeProperty(
this=exp.to_table(source_table_name),
expressions=[exp.Property(this="INCLUDING", value=exp.Var(this="ALL"))],
)
],
),
kind="TABLE",
exists=exists,
)
)
def merge(
self,
target_table: TableName,
source_table: QueryOrDF,
columns_to_types: t.Optional[t.Dict[str, exp.DataType]],
unique_key: t.Sequence[exp.Expression],
when_matched: t.Optional[exp.Whens] = None,
merge_filter: t.Optional[exp.Expression] = None,
**kwargs: t.Any,
) -> None:
# Merge isn't supported until Postgres 15
merge_impl = (
super().merge
if self._connection_pool.get().server_version >= 150000
else partial(logical_merge, self)
)
merge_impl( # type: ignore
target_table,
source_table,
columns_to_types,
unique_key,
when_matched=when_matched,
merge_filter=merge_filter,
)