@@ -202,6 +202,7 @@ class BaseDialect(abc.ABC):
202
202
TYPE_CLASSES : ClassVar [Dict [str , Type [ColType ]]] = {}
203
203
204
204
PLACEHOLDER_TABLE = None # Used for Oracle
205
+ USE_TOP_INSTEAD_LIMIT : bool = False # True for MsSQL or Teradata
205
206
206
207
def parse_table_name (self , name : str ) -> DbPath :
207
208
"Parse the given table name into a DbPath"
@@ -471,7 +472,10 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
471
472
columns = ", " .join (map (compile_fn , elem .columns )) if elem .columns else "*"
472
473
distinct = "DISTINCT " if elem .distinct else ""
473
474
optimizer_hints = self .optimizer_hints (elem .optimizer_hints ) if elem .optimizer_hints else ""
474
- select = f"SELECT { optimizer_hints } { distinct } { columns } "
475
+ if elem .limit_expr is not None and self .USE_TOP_INSTEAD_LIMIT :
476
+ select = f"SELECT TOP { elem .limit_expr } { optimizer_hints } { distinct } { columns } "
477
+ else :
478
+ select = f"SELECT { optimizer_hints } { distinct } { columns } "
475
479
476
480
if elem .table :
477
481
select += " FROM " + self .compile (c , elem .table )
@@ -491,7 +495,7 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
491
495
if elem .order_by_exprs :
492
496
select += " ORDER BY " + ", " .join (map (compile_fn , elem .order_by_exprs ))
493
497
494
- if elem .limit_expr is not None :
498
+ if elem .limit_expr is not None and not self . USE_TOP_INSTEAD_LIMIT :
495
499
has_order_by = bool (elem .order_by_exprs )
496
500
select += " " + self .offset_limit (0 , elem .limit_expr , has_order_by = has_order_by )
497
501
0 commit comments