|
| 1 | +"""Cache Module for Amazon Athena.""" |
| 2 | +import datetime |
| 3 | +import logging |
| 4 | +import re |
| 5 | +from heapq import heappop, heappush |
| 6 | +from typing import Any, Dict, List, Match, NamedTuple, Optional, Tuple, Union |
| 7 | + |
| 8 | +import boto3 |
| 9 | + |
| 10 | +from awswrangler import _utils |
| 11 | + |
| 12 | +_logger: logging.Logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class _CacheInfo(NamedTuple): |
| 16 | + has_valid_cache: bool |
| 17 | + file_format: Optional[str] = None |
| 18 | + query_execution_id: Optional[str] = None |
| 19 | + query_execution_payload: Optional[Dict[str, Any]] = None |
| 20 | + |
| 21 | + |
| 22 | +class _LocalMetadataCacheManager: |
| 23 | + def __init__(self) -> None: |
| 24 | + self._cache: Dict[str, Any] = {} |
| 25 | + self._pqueue: List[Tuple[datetime.datetime, str]] = [] |
| 26 | + self._max_cache_size = 100 |
| 27 | + |
| 28 | + def update_cache(self, items: List[Dict[str, Any]]) -> None: |
| 29 | + """ |
| 30 | + Update the local metadata cache with new query metadata. |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + items : List[Dict[str, Any]] |
| 35 | + List of query execution metadata which is returned by boto3 `batch_get_query_execution()`. |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + None |
| 40 | + None. |
| 41 | + """ |
| 42 | + if self._pqueue: |
| 43 | + oldest_item = self._cache[self._pqueue[0][1]] |
| 44 | + items = list( |
| 45 | + filter(lambda x: x["Status"]["SubmissionDateTime"] > oldest_item["Status"]["SubmissionDateTime"], items) |
| 46 | + ) |
| 47 | + |
| 48 | + cache_oversize = len(self._cache) + len(items) - self._max_cache_size |
| 49 | + for _ in range(cache_oversize): |
| 50 | + _, query_execution_id = heappop(self._pqueue) |
| 51 | + del self._cache[query_execution_id] |
| 52 | + |
| 53 | + for item in items[: self._max_cache_size]: |
| 54 | + heappush(self._pqueue, (item["Status"]["SubmissionDateTime"], item["QueryExecutionId"])) |
| 55 | + self._cache[item["QueryExecutionId"]] = item |
| 56 | + |
| 57 | + def sorted_successful_generator(self) -> List[Dict[str, Any]]: |
| 58 | + """ |
| 59 | + Sorts the entries in the local cache based on query Completion DateTime. |
| 60 | +
|
| 61 | + This is useful to guarantee LRU caching rules. |
| 62 | +
|
| 63 | + Returns |
| 64 | + ------- |
| 65 | + List[Dict[str, Any]] |
| 66 | + Returns successful DDL and DML queries sorted by query completion time. |
| 67 | + """ |
| 68 | + filtered: List[Dict[str, Any]] = [] |
| 69 | + for query in self._cache.values(): |
| 70 | + if (query["Status"].get("State") == "SUCCEEDED") and (query.get("StatementType") in ["DDL", "DML"]): |
| 71 | + filtered.append(query) |
| 72 | + return sorted(filtered, key=lambda e: str(e["Status"]["CompletionDateTime"]), reverse=True) |
| 73 | + |
| 74 | + def __contains__(self, key: str) -> bool: |
| 75 | + return key in self._cache |
| 76 | + |
| 77 | + @property |
| 78 | + def max_cache_size(self) -> int: |
| 79 | + """Property max_cache_size.""" |
| 80 | + return self._max_cache_size |
| 81 | + |
| 82 | + @max_cache_size.setter |
| 83 | + def max_cache_size(self, value: int) -> None: |
| 84 | + self._max_cache_size = value |
| 85 | + |
| 86 | + |
| 87 | +def _parse_select_query_from_possible_ctas(possible_ctas: str) -> Optional[str]: |
| 88 | + """Check if `possible_ctas` is a valid parquet-generating CTAS and returns the full SELECT statement.""" |
| 89 | + possible_ctas = possible_ctas.lower() |
| 90 | + parquet_format_regex: str = r"format\s*=\s*\'parquet\'\s*," |
| 91 | + is_parquet_format: Optional[Match[str]] = re.search(pattern=parquet_format_regex, string=possible_ctas) |
| 92 | + if is_parquet_format is not None: |
| 93 | + unstripped_select_statement_regex: str = r"\s+as\s+\(*(select|with).*" |
| 94 | + unstripped_select_statement_match: Optional[Match[str]] = re.search( |
| 95 | + unstripped_select_statement_regex, possible_ctas, re.DOTALL |
| 96 | + ) |
| 97 | + if unstripped_select_statement_match is not None: |
| 98 | + stripped_select_statement_match: Optional[Match[str]] = re.search( |
| 99 | + r"(select|with).*", unstripped_select_statement_match.group(0), re.DOTALL |
| 100 | + ) |
| 101 | + if stripped_select_statement_match is not None: |
| 102 | + return stripped_select_statement_match.group(0) |
| 103 | + return None |
| 104 | + |
| 105 | + |
| 106 | +def _compare_query_string(sql: str, other: str) -> bool: |
| 107 | + comparison_query = _prepare_query_string_for_comparison(query_string=other) |
| 108 | + _logger.debug("sql: %s", sql) |
| 109 | + _logger.debug("comparison_query: %s", comparison_query) |
| 110 | + if sql == comparison_query: |
| 111 | + return True |
| 112 | + return False |
| 113 | + |
| 114 | + |
| 115 | +def _prepare_query_string_for_comparison(query_string: str) -> str: |
| 116 | + """To use cached data, we need to compare queries. Returns a query string in canonical form.""" |
| 117 | + # for now this is a simple complete strip, but it could grow into much more sophisticated |
| 118 | + # query comparison data structures |
| 119 | + query_string = "".join(query_string.split()).strip("()").lower() |
| 120 | + query_string = query_string[:-1] if query_string.endswith(";") else query_string |
| 121 | + return query_string |
| 122 | + |
| 123 | + |
| 124 | +def _get_last_query_infos( |
| 125 | + max_remote_cache_entries: int, |
| 126 | + boto3_session: Optional[boto3.Session] = None, |
| 127 | + workgroup: Optional[str] = None, |
| 128 | +) -> List[Dict[str, Any]]: |
| 129 | + """Return an iterator of `query_execution_info`s run by the workgroup in Athena.""" |
| 130 | + client_athena: boto3.client = _utils.client(service_name="athena", session=boto3_session) |
| 131 | + page_size = 50 |
| 132 | + args: Dict[str, Union[str, Dict[str, int]]] = { |
| 133 | + "PaginationConfig": {"MaxItems": max_remote_cache_entries, "PageSize": page_size} |
| 134 | + } |
| 135 | + if workgroup is not None: |
| 136 | + args["WorkGroup"] = workgroup |
| 137 | + paginator = client_athena.get_paginator("list_query_executions") |
| 138 | + uncached_ids = [] |
| 139 | + for page in paginator.paginate(**args): |
| 140 | + _logger.debug("paginating Athena's queries history...") |
| 141 | + query_execution_id_list: List[str] = page["QueryExecutionIds"] |
| 142 | + for query_execution_id in query_execution_id_list: |
| 143 | + if query_execution_id not in _cache_manager: |
| 144 | + uncached_ids.append(query_execution_id) |
| 145 | + if uncached_ids: |
| 146 | + new_execution_data = [] |
| 147 | + for i in range(0, len(uncached_ids), page_size): |
| 148 | + new_execution_data.extend( |
| 149 | + client_athena.batch_get_query_execution(QueryExecutionIds=uncached_ids[i : i + page_size]).get( |
| 150 | + "QueryExecutions" |
| 151 | + ) |
| 152 | + ) |
| 153 | + _cache_manager.update_cache(new_execution_data) |
| 154 | + return _cache_manager.sorted_successful_generator() |
| 155 | + |
| 156 | + |
| 157 | +def _check_for_cached_results( |
| 158 | + sql: str, |
| 159 | + boto3_session: boto3.Session, |
| 160 | + workgroup: Optional[str], |
| 161 | + max_cache_seconds: int, |
| 162 | + max_cache_query_inspections: int, |
| 163 | + max_remote_cache_entries: int, |
| 164 | +) -> _CacheInfo: |
| 165 | + """ |
| 166 | + Check whether `sql` has been run before, within the `max_cache_seconds` window, by the `workgroup`. |
| 167 | +
|
| 168 | + If so, returns a dict with Athena's `query_execution_info` and the data format. |
| 169 | + """ |
| 170 | + if max_cache_seconds <= 0: |
| 171 | + return _CacheInfo(has_valid_cache=False) |
| 172 | + num_executions_inspected: int = 0 |
| 173 | + comparable_sql: str = _prepare_query_string_for_comparison(sql) |
| 174 | + current_timestamp: datetime.datetime = datetime.datetime.now(datetime.timezone.utc) |
| 175 | + _logger.debug("current_timestamp: %s", current_timestamp) |
| 176 | + for query_info in _get_last_query_infos( |
| 177 | + max_remote_cache_entries=max_remote_cache_entries, |
| 178 | + boto3_session=boto3_session, |
| 179 | + workgroup=workgroup, |
| 180 | + ): |
| 181 | + query_execution_id: str = query_info["QueryExecutionId"] |
| 182 | + query_timestamp: datetime.datetime = query_info["Status"]["CompletionDateTime"] |
| 183 | + _logger.debug("query_timestamp: %s", query_timestamp) |
| 184 | + if (current_timestamp - query_timestamp).total_seconds() > max_cache_seconds: |
| 185 | + return _CacheInfo( |
| 186 | + has_valid_cache=False, query_execution_id=query_execution_id, query_execution_payload=query_info |
| 187 | + ) |
| 188 | + statement_type: Optional[str] = query_info.get("StatementType") |
| 189 | + if statement_type == "DDL" and query_info["Query"].startswith("CREATE TABLE"): |
| 190 | + parsed_query: Optional[str] = _parse_select_query_from_possible_ctas(possible_ctas=query_info["Query"]) |
| 191 | + if parsed_query is not None: |
| 192 | + if _compare_query_string(sql=comparable_sql, other=parsed_query): |
| 193 | + return _CacheInfo( |
| 194 | + has_valid_cache=True, |
| 195 | + file_format="parquet", |
| 196 | + query_execution_id=query_execution_id, |
| 197 | + query_execution_payload=query_info, |
| 198 | + ) |
| 199 | + elif statement_type == "DML" and not query_info["Query"].startswith("INSERT"): |
| 200 | + if _compare_query_string(sql=comparable_sql, other=query_info["Query"]): |
| 201 | + return _CacheInfo( |
| 202 | + has_valid_cache=True, |
| 203 | + file_format="csv", |
| 204 | + query_execution_id=query_execution_id, |
| 205 | + query_execution_payload=query_info, |
| 206 | + ) |
| 207 | + num_executions_inspected += 1 |
| 208 | + _logger.debug("num_executions_inspected: %s", num_executions_inspected) |
| 209 | + if num_executions_inspected >= max_cache_query_inspections: |
| 210 | + return _CacheInfo(has_valid_cache=False) |
| 211 | + return _CacheInfo(has_valid_cache=False) |
| 212 | + |
| 213 | + |
| 214 | +_cache_manager = _LocalMetadataCacheManager() |
0 commit comments