feat(editor): semantic column navigation for CTE columns
This commit is contained in:
11 files changed
+1456
-6
No files matched your search
@@ -0,0 +1,250 @@
|
||||
/**
|
||||
* CTE (WITH ... AS) navigation helpers shared by Ctrl/Cmd+click and hover handling.
|
||||
*
|
||||
* A CTE appears twice in the semantic model:
|
||||
* - a **definition** row source (`id` prefix `cte:`) carrying `nameSpan` / `bodySpan` /
|
||||
* `cteOutputs` / `cteStars` / `bodySources` enrichment;
|
||||
* - zero or more **reference** row sources (`from:` / `join:` …, `kind: "cte"`) where the
|
||||
* CTE is used as a row source by another query block.
|
||||
*
|
||||
* Derived tables (`kind: "subquery"`) that sit directly inside a CTE body carry the same body
|
||||
* enrichment, which lets a lineage keep tracing through `FROM (SELECT ...) x` to the physical
|
||||
* tables inside the inline view.
|
||||
*/
|
||||
|
||||
import type { SqlSemanticCteOutputColumn, SqlSemanticCteStar, SqlSemanticModel, SqlSemanticRowSource } from "@/lib/sql/semantic/types";
|
||||
|
||||
/** CTE chains can nest (a → b → physical table); stop well before pathological recursion. */
|
||||
const MAX_CTE_ORIGIN_DEPTH = 8;
|
||||
|
||||
function containsPosition(span: { start: number; end: number }, position: number): boolean {
|
||||
return position >= span.start && position <= span.end;
|
||||
}
|
||||
|
||||
function sameName(left: string, right: string): boolean {
|
||||
return left.toLowerCase() === right.toLowerCase();
|
||||
}
|
||||
|
||||
/** CTE definition row sources (`WITH c AS (...)`) in declaration order. */
|
||||
export function cteDefinitionSources(model: SqlSemanticModel): SqlSemanticRowSource[] {
|
||||
return model.rowSources.filter((source) => source.kind === "cte" && source.id.startsWith("cte:"));
|
||||
}
|
||||
|
||||
/** CTE *usages* as row sources (`FROM/JOIN cte`), excluding the definitions themselves. */
|
||||
export function cteReferenceSources(model: SqlSemanticModel): SqlSemanticRowSource[] {
|
||||
return model.rowSources.filter((source) => source.kind === "cte" && !source.id.startsWith("cte:"));
|
||||
}
|
||||
|
||||
function findCteDefinition(model: SqlSemanticModel, name: string): SqlSemanticRowSource | undefined {
|
||||
const definitions = cteDefinitionSources(model);
|
||||
for (let index = definitions.length - 1; index >= 0; index -= 1) {
|
||||
const definition = definitions[index];
|
||||
if (definition && sameName(definition.name, name)) return definition;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export interface CteReferenceHit {
|
||||
reference: SqlSemanticRowSource;
|
||||
definition: SqlSemanticRowSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Position lies on an outer-query CTE reference name or alias (never on the definition name
|
||||
* itself — definitions have no `qualifiedName` and are excluded by id prefix).
|
||||
*/
|
||||
export function findCteReferenceAt(model: SqlSemanticModel, position: number): CteReferenceHit | null {
|
||||
for (const reference of cteReferenceSources(model)) {
|
||||
const onName = !!reference.qualifiedName && containsPosition(reference.qualifiedName.span, position);
|
||||
const onAlias = !!reference.aliasSpan && containsPosition(reference.aliasSpan, position);
|
||||
if (!onName && !onAlias) continue;
|
||||
const definition = findCteDefinition(model, reference.name);
|
||||
if (!definition?.nameSpan) continue;
|
||||
return { reference, definition };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export interface CteColumnHit {
|
||||
definition: SqlSemanticRowSource;
|
||||
/** Matched named output column; absent when the column arrives via a body star. */
|
||||
output?: SqlSemanticCteOutputColumn;
|
||||
/**
|
||||
* Every star projection that could supply the column, in body order (absent when a named output
|
||||
* matched). All of them are kept instead of the first one, because `SELECT a.*, b.*` gives no
|
||||
* way to tell which star owns the column without column metadata — callers pick among the traced
|
||||
* origins with the metadata they already fetch.
|
||||
*/
|
||||
stars?: SqlSemanticCteStar[];
|
||||
}
|
||||
|
||||
/** Nesting depth of the parenthesis range containing `position` (0 at statement level). */
|
||||
function nestingDepthAt(model: SqlSemanticModel, position: number): number {
|
||||
let depth = 0;
|
||||
for (const token of model.tokens) {
|
||||
if (token.span.start > position) break;
|
||||
if (token.span.end > position) return token.depth;
|
||||
depth = token.depth;
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks the matching named output, or the stars that can supply the column.
|
||||
* Note: a body `t.*` star still exposes its columns to the outer query both bare and
|
||||
* CTE-qualified (as `w.col`), so the outer qualifier never participates in star selection;
|
||||
* star qualifier parts only steer physical-source expansion during origin tracing.
|
||||
*/
|
||||
function resolveCteOutputOrStars(definition: SqlSemanticRowSource, columnName: string): CteColumnHit | null {
|
||||
const output = definition.cteOutputs?.find((candidate) => sameName(candidate.name, columnName));
|
||||
if (output) return { definition, output };
|
||||
|
||||
const stars = definition.cteStars ?? [];
|
||||
if (stars.length === 0) return null;
|
||||
return { definition, stars };
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves an outer-query column to the CTE that produces it. The model must be built at the
|
||||
* click/hover position (cursor intent drives qualified-column target resolution).
|
||||
*
|
||||
* - Qualified columns (`w.col`) use the cursor intent target source or the qualifier name, and
|
||||
* resolve *only* through the CTE that owns that qualifier.
|
||||
* - Bare columns resolve only when exactly one CTE *visible at the cursor* supplies the name,
|
||||
* otherwise `null` is returned so callers fall back to physical-table handling. Visibility means
|
||||
* the CTE is declared before the cursor and is referenced at the cursor's own nesting level —
|
||||
* without that guard a later, unrelated CTE body claims bare columns of an earlier query block.
|
||||
*/
|
||||
export function findCteColumnResolution(model: SqlSemanticModel, columnName: string, qualifier?: string): CteColumnHit | null {
|
||||
const intentTargetId = model.cursorIntent.targetSourceId;
|
||||
if (qualifier || intentTargetId) {
|
||||
let target: SqlSemanticRowSource | undefined;
|
||||
if (intentTargetId) {
|
||||
target = model.rowSources.find((source) => source.id === intentTargetId);
|
||||
}
|
||||
if (!target && qualifier) {
|
||||
target = cteReferenceSources(model).find((source) => (!!source.alias && sameName(source.alias, qualifier)) || sameName(source.name, qualifier));
|
||||
}
|
||||
// The qualifier owns the column: a physical table (or any other non-CTE row source) must never
|
||||
// fall through to the bare-column scan below, which ignores qualifiers and would let a
|
||||
// star-bodied CTE claim `t.id` from an unrelated physical table.
|
||||
if (!target || target.kind !== "cte" || target.id.startsWith("cte:")) return null;
|
||||
const definition = findCteDefinition(model, target.name);
|
||||
return definition ? resolveCteOutputOrStars(definition, columnName) : null;
|
||||
}
|
||||
|
||||
const hits: CteColumnHit[] = [];
|
||||
const seenDefinitionIds = new Set<string>();
|
||||
const cursorDepth = nestingDepthAt(model, model.cursor);
|
||||
for (const reference of cteReferenceSources(model)) {
|
||||
const definition = findCteDefinition(model, reference.name);
|
||||
if (!definition || seenDefinitionIds.has(definition.id)) continue;
|
||||
// Only a CTE declared before the cursor *and* referenced in the cursor's own query block can
|
||||
// own a bare column; otherwise an unrelated query block (typically a CTE declared further
|
||||
// down, or one referenced only from a deeper subquery) hijacks the name and click/hover
|
||||
// lands on that far-away CTE.
|
||||
if (definition.sourceSpan.start > model.cursor) continue;
|
||||
if (nestingDepthAt(model, reference.sourceSpan.start) !== cursorDepth) continue;
|
||||
seenDefinitionIds.add(definition.id);
|
||||
const hit = resolveCteOutputOrStars(definition, columnName);
|
||||
if (hit) hits.push(hit);
|
||||
}
|
||||
return hits.length === 1 ? hits[0] : null;
|
||||
}
|
||||
|
||||
/** A physical row source reached after tracing CTE output columns. */
|
||||
export interface CteColumnOrigin {
|
||||
source: SqlSemanticRowSource;
|
||||
column: string;
|
||||
}
|
||||
|
||||
function isPhysicalOriginSource(source: SqlSemanticRowSource): boolean {
|
||||
return source.kind === "table" || source.kind === "table_function" || source.kind === "mutation_target";
|
||||
}
|
||||
|
||||
function sourceMatchesQualifier(source: SqlSemanticRowSource, qualifierParts: readonly string[]): boolean {
|
||||
const qualifier = qualifierParts[qualifierParts.length - 1]?.toLowerCase();
|
||||
if (!qualifier) return false;
|
||||
return source.alias?.toLowerCase() === qualifier || source.name.toLowerCase() === qualifier;
|
||||
}
|
||||
|
||||
function dedupeOrigins(origins: CteColumnOrigin[]): CteColumnOrigin[] {
|
||||
const seen = new Set<string>();
|
||||
const result: CteColumnOrigin[] = [];
|
||||
for (const origin of origins) {
|
||||
const key = `${origin.source.sourceSpan.start}:${origin.source.sourceSpan.end}:${origin.column.toLowerCase()}`;
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
result.push(origin);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function originsForDefinition(model: SqlSemanticModel, definition: SqlSemanticRowSource, hit: CteColumnHit, columnName: string, visited: Set<string>, depth: number): CteColumnOrigin[] {
|
||||
if (depth > MAX_CTE_ORIGIN_DEPTH || visited.has(definition.id)) return [];
|
||||
visited.add(definition.id);
|
||||
|
||||
if (hit.output?.origin) {
|
||||
const { qualifierParts, column } = hit.output.origin;
|
||||
return expandOriginSources(model, definition, qualifierParts, column, visited, depth);
|
||||
}
|
||||
if (hit.stars && hit.stars.length > 0) {
|
||||
// `SELECT a.*, b.*` yields one candidate star per body source; trace them all and let the
|
||||
// caller's column metadata decide which one actually owns the name (see CteColumnHit.stars).
|
||||
return dedupeOrigins(hit.stars.flatMap((star) => expandStarSources(model, definition, star, columnName, visited, depth)));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function followBodySource(model: SqlSemanticModel, ownerDefinition: SqlSemanticRowSource, source: SqlSemanticRowSource, column: string, visited: Set<string>, depth: number): CteColumnOrigin[] {
|
||||
// Recursive CTE self-reference — never trace back into the same definition.
|
||||
if (sameName(source.name, ownerDefinition.name)) return [];
|
||||
if (source.kind === "cte") {
|
||||
const upstream = findCteDefinition(model, source.name);
|
||||
if (!upstream) return [];
|
||||
const upstreamHit = resolveCteOutputOrStars(upstream, column);
|
||||
if (!upstreamHit) return [];
|
||||
return originsForDefinition(model, upstream, upstreamHit, column, visited, depth + 1);
|
||||
}
|
||||
if (source.kind === "subquery") {
|
||||
// Derived table (enriched only when it sits directly inside a CTE body): resolve the column
|
||||
// against the inline view's own projections/stars, then keep tracing inside its body.
|
||||
const innerHit = resolveCteOutputOrStars(source, column);
|
||||
return innerHit ? originsForDefinition(model, source, innerHit, column, visited, depth + 1) : [];
|
||||
}
|
||||
if (isPhysicalOriginSource(source)) {
|
||||
return [{ source, column }];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function expandOriginSources(model: SqlSemanticModel, definition: SqlSemanticRowSource, qualifierParts: readonly string[], column: string, visited: Set<string>, depth: number): CteColumnOrigin[] {
|
||||
const sources = definition.bodySources ?? [];
|
||||
// Bare columns may come from any source, including an upstream CTE (chain); followBodySource
|
||||
// itself decides which source kinds are traceable.
|
||||
const matched = qualifierParts.length > 0 ? sources.filter((source) => sourceMatchesQualifier(source, qualifierParts)) : sources;
|
||||
const origins: CteColumnOrigin[] = [];
|
||||
for (const source of matched) {
|
||||
origins.push(...followBodySource(model, definition, source, column, visited, depth));
|
||||
}
|
||||
return dedupeOrigins(origins);
|
||||
}
|
||||
|
||||
function expandStarSources(model: SqlSemanticModel, definition: SqlSemanticRowSource, star: SqlSemanticCteStar, columnName: string, visited: Set<string>, depth: number): CteColumnOrigin[] {
|
||||
const sources = (definition.bodySources ?? []).filter((source) => !sameName(source.name, definition.name));
|
||||
const matched = star.qualifierParts.length > 0 ? sources.filter((source) => sourceMatchesQualifier(source, star.qualifierParts)) : sources;
|
||||
const origins: CteColumnOrigin[] = [];
|
||||
for (const source of matched) {
|
||||
origins.push(...followBodySource(model, definition, source, columnName, visited, depth));
|
||||
}
|
||||
return dedupeOrigins(origins);
|
||||
}
|
||||
|
||||
/**
|
||||
* Traces a resolved CTE column through simple projections and CTE chains down to the physical
|
||||
* table column(s) whose metadata can drive hover comments. Returns an empty list for
|
||||
* expressions, explicit-list mismatches, cycles, or over-deep chains — callers then fall back.
|
||||
*/
|
||||
export function resolveCteColumnOrigins(model: SqlSemanticModel, hit: CteColumnHit, outerColumnName: string): CteColumnOrigin[] {
|
||||
return originsForDefinition(model, hit.definition, hit, outerColumnName, new Set<string>(), 0);
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import { resolveSqlStatementWindow } from "@/lib/sql/sqlSyntaxTreeWindow";
|
||||
import type {
|
||||
SqlSemanticBuildOptions,
|
||||
SqlSemanticClauseSpans,
|
||||
SqlSemanticCteOutputColumn,
|
||||
SqlSemanticCteStar,
|
||||
SqlSemanticCursorIntent,
|
||||
SqlSemanticIdentifierPart,
|
||||
SqlSemanticModel,
|
||||
@@ -28,6 +30,10 @@ const TABLE_FUNCTION_INTRODUCERS = new Set(["from", "join", "straight_join", "ap
|
||||
const TOP_LEVEL_STATEMENT_WORDS = new Set(["select", "insert", "delete", "merge", "create", "alter", "drop", "truncate", "call", "exec", "execute", "grant", "revoke"]);
|
||||
const SQLSERVER_DEFAULT_SCHEMA = "dbo";
|
||||
const SQLSERVER_UPDATE_STATISTICS_SCOPES = new Set(["all", "index", "table"]);
|
||||
// CTE navigation enrichment (body projection origins + body row sources) is best-effort; on very
|
||||
// large statements it is skipped entirely so click/hover parsing stays cheap.
|
||||
const CTE_ENRICHMENT_STATEMENT_TOKEN_LIMIT = 20_000;
|
||||
const CTE_ENRICHMENT_BODY_TOKEN_LIMIT = 5_000;
|
||||
|
||||
interface ParseState {
|
||||
dialect: SqlSemanticDialectAdapter;
|
||||
@@ -36,6 +42,8 @@ interface ParseState {
|
||||
cteSources: SqlSemanticRowSource[];
|
||||
expandGroupedSources?: boolean;
|
||||
groupedSourceScopes?: SqlSemanticGroupedSourceScope[];
|
||||
/** Enrich derived-table row sources parsed from this state (one level, CTE bodies only). */
|
||||
enrichDerivedTables?: boolean;
|
||||
}
|
||||
|
||||
export interface SqlSemanticGroupedSourceScope {
|
||||
@@ -259,6 +267,136 @@ function parseSelectProjections(tokens: readonly SqlSemanticToken[], dialect: Sq
|
||||
.filter((projection): projection is SqlSemanticProjection => projection != null && projection.name !== "*");
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyzes one comma-separated CTE body SELECT projection for navigation metadata:
|
||||
* a bare/qualified star or a named output with an optional plain-column origin.
|
||||
*/
|
||||
function cteBodyProjectionShape(state: ParseState, group: readonly SqlSemanticToken[]): { output?: SqlSemanticCteOutputColumn; star?: SqlSemanticCteStar } | null {
|
||||
const useful = group.filter((item) => item.kind !== "comment");
|
||||
if (useful.length === 0) return null;
|
||||
const dialect = state.dialect;
|
||||
|
||||
// Bare `*`
|
||||
if (useful.length === 1 && useful[0]?.text === "*") {
|
||||
return { star: { starSpan: useful[0].span, qualifierParts: [] } };
|
||||
}
|
||||
|
||||
// Qualified `q.*` (possibly a multi-part `a.b.*`) — the whole group must be the star chain.
|
||||
const last = useful[useful.length - 1];
|
||||
if (last?.text === "*" && useful[useful.length - 2]?.text === ".") {
|
||||
const qualifierParts: string[] = [];
|
||||
let cursor = useful.length - 3;
|
||||
let valid = cursor >= 0;
|
||||
while (valid && cursor >= 0) {
|
||||
const identifier = useful[cursor];
|
||||
if (!identifier || !tokenIsIdentifier(identifier)) {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
qualifierParts.unshift(identifierPart(identifier, dialect).name);
|
||||
cursor -= 1;
|
||||
if (cursor < 0) break;
|
||||
if (useful[cursor]?.text !== ".") {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
cursor -= 1;
|
||||
}
|
||||
if (valid && qualifierParts.length > 0) {
|
||||
return { star: { starSpan: last.span, qualifierParts } };
|
||||
}
|
||||
}
|
||||
|
||||
const projection = projectionNameFromTokens(useful, dialect);
|
||||
if (!projection || projection.name === "*") return null;
|
||||
|
||||
// `expression AS alias` — navigate to the alias token; the expression itself is not traceable.
|
||||
if (projection.aliasSpan) {
|
||||
return { output: { name: projection.name, jumpSpan: projection.aliasSpan } };
|
||||
}
|
||||
|
||||
// Plain `col` / `q.col` chain (identifiers separated only by dots) — traceable to a body source.
|
||||
let isIdentifierChain = useful.length % 2 === 1;
|
||||
for (let index = 0; isIdentifierChain && index < useful.length; index += 1) {
|
||||
const token = useful[index];
|
||||
if (!token) {
|
||||
isIdentifierChain = false;
|
||||
break;
|
||||
}
|
||||
if (index % 2 === 0) {
|
||||
if (!tokenIsIdentifier(token)) isIdentifierChain = false;
|
||||
} else if (token.text !== ".") {
|
||||
isIdentifierChain = false;
|
||||
}
|
||||
}
|
||||
if (isIdentifierChain) {
|
||||
const identifiers = useful.filter((_, index) => index % 2 === 0);
|
||||
const columnToken = identifiers[identifiers.length - 1];
|
||||
if (columnToken) {
|
||||
return {
|
||||
output: {
|
||||
name: identifierPart(columnToken, dialect).name,
|
||||
jumpSpan: columnToken.span,
|
||||
origin: {
|
||||
qualifierParts: identifiers.slice(0, -1).map((token) => identifierPart(token, dialect).name),
|
||||
column: identifierPart(columnToken, dialect).name,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Any other expression without an alias — select the whole projection span, no origin.
|
||||
return { output: { name: projection.name, jumpSpan: projection.span } };
|
||||
}
|
||||
|
||||
/**
|
||||
* CTE-body counterpart of parseSelectProjections that keeps stars and per-projection spans
|
||||
* instead of flattening everything to bare column names.
|
||||
*/
|
||||
function parseCteBodyProjectionShapes(state: ParseState, bodyTokens: readonly SqlSemanticToken[]): { outputs: SqlSemanticCteOutputColumn[]; stars: SqlSemanticCteStar[] } {
|
||||
const outputs: SqlSemanticCteOutputColumn[] = [];
|
||||
const stars: SqlSemanticCteStar[] = [];
|
||||
const baseDepth = bodyTokens.reduce((min, item) => Math.min(min, item.depth), Number.POSITIVE_INFINITY);
|
||||
if (!Number.isFinite(baseDepth)) return { outputs, stars };
|
||||
const selectDepth = baseDepth;
|
||||
const selectIndex = bodyTokens.findIndex((item) => item.depth === selectDepth && item.kind === "word" && item.normalized === "select");
|
||||
if (selectIndex < 0) return { outputs, stars };
|
||||
let fromIndex = bodyTokens.findIndex((item, index) => index > selectIndex && item.depth === selectDepth && item.kind === "word" && item.normalized === "from");
|
||||
if (fromIndex < 0) fromIndex = bodyTokens.length;
|
||||
const projectionTokens = bodyTokens.slice(selectIndex + 1, fromIndex);
|
||||
for (const group of splitTopLevelByComma(projectionTokens)) {
|
||||
const shape = cteBodyProjectionShape(state, group);
|
||||
if (shape?.output) outputs.push(shape.output);
|
||||
if (shape?.star) stars.push(shape.star);
|
||||
}
|
||||
return { outputs, stars };
|
||||
}
|
||||
|
||||
/** Parses row sources visible inside one CTE body; preceding CTEs are in scope for chaining. */
|
||||
function parseCteBodySources(state: ParseState, bodyTokens: readonly SqlSemanticToken[], precedingCtes: readonly SqlSemanticRowSource[]): SqlSemanticRowSource[] {
|
||||
if (bodyTokens.length === 0) return [];
|
||||
let bodyDepth = Number.POSITIVE_INFINITY;
|
||||
for (const token of bodyTokens) bodyDepth = Math.min(bodyDepth, token.depth);
|
||||
if (!Number.isFinite(bodyDepth)) return [];
|
||||
const first = bodyTokens.find((token) => token.depth === bodyDepth);
|
||||
const last = bodyTokens[bodyTokens.length - 1];
|
||||
if (!first || !last) return [];
|
||||
const bodyState: ParseState = {
|
||||
...state,
|
||||
tokens: bodyTokens as SqlSemanticToken[],
|
||||
statement: {
|
||||
kind: "select",
|
||||
span: { start: first.span.start, end: last.span.end },
|
||||
text: state.statement.text,
|
||||
},
|
||||
cteSources: precedingCtes as SqlSemanticRowSource[],
|
||||
// Body parsing must not leak grouped-join scopes into the outer statement parse.
|
||||
groupedSourceScopes: state.groupedSourceScopes ? [] : undefined,
|
||||
};
|
||||
return parseRowSourcesAtDepth(bodyState, bodyDepth);
|
||||
}
|
||||
|
||||
function parseCteSources(state: ParseState): SqlSemanticRowSource[] {
|
||||
const tokens = state.tokens;
|
||||
const first = tokens.findIndex((item) => item.kind === "word" && item.normalized === "with");
|
||||
@@ -266,6 +404,7 @@ function parseCteSources(state: ParseState): SqlSemanticRowSource[] {
|
||||
const sources: SqlSemanticRowSource[] = [];
|
||||
let index = first + 1;
|
||||
if (tokens[index]?.normalized === "recursive") index += 1;
|
||||
const enrich = tokens.length <= CTE_ENRICHMENT_STATEMENT_TOKEN_LIMIT;
|
||||
|
||||
while (index < tokens.length) {
|
||||
while (tokens[index]?.text === ",") index += 1;
|
||||
@@ -274,13 +413,13 @@ function parseCteSources(state: ParseState): SqlSemanticRowSource[] {
|
||||
const namePart = identifierPart(nameToken, state.dialect);
|
||||
index += 1;
|
||||
|
||||
const explicitColumns: string[] = [];
|
||||
const explicitColumns: Array<{ name: string; span: SqlSemanticSpan }> = [];
|
||||
if (tokens[index]?.text === "(") {
|
||||
const close = findMatchingParenToken(tokens, index);
|
||||
if (close > index) {
|
||||
for (const part of splitTopLevelByComma(tokens.slice(index + 1, close))) {
|
||||
const identifier = part.find(tokenIsIdentifier);
|
||||
if (identifier) explicitColumns.push(identifierPart(identifier, state.dialect).name);
|
||||
if (identifier) explicitColumns.push({ name: identifierPart(identifier, state.dialect).name, span: identifier.span });
|
||||
}
|
||||
index = close + 1;
|
||||
}
|
||||
@@ -292,7 +431,32 @@ function parseCteSources(state: ParseState): SqlSemanticRowSource[] {
|
||||
const bodyClose = findMatchingParenToken(tokens, bodyOpen);
|
||||
const safeBodyClose = bodyClose < 0 ? tokens.length - 1 : bodyClose;
|
||||
const bodyTokens = tokens.slice(bodyOpen + 1, safeBodyClose);
|
||||
const bodyColumns = explicitColumns.length > 0 ? explicitColumns : parseSelectProjections(bodyTokens, state.dialect).map((projection) => projection.name);
|
||||
|
||||
// columns: unchanged flat name list consumed by completion/intention logic.
|
||||
const bodyColumns = explicitColumns.length > 0 ? explicitColumns.map((column) => column.name) : parseSelectProjections(bodyTokens, state.dialect).map((projection) => projection.name);
|
||||
|
||||
let cteOutputs: SqlSemanticCteOutputColumn[] | undefined;
|
||||
let cteStars: SqlSemanticCteStar[] | undefined;
|
||||
let bodySources: SqlSemanticRowSource[] | undefined;
|
||||
if (enrich && bodyTokens.length <= CTE_ENRICHMENT_BODY_TOKEN_LIMIT) {
|
||||
const shapes = parseCteBodyProjectionShapes(state, bodyTokens);
|
||||
cteStars = shapes.stars;
|
||||
if (explicitColumns.length > 0) {
|
||||
// Explicit `WITH c(a,b)` names win; body projection origins are inherited positionally
|
||||
// so hover comments can still trace through the list, while navigation lands on list items.
|
||||
cteOutputs = explicitColumns.map((column, columnIndex) => ({
|
||||
name: column.name,
|
||||
jumpSpan: column.span,
|
||||
...(shapes.outputs[columnIndex]?.origin ? { origin: shapes.outputs[columnIndex]!.origin } : {}),
|
||||
}));
|
||||
} else {
|
||||
cteOutputs = shapes.outputs;
|
||||
}
|
||||
// Derived tables in the body (12-branch `FROM (SELECT ...) X` bodies and friends) receive one
|
||||
// level of enrichment so CTE lineage can keep tracing down to the physical tables inside them.
|
||||
bodySources = parseCteBodySources({ ...state, enrichDerivedTables: true }, bodyTokens, sources);
|
||||
}
|
||||
|
||||
sources.push({
|
||||
id: `cte:${namePart.name}:${sources.length}`,
|
||||
kind: "cte",
|
||||
@@ -300,6 +464,11 @@ function parseCteSources(state: ParseState): SqlSemanticRowSource[] {
|
||||
qualifierParts: [],
|
||||
sourceSpan: { start: nameToken.span.start, end: tokens[safeBodyClose]?.span.end ?? nameToken.span.end },
|
||||
columns: bodyColumns,
|
||||
nameSpan: nameToken.span,
|
||||
bodySpan: { start: tokens[bodyOpen]?.span.start ?? nameToken.span.start, end: tokens[safeBodyClose]?.span.end ?? nameToken.span.end },
|
||||
...(cteOutputs ? { cteOutputs } : {}),
|
||||
...(cteStars ? { cteStars } : {}),
|
||||
...(bodySources ? { bodySources } : {}),
|
||||
});
|
||||
index = safeBodyClose + 1;
|
||||
|
||||
@@ -340,6 +509,19 @@ function mergeColumnAliases(columns: readonly string[], aliases: readonly string
|
||||
return columns.map((column, index) => aliases[index] ?? column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Derived tables can rename the inline-view outputs (`FROM (SELECT id FROM t) x(a)`): the outer
|
||||
* query sees `x.a` while the body still projects `id`. The rename list is positional, so it can
|
||||
* only be applied when the body projects names one-for-one — no stars and equal lengths. Otherwise
|
||||
* the outputs are returned untouched, because a positional rename against a star or a shorter list
|
||||
* would point `x.a` at an unrelated column. `origin` / `jumpSpan` are intentionally kept: both
|
||||
* still describe the body side, which is where tracing and Ctrl+click must continue.
|
||||
*/
|
||||
function applyDerivedTableColumnAliases(outputs: SqlSemanticCteOutputColumn[], stars: readonly SqlSemanticCteStar[], aliases: readonly string[] | undefined): SqlSemanticCteOutputColumn[] {
|
||||
if (!aliases?.length || stars.length > 0 || aliases.length !== outputs.length) return outputs;
|
||||
return outputs.map((output, index) => ({ ...output, name: aliases[index] ?? output.name }));
|
||||
}
|
||||
|
||||
function consumeSqlServerTableHint(tokens: readonly SqlSemanticToken[], index: number, dialect: SqlSemanticDialectAdapter): number {
|
||||
if (dialect.id !== "sqlserver") return index;
|
||||
const openIndex = tokens[index]?.normalized === "with" && tokens[index + 1]?.text === "(" ? index + 1 : index;
|
||||
@@ -358,6 +540,21 @@ function parseSubquerySource(state: ParseState, openIndex: number, introducer: s
|
||||
parseSelectProjections(bodyTokens, state.dialect).map((projection) => projection.name),
|
||||
alias.columns,
|
||||
);
|
||||
// A derived table reached from a CTE body carries the same body metadata a CTE definition does,
|
||||
// so `followBodySource` can resolve its projections/stars and trace one level into the inline
|
||||
// view. Derived tables outside CTE bodies stay metadata-free (the common path stays cheap).
|
||||
let body: Pick<SqlSemanticRowSource, "cteOutputs" | "cteStars" | "bodySources"> = {};
|
||||
if (state.enrichDerivedTables && bodyTokens.length <= CTE_ENRICHMENT_BODY_TOKEN_LIMIT) {
|
||||
const shapes = parseCteBodyProjectionShapes(state, bodyTokens);
|
||||
body = {
|
||||
// Outer column aliases rename the outputs so `x.a` resolves; tracing stays on the body side.
|
||||
cteOutputs: applyDerivedTableColumnAliases(shapes.outputs, shapes.stars, alias.columns),
|
||||
cteStars: shapes.stars,
|
||||
// Enrichment stops here: derived tables nested inside this inline view are not parsed again,
|
||||
// so depth can never re-parse arbitrarily nested views on every model build.
|
||||
bodySources: parseCteBodySources({ ...state, enrichDerivedTables: false }, bodyTokens, state.cteSources),
|
||||
};
|
||||
}
|
||||
return {
|
||||
source: {
|
||||
id: `${introducer}:subquery:${sourceIndex}`,
|
||||
@@ -368,6 +565,7 @@ function parseSubquerySource(state: ParseState, openIndex: number, introducer: s
|
||||
aliasSpan: alias.aliasSpan,
|
||||
sourceSpan: { start: state.tokens[openIndex]?.span.start ?? 0, end: state.tokens[alias.nextIndex - 1]?.span.end ?? alias.aliasSpan?.end ?? state.tokens[close]?.span.end ?? 0 },
|
||||
columns,
|
||||
...body,
|
||||
},
|
||||
nextIndex: alias.nextIndex,
|
||||
};
|
||||
|
||||
@@ -51,6 +51,35 @@ export interface SqlSemanticProjection {
|
||||
aliasSpan?: SqlSemanticSpan;
|
||||
}
|
||||
|
||||
/**
|
||||
* Traceable origin of a CTE output column that is a plain column reference
|
||||
* (`col` or `q.col`) in the CTE body. Expression/alias outputs carry no origin.
|
||||
*/
|
||||
export interface SqlSemanticCteColumnOrigin {
|
||||
/** Qualifier chain before the column, e.g. `t` for `t.id`; empty for a bare `id`. */
|
||||
qualifierParts: string[];
|
||||
/** Referenced column name inside the CTE body. */
|
||||
column: string;
|
||||
}
|
||||
|
||||
/** A named column produced by a CTE (`WITH c(a,b)` list item or SELECT projection). */
|
||||
export interface SqlSemanticCteOutputColumn {
|
||||
/** Output column name as seen by the outer query. */
|
||||
name: string;
|
||||
/** Editor span to select when navigating to this output column inside the CTE body. */
|
||||
jumpSpan: SqlSemanticSpan;
|
||||
/** Present only when the body projection is a traceable plain column reference. */
|
||||
origin?: SqlSemanticCteColumnOrigin;
|
||||
}
|
||||
|
||||
/** A star projection (`*` or `q.*`) inside a CTE body. */
|
||||
export interface SqlSemanticCteStar {
|
||||
/** Span of the `*` token itself (navigation target). */
|
||||
starSpan: SqlSemanticSpan;
|
||||
/** Qualifier chain for `q.*`; empty for a bare `*`. */
|
||||
qualifierParts: string[];
|
||||
}
|
||||
|
||||
export interface SqlSemanticRowSource {
|
||||
id: string;
|
||||
kind: SqlSemanticRowSourceKind;
|
||||
@@ -64,6 +93,20 @@ export interface SqlSemanticRowSource {
|
||||
columnAliases?: string[];
|
||||
metadataTarget?: SqlSemanticMetadataTarget;
|
||||
unresolved?: boolean;
|
||||
/** CTE name token span — only populated on `cte:` definition row sources. */
|
||||
nameSpan?: SqlSemanticSpan;
|
||||
/** CTE body parentheses span — only populated on `cte:` definition row sources. */
|
||||
bodySpan?: SqlSemanticSpan;
|
||||
/**
|
||||
* Named output columns with in-body navigation/trace metadata. Populated on `cte:` definitions,
|
||||
* and — reusing the same shape — on derived tables (`kind: "subquery"`) directly inside a CTE
|
||||
* body, so lineage can continue through `FROM (SELECT ...) x` to the tables inside it.
|
||||
*/
|
||||
cteOutputs?: SqlSemanticCteOutputColumn[];
|
||||
/** Star projections of the body (CTE definitions and enriched derived tables). */
|
||||
cteStars?: SqlSemanticCteStar[];
|
||||
/** Row sources visible inside the body (physical tables + upstream CTEs) — same two owners. */
|
||||
bodySources?: SqlSemanticRowSource[];
|
||||
}
|
||||
|
||||
export interface SqlSemanticClauseSpans {
|
||||
|
||||
@@ -306,8 +306,17 @@ export function mergeSqlObjectNavigationType(left?: SqlObjectNavigationType, rig
|
||||
return left ?? right;
|
||||
}
|
||||
|
||||
// Aligned with the semantic tokenizer (tokens.ts WORD_START/WORD_PART) so Ctrl/Cmd+click,
|
||||
// hover range extraction and Unicode identifiers (e.g. CJK names) agree on identifier chars.
|
||||
const IDENTIFIER_START_CHAR = /^[A-Za-z_@$#\p{ID_Start}]$/u;
|
||||
const IDENTIFIER_PART_CHAR = /^[A-Za-z0-9_@$#\p{ID_Continue}]$/u;
|
||||
|
||||
function isIdentifierStartChar(char: string | undefined): boolean {
|
||||
return !!char && IDENTIFIER_START_CHAR.test(char);
|
||||
}
|
||||
|
||||
function isIdentifierChar(char: string | undefined): boolean {
|
||||
return !!char && /^[A-Za-z0-9_$]$/.test(char);
|
||||
return !!char && IDENTIFIER_PART_CHAR.test(char);
|
||||
}
|
||||
|
||||
function readQuotedPart(text: string, start: number): IdentifierPart | null {
|
||||
@@ -332,7 +341,7 @@ function readQuotedPart(text: string, start: number): IdentifierPart | null {
|
||||
}
|
||||
|
||||
function readUnquotedPart(text: string, start: number): IdentifierPart | null {
|
||||
if (!isIdentifierChar(text[start])) return null;
|
||||
if (!isIdentifierStartChar(text[start])) return null;
|
||||
let end = start + 1;
|
||||
while (end < text.length && isIdentifierChar(text[end])) end += 1;
|
||||
return { value: text.slice(start, end), start, end, quoted: false };
|
||||
|
||||
Reference in new issue
Block a user