Skip to content

Fix nested relation paths generating invalid SQL multi-level dot notation#71

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/start-implementation-phase
Draft

Fix nested relation paths generating invalid SQL multi-level dot notation#71
Copilot wants to merge 3 commits intomasterfrom
copilot/start-implementation-phase

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 7, 2026

SQL only supports two-level dot notation ("table"."column"). Paths like projects.user.name were generating "projects"."user"."name" — invalid in every SQL dialect. The correct output is "user"."name", using the leaf relation segment as the joined table alias.

Changes

interpreter.tsfield() method

  • Applies a second lastIndexOf on the relation path to extract only the leaf alias ('user' from 'projects.user')
  • Passes the leaf alias to joinRelation(alias, context) — ORM implementations are responsible for resolving the full join chain
  • Tracks only the alias in _joins
  • _foreignField() reverted to simple two-part escaping: "alias"."column"
  • usingFieldPrefix() keeps prefix accumulation so nested elemMatch correctly builds the full path before alias extraction
// Before (invalid SQL):
new Field('eq', 'projects.user.name', 'test')
// → "projects"."user"."name" = $1  ❌

// After (valid SQL):
// → "user"."name" = $1  ✅
// joinRelation called with: 'user'

Works for both direct field notation (projects.user.name) and nested elemMatch:

elemMatch('address', elemMatch('city', eq('name', 'NYC')))
// prefix accumulates: 'address.city.' → field 'name' → alias 'city'
// → "city"."name" = $1  ✅

interpreters.spec.ts

  • Corrected 3 tests from the previous session that asserted invalid multi-level SQL output
  • Added describe('auto join nested relations') block covering direct nested field notation, consistent with PR feat: support deeply nested field path #55
Original prompt

Start implementation

[Chronological Review: The conversation began with the user requesting to find potential issues in the file `interpreter.ts`, particularly related to joining nested relations. The user then shifted to requesting the start of the implementation phase, indicating a transition from analysis to action.]

[Intent Mapping:

  1. "find potential issues, especially related to joining nested relations and suggest a fix" - The user is looking for specific problems in the code that could affect functionality.
  2. "Start implementation" - The user is ready to move forward with coding after identifying issues.]

[Technical Inventory:

  • File: /Users/sergiistotskyi/projects/ucast/packages/sql/src/interpreter.ts - This file is central to the user's current work, likely containing logic for SQL interpretation and execution.
  • Concepts: Nested relations and SQL joins are critical to the user's focus, indicating a need for robust handling of complex data structures.]

[Code Archaeology: No specific code changes or functions were discussed in detail yet, but the focus is on identifying issues in the existing code.]

[Progress Assessment: The user has identified a need for issue discovery and is transitioning to implementation. No specific tasks have been completed yet, but the groundwork for understanding the code is being laid.]

[Context Validation: The user is working on the interpreter.ts file and is focused on ensuring that nested relations are handled correctly in SQL operations.]

[Recent Commands Analysis:

  • Last Agent Commands: The user requested to find potential issues and then to start implementation.
  • Tool Results Summary: No specific tool results were generated as the conversation was primarily focused on user requests.
  • Pre-Summary State: The agent was actively discussing potential issues in the interpreter.ts file related to nested relations when the token budget was exceeded.
  • Operation Context: The commands were executed to address the user's goal of improving the SQL interpreter's handling of complex data structures.]
1. Conversation Overview: - Primary Objectives: - "find potential issues, especially related to joining nested relations and suggest a fix" - "Start implementation" - Session Context: The user is focused on the `interpreter.ts` file, seeking to identify and resolve issues related to SQL joins and nested relations before proceeding with implementation. - User Intent Evolution: The user initially sought to identify issues but quickly moved to the implementation phase, indicating readiness to act on findings.
  1. Technical Foundation:
  • File: /Users/sergiistotskyi/projects/ucast/packages/sql/src/interpreter.ts
  • Purpose: This file is crucial for SQL interpretation and execution, particularly in handling nested relations.
  • Current State: The user is in the process of identifying issues before starting implementation.
  1. Codebase Status:
  • File Name: interpreter.ts
  • Purpose: Contains logic for SQL interpretation, particularly for handling nested relations.
  • Current State: No specific modifications have been made yet, but the user is preparing to implement fixes.
  • Key Code Segments: Not yet discussed in detail.
  • Dependencies: Relates to SQL execution and data handling.
  1. Problem Resolution:
  • Issues Encountered: Potential issues with joining nested relations.
  • Solutions Implemented: None yet, as the user is still in the analysis phase.
  • Debugging Context: Ongoing identification of issues before implementation.
  • Lessons Learned: Understanding of nested relations is critical for SQL operations.
  1. Progress Tracking:
  • Completed Tasks: None yet; the user is still in the analysis phase.
  • Partially Complete Work: Identifying issues related to nested relations.
  • Validated Outcomes: None yet.
  1. Active Work State:
  • Current Focus: Identifying issues in interpreter.ts related to nested relations.
  • Recent Context: The user has requested to find potential issues and then to start implementation.
  • Working Code: No specific code snippets have been discussed yet.
  • Immediate Context: The user is preparing to address issues in the SQL interpreter.
  1. Recent Operations:
  • Last Agent Commands:
    • "find potential issues, especially related to joining nested relations and suggest a fix"
    • "Start implementation"
  • Tool Results Summary: No specific tool results were generated; the focus was on user requests.
  • Pre-Summary State: The agent was discussing potential issues in interpreter.ts when the token budget was exceeded.
  • Operation Context: The commands were executed to align with the user's goal of improving the SQL interpreter's handling of nested relations.
  1. Continuation Plan:
  • [Pending Task 1]: Identify specific issues in interpreter.ts related to nested relations and suggest fixes.
  • [Pending Task 2]: Begin implementation of identified fixes once issues are clarified.
  • [Priority Information]: Addressing ...

Created from VS Code.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

- Fix usingFieldPrefix to accumulate prefix instead of overwriting
- Fix field() to use lastIndexOf for proper nested relation path extraction
- Fix _foreignField to properly escape each part of a nested relation name
- Add tests for nested elemMatch behavior

Co-authored-by: stalniy <1159966+stalniy@users.noreply.github.com>
Copilot AI changed the title [WIP] Start implementation for nested relations in interpreter.ts Fix nested relation joining in SQL interpreter Mar 7, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.06%. Comparing base (8c518ea) to head (2ed2f3a).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #71      +/-   ##
==========================================
+ Coverage   91.93%   92.06%   +0.12%     
==========================================
  Files          12       12              
  Lines         248      252       +4     
  Branches       40       41       +1     
==========================================
+ Hits          228      232       +4     
  Misses         12       12              
  Partials        8        8              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

For deeply nested field paths like 'projects.user.name', extract only the
leaf relation segment 'user' as the SQL alias instead of the full path.

SQL databases only support two-level dot notation ("table"."column"),
so generating "projects"."user"."name" is invalid. The correct output
is "user"."name" where 'user' is the joined table alias.

- field(): use second lastIndexOf to extract leaf alias from relation path
- _foreignField(): revert to simple two-part escaping (no multi-segment split)
- usingFieldPrefix(): keep prefix accumulation from previous fix
- Fix 3 wrong tests that expected invalid multi-level SQL dot notation
- Rename test to clarify it checks leaf relation name behaviour
- Add 'auto join nested relations' describe block matching PR #55

Co-authored-by: stalniy <1159966+stalniy@users.noreply.github.com>
Copilot AI changed the title Fix nested relation joining in SQL interpreter Fix nested relation paths generating invalid SQL multi-level dot notation Mar 8, 2026
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.

2 participants