feat: support TOP operator by pik94 · Pull Request #756 · datafold/data-diff · GitHub
Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions data_diff/databases/base.py
10 changes: 7 additions & 3 deletions data_diff/databases/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ def is_distinct_from(self, a: str, b: str) -> str:
# See: https://stackoverflow.com/a/18684859/857383
return f"(({a}<>{b} OR {a} IS NULL OR {b} IS NULL) AND NOT({a} IS NULL AND {b} IS NULL))"

def offset_limit(
self, offset: Optional[int] = None, limit: Optional[int] = None, has_order_by: Optional[bool] = None
def limit_select(
self,
select_query: str,
offset: Optional[int] = None,
limit: Optional[int] = None,
has_order_by: Optional[bool] = None,
) -> str:
if offset:
raise NotImplementedError("No support for OFFSET in query")
Expand All @@ -121,7 +125,7 @@ def offset_limit(
result += "ORDER BY 1"

result += f" OFFSET 0 ROWS FETCH NEXT {limit} ROWS ONLY"
return result
return f"SELECT * FROM ({select_query}) AS LIMITED_SELECT {result}"

def constant_values(self, rows) -> str:
values = ", ".join("(%s)" % ", ".join(self._constant_value(v) for v in row) for row in rows)
Expand Down
10 changes: 7 additions & 3 deletions data_diff/databases/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ def quote(self, s: str):
def to_string(self, s: str):
return f"cast({s} as varchar(1024))"

def offset_limit(
self, offset: Optional[int] = None, limit: Optional[int] = None, has_order_by: Optional[bool] = None
def limit_select(
self,
select_query: str,
offset: Optional[int] = None,
limit: Optional[int] = None,
has_order_by: Optional[bool] = None,
) -> str:
if offset:
raise NotImplementedError("No support for OFFSET in query")

return f"FETCH NEXT {limit} ROWS ONLY"
return f"SELECT * FROM ({select_query}) FETCH NEXT {limit} ROWS ONLY"

def concat(self, items: List[str]) -> str:
joined_exprs = " || ".join(items)
Expand Down
19 changes: 13 additions & 6 deletions tests/test_query.py