Skip to content
Open
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
5 changes: 4 additions & 1 deletion doc/code/analytics/0_attack_results.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,10 @@ totals. The report and its initial page retain one consistent read transaction.

Scalar membership filters use indexable set predicates. Scalar facets project
values once, then combine deduplication and ordering before paging instead of
sorting the same values twice. SQLite 3.35+ uses query-local materialized projections to
sorting the same values twice. Facets with derived keys, such as Operation,
attack type, or objective target, group each result's projected key and label,
so SQLite's sorter does not carry the embedded identifier JSON their fallbacks
reference. SQLite 3.35+ uses query-local materialized projections to
avoid repeated JSON decoding; older SQLite keeps semantically equivalent
unhinted queries. None of this changes storage, persisted identities, schema,
global settings, or response caching.
Expand Down
11 changes: 11 additions & 0 deletions pyrit/memory/attack_analytics_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,17 @@ def _scalar_facet(self, *, query: AttackAnalyticsFacetQuery, source: _Source) ->
force=True,
)
kind, value, label = projected
else:
# SQLite's GROUP BY sorter copies every column a key references, including identifier JSON
# read only by untaken fallbacks, so group the small per-result keys projected first.
projected_values = self._materialize_sqlite(
select(kind.label("kind0"), value.label("value0"), label.label("label0"))
.select_from(origin)
.where(*conditions)
.cte("facet_values")
)
origin, conditions = projected_values, []
kind, value, label = projected_values.c.kind0, projected_values.c.value0, projected_values.c.label0
if query.search or query.search_pattern is not None:
conditions.append(self._search_condition(label=label, search=query.search, pattern=query.search_pattern))
minimum = func.min(self._collate(label))
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/memory/test_attack_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,16 @@ def test_mssql_scalar_facets_group_projected_keys_with_positional_bindings(dimen
assert "GROUP BY facet_values.kind0, facet_values.value0" in sql


@pytest.mark.parametrize("dimension", ["operation", "scenario", "attack_type", "objective_target"])
def test_sqlite_scalar_facets_group_materialized_keys(dimension: str) -> None:
query = AttackAnalyticsFacetQuery(dimension=AttackAnalyticsDimension(name=dimension))
with patch("pyrit.memory.attack_analytics_query.sqlite3.sqlite_version_info", (3, 35, 0)):
statement = AttackAnalyticsQueryCompiler(dialect="sqlite", filters=query.filters).facet(query)
sql = str(statement.compile(dialect=sqlite.dialect()))
assert "facet_values AS MATERIALIZED" in sql
assert "GROUP BY facet_values.kind0, facet_values.value0" in sql


@pytest.mark.parametrize(
("method", "names", "members"),
[
Expand Down
Loading