Skip to content
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
18 changes: 13 additions & 5 deletions envs/sql_optim_env/server/graders.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ class GradeMask:


# Endings a keyword may carry and still name the same thing: plurals, tenses,
# and the -ate/-ation family that the deliberate stems (`aggreg`, `concatenat`)
# are written to reach. Any other letter or digit after a keyword means it is
# only the start of a different word (`star` in `started`, `join` in `joint`).
# `-ly` adverbs (`explicitly`), and the -ate/-ation family that the deliberate
# stems (`aggreg`, `concatenat`) are written to reach. Any other letter or digit
# after a keyword means it is only the start of a different word (`star` in
# `started`, `join` in `joint`).
_KW_SUFFIXES = (
"s",
"es",
"d",
"ed",
"e",
"ly",
"ing",
"ion",
"ions",
Expand All @@ -71,11 +73,14 @@ class GradeMask:
"ated",
"ating",
)
# A final consonant can double before -ing/-ed (`scan` -> `scanning`,
# `map` -> `mapped`).
_KW_CONSONANTS = frozenset("bcdfghjklmnpqrstvz")


@functools.lru_cache(maxsize=None)
def _kw_pattern(keyword: str) -> "re.Pattern[str]":
"""Compile *keyword* to match as a whole word, allowing `_KW_SUFFIXES`.
"""Compile *keyword* to match as a whole word, allowing its usual endings.

The boundary is a letter or digit, not `\\w`: an underscore separates words
here, so a structured `issue_type` such as `select_star` still matches
Expand All @@ -87,7 +92,10 @@ def _kw_pattern(keyword: str) -> "re.Pattern[str]":
if kw[:1].isalnum():
pattern = r"(?<![a-z0-9])" + pattern
if kw[-1:].isalnum():
pattern += r"(?:" + "|".join(_KW_SUFFIXES) + r")?(?![a-z0-9])"
endings = list(_KW_SUFFIXES)
if kw[-1] in _KW_CONSONANTS:
endings.append(re.escape(kw[-1]) + r"(?:ing|ed)")
pattern += r"(?:" + "|".join(endings) + r")?(?![a-z0-9])"
return re.compile(pattern)


Expand Down
10 changes: 8 additions & 2 deletions tests/envs/test_sql_optim_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,9 @@ class TestIssueDetectionMatching:
Ground-truth keywords were matched as plain substrings, so `star` (for
`SELECT *`) matched `started`, and a summary that never mentioned the issue
still earned its share of the 25% detection slice. A keyword may still carry
an inflection (`joins`, `ranking`), and the deliberate stems (`aggreg`,
`concatenat`) still reach the words they were written for.
an inflection (`joins`, `ranking`, `explicitly`, `scanning`), and the
deliberate stems (`aggreg`, `concatenat`) still reach the words they were
written for.
"""

@pytest.mark.parametrize(
Expand Down Expand Up @@ -1210,6 +1211,11 @@ def test_keyword_inside_another_word_does_not_match(self, keyword, text):
("select *", "SELECT * reads every column"),
("like '%", "LIKE '%buy%' scans"),
("CTE", "move it into a CTE"),
("explicit", "explicitly join the tables"),
("implicit", "the comma implicitly cross joins"),
("full scan", "a full scanning pass"),
("zone map", "zone mapping is disabled"),
("zone map", "the column is zone mapped"),
],
)
def test_whole_words_inflections_and_stems_still_match(self, keyword, text):
Expand Down
Loading