Skip to content

feat(autocomplete): rank columns of nearest table first (closes #507) - #679

Open
adisusilayasa wants to merge 2 commits into
TabularisDB:mainfrom
adisusilayasa:feat/autocomplete-nearest-table-ranking
Open

feat(autocomplete): rank columns of nearest table first (closes #507)#679
adisusilayasa wants to merge 2 commits into
TabularisDB:mainfrom
adisusilayasa:feat/autocomplete-nearest-table-ranking

Conversation

@adisusilayasa

Copy link
Copy Markdown
Contributor

Problem

Follow-up to #505 / closes #507. When multiple tables are in scope, all their columns were given the same 0_<name> sortText prefix. In an ON clause or after WHERE, columns from the table being joined or the primary FROM table were mixed in alphabetical order with all other in-scope tables.

Solution

  1. Expose lastTableRef in analyzeSqlContext:
    • In ON / USING clauses: captures the table/alias being joined right now.
    • In WHERE / SELECT / SET clauses: defaults to the primary FROM table.
    • Properly handles subqueries, restoring the outer scope's table reference on closing parenthesis.
  2. Rank Nearest Table Columns First:
    • When multiple tables are in scope, columns from the nearest table receive sortText: "0_0_<col>", while columns from other tables receive sortText: "0_1_<col>".
    • Single-table queries remain unchanged with sortText: "0_<col>".

Tests

  • Added unit tests in tests/utils/sqlContext.test.ts for lastTableRef exposure across single/multi-table FROMs, aliases, schema-qualified tables, JOIN chains, subqueries, UPDATE, and INSERT INTO.
  • Added provider-level sorting assertion in tests/utils/autocomplete.test.ts verifying that nearest table columns sort before other tables in ON and WHERE clauses.

Comment thread src/utils/sqlContext.ts
case 'SELECT':
frame.clause = 'select';
frame.isStatement = true;
frame.awaitingAlias = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

WARNING: Subquery statement scopes leak the outer frame's table refs.

When a subquery is opened by IN ( or a bare (, createFrame builds the frame with isStatement: false and inherits the parent's fromTableRef/currentJoinTableRef/lastTableRef. When SELECT then flips isStatement to true, those inherited refs are not cleared. If the subquery's FROM table has no alias, recordIdentifier's from branch only updates lastTableRef (the else arm), leaving fromTableRef pointing at the outer table. The final resolution fromTableRef ?? lastTableRef then returns the outer ref instead of the subquery's own table.

Repro: analyzeSqlContext('SELECT * FROM orders o WHERE o.id IN (SELECT id FROM users WHERE ').lastTableRef returns 'o' rather than 'users'.

Consider nulling fromTableRef, currentJoinTableRef, and lastTableRef here (and in the other statement-keyword cases: INSERT/UPDATE/DELETE/REPLACE) when the frame newly becomes a statement scope.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

@kilo-code-bot

kilo-code-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • src/utils/sqlContext.ts
  • tests/utils/sqlContext.test.ts
Previous Review Summary (commit 1c90484)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 1c90484)

Status: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
src/utils/sqlContext.ts 274 Subquery statement scopes leak the outer frame's table refs; an unaliased subquery FROM table resolves to the outer scope's table instead of its own.
Files Reviewed (4 files)
  • src/utils/sqlContext.ts - 1 issue
  • src/utils/autocomplete.ts - 0 issues
  • tests/utils/autocomplete.test.ts - 0 issues
  • tests/utils/sqlContext.test.ts - 0 issues

Fix these issues in Kilo Cloud


Reviewed by glm-5.2 · Input: 39.3K · Output: 13.2K · Cached: 385.5K

@adisusilayasa

Copy link
Copy Markdown
Contributor Author

Fixed in 46f288a: cleared fromTableRef, currentJoinTableRef, and lastTableRef when entering new statement scopes (SELECT, INSERT, UPDATE, DELETE, REPLACE), with regression tests for both aliased and unaliased subqueries.

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.

[Feat]: SQL autocomplete — rank columns of the nearest table first

1 participant