dbx icon indicating copy to clipboard operation
dbx copied to clipboard

[Bug] Query editor Ctrl/Cmd+click misses derived-table aliases and treats a table alias as a database

Open feichonglzw opened this issue 1 week ago • 2 comments

Bug Description

Ctrl/Cmd+click in the query editor does not resolve two common SQL aliases the way DataGrip does.

  1. A bare output column of a derived table (FROM (SELECT ...) alias) does not jump back to that column's definition.
  2. On MySQL, an alias-qualified column such as o.created_date is treated as database.table. The editor tries to open a table named after the alias (o) and fails because that object does not exist.

This is a gap in the editor's own click navigation, not a MySQL dialect rule and not the Rust sqlparser path. Hover already checks whether a qualifier is a table alias; click navigation does not.

Steps to Reproduce

  1. Open a MySQL query editor.
  2. Paste the SQL below.
  3. Ctrl/Cmd+click the outer amount_index.
  4. Ctrl/Cmd+click created_date in o.created_date >= '2026-11-01'.
SELECT amount_index,
       amount_month,
       amount_last_month
FROM (SELECT src.total AS amount_index,
             sum(c.amount) AS amount_month
      FROM orders o
               LEFT JOIN charges c ON o.id = c.order_id
      WHERE o.created_date >= '2026-11-01') t1
         LEFT JOIN (SELECT sum(c.amount) AS amount_last_month
                    FROM orders o
                             LEFT JOIN charges c ON o.id = c.order_id
                    WHERE o.created_date >= '2026-10-01') t2 ON 1 = 1;

Expected Behavior

  • Ctrl/Cmd+click on the outer amount_index jumps to src.total AS amount_index inside derived table t1.
  • Ctrl/Cmd+click on created_date in o.created_date opens the DDL of the real table orders, because o is that table's alias.
  • The same alias inside t2 resolves to that subquery's orders, not a table named o.
  • A name produced by more than one derived table should not jump to an arbitrary definition.

Actual Behavior

  • Clicking the outer amount_index does not jump to its definition.
  • Clicking o.created_date tries to open a non-existent table o and errors out.

Cause

Query-editor Ctrl/Cmd+click uses the desktop semantic scanner, not Rust sqlparser.

  • Derived tables outside a WITH clause record output names but not the span of AS <alias>. Jump targets are only built for CTE outputs, so FROM (SELECT ...) t1 has nothing to jump to. That metadata was intentionally left off the common path.
  • MySQL is not a schema-qualified dialect, so a two-part name is parsed as database.table before alias lookup. o.created_date therefore becomes database o, table created_date. Hover already rejects a qualifier that is a known table alias; click navigation does not.

Relevant code:

  • apps/desktop/src/components/editor/useQueryEditorObjectNavigation.ts
  • apps/desktop/src/lib/sql/semantic/model.ts (parseSubquerySource, CTE-only derived-table enrichment)
  • apps/desktop/src/lib/sql/semantic/cteNavigation.ts
  • apps/desktop/src/lib/editor/hoverTableLookup.ts
  • apps/desktop/src/lib/sql/queryCursorTableTarget.ts

Why this is not a plugin

The current plugin contribution points are connection, workbench, context menu, result view, command, and MCP. None of them can replace the query editor's click resolver or attach a jump span to a derived-table output. Fixing this needs a host change, or a new editor-navigation extension point.

Environment

  • Database: MySQL (two-part names are database.table; no schema level)
  • Source: main @ b6d611ab0 (v0.6.75)
  • OS / runtime: not captured from the app's About panel. Reproduced from the query-editor navigation code, not from a packaged build log.

Priority

P2. Query editing still works; only declaration navigation is wrong, and the alias case opens a missing object.

feichonglzw avatar Nov 26 '26 06:11 feichonglzw

/claim

imcman avatar Nov 26 '26 07:11 imcman

✅ @imcman 已认领 #15731,开始处理吧!

dbx-bot[bot] avatar Nov 26 '26 07:11 dbx-bot[bot]