Repository navigation
Validate VECTOR_DISTANCE dimensions in TSql170 - #2
allancascante wants to merge 7 commits into
Conversation
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>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adversarial reviewRisk: high. Human review required. PR: cenfosoft/SqlScriptDOM #2 Deterministic checks
Findingsvector-distance-dimension-validation-gap: VECTORµsoft#95;DISTANCE dimension check silently skips non-literal inputs and accepts invalid expressionsSeverity: high; confidence: 0.92; disposition: unreviewed The new validation is implemented as a post-parse AST walk that only checks µsoft#96;node.Parametersµsoft#91;3µsoft#93; as IntegerLiteralµsoft#96;. If the fourth parameter is not an µsoft#96;IntegerLiteralµsoft#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 µsoft#96;VECTORµsoft#95;DISTANCEµsoft#40;µsoft#39;cosineµsoft#39;, v1, v2, @dimµsoft#41;µsoft#96;, µsoft#96;VECTORµsoft#95;DISTANCEµsoft#40;µsoft#39;cosineµsoft#39;, v1, v2, -1µsoft#41;µsoft#96;, or µsoft#96;VECTORµsoft#95;DISTANCEµsoft#40;µsoft#39;cosineµsoft#39;, v1, v2, CASTµsoft#40;0 AS intµsoft#41;µsoft#41;µsoft#96; can slip through as valid if the grammar permits them. The check is also based on µsoft#96;NumberStyles.Noneµsoft#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 µsoft#96;TSql170Parser.Parseµsoft#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 µsoft#96;IntegerLiteralµsoft#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 uncoveredSeverity: medium; confidence: 0.88; disposition: unreviewed The new test coverage is limited to one exact case: µsoft#96;VECTORµsoft#95;DISTANCEµsoft#40;µsoft#39;cosineµsoft#39;, v1, v2, 0µsoft#41;µsoft#96;. It does not exercise the boundaries specified by the validator µsoft#40;µsoft#96;1µsoft#96; and µsoft#96;1984µsoft#96;µsoft#41;, the out-of-range values µsoft#40;µsoft#96;-1µsoft#96;, µsoft#96;1985µsoft#96;, and numbers outside the accepted integer widthµsoft#41;, or non-literal expressions such as variables, casts, unary negatives, or other scalar expressions. This matters because the implementation intentionally ignores non-µsoft#96;IntegerLiteralµsoft#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 µsoft#40;1 and 1984µsoft#41;, invalid boundary cases µsoft#40;-1 and 1985µsoft#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
Risk assessment (rubric initial)
JEV assessment: high (confidence 0.83), model jev-1.14.0, tokens 658/40
Label: adversarial-review:human-review (applied); automatic merge: not-eligible. Blockers / follow-up
Limitations
|
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
VECTOR_DISTANCEargument.46146when an integer dimension is outside the valid range of 1 through 1984.Implementation
Sql170ExpressionVisitorto inspectVECTOR_DISTANCEfunction calls.int.TryParseand C# pattern matching.TSql170Parserafter parsing.Vector dimension must be an integer between 1 and 1984.TDD Evidence
1/1aterrors.Count > 0because the parser returned no errors.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