Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 7457caa

Browse files
committed
Using mixin for optimizer_hints support
1 parent 21f2537 commit 7457caa

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

docs/intro.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ List of available abstract mixins:
463463

464464
- `AbstractMixin_TimeTravel` - Only snowflake & bigquery
465465

466+
- `AbstractMixin_OptimizerHints` - Only oracle & mysql
467+
466468
More will be added in the future.
467469

468470
Note that it's still possible to use user-defined mixins that aren't on this list.

sqeleton/abcs/database_types.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,6 @@ def offset_limit(self, offset: Optional[int] = None, limit: Optional[int] = None
201201
def explain_as_text(self, query: str) -> str:
202202
"Provide SQL for explaining a query, returned as table(varchar)"
203203

204-
@abstractmethod
205-
def optimizer_hints(self, hints: str) -> str:
206-
"Provide SQL for enclosing optimizer hints"
207-
208204
@abstractmethod
209205
def timestamp_value(self, t: datetime) -> str:
210206
"Provide SQL for the given timestamp value"

sqeleton/abcs/mixins.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,15 @@ def time_travel(
145145
146146
Must specify exactly one of `timestamp`, `offset` or `statement`.
147147
"""
148+
149+
class AbstractMixin_OptimizerHints(AbstractMixin):
150+
@abstractmethod
151+
def optimizer_hints(
152+
self,
153+
optimizer_hints: str
154+
) -> str:
155+
"""Creates a compatible optimizer_hints string
156+
157+
Parameters:
158+
optimizer_hints - string of opimizer hints
159+
"""

sqeleton/databases/base.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
Boolean,
3737
)
3838
from ..abcs.mixins import Compilable
39-
from ..abcs.mixins import AbstractMixin_Schema, AbstractMixin_RandomSample, AbstractMixin_NormalizeValue
39+
from ..abcs.mixins import (
40+
AbstractMixin_Schema,
41+
AbstractMixin_RandomSample,
42+
AbstractMixin_NormalizeValue,
43+
AbstractMixin_OptimizerHints
44+
)
4045
from ..bound_exprs import bound_table
4146

4247
logger = logging.getLogger("database")
@@ -134,6 +139,14 @@ def random_sample_ratio_approx(self, tbl: AbstractTable, ratio: float) -> Abstra
134139
return tbl.where(Random() < ratio)
135140

136141

142+
class Mixin_OptimizerHints(AbstractMixin_OptimizerHints):
143+
def optimizer_hints(
144+
self,
145+
hints: str
146+
) -> str:
147+
return f"/*+ {hints} */ "
148+
149+
137150
class BaseDialect(AbstractDialect):
138151
SUPPORTS_PRIMARY_KEY = False
139152
SUPPORTS_INDEXES = False
@@ -168,8 +181,6 @@ def current_timestamp(self) -> str:
168181
def explain_as_text(self, query: str) -> str:
169182
return f"EXPLAIN {query}"
170183

171-
def optimizer_hints(self, hints: str) -> str:
172-
raise NotImplementedError(f"Optimizer hints not yet implemented in {self.__class__}")
173184

174185
def _constant_value(self, v):
175186
if v is None:

sqeleton/databases/mysql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
AbstractMixin_Regex,
1818
AbstractMixin_RandomSample,
1919
)
20-
from .base import ThreadedDatabase, import_helper, ConnectError, BaseDialect, Compilable
20+
from .base import Mixin_OptimizerHints, ThreadedDatabase, import_helper, ConnectError, BaseDialect, Compilable
2121
from .base import MD5_HEXDIGITS, CHECKSUM_HEXDIGITS, TIMESTAMP_PRECISION_POS, Mixin_Schema, Mixin_RandomSample
2222
from ..queries.ast_classes import BinBoolOp
2323

@@ -54,7 +54,7 @@ def test_regex(self, string: Compilable, pattern: Compilable) -> Compilable:
5454
return BinBoolOp("REGEXP", [string, pattern])
5555

5656

57-
class Dialect(BaseDialect, Mixin_Schema):
57+
class Dialect(BaseDialect, Mixin_Schema, Mixin_OptimizerHints):
5858
name = "MySQL"
5959
ROUNDS_ON_PREC_LOSS = True
6060
SUPPORTS_PRIMARY_KEY = True

sqeleton/databases/oracle.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ..abcs.mixins import AbstractMixin_MD5, AbstractMixin_NormalizeValue, AbstractMixin_Schema
1818
from ..abcs import Compilable
1919
from ..queries import this, table, SKIP
20-
from .base import BaseDialect, ThreadedDatabase, import_helper, ConnectError, QueryError, Mixin_RandomSample
20+
from .base import BaseDialect, Mixin_OptimizerHints, ThreadedDatabase, import_helper, ConnectError, QueryError, Mixin_RandomSample
2121
from .base import TIMESTAMP_PRECISION_POS
2222

2323
SESSION_TIME_ZONE = None # Changed by the tests
@@ -72,7 +72,7 @@ def list_tables(self, table_schema: str, like: Compilable = None) -> Compilable:
7272
)
7373

7474

75-
class Dialect(BaseDialect, Mixin_Schema):
75+
class Dialect(BaseDialect, Mixin_Schema, Mixin_OptimizerHints):
7676
name = "Oracle"
7777
SUPPORTS_PRIMARY_KEY = True
7878
SUPPORTS_INDEXES = True
@@ -130,9 +130,6 @@ def constant_values(self, rows) -> str:
130130
def explain_as_text(self, query: str) -> str:
131131
raise NotImplementedError("Explain not yet implemented in Oracle")
132132

133-
def optimizer_hints(self, s: str):
134-
return f"/*+ {s} */ "
135-
136133
def parse_type(
137134
self,
138135
table_path: DbPath,

0 commit comments

Comments
 (0)