@@ -91,14 +91,14 @@ class ITable(AbstractTable):
9191 source_table : Any
9292 schema : Schema = None
9393
94- def select (self , * exprs , distinct = SKIP , ** named_exprs ):
94+ def select (self , * exprs , distinct = SKIP , optimizer_hints = SKIP , ** named_exprs ):
9595 """Create a new table with the specified fields"""
9696 exprs = args_as_tuple (exprs )
9797 exprs = _drop_skips (exprs )
9898 named_exprs = _drop_skips_dict (named_exprs )
9999 exprs += _named_exprs_as_aliases (named_exprs )
100100 resolve_names (self .source_table , exprs )
101- return Select .make (self , columns = exprs , distinct = distinct )
101+ return Select .make (self , columns = exprs , distinct = distinct , optimizer_hints = optimizer_hints )
102102
103103 def where (self , * exprs ):
104104 exprs = args_as_tuple (exprs )
@@ -682,6 +682,7 @@ class Select(ExprNode, ITable, Root):
682682 having_exprs : Sequence [Expr ] = None
683683 limit_expr : int = None
684684 distinct : bool = False
685+ optimizer_hints : Sequence [Expr ] = None
685686
686687 @property
687688 def schema (self ):
@@ -699,7 +700,8 @@ def compile(self, parent_c: Compiler) -> str:
699700
700701 columns = ", " .join (map (c .compile , self .columns )) if self .columns else "*"
701702 distinct = "DISTINCT " if self .distinct else ""
702- select = f"SELECT { distinct } { columns } "
703+ optimizer_hints = c .dialect .optimizer_hints (self .optimizer_hints ) if self .optimizer_hints else ""
704+ select = f"SELECT { optimizer_hints } { distinct } { columns } "
703705
704706 if self .table :
705707 select += " FROM " + c .compile (self .table )
@@ -729,15 +731,19 @@ def compile(self, parent_c: Compiler) -> str:
729731 return select
730732
731733 @classmethod
732- def make (cls , table : ITable , distinct : bool = SKIP , ** kwargs ):
734+ def make (cls , table : ITable , distinct : bool = SKIP , optimizer_hints : str = SKIP , ** kwargs ):
733735 assert "table" not in kwargs
734736
735737 if not isinstance (table , cls ): # If not Select
736738 if distinct is not SKIP :
737739 kwargs ["distinct" ] = distinct
740+ if optimizer_hints is not SKIP :
741+ kwargs ["optimizer_hints" ] = optimizer_hints
738742 return cls (table , ** kwargs )
739743
740744 # We can safely assume isinstance(table, Select)
745+ if optimizer_hints is not SKIP :
746+ kwargs ["optimizer_hints" ] = optimizer_hints
741747
742748 if distinct is not SKIP :
743749 if distinct == False and table .distinct :
@@ -752,7 +758,7 @@ def make(cls, table: ITable, distinct: bool = SKIP, **kwargs):
752758 if getattr (table , k ) is not None :
753759 if k == "where_exprs" : # Additive attribute
754760 kwargs [k ] = getattr (table , k ) + v
755- elif k == "distinct" :
761+ elif k in [ "distinct" , "optimizer_hints" ] :
756762 pass
757763 else :
758764 raise ValueError (k )
0 commit comments