@@ -202,7 +202,6 @@ 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
206
205
207
206
def parse_table_name (self , name : str ) -> DbPath :
208
207
"Parse the given table name into a DbPath"
@@ -472,10 +471,7 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
472
471
columns = ", " .join (map (compile_fn , elem .columns )) if elem .columns else "*"
473
472
distinct = "DISTINCT " if elem .distinct else ""
474
473
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 } "
479
475
480
476
if elem .table :
481
477
select += " FROM " + self .compile (c , elem .table )
@@ -495,9 +491,9 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
495
491
if elem .order_by_exprs :
496
492
select += " ORDER BY " + ", " .join (map (compile_fn , elem .order_by_exprs ))
497
493
498
- if elem .limit_expr is not None and not self . USE_TOP_INSTEAD_LIMIT :
494
+ if elem .limit_expr is not None :
499
495
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 )
501
497
502
498
if parent_c .in_select :
503
499
select = f"({ select } ) { c .new_unique_name ()} "
@@ -609,14 +605,17 @@ def render_inserttotable(self, c: Compiler, elem: InsertToTable) -> str:
609
605
610
606
return f"INSERT INTO { self .compile (c , elem .path )} { columns } { expr } "
611
607
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 ,
614
614
) -> str :
615
- "Provide SQL fragment for limit and offset inside a select"
616
615
if offset :
617
616
raise NotImplementedError ("No support for OFFSET in query" )
618
617
619
- return f"LIMIT { limit } "
618
+ return f"SELECT * FROM ( { select_query } ) AS LIMITED_SELECT LIMIT { limit } "
620
619
621
620
def concat (self , items : List [str ]) -> str :
622
621
"Provide SQL for concatenating a bunch of columns into a string"
@@ -1107,7 +1106,7 @@ def _query_cursor(self, c, sql_code: str) -> QueryResult:
1107
1106
return result
1108
1107
except Exception as _e :
1109
1108
# logger.exception(e)
1110
- # logger.error(f' Caused by SQL: {sql_code}' )
1109
+ logger .error (f" Caused by SQL: { sql_code } " )
1111
1110
raise
1112
1111
1113
1112
def _query_conn (self , conn , sql_code : Union [str , ThreadLocalInterpreter ]) -> QueryResult :
0 commit comments