Skip to content

Validate VECTOR_DISTANCE dimensions in TSql170 - #2

Open
allancascante wants to merge 7 commits into
cenfosoft:mainfrom
allancascante:feature/vector-distance-dimension-validation
Open

allancascante wants to merge 7 commits into
cenfosoft:mainfrom
allancascante:feature/vector-distance-dimension-validation

Conversation

@allancascante

Copy link
Copy Markdown

Summary

  • Add T-SQL v170 AST validation for the fourth VECTOR_DISTANCE argument.
  • Report parse error 46146 when an integer dimension is outside the valid range of 1 through 1984.
  • Add an MSTest regression test for an invalid dimension of 0.

Implementation

  • Add Sql170ExpressionVisitor to inspect VECTOR_DISTANCE function calls.
  • Validate literal dimensions using invariant int.TryParse and C# pattern matching.
  • Run the visitor from TSql170Parser after parsing.
  • Return: Vector dimension must be an integer between 1 and 1984.

TDD Evidence

  • RED: With the visitor hook disabled, the focused test failed 1/1 at errors.Count > 0 because the parser returned no errors.
  • GREEN: With the visitor enabled, the same focused test passed 1/1.

Testing

Passed:

dotnet test Test/SqlDom/UTSqlScriptDom.csproj -c Debug -f net8.0 --filter "FullyQualifiedName~TestVectorDistance_InvalidDimension_ShouldReturnParseError"

The full net8.0 suite was started but canceled after test discovery. No test failure was reported before cancellation.

Code Changes

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace C# 9 patterns with equivalent C# 7.3-compatible checks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@allancascante allancascante added the adversarial-review:run Authorize an adversarial review after checks pass label Nov 30, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:run Authorize an adversarial review after checks pass labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 03:33 — with GitHub Actions Active
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Adversarial review

Risk: high. Human review required.

PR: cenfosoft/SqlScriptDOM #2
Head SHA: 7abb92aad5474860c5578e090021138232d1d751
Trusted configuration: 53c42b2045d2675bf0547620886741e4712ba0f6
Status: Human Review Required; rounds: 1/3

Workflow run

Deterministic checks

Check Platform Conclusion
build ubuntu passed
build windows passed
roslyn independent passed
semgrep independent passed
test ubuntu passed
test windows passed

Findings

vector-distance-dimension-validation-gap: VECTOR&microsoft#95;DISTANCE dimension check silently skips non-literal inputs and accepts invalid expressions

Severity: high; confidence: 0.92; disposition: unreviewed

The new validation is implemented as a post-parse AST walk that only checks &microsoft#96;node.Parameters&microsoft#91;3&microsoft#93; as IntegerLiteral&microsoft#96;. If the fourth parameter is not an &microsoft#96;IntegerLiteral&microsoft#96;, the visitor immediately returns without reporting an error, even though the function is supposed to enforce a dimension in the inclusive range 1..1984. This means malformed but syntactically accepted calls such as &microsoft#96;VECTOR&microsoft#95;DISTANCE&microsoft#40;&microsoft#39;cosine&microsoft#39;, v1, v2, @dim&microsoft#41;&microsoft#96;, &microsoft#96;VECTOR&microsoft#95;DISTANCE&microsoft#40;&microsoft#39;cosine&microsoft#39;, v1, v2, -1&microsoft#41;&microsoft#96;, or &microsoft#96;VECTOR&microsoft#95;DISTANCE&microsoft#40;&microsoft#39;cosine&microsoft#39;, v1, v2, CAST&microsoft#40;0 AS int&microsoft#41;&microsoft#41;&microsoft#96; can slip through as valid if the grammar permits them. The check is also based on &microsoft#96;NumberStyles.None&microsoft#96;, so it is not a general semantic validation of the scalar expression; it is a narrow literal-only gate. Because this check is injected after parsing in &microsoft#96;TSql170Parser.Parse&microsoft#96;, it creates inconsistent behavior: some invalid values are rejected, while other semantically invalid dimension expressions are not checked at all.

Recommendation: Validate the dimension expression semantically rather than only checking &microsoft#96;IntegerLiteral&microsoft#96;. If the grammar allows scalar expressions, parse and evaluate them in a way that handles unary negatives, variables, casts, and other expression forms before emitting the 46146 error. Add tests for boundary values and non-literal expression inputs to lock in the intended behavior.

vector-distance-test-coverage-gap: Only a single literal invalid-dimension test was added, leaving boundary and expression cases uncovered

Severity: medium; confidence: 0.88; disposition: unreviewed

The new test coverage is limited to one exact case: &microsoft#96;VECTOR&microsoft#95;DISTANCE&microsoft#40;&microsoft#39;cosine&microsoft#39;, v1, v2, 0&microsoft#41;&microsoft#96;. It does not exercise the boundaries specified by the validator &microsoft#40;&microsoft#96;1&microsoft#96; and &microsoft#96;1984&microsoft#96;&microsoft#41;, the out-of-range values &microsoft#40;&microsoft#96;-1&microsoft#96;, &microsoft#96;1985&microsoft#96;, and numbers outside the accepted integer width&microsoft#41;, or non-literal expressions such as variables, casts, unary negatives, or other scalar expressions. This matters because the implementation intentionally ignores non-&microsoft#96;IntegerLiteral&microsoft#96; dimensions, so the current tests do not protect against the acceptance gap documented in the implementation. The code does not include a regression test for the real semantic boundary conditions the visitor is supposed to enforce, which is a substantial coverage hole for a new parser validation rule.

Recommendation: Expand the test matrix to include valid boundary cases &microsoft#40;1 and 1984&microsoft#41;, invalid boundary cases &microsoft#40;-1 and 1985&microsoft#41;, and non-literal dimension expressions that should be rejected or accepted according to the SQL Server semantics. Keep the exact-script/baseline pattern consistent with the existing 170 parser tests to guard against future parser drift.

Model rounds

Round Provider / model Outcome Tokens in/out Estimated USD
1 github-copilot / mai-code-1.1-flash completed 19111/3917 0.008523

Risk assessment (rubric initial)

Factor Value Level
Changed code lines +80 / -0 low
Added-line coverage 28/30 added executable lines mid
Adversarial findings 0 low, 1 medium mid
Static-analysis warnings in changed files 0 low
Parser round-trip tests absent —
Unresolved high/critical finding yes (blocks merge) —

JEV assessment: high (confidence 0.83), model jev-1.14.0, tokens 658/40

  • JEV classified the overall risk as high.
  • An unresolved high or critical adversarial finding blocks merge readiness.
  • The adversarial review audit is incomplete; see blockers.

Label: adversarial-review:human-review (applied); automatic merge: not-eligible.

Blockers / follow-up

  • One or more configured review rounds are incomplete.
  • Unresolved high finding: vector-distance-dimension-validation-gap.

Limitations

  • Advisory only; existing CI remains authoritative.
  • Numeric risk weights and thresholds are not approved. No numeric score or merge clearance is produced.
  • Offline input validation does not establish GitHub event or artifact authenticity.
  • An unresolved critical/high finding requires human review; no further model calls were made.

@github-actions github-actions Bot added the adversarial-review:human-review Adversarial review requires human attention label Nov 30, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:run Authorize an adversarial review after checks pass adversarial-review:human-review Adversarial review requires human attention labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 04:05 — with GitHub Actions Active
@github-actions github-actions Bot added the adversarial-review:human-review Adversarial review requires human attention label Nov 30, 2026
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:run Authorize an adversarial review after checks pass labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 16:49 — with GitHub Actions Active
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:run Authorize an adversarial review after checks pass labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 17:16 — with GitHub Actions Active
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:run Authorize an adversarial review after checks pass labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 19:10 — with GitHub Actions Active
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:run Authorize an adversarial review after checks pass labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 19:25 — with GitHub Actions Active
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:human-review Adversarial review requires human attention adversarial-review:run Authorize an adversarial review after checks pass labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 21:30 — with GitHub Actions Active
@github-actions github-actions Bot added the adversarial-review:human-review Adversarial review requires human attention label Nov 30, 2026
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:run Authorize an adversarial review after checks pass labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 23:27 — with GitHub Actions Active
@allancascante allancascante added adversarial-review:run Authorize an adversarial review after checks pass and removed adversarial-review:run Authorize an adversarial review after checks pass labels Nov 30, 2026
@allancascante
allancascante deployed to adversarial-review November 30, 2026 23:47 — with GitHub Actions Active

This branch was successfully deployed

1 active (outdated) deployment
adversarial-review — 7abb92aa Deployed Nov 30, 2026 by allancascante via review #10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adversarial-review:human-review Adversarial review requires human attention adversarial-review:run Authorize an adversarial review after checks pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant