-
-
Notifications
You must be signed in to change notification settings - Fork 54
Add SQL parser implementation based on OrchardCore SQL Grammar #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: sebastienros <[email protected]>
Co-authored-by: sebastienros <[email protected]>
Co-authored-by: sebastienros <[email protected]>
|
@copilot here is some knowledge that will help you to continue your task. With following logic:
|
…sing Co-authored-by: sebastienros <[email protected]>
Fixed! Applied your guidance about flat tuples and used Key changes in commit 4828caa:
The remaining failing test is a CTE with explicit column list |
|
@copilot can you write more comprehensive tests that ensure the parsed structure is correct, instead of just checking for null values. |
Co-authored-by: sebastienros <[email protected]>
Added 24 comprehensive structural validation tests in commit 263cd80. These tests verify the actual parsed AST structure including:
Example test: [Fact]
public void ParsedBetweenExpressionShouldHaveCorrectBounds()
{
var result = SqlParser.Parse("SELECT * FROM users WHERE id BETWEEN 1 AND 100");
var statement = GetSelectStatement(result);
var betweenExpr = Assert.IsType<BetweenExpression>(statement.WhereClause.Expression);
Assert.False(betweenExpr.IsNot);
var lower = Assert.IsType<LiteralExpression>(betweenExpr.Lower);
Assert.Equal(1m, lower.Value);
var upper = Assert.IsType<LiteralExpression>(betweenExpr.Upper);
Assert.Equal(100m, upper.Value);
}The tests now validate the parser produces correct AST structures, not just non-null results. |
|
I remembered we have a PR in OC, this should be done in OC, not Parlot, yes? |
|
I remember. There is nothing close to this in the pr in OC. Not even a Select class. My goal by doing it here is that copilot has more context and it will serve as a bigger sample. Once the parser works we can copy it in OC and implement the SQL translation logic. It's also a learning experience. I can see what copilot is missing to be able to generate parsers and eventually provide instructions that will make it more robust. |
|
That's why I see Copilt nominated to harvest this year :) |
|
@copilot SelectStatement should have a list of strings for its selectors. There is no need to treat '*' specifically. |
Co-authored-by: sebastienros <[email protected]>
Simplified in commit ca61e68. Changes:
Example: var result = SqlParser.Parse("SELECT id, COUNT(*) AS total FROM users");
var selectors = result.Statements[0].UnionStatements[0].Statement.SelectStatement.Selectors;
// selectors[0] = "id"
// selectors[1] = "COUNT(*) AS total"29/48 tests passing (up from 27). |
|
I fixed Aliases. Next is fixing JOIN conditions. |
Implements a SQL parser using Parlot's Fluent API based on the OrchardCore SQL Grammar, supporting SELECT statements with CTEs, UNIONs, JOINs, window functions, and complex expressions.
Implementation
AST Classes (
src/Samples/Sql/SqlAst.cs)SelectStatement,WithClause,UnionStatementBetweenExpression,InExpression,FunctionCallFromClause,WhereClause,JoinStatement,GroupByClause,OrderByClause,OverClauseIdentifier,BinaryOperator,UnaryOperator,JoinKind,SelectRestrictionIReadOnlyList<string>for selectors (e.g.,["*"],["id", "name AS user_name"],["COUNT(*)"])IReadOnlyList<string>for column names*and column lists - both are string arrays for simplicityParser (
src/Samples/Sql/SqlParser.cs)AndSkipto reduce tuple nesting and avoid compilation issuesTests (
test/Parlot.Tests/Sql/SqlParserTests.cs)Key Implementation Details
Fixed compilation issues by:
.And()creates flat tuples, not nested onesAndSkip: Skip keywords like BY, AS, parentheses to reduce tuple size and avoid nestingOptional()resultsSimplified API Design:
SelectorList,StarSelector,ColumnItemList,ColumnItem,ColumnSource)ExpressionToString()helperTest Results
Example
The comprehensive tests ensure the parser produces correct AST structures beyond simple null checks, validating node types, operators, identifiers, and values throughout the parse tree.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.