Skip to content

FIX: Keep Azure SQL analytics heatmaps within the report budget - #4790

Open
Behnam (behnam-o) wants to merge 3 commits into
microsoft:mainfrom
behnam-o:fix/14081-analytics-heatmap-timeout
Open

Behnam (behnam-o) wants to merge 3 commits into
microsoft:mainfrom
behnam-o:fix/14081-analytics-heatmap-timeout

Conversation

@behnam-o

Copy link
Copy Markdown
Collaborator

Description

Analytics heatmaps on Azure SQL time out for every filter. SQL Server takes 6–9 s just to compile the heatmap (matrix) statement, which exceeds the 5 s report budget. Stored JSON null harm categories also make OPENJSON fail with error 13609. This PR makes harm-category and attack-type charts finish well within the budget.

  • Compact profiles on SQL Server: harm-category and attack-type charts now use the existing compact-profile path, so the SDK aggregates a small set of pre-counted profiles instead of SQL compiling the full matrix. SQL Server returns raw harm arrays, which the SDK already decodes. Converter axes stay on the SQL path because only SQLite canonicalizes legacy converter objects.
  • Per-identifier completeness: for attack/converter axes, SQL Server now checks identifier completeness once per identifier, as SQLite already does, instead of parsing every result's embedded identifier JSON. Rows that fail the check still go through the legacy branches, so results don't change.
  • OPENJSON guard: membership expansions on SQL Server pass only arrays to OPENJSON, so a stored JSON null is classified as missing instead of raising.

Measured against a production database (30,767 results, 23,097 with JSON null harm categories), with the default 5 s budget and a warm pool:

Chart Before After
Harm category × attack type, operation = missing 504 timeout 1.4 s
Harm category × attack type 504 timeout 1.9 s
Attack type × harm category 504 timeout 1.5 s
Harm-category groups error 13609 0.4 s
Attack-type groups 14 s (SQL path) 1.4 s

All five outputs are identical to this branch's full SQL path. Attack-type groups are also identical to main's SQL path; main can't produce the harm-category charts.

Other heatmap axes (for example objective target or model) still use the SQL matrix statement and may still exceed the budget on large SQL Server databases; that is not addressed here. Related: #4648 bounds slow scenario-history reads, a separate problem.

On SQL Server, heatmaps compiled one large matrix statement whose compile
time alone (6-9 s) exceeded the 5 s report budget, and stored JSON null
harm categories made OPENJSON fail with error 13609.

- Let SQL Server use the compact-profile path for harm-category and
  attack-type charts; the SDK aggregates raw harm arrays.
- Check attack/converter identifier completeness once per identifier on
  SQL Server too, not by parsing each result's embedded JSON.
- Pass only arrays to OPENJSON when expanding harm/converter memberships.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 01:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The SQL Server profile path normalizes Unicode chart keys differently from SQL grouping and drilldown, causing missing drilldown results and unstable keys when falling back to SQL.

1 open issue
What changed in this PR

Reduces Azure SQL attack-result analytics query costs to help supported charts finish within the report budget.

Changes:

  • Enables bounded compact profiles for SQL Server harm-category and attack-type charts.
  • Checks attack/converter identifier completeness per identifier.
  • Guards OPENJSON against non-array values.
File Description
tests/​unit/​memory/​test_attack_analytics.py Adds SQL Server query-compilation and profile-eligibility checks.
pyrit/​memory/​attack_analytics.py Updates compact-profile docstrings for broader backend support.
pyrit/​memory/​attack_analytics_query.py Implements SQL Server profiles, identifier checks, and guarded array expansion.
pyrit/​analytics/​_profile_aggregation.py Removes SQLite-only wording from docstrings.
doc/​contributing/​11_memory_models.md Documents SQL Server compact-profile support.
doc/​code/​analytics/​0_attack_results.md Explains supported charts, profile limits, and SQL fallback behavior.

Review effort: Balanced

Comment thread pyrit/memory/attack_analytics_query.py Outdated
Comment on lines +243 to +244
return self._bounded_profiles(
profiles=compiler._compact_profile_inputs(dimensions),

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed in 1519279.

SQL Server profiles now also return LOWER(source) as key0/key1, and the SDK uses those as chart keys instead of Python's str.lower. Keys therefore come from the same function as SQL grouping and drilldown. SQLite is unchanged: its UnicodeLower already is Python str.lower.

I checked this on Azure SQL. LOWER over the JSON text gives the same result as LOWER over each OPENJSON member (the SQL path) for ΟΣ, İ, DŽ, ẞ and U+1C90, while Python disagrees on 4 of those 5. The one case where the two can differ is a \u-escaped member, because LOWER doesn't decode escapes. The reader falls back to the SQL path when a key contains \u.

Tests: SDK aggregation with the sigma example (keys and labels), misaligned key arrays rejected, escaped keys fall back, and the SQL Server compile asserts the LOWER key columns. Against the production database, all five harm/attack-type charts are still identical to the full SQL path, at 0.4–1.7 s.

Python str.lower differs from SQL Server LOWER (for example final sigma,
dotted capital I, and characters newer than the collation's Unicode
tables), so compact-profile chart keys could disagree with SQL grouping
and drilldown. SQL Server profiles now return LOWER(source) as key0/key1
and the SDK uses those keys. LOWER over serialized JSON cannot fold
\u-escaped members, so such probes fall back to the SQL path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants