11from __future__ import annotations
22
3+ import abc
34import logging
45from typing import (
56 TYPE_CHECKING ,
67 Collection ,
7- Optional ,
88)
99
1010import attr
11- from sqlalchemy .exc import DatabaseError
1211
13- from dl_core .data_processing .cache .exc import CachePreparationFailed
1412from dl_core .data_processing .cache .primitives import (
1513 BIQueryCacheOptions ,
1614 CacheTTLConfig ,
1715 CacheTTLInfo ,
1816 DataKeyPart ,
1917 LocalKeyRepresentation ,
2018)
21- from dl_core .query .bi_query import QueryAndResultInfo
2219from dl_core .serialization import hashable_dumps
2320from dl_core .us_connection_base import (
2421 ConnectionBase ,
2724
2825
2926if TYPE_CHECKING :
30- from sqlalchemy .engine .default import DefaultDialect
31- from sqlalchemy .sql import Select
32-
33- from dl_constants .enums import UserDataType
3427 from dl_constants .types import TJSONExt
3528 from dl_core .data_processing .prepared_components .primitives import PreparedFromInfo
3629 from dl_core .data_source .base import DataSource
37- from dl_core .us_manager .local_cache import USEntryBuffer
3830
3931
4032LOGGER = logging .getLogger (__name__ )
4133
4234
4335@attr .s
44- class CacheOptionsBuilderBase :
36+ class CacheOptionsBuilderBase ( abc . ABC ) :
4537 default_ttl_config : CacheTTLConfig = attr .ib (factory = CacheTTLConfig )
46- _is_bleeding_edge_user : bool = attr .ib (default = False )
4738
4839 def get_actual_ttl_config (
4940 self ,
@@ -58,28 +49,6 @@ def get_actual_ttl_config(
5849
5950 return ctc
6051
61- @staticmethod
62- def get_query_str_for_cache (query : Select , dialect : DefaultDialect ) -> str :
63- try :
64- compiled_query = query .compile (dialect = dialect )
65- except DatabaseError as err :
66- raise CachePreparationFailed from err
67-
68- if isinstance (compiled_query .params , dict ):
69- ordered_params = sorted (
70- compiled_query .params .items (),
71- key = lambda item : item [0 ],
72- )
73- else :
74- ordered_params = compiled_query .params
75-
76- return ";" .join (
77- (
78- str (compiled_query ),
79- str (ordered_params ),
80- )
81- )
82-
8352 @staticmethod
8453 def config_to_ttl_info (ttl_config : CacheTTLConfig ) -> CacheTTLInfo :
8554 return CacheTTLInfo (
@@ -97,133 +66,43 @@ def get_cache_ttl_info(self, data_source_list: Collection[DataSource]) -> CacheT
9766 )
9867 return self .config_to_ttl_info (ttl_config = ttl_config )
9968
100- def get_data_key (
101- self ,
102- * ,
103- query_res_info : QueryAndResultInfo ,
104- from_info : Optional [PreparedFromInfo ] = None ,
105- base_key : LocalKeyRepresentation = LocalKeyRepresentation (), # noqa: B008
106- ) -> Optional [LocalKeyRepresentation ]:
107- return base_key
108-
10969
11070@attr .s
11171class DatasetOptionsBuilder (CacheOptionsBuilderBase ):
112- cache_enabled : bool = attr .ib (kw_only = True , default = True )
113-
114- def get_cache_options (
115- self ,
116- joint_dsrc_info : PreparedFromInfo ,
117- data_key : LocalKeyRepresentation ,
118- ) -> BIQueryCacheOptions :
72+ @abc .abstractmethod
73+ def get_cache_enabled (self , joint_dsrc_info : PreparedFromInfo ) -> bool :
11974 raise NotImplementedError
12075
121-
122- @attr .s
123- class CompengOptionsBuilder (DatasetOptionsBuilder ): # TODO: Move to compeng package
124- cache_enabled : bool = attr .ib (kw_only = True , default = True )
125-
12676 def get_cache_options (
12777 self ,
12878 joint_dsrc_info : PreparedFromInfo ,
12979 data_key : LocalKeyRepresentation ,
13080 ) -> BIQueryCacheOptions :
13181 ttl_info = self .get_cache_ttl_info (data_source_list = joint_dsrc_info .data_source_list ) # type: ignore # 2024-01-24 # TODO: Argument "data_source_list" to "get_cache_ttl_info" of "CacheOptionsBuilderBase" has incompatible type "tuple[DataSource, ...] | None"; expected "Collection[DataSource]" [arg-type]
82+ cache_enabled = self .get_cache_enabled (joint_dsrc_info = joint_dsrc_info )
13283 return BIQueryCacheOptions (
133- cache_enabled = self . cache_enabled ,
134- key = data_key ,
84+ cache_enabled = cache_enabled ,
85+ key = data_key if cache_enabled else None ,
13586 ttl_sec = ttl_info .ttl_sec ,
13687 refresh_ttl_on_read = ttl_info .refresh_ttl_on_read ,
13788 )
13889
139- def get_data_key (
140- self ,
141- * ,
142- query_res_info : QueryAndResultInfo ,
143- from_info : Optional [PreparedFromInfo ] = None ,
144- base_key : LocalKeyRepresentation = LocalKeyRepresentation (), # noqa: B008
145- ) -> Optional [LocalKeyRepresentation ]:
146- # TODO: Remove after switching to new cache keys
147- compiled_query = self .get_query_str_for_cache (
148- query = query_res_info .query ,
149- dialect = from_info .query_compiler .dialect , # type: ignore # 2024-01-24 # TODO: Item "None" of "PreparedFromInfo | None" has no attribute "query_compiler" [union-attr]
150- )
151- return base_key .extend (part_type = "query" , part_content = compiled_query )
152-
15390
15491@attr .s
155- class SelectorCacheOptionsBuilder (DatasetOptionsBuilder ):
156- _is_bleeding_edge_user : bool = attr .ib (default = False )
157- _us_entry_buffer : USEntryBuffer = attr .ib (kw_only = True )
92+ class CompengOptionsBuilder (DatasetOptionsBuilder ): # TODO: Move to compeng package
93+ cache_enabled : bool = attr .ib (kw_only = True , default = True )
94+
95+ def get_cache_enabled (self , joint_dsrc_info : PreparedFromInfo ) -> bool :
96+ return self .cache_enabled
97+
15898
99+ @attr .s
100+ class SelectorCacheOptionsBuilder (DatasetOptionsBuilder ): # TODO: Rename to SourceDbCacheOptionsBuilder
159101 def get_cache_enabled (self , joint_dsrc_info : PreparedFromInfo ) -> bool :
160102 assert joint_dsrc_info .data_source_list is not None
161103 cache_enabled = all (dsrc .cache_enabled for dsrc in joint_dsrc_info .data_source_list )
162104 return cache_enabled
163105
164- def get_cache_options (
165- self ,
166- joint_dsrc_info : PreparedFromInfo ,
167- data_key : LocalKeyRepresentation ,
168- ) -> BIQueryCacheOptions :
169- """Returns cache key, TTL for new entries, refresh TTL flag"""
170-
171- ttl_info = self .get_cache_ttl_info (data_source_list = joint_dsrc_info .data_source_list ) # type: ignore # 2024-01-24 # TODO: Argument "data_source_list" to "get_cache_ttl_info" of "CacheOptionsBuilderBase" has incompatible type "tuple[DataSource, ...] | None"; expected "Collection[DataSource]" [arg-type]
172- cache_enabled = self .get_cache_enabled (joint_dsrc_info = joint_dsrc_info )
173- return BIQueryCacheOptions (
174- cache_enabled = cache_enabled ,
175- key = data_key if cache_enabled else None ,
176- ttl_sec = ttl_info .ttl_sec ,
177- refresh_ttl_on_read = ttl_info .refresh_ttl_on_read ,
178- )
179-
180- def make_data_select_cache_key (
181- self ,
182- from_info : PreparedFromInfo ,
183- compiled_query : str ,
184- user_types : list [UserDataType ],
185- is_bleeding_edge_user : bool ,
186- base_key : LocalKeyRepresentation = LocalKeyRepresentation (), # noqa: B008
187- ) -> LocalKeyRepresentation :
188- # TODO: Remove after switching to new cache keys,
189- # but put the db_name + target_connection.get_cache_key_part() parts somewhere
190- assert from_info .target_connection_ref is not None
191- target_connection = self ._us_entry_buffer .get_entry (from_info .target_connection_ref )
192- assert isinstance (target_connection , ConnectionBase )
193- connection_id = target_connection .uuid
194- assert connection_id is not None
195-
196- local_key_rep = base_key
197- local_key_rep = local_key_rep .extend (part_type = "query" , part_content = str (compiled_query ))
198- local_key_rep = local_key_rep .extend (part_type = "user_types" , part_content = tuple (user_types or ()))
199- local_key_rep = local_key_rep .extend (
200- part_type = "is_bleeding_edge_user" ,
201- part_content = is_bleeding_edge_user ,
202- )
203-
204- return local_key_rep
205-
206- def get_data_key (
207- self ,
208- * ,
209- query_res_info : QueryAndResultInfo ,
210- from_info : Optional [PreparedFromInfo ] = None ,
211- base_key : LocalKeyRepresentation = LocalKeyRepresentation (), # noqa: B008
212- ) -> Optional [LocalKeyRepresentation ]:
213- # TODO: Remove after switching to new cache keys
214- compiled_query = self .get_query_str_for_cache (
215- query = query_res_info .query ,
216- dialect = from_info .query_compiler .dialect , # type: ignore # 2024-01-24 # TODO: Item "None" of "PreparedFromInfo | None" has no attribute "query_compiler" [union-attr]
217- )
218- data_key : Optional [LocalKeyRepresentation ] = self .make_data_select_cache_key (
219- base_key = base_key ,
220- from_info = from_info , # type: ignore # 2024-01-24 # TODO: Argument "from_info" to "make_data_select_cache_key" of "SelectorCacheOptionsBuilder" has incompatible type "PreparedFromInfo | None"; expected "PreparedFromInfo" [arg-type]
221- compiled_query = compiled_query ,
222- user_types = query_res_info .user_types ,
223- is_bleeding_edge_user = self ._is_bleeding_edge_user ,
224- )
225- return data_key
226-
227106
228107@attr .s
229108class DashSQLCacheOptionsBuilder (CacheOptionsBuilderBase ):
0 commit comments