-
Notifications
You must be signed in to change notification settings - Fork 3
[Refactor] Add basic formatter #89
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
base: main
Are you sure you want to change the base?
Conversation
|
@HazelYuAhiru Because the test case assumes your output will match its input, I want to make sure our AST matches. The changes I made are:
|
|
I'm still reviewing the main formatter functions, but I was wondering that should we separate the formatter and parser code into different files with their own tests? Like having |
HazelYuAhiru
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements a basic query formatter that converts custom AST (Abstract Syntax Tree) nodes into JSON format compatible with the mosql library, which then generates SQL text. The implementation provides a two-step conversion pipeline: AST → JSON → SQL.
Key Changes:
- Added
QueryFormatterclass withformat()method that orchestrates the AST-to-SQL conversion - Implemented clause-specific formatters (
format_select,format_from,format_where, etc.) to handle different SQL clauses - Created comprehensive test case
test_basic_format()that validates formatting of a complex query with JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, and OFFSET clauses
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
| core/query_formatter.py | New formatter implementation with AST-to-JSON conversion functions and support for all major SQL clauses including JOIN operations |
| tests/test_query_formatter.py | New test file with test_basic_format() validating the formatter against a comprehensive multi-clause SQL query, plus normalize_sql() helper for SQL comparison |
Comments suppressed due to low confidence (3)
core/query_formatter.py:8
- Import of 'ColumnNode' is not used.
Import of 'LiteralNode' is not used.
Import of 'OperatorNode' is not used.
Import of 'FunctionNode' is not used.
Import of 'LimitNode' is not used.
Import of 'OffsetNode' is not used.
Import of 'SubqueryNode' is not used.
Import of 'VarNode' is not used.
Import of 'VarSetNode' is not used.
from core.ast.node import (
QueryNode, SelectNode, FromNode, WhereNode, TableNode, ColumnNode,
LiteralNode, OperatorNode, FunctionNode, GroupByNode, HavingNode,
OrderByNode, LimitNode, OffsetNode, SubqueryNode, VarNode, VarSetNode,
JoinNode
)
tests/test_query_formatter.py:7
- Import of 'SubqueryNode' is not used.
Import of 'VarNode' is not used.
Import of 'VarSetNode' is not used.
from core.ast.node import (
OrderByItemNode, QueryNode, SelectNode, FromNode, WhereNode, TableNode, ColumnNode,
LiteralNode, OperatorNode, FunctionNode, GroupByNode, HavingNode,
OrderByNode, LimitNode, OffsetNode, SubqueryNode, VarNode, VarSetNode, JoinNode
)
tests/test_query_formatter.py:9
- Import of 'get_query' is not used.
from data.queries import get_query
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
baiqiushi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @colinthebomb1 for the PR! It looks great overall! Only a few minor changes are needed. Please take care of them. Thanks!
Overview:
This PR implements a basic formatter with a basic test for converting from our custom AST into JSON that can be understood by mosql, which turns it into plaintext. AST -> JSON -> SQL TEXT (via mosql).
Code Changes:
tests/test_query_parser.pyaddedtest_basic_format()to run a basic test of the formatter andnormalize_sql()for comparing if the expected sql is equal to our output.format()and it's clause specific formatters:format_select(),format_from()...etc incore/query_parser.py, responsible for converting AST to JSON clause by clause.