@@ -202,7 +202,6 @@ class BaseDialect(abc.ABC):
202202 TYPE_CLASSES : ClassVar [Dict [str , Type [ColType ]]] = {}
203203
204204 PLACEHOLDER_TABLE = None # Used for Oracle
205- USE_TOP_INSTEAD_LIMIT : bool = False # True for MsSQL or Teradata
206205
207206 def parse_table_name (self , name : str ) -> DbPath :
208207 "Parse the given table name into a DbPath"
@@ -472,10 +471,7 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
472471 columns = ", " .join (map (compile_fn , elem .columns )) if elem .columns else "*"
473472 distinct = "DISTINCT " if elem .distinct else ""
474473 optimizer_hints = self .optimizer_hints (elem .optimizer_hints ) if elem .optimizer_hints else ""
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 } "
474+ select = f"SELECT { optimizer_hints } { distinct } { columns } "
479475
480476 if elem .table :
481477 select += " FROM " + self .compile (c , elem .table )
@@ -495,9 +491,9 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
495491 if elem .order_by_exprs :
496492 select += " ORDER BY " + ", " .join (map (compile_fn , elem .order_by_exprs ))
497493
498- if elem .limit_expr is not None and not self . USE_TOP_INSTEAD_LIMIT :
494+ if elem .limit_expr is not None :
499495 has_order_by = bool (elem .order_by_exprs )
500- select += " " + self .offset_limit ( 0 , elem .limit_expr , has_order_by = has_order_by )
496+ select = self .limit_select ( select_query = select , offset = 0 , limit = elem .limit_expr , has_order_by = has_order_by )
501497
502498 if parent_c .in_select :
503499 select = f"({ select } ) { c .new_unique_name ()} "
@@ -609,14 +605,17 @@ def render_inserttotable(self, c: Compiler, elem: InsertToTable) -> str:
609605
610606 return f"INSERT INTO { self .compile (c , elem .path )} { columns } { expr } "
611607
612- def offset_limit (
613- self , offset : Optional [int ] = None , limit : Optional [int ] = None , has_order_by : Optional [bool ] = None
608+ def limit_select (
609+ self ,
610+ select_query : str ,
611+ offset : Optional [int ] = None ,
612+ limit : Optional [int ] = None ,
613+ has_order_by : Optional [bool ] = None ,
614614 ) -> str :
615- "Provide SQL fragment for limit and offset inside a select"
616615 if offset :
617616 raise NotImplementedError ("No support for OFFSET in query" )
618617
619- return f"LIMIT { limit } "
618+ return f"SELECT * FROM ( { select_query } ) AS LIMITED_SELECT LIMIT { limit } "
620619
621620 def concat (self , items : List [str ]) -> str :
622621 "Provide SQL for concatenating a bunch of columns into a string"
@@ -1107,7 +1106,7 @@ def _query_cursor(self, c, sql_code: str) -> QueryResult:
11071106 return result
11081107 except Exception as _e :
11091108 # logger.exception(e)
1110- # logger.error(f' Caused by SQL: {sql_code}' )
1109+ # logger.error(f" Caused by SQL: {sql_code}" )
11111110 raise
11121111
11131112 def _query_conn (self , conn , sql_code : Union [str , ThreadLocalInterpreter ]) -> QueryResult :
0 commit comments