Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit c1bac7f

Browse files
author
Sergey Vasilyev
committed
Squash abstract dialect into simply base dialect
1 parent f213b26 commit c1bac7f

2 files changed

Lines changed: 38 additions & 98 deletions

File tree

data_diff/abcs/database_types.py

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -174,100 +174,6 @@ class UnknownColType(ColType):
174174
supported = False
175175

176176

177-
class AbstractDialect(ABC):
178-
"""Dialect-dependent query expressions"""
179-
180-
@abstractmethod
181-
def compile(self, compiler: AbstractCompiler, elem, params=None) -> str:
182-
raise NotImplementedError
183-
184-
@abstractmethod
185-
def parse_table_name(self, name: str) -> DbPath:
186-
"Parse the given table name into a DbPath"
187-
188-
@property
189-
@abstractmethod
190-
def name(self) -> str:
191-
"Name of the dialect"
192-
193-
@classmethod
194-
@abstractmethod
195-
def load_mixins(cls, *abstract_mixins) -> Self:
196-
"Load a list of mixins that implement the given abstract mixins"
197-
198-
@property
199-
@abstractmethod
200-
def ROUNDS_ON_PREC_LOSS(self) -> bool:
201-
"True if db rounds real values when losing precision, False if it truncates."
202-
203-
@abstractmethod
204-
def quote(self, s: str):
205-
"Quote SQL name"
206-
207-
@abstractmethod
208-
def concat(self, items: List[str]) -> str:
209-
"Provide SQL for concatenating a bunch of columns into a string"
210-
211-
@abstractmethod
212-
def is_distinct_from(self, a: str, b: str) -> str:
213-
"Provide SQL for a comparison where NULL = NULL is true"
214-
215-
@abstractmethod
216-
def to_string(self, s: str) -> str:
217-
# TODO rewrite using cast_to(x, str)
218-
"Provide SQL for casting a column to string"
219-
220-
@abstractmethod
221-
def random(self) -> str:
222-
"Provide SQL for generating a random number betweein 0..1"
223-
224-
@abstractmethod
225-
def current_timestamp(self) -> str:
226-
"Provide SQL for returning the current timestamp, aka now"
227-
228-
@abstractmethod
229-
def current_database(self) -> str:
230-
"Provide SQL for returning the current default database."
231-
232-
@abstractmethod
233-
def current_schema(self) -> str:
234-
"Provide SQL for returning the current default schema."
235-
236-
@abstractmethod
237-
def offset_limit(
238-
self, offset: Optional[int] = None, limit: Optional[int] = None, has_order_by: Optional[bool] = None
239-
) -> str:
240-
"Provide SQL fragment for limit and offset inside a select"
241-
242-
@abstractmethod
243-
def explain_as_text(self, query: str) -> str:
244-
"Provide SQL for explaining a query, returned as table(varchar)"
245-
246-
@abstractmethod
247-
def timestamp_value(self, t: datetime) -> str:
248-
"Provide SQL for the given timestamp value"
249-
250-
@abstractmethod
251-
def set_timezone_to_utc(self) -> str:
252-
"Provide SQL for setting the session timezone to UTC"
253-
254-
@abstractmethod
255-
def parse_type(
256-
self,
257-
table_path: DbPath,
258-
col_name: str,
259-
type_repr: str,
260-
datetime_precision: int = None,
261-
numeric_precision: int = None,
262-
numeric_scale: int = None,
263-
) -> ColType:
264-
"Parse type info as returned by the database"
265-
266-
@abstractmethod
267-
def to_comparable(self, value: str, coltype: ColType) -> str:
268-
"""Ensure that the expression is comparable in ``IS DISTINCT FROM``."""
269-
270-
271177
class AbstractTable(ABC):
272178
@abstractmethod
273179
def select(self, *exprs, distinct=False, **named_exprs) -> "AbstractTable":

data_diff/databases/base.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from data_diff.abcs.database_types import (
3535
Array,
3636
Struct,
37-
AbstractDialect,
3837
AbstractTable,
3938
ColType,
4039
Integer,
@@ -104,7 +103,7 @@ class Compiler(AbstractCompiler):
104103
_counter: List = field(default_factory=lambda: [0])
105104

106105
@property
107-
def dialect(self) -> AbstractDialect:
106+
def dialect(self) -> "Dialect":
108107
return self.database.dialect
109108

110109
# TODO: DEPRECATED: Remove once the dialect is used directly in all places.
@@ -221,7 +220,7 @@ def optimizer_hints(self, hints: str) -> str:
221220
return f"/*+ {hints} */ "
222221

223222

224-
class BaseDialect(AbstractDialect):
223+
class BaseDialect(abc.ABC):
225224
SUPPORTS_PRIMARY_KEY = False
226225
SUPPORTS_INDEXES = False
227226
TYPE_CLASSES: Dict[str, type] = {}
@@ -230,6 +229,7 @@ class BaseDialect(AbstractDialect):
230229
PLACEHOLDER_TABLE = None # Used for Oracle
231230

232231
def parse_table_name(self, name: str) -> DbPath:
232+
"Parse the given table name into a DbPath"
233233
return parse_table_name(name)
234234

235235
def compile(self, compiler: Compiler, elem, params=None) -> str:
@@ -638,12 +638,14 @@ def render_inserttotable(self, c: Compiler, elem: InsertToTable) -> str:
638638
def offset_limit(
639639
self, offset: Optional[int] = None, limit: Optional[int] = None, has_order_by: Optional[bool] = None
640640
) -> str:
641+
"Provide SQL fragment for limit and offset inside a select"
641642
if offset:
642643
raise NotImplementedError("No support for OFFSET in query")
643644

644645
return f"LIMIT {limit}"
645646

646647
def concat(self, items: List[str]) -> str:
648+
"Provide SQL for concatenating a bunch of columns into a string"
647649
assert len(items) > 1
648650
joined_exprs = ", ".join(items)
649651
return f"concat({joined_exprs})"
@@ -653,24 +655,31 @@ def to_comparable(self, value: str, coltype: ColType) -> str:
653655
return value
654656

655657
def is_distinct_from(self, a: str, b: str) -> str:
658+
"Provide SQL for a comparison where NULL = NULL is true"
656659
return f"{a} is distinct from {b}"
657660

658661
def timestamp_value(self, t: DbTime) -> str:
662+
"Provide SQL for the given timestamp value"
659663
return f"'{t.isoformat()}'"
660664

661665
def random(self) -> str:
666+
"Provide SQL for generating a random number betweein 0..1"
662667
return "random()"
663668

664669
def current_timestamp(self) -> str:
670+
"Provide SQL for returning the current timestamp, aka now"
665671
return "current_timestamp()"
666672

667673
def current_database(self) -> str:
674+
"Provide SQL for returning the current default database."
668675
return "current_database()"
669676

670677
def current_schema(self) -> str:
678+
"Provide SQL for returning the current default schema."
671679
return "current_schema()"
672680

673681
def explain_as_text(self, query: str) -> str:
682+
"Provide SQL for explaining a query, returned as table(varchar)"
674683
return f"EXPLAIN {query}"
675684

676685
def _constant_value(self, v):
@@ -719,7 +728,7 @@ def parse_type(
719728
numeric_precision: int = None,
720729
numeric_scale: int = None,
721730
) -> ColType:
722-
""" """
731+
"Parse type info as returned by the database"
723732

724733
cls = self._parse_type_repr(type_repr)
725734
if cls is None:
@@ -762,6 +771,7 @@ def _convert_db_precision_to_digits(self, p: int) -> int:
762771

763772
@classmethod
764773
def load_mixins(cls, *abstract_mixins) -> Self:
774+
"Load a list of mixins that implement the given abstract mixins"
765775
mixins = {m for m in cls.MIXINS if issubclass(m, abstract_mixins)}
766776

767777
class _DialectWithMixins(cls, *mixins, *abstract_mixins):
@@ -771,6 +781,30 @@ class _DialectWithMixins(cls, *mixins, *abstract_mixins):
771781
return _DialectWithMixins()
772782

773783

784+
@property
785+
@abstractmethod
786+
def name(self) -> str:
787+
"Name of the dialect"
788+
789+
@property
790+
@abstractmethod
791+
def ROUNDS_ON_PREC_LOSS(self) -> bool:
792+
"True if db rounds real values when losing precision, False if it truncates."
793+
794+
@abstractmethod
795+
def quote(self, s: str):
796+
"Quote SQL name"
797+
798+
@abstractmethod
799+
def to_string(self, s: str) -> str:
800+
# TODO rewrite using cast_to(x, str)
801+
"Provide SQL for casting a column to string"
802+
803+
@abstractmethod
804+
def set_timezone_to_utc(self) -> str:
805+
"Provide SQL for setting the session timezone to UTC"
806+
807+
774808
T = TypeVar("T", bound=BaseDialect)
775809

776810

0 commit comments

Comments
 (0)