Skip to content

Commit 7ea9adb

Browse files
Escape variable names in SELECT queries
Closes #37
1 parent 4dccee4 commit 7ea9adb

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

mcbackend/backends/clickhouse.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ def append(
164164
params: Dict[str, Any] = {"_draw_idx": self._draw_idx, **draw, **stat}
165165
self._draw_idx += 1
166166
if not self._insert_query:
167-
names = ", ".join(params.keys())
168-
self._insert_query = f"INSERT INTO {self.cid} ({names}) VALUES"
167+
names = "`,`".join(params.keys())
168+
self._insert_query = f"INSERT INTO {self.cid} (`{names}`) VALUES"
169169
self._insert_queue.append(params)
170170

171171
if (
@@ -199,9 +199,9 @@ def _get_row_at(
199199
var_names: Sequence[str],
200200
) -> Dict[str, numpy.ndarray]:
201201
self._commit()
202-
names = ",".join(var_names)
202+
names = "`,`".join(var_names)
203203
# NOTE: The trailing , 👇 is included to get the same signature when len(var_names) == 1.
204-
query = f"SELECT ({names},) FROM {self.cid} WHERE _draw_idx={idx};"
204+
query = f"SELECT (`{names}`,) FROM {self.cid} WHERE _draw_idx={idx};"
205205
data = self._client.execute(query)
206206
if not data:
207207
raise Exception(f"No record found for draw index {idx}.")

mcbackend/test_backend_clickhouse.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ def test_get_row_at(self):
283283
assert len(chain._get_row_at(5, var_names=["v1"])) == 1
284284
pass
285285

286-
@pytest.mark.xfail(reason="issue #37")
287286
def test_exotic_var_names(self):
288287
run, chains = fully_initialized(
289288
self.backend,

0 commit comments

Comments
 (0)