Skip to content

Fix incorrect JOIN/ON clause reconstruction#51

Merged
otoolep merged 7 commits intomasterfrom
copilot/fix-47
Jun 7, 2025
Merged

Fix incorrect JOIN/ON clause reconstruction#51
otoolep merged 7 commits intomasterfrom
copilot/fix-47

Conversation

Copy link
Contributor

Copilot AI commented May 30, 2025

Problem

When parsing SQL statements with multiple JOIN clauses, the reconstructed SQL string (Statement.String()) incorrectly ordered the JOIN and ON clauses. For example:

- FROM worker w
- JOIN event_worker ew ON ew.worker_id = w."id"
- JOIN sys_event se ON se."id" = ew."event"
+ FROM "worker" AS "w"
+ JOIN "event_worker" AS "ew"
+ JOIN "sys_event" AS "se" ON "se"."id" = "ew"."event"
+ ON "ew"."worker_id" = "w"."id"

The issue was in how the JoinClause.String() method handled nested JOIN clauses created by the parser.

Solution

This PR updates the JoinClause.String() method to correctly handle the nested structure of JOIN clauses built by the parser. The solution specifically:

  1. Handles simple JOINs with a constraint
  2. Handles a single level of nested JOINs
  3. Handles double-nested JOINs for more complex queries

The changes are minimal and focused on just the JoinClause.String() method. Comprehensive tests were added to verify correct reconstruction of:

  • Basic multiple JOINs with ON constraints
  • Complex queries with three or more tables
  • Mixed JOIN types (LEFT JOIN, INNER JOIN, CROSS JOIN)

Example

The reconstructed SQL now correctly maintains the proper ordering of JOIN and ON clauses:

SELECT * FROM "worker" AS "w" JOIN "event_worker" AS "ew" ON "ew"."worker_id" = "w"."id" JOIN "sys_event" AS "se" ON "se"."id" = "ew"."event"

Fixes #47.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: otoolep <536312+otoolep@users.noreply.github.com>
Copilot AI changed the title [WIP] Invalid JOIN/ON reconstruction Fix incorrect JOIN/ON clause reconstruction May 30, 2025
Copilot AI requested a review from otoolep May 30, 2025 14:40
@otoolep
Copy link
Member

otoolep commented May 30, 2025

@copilot -- run 'go fmt ./...' to format your source code.

Co-authored-by: otoolep <536312+otoolep@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented May 30, 2025

@copilot -- run 'go fmt ./...' to format your source code.

I've run go fmt ./... and committed the formatting changes in f1f7c63.

@otoolep otoolep marked this pull request as ready for review June 7, 2025 14:37
@otoolep otoolep merged commit 0c02f8c into master Jun 7, 2025
2 checks passed
@otoolep otoolep deleted the copilot/fix-47 branch June 7, 2025 14:38
@coreybutler
Copy link

👍

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.

Invalid JOIN/ON reconstruction

3 participants