-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
query_headers.py
42 lines (31 loc) · 1.57 KB
/
query_headers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Any, Dict
from dbt.adapters.base.query_headers import MacroQueryStringSetter, _QueryComment
from dbt.adapters.contracts.connection import AdapterRequiredConfig
class AthenaMacroQueryStringSetter(MacroQueryStringSetter):
def __init__(self, config: AdapterRequiredConfig, query_header_context: Dict[str, Any]):
super().__init__(config, query_header_context)
self.comment = _AthenaQueryComment(None)
class _AthenaQueryComment(_QueryComment):
"""
Athena DDL does not always respect /* ... */ block quotations.
This function is the same as _QueryComment.add except that
a leading "-- " is prepended to the query_comment and any newlines
in the query_comment are replaced with " ". This allows the default
query_comment to be added to `create external table` statements.
"""
def add(self, sql: str) -> str:
if not self.query_comment:
return sql
# alter or vacuum statements don't seem to support properly query comments
# let's just exclude them
if any(map(sql.lower().__contains__, ["alter", "drop", "optimize", "vacuum", "msck"])):
return sql
cleaned_query_comment = self.query_comment.strip().replace("\n", " ")
if self.append:
# replace last ';' with '<comment>;'
sql = sql.rstrip()
if sql[-1] == ";":
sql = sql[:-1]
return f"{sql}\n-- /* {cleaned_query_comment} */;"
return f"{sql}\n-- /* {cleaned_query_comment} */"
return f"-- /* {cleaned_query_comment} */\n{sql}"