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

Commit fd0888a

Browse files
committed
feat: support TOP operator
1 parent 312a9c5 commit fd0888a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

data_diff/databases/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class BaseDialect(abc.ABC):
241241
TYPE_CLASSES: ClassVar[Dict[str, Type[ColType]]] = {}
242242

243243
PLACEHOLDER_TABLE = None # Used for Oracle
244+
USE_TOP_INSTEAD_LIMIT: bool = False # True for MsSQL or Teradata
244245

245246
def parse_table_name(self, name: str) -> DbPath:
246247
"Parse the given table name into a DbPath"
@@ -512,7 +513,10 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
512513
columns = ", ".join(map(compile_fn, elem.columns)) if elem.columns else "*"
513514
distinct = "DISTINCT " if elem.distinct else ""
514515
optimizer_hints = self.optimizer_hints(elem.optimizer_hints) if elem.optimizer_hints else ""
515-
select = f"SELECT {optimizer_hints}{distinct}{columns}"
516+
if elem.limit_expr is not None and self.USE_TOP_INSTEAD_LIMIT:
517+
select = f"SELECT TOP {elem.limit_expr} {optimizer_hints}{distinct}{columns}"
518+
else:
519+
select = f"SELECT {optimizer_hints}{distinct}{columns}"
516520

517521
if elem.table:
518522
select += " FROM " + self.compile(c, elem.table)
@@ -532,7 +536,7 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
532536
if elem.order_by_exprs:
533537
select += " ORDER BY " + ", ".join(map(compile_fn, elem.order_by_exprs))
534538

535-
if elem.limit_expr is not None:
539+
if elem.limit_expr is not None and not self.USE_TOP_INSTEAD_LIMIT:
536540
has_order_by = bool(elem.order_by_exprs)
537541
select += " " + self.offset_limit(0, elem.limit_expr, has_order_by=has_order_by)
538542

0 commit comments

Comments
 (0)