Skip to content

Commit

Permalink
fix: snuba admin queries check (#6005)
Browse files Browse the repository at this point in the history
* fix: snuba admin queries check

* add test

* typing
  • Loading branch information
MeredithAnya authored Jun 5, 2024
1 parent 9441705 commit 0d888c8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 0 additions & 1 deletion snuba/admin/clickhouse/predefined_system_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class PartCreations(SystemQuery):
AND table = '{{table}}'
AND event_time > now() - toIntervalMinute(10)
and event_type = 'NewPart'
"""


Expand Down
3 changes: 3 additions & 0 deletions snuba/admin/clickhouse/system_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def is_query_select(sql_query: str) -> bool:
"""
Simple validation to ensure query is a select command
"""
sql_query = " ".join(sql_query.split())
match = SYSTEM_QUERY_RE.match(sql_query)
return True if match else False

Expand All @@ -87,6 +88,7 @@ def is_query_show(sql_query: str) -> bool:
"""
Simple validation to ensure query is a show command
"""
sql_query = " ".join(sql_query.split())
match = SHOW_QUERY_RE.match(sql_query)
return True if match else False

Expand All @@ -95,6 +97,7 @@ def is_query_describe(sql_query: str) -> bool:
"""
Simple validation to ensure query is a describe command
"""
sql_query = " ".join(sql_query.split())
match = DESCRIBE_QUERY_RE.match(sql_query)
return True if match else False

Expand Down
25 changes: 24 additions & 1 deletion tests/admin/test_system_queries.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from snuba.admin.clickhouse.common import InvalidCustomQuery
from snuba.admin.clickhouse.system_queries import validate_system_query
from snuba.admin.clickhouse.system_queries import is_query_select, validate_system_query


@pytest.mark.parametrize(
Expand Down Expand Up @@ -39,3 +39,26 @@ def test_valid_system_query(sql_query: str) -> None:
def test_invalid_system_query(sql_query: str) -> None:
with pytest.raises(InvalidCustomQuery):
validate_system_query(sql_query)


select_sql = """
SELECT
table,
query,
format,
query_id,
bytes,
flush_time,
flush_query_id
FROM
system.asynchronous_insert_log
WHERE
status = 'Ok'
AND database = 'default'
AND flush_time > now() - toIntervalMinute(10)
ORDER BY table, flush_time
"""


def test_is_query_select() -> None:
assert is_query_select(select_sql) == True

0 comments on commit 0d888c8

Please sign in to comment.