-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathclickhouse.py
More file actions
83 lines (71 loc) · 2.65 KB
/
Copy pathclickhouse.py
File metadata and controls
83 lines (71 loc) · 2.65 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import os
from typing import Any, Dict, List, Optional
import clickhouse_connect
from clickhouse_connect.driver import Client
from torchci.utils import cache_json, REPO_ROOT
def get_clickhouse_client() -> Client:
endpoint = os.environ["CLICKHOUSE_ENDPOINT"]
# I cannot figure out why these values aren't being handled automatically
# when it is fine in the lambda
if endpoint.startswith("https://"):
endpoint = endpoint[len("https://") :]
if endpoint.endswith(":8443"):
endpoint = endpoint[: -len(":8443")]
return clickhouse_connect.get_client(
host=endpoint,
user=os.environ["CLICKHOUSE_USERNAME"],
password=os.environ["CLICKHOUSE_PASSWORD"],
secure=True,
interface="https",
port=8443,
)
def query_clickhouse_saved(
queryName: str, inputParams: Dict[str, Any], useChQueryCache=False
) -> Any:
"""
Queries ClickHouse using a saved query file and parameters.
:param useChQueryCache: If True, caches the query result on ClickHouse side (1 minute TTL).
:return:
"""
path = REPO_ROOT / "torchci" / "clickhouse_queries" / queryName
with open(path / "query.sql") as f:
queryText = f.read()
with open(path / "params.json") as f:
paramsText = json.load(f).get("params", {})
queryParams = {name: inputParams[name] for name in paramsText}
return query_clickhouse(queryText, queryParams, use_ch_query_cache=useChQueryCache)
def query_clickhouse(
query: str,
params: Dict[str, Any],
use_cache: bool = False,
use_ch_query_cache=False,
) -> Any:
"""
Queries ClickHouse. Returns datetime in YYYY-MM-DD HH:MM:SS format.
:param use_ch_query_cache: If True, uses ClickHouse's query cache (1 minute TTL).
"""
settings = None
if use_ch_query_cache:
settings = {"use_query_cache": 1}
def convert_to_json_list(res: str) -> List[Dict[str, Any]]:
rows = []
for row in res.decode().split("\n"): # type: ignore[attr-defined]
if row:
rows.append(json.loads(row))
return rows
if not use_cache:
res = get_clickhouse_client().raw_query(
query, params, settings=settings, fmt="JSONEachRow"
)
return convert_to_json_list(res)
else:
@cache_json
def cache_query_clickhouse(
query, params, settings: Optional[Dict[str, Any]] = None
) -> Any:
res = get_clickhouse_client().raw_query(
query, params, settings=settings, fmt="JSONEachRow"
)
return convert_to_json_list(res)
return cache_query_clickhouse(query, params, settings)