Skip to content

Commit 0cc00b5

Browse files
authored
Adding support for CREATE EVENT SESSION statement with NOT LIKE predicate (#158)
1 parent 092a97c commit 0cc00b5

24 files changed

+313
-67
lines changed

.github/BUG_FIXING_GUIDE.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Bug Fixing Guide for SqlScriptDOM
2+
3+
This guide provides a summary of the typical workflow for fixing a bug in the SqlScriptDOM parser, based on practical experience. For a more comprehensive overview of the project structure and code generation, please refer to the main [Copilot / AI instructions for SqlScriptDOM](copilot-instructions.md).
4+
5+
## Summary of the Bug-Fixing Workflow
6+
7+
The process of fixing a bug, especially one that involves adding new syntax, follows these general steps:
8+
9+
1. **Grammar Modification**:
10+
* Identify the correct grammar rule to modify in the `SqlScriptDom/Parser/TSql/*.g` files.
11+
* Apply the necessary changes to all relevant `.g` files, from the version where the syntax was introduced up to the latest version (e.g., `TSql130.g` through `TSql170.g` and `TSqlFabricDW.g`).
12+
13+
2. **Abstract Syntax Tree (AST) Update**:
14+
* If the new syntax requires a new AST node or enum member, edit `SqlScriptDom/Parser/TSql/Ast.xml`. For example, adding a new operator like `NOT LIKE` required adding a `NotLike` member to the `BooleanComparisonType` enum.
15+
16+
3. **Script Generation Update**:
17+
* Update the script generator to handle the new AST node or enum. This typically involves modifying files in `SqlScriptDom/ScriptDom/SqlServer/ScriptGenerator/`. For the `NOT LIKE` example, this meant adding an entry to the `_booleanComparisonTypeGenerators` dictionary in `SqlScriptGeneratorVisitor.CommonPhrases.cs`.
18+
19+
4. **Build the Project**:
20+
* After making code changes, run a build to regenerate the parser and ensure everything compiles correctly:
21+
```bash
22+
dotnet build
23+
```
24+
25+
5. **Add a Unit Test**:
26+
* Create a new `.sql` file in `Test/SqlDom/TestScripts/` that contains the specific syntax for the new test case.
27+
28+
6. **Define the Test Case**:
29+
* Add a new `ParserTest` entry to the appropriate `Only<version>SyntaxTests.cs` files (e.g., `Only130SyntaxTests.cs`). This entry points to your new test script and defines the expected number of parsing errors for each SQL Server version.
30+
31+
7. **Generate and Verify Baselines**:
32+
This is a critical and multi-step process:
33+
* **a. Create Placeholder Baseline Files**: Before running the test, create empty or placeholder baseline files in the corresponding `Test/SqlDom/Baselines<version>/` directories. The filename must match the test script's filename.
34+
* **b. Run the Test to Get the Generated Script**: Run the specific test that you just added. It is *expected to fail* because the placeholder baseline will not match the script generated by the parser.
35+
```bash
36+
# Example filter for running a specific test
37+
dotnet test --filter "FullyQualifiedName~YourTestMethodName"
38+
```
39+
* **c. Update the Baseline Files**: Copy the "Actual" output from the test failure log. This is the correctly formatted script generated from the AST. Paste this content into all the baseline files you created in step 7a.
40+
* **d. Re-run the Tests**: Run the same test command again. This time, the tests should pass, confirming that the generated script matches the new baseline.
41+
42+
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.

.github/copilot-instructions.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,65 @@
1+
# Copilot / AI instructions for SqlScriptDOM
2+
13
ScriptDom is a library for parsing and generating T-SQL scripts. It is primarily used by DacFx to build database projects, perform schema comparisons, and generate scripts for deployment.
24

3-
T-SQL syntax definitions are defined in the .g files in SqlScriptDom/Parser/TSql/. The file names map to SQL Server versions, e.g. TSql170.g corresponds to the syntax definitions for SQL Server 2025, TSql160.g to SQL Server 2022, etc. Syntax for Azure SQL Database should always be based on the latest SQL Server version.
5+
## Key points (quick read)
6+
- Grammar files live in: `SqlScriptDom/Parser/TSql/` — each file corresponds to a SQL Server version (e.g. `TSql170.g` for 170 / SQL Server 2025).
7+
- Grammar format: ANTLR v2. Generated C# lexer/parser code is produced during the build (see `GenerateFiles.props`).
8+
- Build & tests: use the .NET SDK pinned in `global.json`. Typical commands from repo root:
9+
- `dotnet build -c Debug`
10+
- `dotnet test Test/SqlDom/UTSqlScriptDom.csproj -c Debug`
11+
- To regenerate parser/token/AST sources explicitly, build the main project (generation targets are hooked into its build):
12+
- `dotnet build SqlScriptDom/Microsoft.SqlServer.TransactSql.ScriptDom.csproj -c Debug`
13+
- (or) `dotnet msbuild SqlScriptDom/Microsoft.SqlServer.TransactSql.ScriptDom.csproj -t:GLexerParserCompile;GSqlTokenTypesCompile;CreateAST -p:Configuration=Debug`
14+
15+
## Why files are generated and where
16+
- `SqlScriptDom/GenerateFiles.props` contains the MSBuild targets invoked during the library build:
17+
- `GSqlTokenTypesCompile` / `GLexerParserCompile` -> run ANTLR and post-process outputs (powershell/sed scripts)
18+
- `CreateAST` -> runs AstGen tool (from `tools/AstGen`) to generate AST visitor/fragment classes
19+
- `GenerateEverything` -> runs ScriptGenSettingsGenerator and TokenListGenerator
20+
- The Antlr binary is downloaded to the path defined in `Directory.Build.props` (`AntlrLocation`) when the build runs (via the `InstallAntlr` target).
21+
- Generated C# files are written to `$(CsGenIntermediateOutputPath)` (under `obj/...` by default). Do not hand-edit generated files — change the .g grammar or post-processing scripts instead.
22+
23+
## Important files and folders (read these first)
24+
- `SqlScriptDom/Parser/TSql/*.g` — ANTLR v2 grammar files (TSql80..TSql170 etc.). Example: `TSql170.g` defines new-170 syntax.
25+
- `SqlScriptDom/GenerateFiles.props` and `Directory.Build.props` — define code generation targets and antlr location.
26+
- `SqlScriptDom/ParserPostProcessing.sed`, `LexerPostProcessing.sed`, `TSqlTokenTypes.ps1` — post-processing for generated C# sources and tokens.
27+
- `tools/` — contains code generators used during build: `AstGen`, `ScriptGenSettingsGenerator`, `TokenListGenerator`.
28+
- `Test/SqlDom/` — unit tests, baselines and test scripts. See `Only170SyntaxTests.cs`, `TestScripts/`, and `Baselines170/`.
29+
30+
## Developer workflow & conventions (typical change cycle)
31+
1. Add/modify grammar rule(s) in the correct `TSql*.g` (pick the _version_ the syntax belongs to).
32+
2. If tokens or token ordering change, update `TSqlTokenTypes.g` (and the sed/ps1 post-processors if necessary).
33+
3. Rebuild the ScriptDom project to regenerate parser and AST (`dotnet build` will run generation). Use the targeted msbuild targets if you only want generation.
34+
4. Add tests:
35+
- Put the input SQL in `Test/SqlDom/TestScripts/` (filename is case sensitive and used as an embedded resource).
36+
- Add/confirm baseline output in `Test/SqlDom/Baselines<version>/` (the UT project embeds these baselines as resources).
37+
- Update the appropriate `Only<version>SyntaxTests.cs` (e.g., `Only170SyntaxTests.cs`) by adding a `ParserTest170("MyNewTest.sql", ...)` entry. See `ParserTest.cs` and `ParserTestOutput.cs` for helper constructors and verification semantics.
38+
5. Run `dotnet test Test/SqlDom/UTSqlScriptDom.csproj -c Debug` and iterate until tests pass.
39+
40+
## Testing details and how tests assert correctness
41+
- Tests run a full parse -> script generator -> reparse round-trip. Baseline comparison verifies pretty-printed generated scripts exactly match the stored baseline.
42+
- Expected parse errors (where applicable) are verified by number and exact error messages; test helpers live in `ParserTest.cs`, `ParserTestOutput.cs`, and `ParserTestUtils.cs`.
43+
- If a test fails due to mismatch in generated script, compare the generated output (the test harness logs it) against the baseline to spot formatting/structure differences.
44+
45+
## Bug Fixing and Baseline Generation
46+
For a practical guide on fixing bugs, including the detailed workflow for generating test baselines, see the [Bug Fixing Guide](BUG_FIXING_GUIDE.md).
47+
48+
## Editing generated outputs, debugging generation
49+
- Never edit generated files permanently (they live under `obj/...`/CsGenIntermediateOutputPath). Instead change:
50+
- `.g` grammar files
51+
- post-processing scripts (`*.ps1`/`*.sed`)
52+
- AST XML in `SqlScriptDom/Parser/TSql/Ast.xml` if AST node shapes need to change (used by `tools/AstGen`).
53+
- To see antlr output/errors, force verbose generation by setting MSBuild property `OutputErrorInLexerParserCompile=true` on the command line (e.g. `dotnet msbuild -t:GLexerParserCompile -p:OutputErrorInLexerParserCompile=true`).
54+
- If the antlr download fails during build, manually download `antlr-2.7.5.jar` (for non-Windows) or `.exe` (for Windows) and place it at the location defined in `Directory.Build.props` or override `AntlrLocation` when invoking msbuild.
55+
456

5-
The grammar files are in ANTLR v2 format. C# code is generated from these grammar files as part of the build process.
57+
## Patterns & code style to follow (examples you will see)
58+
- Grammar rule pattern: `ruleName returns [Type vResult = this.FragmentFactory.CreateFragment<Type>()] { ... } : ( alternatives ) ;` — this pattern initializes an AST fragment via FragmentFactory.
59+
- Parser-generated code frequently uses `Match(<token>, CodeGenerationSupporter.<Symbol>)` and `ThrowParseErrorException("SQLxxxx", ...)` for diagnostics.
60+
- The codebase prefers using the factory and fragment visitors for AST creation and script generation. Look at `ScriptDom/SqlServer/ScriptGenerator` for script generation patterns.
661

7-
For each new syntax definition, ScriptDom needs to be able to parse it successfully, and roundtrip back to the original script via the script generator.
62+
## Grammar Gotchas & Common Pitfalls
63+
- **Operator vs. Function-Style Predicates:** Be careful to distinguish between standard T-SQL operators (like `NOT LIKE`, `>`, `=`) and the function-style predicates used in some contexts (like `package.equals(...)` in `CREATE EVENT SESSION`). For example, `NOT LIKE` in an event session's `WHERE` clause is a standard comparison operator, not a function call. Always verify the exact T-SQL syntax before modifying the grammar.
64+
- **Logical `NOT` vs. Compound Operators:** The grammar handles the logical `NOT` operator (e.g., `WHERE NOT (condition)`) in a general way, often in a `booleanExpressionUnary` rule. This is distinct from compound operators like `NOT LIKE` or `NOT IN`, which are typically parsed as a single unit within a comparison rule. Don't assume that because `NOT` is supported, `NOT LIKE` will be automatically supported in all predicate contexts.
865

9-
Changes need to have accompanying tests in Only170SyntaxTests.cs or the one for its respective version. The test framework should already verify the parser and script generator; you just need to add the test scripts to TestScripts and corresponding Baselines folder. Older syntaxes should be supported unless explicitly stated otherwise.

SqlScriptDom/Parser/TSql/BooleanComparisonType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,10 @@ public enum BooleanComparisonType
6666
/// The distinct predicate, IS NOT DISTINCT FROM.
6767
/// </summary>
6868
IsNotDistinctFrom = 12,
69+
70+
/// <summary>
71+
/// The NOT LIKE predicate
72+
/// </summary>
73+
NotLike = 13,
6974
}
7075
}

SqlScriptDom/Parser/TSql/CodeGenerationSupporter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ internal static class CodeGenerationSupporter
556556
internal const string Level3 = "LEVEL_3";
557557
internal const string Level4 = "LEVEL_4";
558558
internal const string Library = "LIBRARY";
559+
internal const string Like = "LIKE";
559560
internal const string LifeTime = "LIFETIME";
560561
internal const string Linux = "LINUX";
561562
internal const string List = "LIST";

SqlScriptDom/Parser/TSql/TSql130.g

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7289,16 +7289,14 @@ eventDeclarationComparisonPredicate [BooleanComparisonExpression vParent, EventS
72897289
BooleanComparisonType vType = BooleanComparisonType.Equals;
72907290
ScalarExpression eventValue;
72917291
}
7292-
: vType = comparisonOperator eventValue = eventDeclarationValue
7293-
{
7294-
vSourceDeclaration.Value = vSource;
7295-
vParent.FirstExpression = vSourceDeclaration;
7296-
vParent.ComparisonType = vType;
7297-
vParent.SecondExpression = eventValue;
7298-
}
7299-
;
7300-
7301-
dropEventDeclarationList [AlterEventSessionStatement vParent]
7292+
: (vType = comparisonOperator | {LA(2) == Like}? tNot:Not tLike:Like { vType = BooleanComparisonType.NotLike; }) eventValue = eventDeclarationValue
7293+
{
7294+
vSourceDeclaration.Value = vSource;
7295+
vParent.FirstExpression = vSourceDeclaration;
7296+
vParent.ComparisonType = vType;
7297+
vParent.SecondExpression = eventValue;
7298+
}
7299+
;dropEventDeclarationList [AlterEventSessionStatement vParent]
73027300
{
73037301
EventSessionObjectName vDropEventDeclaration;
73047302
}

SqlScriptDom/Parser/TSql/TSql140.g

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7675,16 +7675,14 @@ eventDeclarationComparisonPredicate [BooleanComparisonExpression vParent, EventS
76757675
BooleanComparisonType vType = BooleanComparisonType.Equals;
76767676
ScalarExpression eventValue;
76777677
}
7678-
: vType = comparisonOperator eventValue = eventDeclarationValue
7679-
{
7680-
vSourceDeclaration.Value = vSource;
7681-
vParent.FirstExpression = vSourceDeclaration;
7682-
vParent.ComparisonType = vType;
7683-
vParent.SecondExpression = eventValue;
7684-
}
7685-
;
7686-
7687-
dropEventDeclarationList [AlterEventSessionStatement vParent]
7678+
: (vType = comparisonOperator | {LA(2) == Like}? tNot:Not tLike:Like { vType = BooleanComparisonType.NotLike; }) eventValue = eventDeclarationValue
7679+
{
7680+
vSourceDeclaration.Value = vSource;
7681+
vParent.FirstExpression = vSourceDeclaration;
7682+
vParent.ComparisonType = vType;
7683+
vParent.SecondExpression = eventValue;
7684+
}
7685+
;dropEventDeclarationList [AlterEventSessionStatement vParent]
76887686
{
76897687
EventSessionObjectName vDropEventDeclaration;
76907688
}

SqlScriptDom/Parser/TSql/TSql150.g

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8227,16 +8227,14 @@ eventDeclarationComparisonPredicate [BooleanComparisonExpression vParent, EventS
82278227
BooleanComparisonType vType = BooleanComparisonType.Equals;
82288228
ScalarExpression eventValue;
82298229
}
8230-
: vType = comparisonOperator eventValue = eventDeclarationValue
8231-
{
8232-
vSourceDeclaration.Value = vSource;
8233-
vParent.FirstExpression = vSourceDeclaration;
8234-
vParent.ComparisonType = vType;
8235-
vParent.SecondExpression = eventValue;
8236-
}
8237-
;
8238-
8239-
dropEventDeclarationList [AlterEventSessionStatement vParent]
8230+
: (vType = comparisonOperator | {LA(2) == Like}? tNot:Not tLike:Like { vType = BooleanComparisonType.NotLike; }) eventValue = eventDeclarationValue
8231+
{
8232+
vSourceDeclaration.Value = vSource;
8233+
vParent.FirstExpression = vSourceDeclaration;
8234+
vParent.ComparisonType = vType;
8235+
vParent.SecondExpression = eventValue;
8236+
}
8237+
;dropEventDeclarationList [AlterEventSessionStatement vParent]
82408238
{
82418239
EventSessionObjectName vDropEventDeclaration;
82428240
}

SqlScriptDom/Parser/TSql/TSql160.g

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8252,16 +8252,14 @@ eventDeclarationComparisonPredicate [BooleanComparisonExpression vParent, EventS
82528252
BooleanComparisonType vType = BooleanComparisonType.Equals;
82538253
ScalarExpression eventValue;
82548254
}
8255-
: vType = comparisonOperator eventValue = eventDeclarationValue
8256-
{
8257-
vSourceDeclaration.Value = vSource;
8258-
vParent.FirstExpression = vSourceDeclaration;
8259-
vParent.ComparisonType = vType;
8260-
vParent.SecondExpression = eventValue;
8261-
}
8262-
;
8263-
8264-
dropEventDeclarationList [AlterEventSessionStatement vParent]
8255+
: (vType = comparisonOperator | {LA(2) == Like}? tNot:Not tLike:Like { vType = BooleanComparisonType.NotLike; }) eventValue = eventDeclarationValue
8256+
{
8257+
vSourceDeclaration.Value = vSource;
8258+
vParent.FirstExpression = vSourceDeclaration;
8259+
vParent.ComparisonType = vType;
8260+
vParent.SecondExpression = eventValue;
8261+
}
8262+
;dropEventDeclarationList [AlterEventSessionStatement vParent]
82658263
{
82668264
EventSessionObjectName vDropEventDeclaration;
82678265
}

SqlScriptDom/Parser/TSql/TSql170.g

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8278,16 +8278,14 @@ eventDeclarationComparisonPredicate [BooleanComparisonExpression vParent, EventS
82788278
BooleanComparisonType vType = BooleanComparisonType.Equals;
82798279
ScalarExpression eventValue;
82808280
}
8281-
: vType = comparisonOperator eventValue = eventDeclarationValue
8282-
{
8283-
vSourceDeclaration.Value = vSource;
8284-
vParent.FirstExpression = vSourceDeclaration;
8285-
vParent.ComparisonType = vType;
8286-
vParent.SecondExpression = eventValue;
8287-
}
8288-
;
8289-
8290-
dropEventDeclarationList [AlterEventSessionStatement vParent]
8281+
: (vType = comparisonOperator | {LA(2) == Like}? tNot:Not tLike:Like { vType = BooleanComparisonType.NotLike; }) eventValue = eventDeclarationValue
8282+
{
8283+
vSourceDeclaration.Value = vSource;
8284+
vParent.FirstExpression = vSourceDeclaration;
8285+
vParent.ComparisonType = vType;
8286+
vParent.SecondExpression = eventValue;
8287+
}
8288+
;dropEventDeclarationList [AlterEventSessionStatement vParent]
82918289
{
82928290
EventSessionObjectName vDropEventDeclaration;
82938291
}

SqlScriptDom/Parser/TSql/TSqlFabricDW.g

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8252,16 +8252,14 @@ eventDeclarationComparisonPredicate [BooleanComparisonExpression vParent, EventS
82528252
BooleanComparisonType vType = BooleanComparisonType.Equals;
82538253
ScalarExpression eventValue;
82548254
}
8255-
: vType = comparisonOperator eventValue = eventDeclarationValue
8256-
{
8257-
vSourceDeclaration.Value = vSource;
8258-
vParent.FirstExpression = vSourceDeclaration;
8259-
vParent.ComparisonType = vType;
8260-
vParent.SecondExpression = eventValue;
8261-
}
8262-
;
8263-
8264-
dropEventDeclarationList [AlterEventSessionStatement vParent]
8255+
: (vType = comparisonOperator | {LA(2) == Like}? tNot:Not tLike:Like { vType = BooleanComparisonType.NotLike; }) eventValue = eventDeclarationValue
8256+
{
8257+
vSourceDeclaration.Value = vSource;
8258+
vParent.FirstExpression = vSourceDeclaration;
8259+
vParent.ComparisonType = vType;
8260+
vParent.SecondExpression = eventValue;
8261+
}
8262+
;dropEventDeclarationList [AlterEventSessionStatement vParent]
82658263
{
82668264
EventSessionObjectName vDropEventDeclaration;
82678265
}

0 commit comments

Comments
 (0)