Skip to content

Conversation

@colinthebomb1
Copy link
Collaborator

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:

  • In tests/test_query_parser.py added test_basic_format() to run a basic test of the formatter and normalize_sql() for comparing if the expected sql is equal to our output.
  • Implemented format() and it's clause specific formatters: format_select(), format_from() ...etc in core/query_parser.py, responsible for converting AST to JSON clause by clause.

@colinthebomb1
Copy link
Collaborator Author

@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:

  • Implemented order by using new OrderByItem node
order_by_clause = OrderByNode([
        OrderByItemNode(dept_alias, SortOrder.DESC),
        OrderByItemNode(count_alias, SortOrder.DESC)  
])  
  • Changed all sets to lists
  • added dept alias for order by dept_alias = ColumnNode("dept_name")
  • added alias to count star FunctionNode count_star = FunctionNode("COUNT", {ColumnNode("*")}, 'emp_count')

@colinthebomb1 colinthebomb1 marked this pull request as draft November 21, 2025 00:14
@HazelYuAhiru
Copy link
Collaborator

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 query_formatter.py and query_formatter_test.py to avoid future merge conflicts.

@colinthebomb1 colinthebomb1 marked this pull request as ready for review December 1, 2025 20:19
Copy link
Collaborator

@HazelYuAhiru HazelYuAhiru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link

Copilot AI left a 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 QueryFormatter class with format() 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.

Copy link
Contributor

@baiqiushi baiqiushi left a 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants