Skip to content

fix extract_signature dbapi2: handling square brakets in table name #1947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions elasticapm/instrumentation/packages/dbapi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def _scan_for_table_with_tokens(tokens, keyword):


def tokenize(sql):
# split on anything that is not a word character, excluding dots
return [t for t in re.split(r"([^\w.])", sql) if t != ""]
# split on anything that is not a word character or a square bracket, excluding dots
return [t for t in re.split(r"([^\w.\[\]])", sql) if t != ""]


def scan(tokens):
Expand Down
15 changes: 15 additions & 0 deletions tests/instrumentation/dbapi2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,18 @@ def test_extract_signature_for_procedure_call(sql, expected):
def test_extract_action_from_signature(sql, expected):
actual = extract_action_from_signature(sql, "query")
assert actual == expected


@pytest.mark.parametrize(
["sql", "expected"],
[
("SELECT username FROM user", "SELECT FROM user"),
("SELECT username FROM [user]", "SELECT FROM [user]"),
("SELECT username FROM [db].[user]", "SELECT FROM [db].[user]"),
("SELECT username FROM db.[user]", "SELECT FROM db.[user]"),
("SELECT username FROM [db].user", "SELECT FROM [db].user"),
],
)
def test_extract_signature_when_using_square_brackets(sql, expected):
actual = extract_signature(sql)
assert actual == expected