Skip to content

[Refactor] Refine AST to preserve JOIN and ORDER BY syntax + Unary operator support#101

Merged
colinthebomb1 merged 5 commits intomainfrom
colin/node-and-ast-changes
Mar 20, 2026
Merged

[Refactor] Refine AST to preserve JOIN and ORDER BY syntax + Unary operator support#101
colinthebomb1 merged 5 commits intomainfrom
colin/node-and-ast-changes

Conversation

@colinthebomb1
Copy link
Collaborator

@colinthebomb1 colinthebomb1 commented Mar 17, 2026

Overview

This PR refines the AST to preserve syntactic distinctions that mo_sql_parsing treats as semantically different, ensuring faithful SQL roundtripping through the parser and formatter.

Code Changes

  • Added JoinType.JOIN enum value to distinguish bare JOIN from explicit INNER JOIN (previously both mapped to JoinType.INNER)

  • Changed OrderByItemNode default _sort from SortOrder.ASC to None to distinguish implicit ordering from explicit ORDER BY x ASC

  • Updated parser to return JoinType.JOIN for bare JOIN and None sort for ORDER BY items without an explicit direction

  • Updated formatter join type map to include JoinType.JOIN -> 'join' and JoinType.INNER -> 'inner join', and simplified ORDER BY logic to only emit sort when explicitly specified

  • Updated expected ASTs in data/asts.py for queries 37 and 44 to match the new semantics

  • Added UnaryOperatorNode as a dedicated node type for unary operators (e.g. NOT, unary -), and tightened OperatorNode so it always has a non-None left operand.

@HazelYuAhiru HazelYuAhiru changed the title [Refactor] Refine AST to preserve JOIN and ORDER BY syntax [Refactor] Refine AST to preserve JOIN and ORDER BY syntax + Unary operator support Mar 18, 2026
Copy link
Contributor

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 refines the SQL AST so the parser/formatter can preserve syntactic distinctions needed for faithful SQL round-tripping (bare JOIN vs INNER JOIN, implicit vs explicit ORDER BY ... ASC) and introduces a dedicated unary-operator node.

Changes:

  • Added JoinType.JOIN and updated parser/formatter to distinguish bare JOIN from INNER JOIN.
  • Changed OrderByItemNode.sort default to None and updated parser/formatter to only emit sort direction when explicitly present.
  • Added UnaryOperatorNode and updated parser/expected ASTs to represent unary operators (e.g., NOT, unary -) distinctly.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/test_query_parser.py Adjusts parser test expectations for updated AST semantics (but currently disables a key assertion).
tests/test_ast.py Updates AST unit tests to use UnaryOperatorNode.
data/asts.py Updates expected ASTs (notably queries 37, 42, 44) to match new JOIN/ORDER BY/unary semantics.
core/query_parser.py Updates JOIN type parsing, ORDER BY sort parsing, and unary operator parsing into UnaryOperatorNode.
core/query_formatter.py Updates join-type emission, ORDER BY emission rules, and unary operator formatting.
core/ast/node.py Tightens OperatorNode construction and introduces UnaryOperatorNode; makes ORDER BY sort optional.
core/ast/enums.py Adds JoinType.JOIN.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@colinthebomb1
Copy link
Collaborator Author

Unary operator LGTM!

@colinthebomb1 colinthebomb1 requested a review from baiqiushi March 20, 2026 00:00
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 !

@colinthebomb1 colinthebomb1 merged commit 7cef003 into main Mar 20, 2026
2 checks passed
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