Skip to content

Commit c152271

Browse files
llaliMaksim VlasovAasim Khan
authored
Sync repos: Release 170.157.0 (#186)
* Merged PR 1821782: Fixing the parse issue with vector type Parser would timeout or crash when parsing queries with VECTOR data types in deeply nested JOIN structures due to exponential complexity (O(2^n)) in ANTLR's syntactic predicate lookahead. The `selectTableReferenceElement` grammar rule uses recursive syntactic predicates that cause exponential backtracking when VECTOR types appear in complex join hierarchies. Each additional nesting level doubles the parsing attempts. Implemented conditional optimization using `ContainsVectorInLookahead()` helper method: - Scans ahead up to 200 tokens to detect VECTOR keyword - When VECTOR detected: applies `SaveGuessing` optimization to reduce complexity from O(2^n) to O(n²) - When no VECTOR: uses standard grammar path with minimal overhead (~1-2ms) - **TSql170.g**: Modified `selectTableReferenceElement` rule with conditional SaveGuessing - **TSql170ParserBaseInternal.cs**: Added `ContainsVectorInLookahead()` helper method - **ComplexQueryTests170.sql**: Added comprehensive test coverage (6 queries) - 5 VECTOR scenarios: CAST, CONVERT, deeply nested JOINs with VECTOR types - 1 non-VECTOR scenario: Complex 6-way nested JOIN to validate no regression - All 1,122 tests passing (0 failures) - VECTOR queries: Previously timeout/crash → now parse successfully - Non-VECTOR queries: No measurable performance impact - Cross-version validation: Appropriate error counts for TSql80-160 - **Overhead**: O(1) per table reference (200-token scan) - **Typical queries**: <2ms additional overhead - **VECTOR queries**: Massive improvement (timeout → completes) - **Verdict**: Acceptable tradeoff for critical fix Related work items: #4819213 * Merged PR 1921093: [Fabric DW] Add support for Fabric DW specific AI functions This PR adds support for Fabric DW specific AI functions: - `ai_analyze_sentiment(<input>)` - `ai_classify(<input>, <class1>, <class2>[, <class 3>, ...])` - `ai_extract(<input>, <label1>[, <label2>, ...])` - `ai_fix_grammar(<input>)` - `ai_generate_response(<promptPart1>[, <promptPart2>])` - `ai_summarize(<input>)` - `ai_translate(<input>, <lang>)` Detailed design document is here: https://microsoft.sharepoint.com/:w:/r/teams/AzurePolaris/_layouts/15/doc2.aspx?sourcedoc=%7B75BAC667-A870-4482-8A37-F80E6EC8FCE0%7D&file=AI%20and%20Extensibiliy.docx Before submitting your pull request, please ensure you have completed the following: - [x] The [Common checklist](https://msdata.visualstudio.com/SQLToolsAndLibraries/_git/Common?path=/Templates/PR%20Checklist%20for%20SQLToolsAndLibraries.md&version=GBmain&_a=preview) has been reviewed and followed - [x] Code changes are accompanied by appropriate unit tests - [x] Identified and included SMEs needed to review code changes - [X] Follow the [steps](https://msdata.visualstudio.com/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki/33838/Adding-or-Extending-TSql-support-in-Sql-Dom?anchor=make-the-changes-in) here to make changes in the code - [x] Follow the [steps](https://msdata.visualstudio.com/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki/33838/Adding-or-Extending-TSql-support-in-Sql-Dom?anchor=to-extend-the-tests-do-the-following%3A) here to add new tests for your feature - [ ] Update relevant documentation in the [wiki](https://dev.azure.com/msdata/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki) and the README.md file ---- New feature: Adding support for Fabric DW–specific AI built-in functions. This pull request extends the SQL parser for Fabric DW by introducing new AI functions and their corresponding AST representations, script generators, and tests. Key changes include: - **`TSqlFabricDW.g`**: Added grammar rules for AI functions (e.g., AI_ANALYZE_SENTIMENT, AI_CLASSIFY, AI_EXTRACT, AI_GENERATE_RESPONSE, AI_SUMMARIZE, AI_TRANSLATE). - **`Ast.xml`**: Introduced new AST classes to represent the AI function calls. - **Script Generator Files**: Created new files in `SqlScriptDom/ScriptDom/SqlServer/ScriptGenerator/` to define visitors for the AI functions. - **`CodeGenerationSupporter.cs`**: Added new constants to support the AI function tokens. - **Test Files**: Included multiple test scripts and negative tests under `/Test/SqlDom/` to validate the syntax and error handling for the newly added functions. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot --> Related work items: #4924682 * Merged PR 1921976: Fixing IIF parsing when it occurs inside a parenthesis. # Pull Request Template for ScriptDom ## Description Fixes: #28 The TSql160Parser.ParseExpression() was failing to parse valid T-SQL expressions where IIF with boolean operators was wrapped in parentheses: ```sql SELECT 1 WHERE (IIF(1 > 0 AND 2 > 1, 1, 0)) = 1 ``` Customers reported this issue when using IIF expressions with multiple boolean operators (AND, OR, >, <, etc.) wrapped in parentheses. The original code used a simple counter (insideIIf) to track when inside an IIF call. The counter was decremented on the first boolean operator inside the IIF (e.g., >). When a second operator like AND was encountered, insideIIf was already 0, causing it to be misidentified as a top-level boolean operator - which incorrectly signaled that the outer parentheses contained a boolean expression instead of a scalar expression. Replaced the simple counter with stack-based parenthesis depth tracking. This properly tracks when we're inside an IIF by remembering the parenthesis depth where each IIF started, rather than incorrectly decrementing on boolean operators. This also correctly handles: Nested IIF expressions: `(IIF(IIF(a > 1, b, c) > 2, 1, 0))` Deeply nested parentheses: `((((IIF(1 > 0 AND 2 > 1, 1, 0)))) = 1` Multiple IIF expressions: `(IIF(...)) = 1 AND (IIF(...)) = 1` ## Code Change - [ ] The [Common checklist](https://msdata.visualstudio.com/SQLToolsAndLibraries/_git/Common?path=/Templates/PR%20Checklist%20for%20SQLToolsAndLibraries.md&version=GBmain&_a=preview) has been reviewed and followed - [ ] Code changes are accompanied by appropriate unit tests - [ ] Identified and included SMEs needed to review code changes - [ ] Follow the [steps](https://msdata.visualstudio.com/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki/33838/Adding-or-Extending-TSql-support-in-Sql-Dom?anchor=make-the-changes-in) here to make changes in the code ## Testing - [ ] Follow the [steps](https://msdata.visualstudio.com/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki/33838/Adding-or-Extending-TSql-support-in-Sql-Dom?anchor=to-extend-the-tests-do-the-following%3A) here to add new tests for your feature ## Documentation - [ ] Update relevant documentation in the [wiki](https://dev.azure.com/msdata/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki) and the README.md file ## Additional Information Please provide any additional information that might be helpful for the reviewers ---- #### AI description (iteration 1) #### PR Classification Bug fix to correct the parsing of IIF expressions when they occur inside parentheses. #### PR Summary This pull request fixes the incorrect handling of nested IIF expressions by replacing a simple counter with a stack-based approach and a pending flag, ensuring accurate tracking of IIF parentheses. The update enhances the SQL parser and adds regression tests to validate various scenarios. - `SqlScriptDom/Parser/TSql/TSql80ParserBaseInternal.cs`: Refactor... * Merged PR 1932484: Adding release notes for 170.157.0 # Pull Request Template for ScriptDom ## Description Adding release notes for 170.157.0 Before submitting your pull request, please ensure you have completed the following: ## Code Change - [ ] The [Common checklist](https://msdata.visualstudio.com/SQLToolsAndLibraries/_git/Common?path=/Templates/PR%20Checklist%20for%20SQLToolsAndLibraries.md&version=GBmain&_a=preview) has been reviewed and followed - [ ] Code changes are accompanied by appropriate unit tests - [ ] Identified and included SMEs needed to review code changes - [ ] Follow the [steps](https://msdata.visualstudio.com/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki/33838/Adding-or-Extending-TSql-support-in-Sql-Dom?anchor=make-the-changes-in) here to make changes in the code ## Testing - [ ] Follow the [steps](https://msdata.visualstudio.com/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki/33838/Adding-or-Extending-TSql-support-in-Sql-Dom?anchor=to-extend-the-tests-do-the-following%3A) here to add new tests for your feature ## Documentation - [ ] Update relevant documentation in the [wiki](https://dev.azure.com/msdata/SQLToolsAndLibraries/_wiki/wikis/SQLToolsAndLibraries.wiki) and the README.md file ## Additional Information Please provide any additional information that might be helpful for the reviewers Adding release notes for 170.157.0 ---- #### AI description (iteration 1) #### PR Classification Documentation update providing the release notes for version 170.157.0. #### PR Summary This pull request adds a new release notes file for Microsoft.SqlServer.TransactSql.ScriptDom 170.157.0, detailing supported platforms, dependency updates, new AI function support, and fixes for specific issues. - `release-notes/170/170.157.0.md`: New file containing the release information including target platform support, updated .NET SDK dependency (8.0.415), addition of Fabric DW-specific AI functions, and fixes for issues #161 and #28. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot --> --------- Co-authored-by: Maksim Vlasov <maksimvlasov@microsoft.com> Co-authored-by: Aasim Khan <Aasim.Khan@microsoft.com>
1 parent ab77781 commit c152271

40 files changed

+1651
-105
lines changed

.github/instructions/bug_fixing.guidelines.instructions.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ The process of fixing a bug, especially one that involves adding new syntax, fol
9090

9191
By following these steps, you can ensure that new syntax is correctly parsed, represented in the AST, generated back into a script, and fully validated by the testing framework without breaking existing functionality.
9292

93+
## Testing Best Practices
94+
95+
### ✅ DO: Use Existing Test Framework
96+
- Add test methods to existing test classes like `Only170SyntaxTests.cs`
97+
- Use the established `TSqlParser.Parse()` pattern for verification
98+
- Example:
99+
```csharp
100+
[TestMethod]
101+
public void VerifyNewSyntax()
102+
{
103+
var parser = new TSql170Parser(true);
104+
var result = parser.Parse(new StringReader("YOUR SQL HERE"), out var errors);
105+
Assert.AreEqual(0, errors.Count, "Should parse without errors");
106+
}
107+
```
108+
109+
### ❌ DON'T: Create New Test Projects
110+
- **Never** create standalone `.csproj` files for testing parser functionality
111+
- **Never** create new console applications or test runners
112+
- This causes build issues and doesn't integrate with the existing test infrastructure
113+
93114
## Special Case: Extending Grammar Rules from Literals to Expressions
94115

95116
A common type of bug involves extending existing grammar rules that only accept literal values (like integers or strings) to accept full expressions (parameters, variables, outer references, etc.). This pattern was used to fix the VECTOR_SEARCH TOP_N parameter issue.

.github/instructions/grammer.guidelines.instructions.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,32 @@ yourContextParameterRule returns [ScalarExpression vResult]
6464
Most script generators using `GenerateNameEqualsValue()` or similar methods work automatically with `ScalarExpression`. No changes typically needed.
6565

6666
#### Step 4: Add Test Coverage
67-
```sql
68-
-- Test parameter
69-
FUNCTION_NAME(PARAM = @parameter)
70-
71-
-- Test outer reference
72-
FUNCTION_NAME(PARAM = outerref.column)
73-
74-
-- Test computed expression
75-
FUNCTION_NAME(PARAM = value + 1)
67+
Add tests within the existing test framework:
68+
```csharp
69+
[TestMethod]
70+
public void VerifyGrammarExtension()
71+
{
72+
var parser = new TSql170Parser(true);
73+
74+
// Test parameter
75+
var sql1 = "SELECT FUNCTION_NAME(PARAM = @parameter)";
76+
var result1 = parser.Parse(new StringReader(sql1), out var errors1);
77+
Assert.AreEqual(0, errors1.Count, "Parameter syntax should work");
78+
79+
// Test outer reference
80+
var sql2 = "SELECT FUNCTION_NAME(PARAM = outerref.column)";
81+
var result2 = parser.Parse(new StringReader(sql2), out var errors2);
82+
Assert.AreEqual(0, errors2.Count, "Outer reference syntax should work");
83+
84+
// Test computed expression
85+
var sql3 = "SELECT FUNCTION_NAME(PARAM = value + 1)";
86+
var result3 = parser.Parse(new StringReader(sql3), out var errors3);
87+
Assert.AreEqual(0, errors3.Count, "Computed expression syntax should work");
88+
}
7689
```
7790

91+
**⚠️ CRITICAL**: Add this test method to an existing test class (e.g., `Only170SyntaxTests.cs`). **Never create standalone test projects.**
92+
7893
### Real-World Example: VECTOR_SEARCH TOP_N
7994

8095
**Problem**: `VECTOR_SEARCH` TOP_N parameter only accepted integer literals.

.github/instructions/parser.guidelines.instructions.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,21 @@ case TSql80ParserInternal.Identifier:
5959
## Step-by-Step Fix Process
6060

6161
### 1. Reproduce the Issue
62-
Create a test case to confirm the bug:
63-
```sql
64-
SELECT 1 WHERE (REGEXP_LIKE('a', 'pattern')); -- Should fail without fix
62+
Create a test case within the existing test framework to confirm the bug:
63+
```csharp
64+
[TestMethod]
65+
public void ReproduceParenthesesIssue()
66+
{
67+
var parser = new TSql170Parser(true);
68+
var sql = "SELECT 1 WHERE (REGEXP_LIKE('a', 'pattern'));";
69+
var result = parser.Parse(new StringReader(sql), out var errors);
70+
// Should fail before fix, pass after fix
71+
Assert.AreEqual(0, errors.Count, "Should parse without errors after fix");
72+
}
6573
```
6674

75+
**⚠️ IMPORTANT**: Add this test to an existing test class like `Only170SyntaxTests.cs`, **do not** create a new test project.
76+
6777
### 2. Identify the Predicate Constant
6878
Find the predicate identifier in `CodeGenerationSupporter`:
6979
```csharp

.github/instructions/testing.guidelines.instructions.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,43 @@ The SqlScriptDOM testing framework validates parser functionality through:
1313
4. **Version-Specific Testing** - Tests syntax across multiple SQL Server versions (SQL 2000-2025)
1414
5. **Exact T-SQL Verification** - When testing specific T-SQL syntax from prompts or user requests, the **exact T-SQL statement must be included and verified** in the test to ensure the specific syntax works as expected
1515

16+
## Quick Verification Tests
17+
18+
For rapid verification and debugging, add simple test methods directly to existing test classes:
19+
20+
```csharp
21+
[TestMethod]
22+
[Priority(0)]
23+
[SqlStudioTestCategory(Category.UnitTest)]
24+
public void VerifyMyNewSyntax()
25+
{
26+
var parser = new TSql170Parser(true);
27+
28+
// Test basic syntax
29+
var query1 = "SELECT YOUR_NEW_FUNCTION('param1', 'param2');";
30+
var result1 = parser.Parse(new StringReader(query1), out var errors1);
31+
Assert.AreEqual(0, errors1.Count, "Basic syntax should parse");
32+
33+
// Test complex variations
34+
var query2 = "SELECT YOUR_NEW_FUNCTION(@variable);";
35+
var result2 = parser.Parse(new StringReader(query2), out var errors2);
36+
Assert.AreEqual(0, errors2.Count, "Variable syntax should parse");
37+
38+
Console.WriteLine("✅ All tests passed!");
39+
}
40+
```
41+
42+
**Where to Add Quick Tests:**
43+
- **SQL Server 2025 (170) features**: Add to `Test/SqlDom/Only170SyntaxTests.cs`
44+
- **SQL Server 2022 (160) features**: Add to `Test/SqlDom/Only160SyntaxTests.cs`
45+
- **Earlier versions**: Add to corresponding `Only<version>SyntaxTests.cs`
46+
47+
**When to Use:**
48+
- Quick verification during development
49+
- Debugging parser issues
50+
- Initial syntax validation before full test suite
51+
- Rapid prototyping of test cases
52+
1653
## Test Framework Architecture
1754

1855
### Core Components
@@ -29,6 +66,30 @@ The SqlScriptDOM testing framework validates parser functionality through:
2966
3. **Validate Phase**: Generated output is compared against baseline file
3067
4. **Error Validation**: Parse error count is compared against expected error count for each SQL version
3168

69+
## ❌ Anti-Patterns: What NOT to Do
70+
71+
### Do NOT Create New Test Projects
72+
73+
-**Don't create new `.csproj` files for testing**
74+
-**Don't create console applications** like `TestVectorParser.csproj` or `debug_complex.csproj`
75+
-**Don't create standalone test runners**
76+
-**Don't add new projects to the solution for testing**
77+
78+
### Why This Causes Problems
79+
80+
1. **Build Issues**: New projects often fail to build due to missing dependencies
81+
2. **Integration Problems**: Standalone projects don't integrate with existing test infrastructure
82+
3. **Maintenance Overhead**: Additional projects require separate maintenance and documentation
83+
4. **CI/CD Conflicts**: Build pipelines aren't configured for ad-hoc test projects
84+
5. **Resource Waste**: Creates duplicate testing infrastructure instead of using established patterns
85+
86+
### The Correct Approach
87+
88+
**Always add test methods to existing test classes**:
89+
- Add to `Test/SqlDom/Only170SyntaxTests.cs` for SQL Server 2025 features
90+
- Add to `Test/SqlDom/Only160SyntaxTests.cs` for SQL Server 2022 features
91+
- Use the established test framework patterns documented in this guide
92+
3293
## Adding New Tests
3394

3495
### 1. Create Test Script
@@ -639,6 +700,52 @@ new ParserTest170("ErrorConditionTests170.sql",
639700
nErrors160: 3, nErrors170: 3),
640701
```
641702

703+
## Real-World Example: VECTOR Parsing Verification
704+
705+
This example shows the correct approach used to verify VECTOR data type parsing functionality:
706+
707+
```csharp
708+
[TestMethod]
709+
[Priority(0)]
710+
[SqlStudioTestCategory(Category.UnitTest)]
711+
public void VerifyComplexQueryFix()
712+
{
713+
// Test VECTOR parsing in various contexts - this is the real bug we found and fixed
714+
var parser = new TSql170Parser(true);
715+
716+
// Test 1: Basic VECTOR with base type
717+
var query1 = "SELECT CAST('[1,2,3]' AS VECTOR(3, Float32));";
718+
var result1 = parser.Parse(new StringReader(query1), out var errors1);
719+
Assert.AreEqual(0, errors1.Count, "Basic VECTOR with base type should parse");
720+
721+
// Test 2: VECTOR in complex CAST (from original failing query)
722+
var query2 = "SELECT CAST('[-6.464173E+08,1.040823E+07,1.699169E+08]' AS VECTOR(3, Float32));";
723+
var result2 = parser.Parse(new StringReader(query2), out var errors2);
724+
Assert.AreEqual(0, errors2.Count, "VECTOR with scientific notation should parse");
725+
726+
// Test 3: VECTOR in CONVERT (from original failing query)
727+
var query3 = "SELECT CONVERT(VECTOR(77), '[-7.230808E+08,4.075427E+08]');";
728+
var result3 = parser.Parse(new StringReader(query3), out var errors3);
729+
Assert.AreEqual(0, errors3.Count, "VECTOR in CONVERT should parse");
730+
731+
// Test 4: VECTOR in JOIN context (simplified version of original complex query)
732+
var query4 = @"SELECT t1.id
733+
FROM table1 t1
734+
INNER JOIN table2 t2 ON t1.vector_col = CAST('[1,2,3]' AS VECTOR(3, Float32));";
735+
var result4 = parser.Parse(new StringReader(query4), out var errors4);
736+
Assert.AreEqual(0, errors4.Count, "VECTOR in JOIN condition should parse");
737+
738+
Console.WriteLine("✅ All VECTOR parsing tests passed - the original VECTOR bug is fixed!");
739+
}
740+
```
741+
742+
**Key Points from This Example:**
743+
1. Test was added directly to `Only170SyntaxTests.cs` - no new project created
744+
2. Tests multiple contexts where the syntax appears (CAST, CONVERT, JOIN)
745+
3. Uses inline assertions with descriptive messages
746+
4. References the original bug being fixed
747+
5. Provides immediate feedback via Console.WriteLine
748+
642749
## Advanced Testing Patterns
643750

644751
### Multi-File Tests

SqlScriptDom/Parser/TSql/Ast.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,6 +4769,38 @@
47694769
<Member Name="ModelName" Type="SchemaObjectName" Summary="Model name used in USE MODEL clause." />
47704770
<Member Name="OptionalParameters" Type="ScalarExpression" Summary="Optional PARAMETERS clause, must evaluate to JSON." />
47714771
</Class>
4772+
<Class Name="AIAnalyzeSentimentFunctionCall" Base="PrimaryExpression" Summary="Represents the ai_analyze_sentiment built-in.">
4773+
<InheritedClass Name="PrimaryExpression" />
4774+
<Member Name="Input" Type="ScalarExpression" Summary="Input text to analyze sentiment for." />
4775+
</Class>
4776+
<Class Name="AIFixGrammarFunctionCall" Base="PrimaryExpression" Summary="Represents the ai_fix_grammar built-in.">
4777+
<InheritedClass Name="PrimaryExpression" />
4778+
<Member Name="Input" Type="ScalarExpression" Summary="Input text to fix grammar for." />
4779+
</Class>
4780+
<Class Name="AIClassifyFunctionCall" Base="PrimaryExpression" Summary="Represents AI_CLASSIFY(...) function call.">
4781+
<InheritedClass Name="PrimaryExpression" />
4782+
<Member Name="Input" Type="ScalarExpression" Summary="Variable or column identifier providing the text to classify." />
4783+
<Member Name="Labels" Type="ScalarExpression" Collection="true" Summary="One or more label expressions (at least two)." />
4784+
</Class>
4785+
<Class Name="AIExtractFunctionCall" Base="PrimaryExpression" Summary="Represents the ai_extract built-in.">
4786+
<InheritedClass Name="PrimaryExpression" />
4787+
<Member Name="Input" Type="ScalarExpression" Summary="Input text to extract from." />
4788+
<Member Name="Labels" Type="ScalarExpression" Collection="true" Summary="One or more label expressions." />
4789+
</Class>
4790+
<Class Name="AIGenerateResponseFunctionCall" Base="PrimaryExpression" Summary="Represents AI_GENERATE_RESPONSE(...) function call.">
4791+
<InheritedClass Name="PrimaryExpression" />
4792+
<Member Name="PromptPart1" Type="ScalarExpression" Summary="First prompt part (required)." />
4793+
<Member Name="PromptPart2" Type="ScalarExpression" Summary="Second prompt part (optional)." />
4794+
</Class>
4795+
<Class Name="AISummarizeFunctionCall" Base="PrimaryExpression" Summary="Represents AI_SUMMARIZE(...) function call.">
4796+
<InheritedClass Name="PrimaryExpression" />
4797+
<Member Name="Input" Type="ScalarExpression" Summary="Variable or column identifier providing the text to summarize." />
4798+
</Class>
4799+
<Class Name="AITranslateFunctionCall" Base="PrimaryExpression" Summary="Represents AI_TRANSLATE(...) function call.">
4800+
<InheritedClass Name="PrimaryExpression" />
4801+
<Member Name="Input" Type="ScalarExpression" Summary="Text to translate; variable or column reference." />
4802+
<Member Name="Language" Type="ScalarExpression" Summary="Target language; typically string literal or identifier." />
4803+
</Class>
47724804
<Class Name="VectorSearchTableReference" Base="TableReferenceWithAlias" Summary="Represents the VECTOR_SEARCH table-valued function call.">
47734805
<InheritedClass Name="TableReferenceWithAlias" />
47744806
<Member Name="Table" Type="TableReferenceWithAlias" Summary="Table on which perform the search." />

SqlScriptDom/Parser/TSql/CodeGenerationSupporter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ internal static class CodeGenerationSupporter
101101
internal const string Aggregate = "AGGREGATE";
102102
internal const string AiGenerateChunks = "AI_GENERATE_CHUNKS";
103103
internal const string AIGenerateEmbeddings = "AI_GENERATE_EMBEDDINGS";
104+
internal const string AIAnalyzeSentiment = "AI_ANALYZE_SENTIMENT";
105+
internal const string AIClassify = "AI_CLASSIFY";
106+
internal const string AIExtract = "AI_EXTRACT";
107+
internal const string AIFixGrammar = "AI_FIX_GRAMMAR";
108+
internal const string AIGenerateResponse = "AI_GENERATE_RESPONSE";
109+
internal const string AISummarize = "AI_SUMMARIZE";
110+
internal const string AITranslate = "AI_TRANSLATE";
104111
internal const string Algorithm = "ALGORITHM";
105112
internal const string AlterColumn = "ALTERCOLUMN";
106113
internal const string All = "ALL";

SqlScriptDom/Parser/TSql/TSql170.g

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19142,9 +19142,21 @@ joinElement[SubDmlFlags subDmlFlags, ref TableReference vResult]
1914219142
;
1914319143

1914419144
selectTableReferenceElement [SubDmlFlags subDmlFlags] returns [TableReference vResult = null]
19145+
{
19146+
IToken tAfterJoinParenthesis = null;
19147+
}
1914519148
:
19149+
// Apply SaveGuessing optimization ONLY when VECTOR keyword is detected in lookahead
19150+
// This fixes VECTOR parsing in deeply nested JOINs without breaking other valid SQL patterns
19151+
{ContainsVectorInLookahead()}?
19152+
({ if (!SkipGuessing(tAfterJoinParenthesis)) }:
19153+
(joinParenthesis[subDmlFlags])=> ({ SaveGuessing(out tAfterJoinParenthesis); }:))=>
19154+
({ if (!SkipGuessing(tAfterJoinParenthesis)) }:
19155+
vResult=joinParenthesis[subDmlFlags])
19156+
|
19157+
// Standard syntactic predicate for all other cases
1914619158
(joinParenthesis[subDmlFlags])=>
19147-
vResult=joinParenthesis[subDmlFlags]
19159+
vResult=joinParenthesis[subDmlFlags]
1914819160
| vResult=selectTableReferenceElementWithoutJoinParenthesis[subDmlFlags]
1914919161
;
1915019162

SqlScriptDom/Parser/TSql/TSql170ParserBaseInternal.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,44 @@ protected SecurityObjectKind ParseSecurityObjectKindTSql170(Identifier identifie
7070
return TSql160ParserBaseInternal.ParseSecurityObjectKind(identifier1, identifier2);
7171
}
7272
}
73+
74+
/// <summary>
75+
/// Checks if VECTOR keyword appears in the upcoming tokens within a reasonable lookahead window.
76+
/// Used to determine if SaveGuessing optimization is needed for VECTOR data type parsing.
77+
/// </summary>
78+
/// <returns>true if VECTOR keyword found in lookahead; false otherwise</returns>
79+
protected bool ContainsVectorInLookahead()
80+
{
81+
// Scan ahead looking for VECTOR keyword (case-insensitive identifier match)
82+
83+
const int LookaheadLimit = 100; // Define a named constant for the lookahead limit
84+
// We scan up to LookaheadLimit tokens to handle deeply nested JOIN structures with VECTOR types
85+
for (int i = 1; i <= LookaheadLimit; i++)
86+
{
87+
IToken token;
88+
try
89+
{
90+
token = LT(i);
91+
}
92+
catch (Exception ex)
93+
{
94+
Debug.WriteLine($"Error accessing token at lookahead index {i}: {ex.Message}");
95+
break;
96+
}
97+
if (token == null || token.Type == Token.EOF_TYPE)
98+
{
99+
break;
100+
}
101+
102+
// Check if this is an identifier token with text "VECTOR"
103+
if (token.Type == TSql170ParserInternal.Identifier &&
104+
string.Equals(token.getText(), CodeGenerationSupporter.Vector, StringComparison.OrdinalIgnoreCase))
105+
{
106+
return true;
107+
}
108+
}
109+
110+
return false;
111+
}
73112
}
74113
}

0 commit comments

Comments
 (0)