fix(sql-analysis): tolerate Oracle admin DDL in diagnostics

This commit is contained in:
zipg authored and GitHub committed 2026-09-20 11:06:40 +08:00
1 parent a67bbb4ed5
commit 88ba58a79c
9 files changed
+241 -99

No files matched your search

+21 -15
View File
@@ -1690,13 +1690,13 @@ class SqlCompletionProvider {
const emptyTableNameCompletion = !context.prefix && (context.suggestTables || context.exclusiveTableSuggestions);
if (!pendingJoinKeyword && !emptyTableNameCompletion && !context.tableAliasAfterCursor && context.referencedTables.length > 0 && !context.suggestColumns && !context.insertTable && supportsTableAliases(this.databaseType)) {
this.items.push(...buildAliasItems(context, this.databaseType, this.input.keywordCase));
this.items.push(...buildAliasItems(context));
}
if (!context.exclusiveColumnSuggestions && context.suggestTables) {
const autoAliasTables = !!this.input.autoAliasTables && context.autoAliasTableCompletions && !context.tableCompletionTargetAliasUnsafe && supportsTableAliases(this.databaseType);
this.items.push(...buildForeignKeyRelatedTableItems(context, completionTables, this.input.foreignKeysByTable, this.dialect, autoAliasTables, this.databaseType, this.input.keywordCase, this.input.currentSchema));
this.items.push(...buildTableItems(context, completionTables, this.dialect, autoAliasTables, context.referencedTables, this.databaseType, this.input.currentSchema, this.input.keywordCase));
this.items.push(...buildForeignKeyRelatedTableItems(context, completionTables, this.input.foreignKeysByTable, this.dialect, autoAliasTables, this.databaseType, this.input.currentSchema));
this.items.push(...buildTableItems(context, completionTables, this.dialect, autoAliasTables, context.referencedTables, this.databaseType, this.input.currentSchema));
if (this.databaseType === "clickhouse") {
this.items.push(...buildClickHouseFunctionItems(context.prefix, context.openingParenAfterCursor, "table"));
}
@@ -1725,7 +1725,7 @@ class SqlCompletionProvider {
if (context.prefix) {
for (const item of this.items) {
// Alias snippets reuse the prefix as a label while applying alias SQL, so they are not exact name matches.
const isAliasSnippet = item.type === "snippet" && item.apply === formatAliasCompletionApply(item.label, this.databaseType, this.input.keywordCase);
const isAliasSnippet = item.type === "snippet" && item.apply === formatAliasCompletionApply(item.label);
const isExactLabelMatch = !isAliasSnippet && item.label.toLowerCase() === context.prefix.toLowerCase();
const isExactFilterTextMatch = item.filterText?.toLowerCase() === context.prefix.toLowerCase();
if (isExactLabelMatch || isExactFilterTextMatch) {
@@ -3850,7 +3850,6 @@ function buildTableItems(
referencedTables: SqlCompletionReferencedTable[] = [],
databaseType?: DatabaseType,
currentSchema?: string,
keywordCase?: SqlKeywordCase,
): SqlCompletionItem[] {
const { prefix } = context;
const qualifierSchema = context.qualifier?.split(".").filter(Boolean).pop();
@@ -3873,7 +3872,7 @@ function buildTableItems(
label: table.name,
type: "table" as const,
detail,
apply: formatTableAliasApply(applyName, alias, databaseType, keywordCase),
apply: formatTableAliasApply(applyName, alias),
boost: computeBoost(table.name, prefix) + 1000 + (table.boost ?? 0),
dedupeKey: table.applyName || ambiguousTableName || (databaseType === "oracle" && table.schema) ? applyName : undefined,
};
@@ -3889,7 +3888,6 @@ function buildForeignKeyRelatedTableItems(
dialect?: SqlCompletionApplyDialect,
autoAliasTables = false,
databaseType?: DatabaseType,
keywordCase?: SqlKeywordCase,
currentSchema?: string,
): SqlCompletionItem[] {
if (!foreignKeysByTable || context.referencedTables.length === 0) return [];
@@ -3928,7 +3926,7 @@ function buildForeignKeyRelatedTableItems(
label: table.name,
type: "table" as const,
detail,
apply: formatTableAliasApply(applyName, alias, databaseType, keywordCase),
apply: formatTableAliasApply(applyName, alias),
boost: computeBoost(table.name, context.prefix) + 3600,
// Mirror buildTableItems' dedupeKey so an FK candidate and the regular
// candidate for the same schema-qualified table collapse to one entry.
@@ -4443,7 +4441,7 @@ function buildReferencedAliasItems(context: SqlCompletionContext, t?: SqlComplet
return items;
}
function buildAliasItems(context: SqlCompletionContext, databaseType?: DatabaseType, keywordCase?: SqlKeywordCase): SqlCompletionItem[] {
function buildAliasItems(context: SqlCompletionContext): SqlCompletionItem[] {
const items: SqlCompletionItem[] = [];
const existingAliases = new Set(context.referencedTables.map((ref) => ref.alias?.toLowerCase()).filter((alias): alias is string => !!alias));
const seen = new Set<string>(existingAliases);
@@ -4457,20 +4455,28 @@ function buildAliasItems(context: SqlCompletionContext, databaseType?: DatabaseT
label: candidate,
type: "snippet" as const,
detail: `alias for ${ref.name}`,
apply: formatAliasCompletionApply(candidate, databaseType, keywordCase),
apply: formatAliasCompletionApply(candidate),
boost: 1600 - items.length,
});
}
return items;
}
function formatTableAliasApply(tableName: string, alias: string, databaseType?: DatabaseType, keywordCase?: SqlKeywordCase): string {
/**
* Table aliases are always emitted in the implicit form (`orders o`).
*
* `AS` is not valid for table aliases on every engine: Oracle, and the Oracle-compatible profiles
* (OceanBase Oracle, Kingbase in Oracle mode, ...), reject `FROM orders AS o`. The implicit form is
* accepted by every dialect DBX supports, so generated SQL stays portable across
* PostgreSQL / Oracle / Kingbase (issue #9525).
*/
function formatTableAliasApply(tableName: string, alias: string): string {
if (!alias) return tableName;
return isOracleLikeDatabase(databaseType) ? `${tableName} ${alias}` : `${tableName} ${applySqlKeywordCase("AS", keywordCase)} ${alias}`;
return `${tableName} ${alias}`;
}
function formatAliasCompletionApply(alias: string, databaseType?: DatabaseType, keywordCase?: SqlKeywordCase): string {
return isOracleLikeDatabase(databaseType) ? `${alias} ` : `${applySqlKeywordCase("AS", keywordCase)} ${alias} `;
function formatAliasCompletionApply(alias: string): string {
return `${alias} `;
}
function generateAlias(tableName: string, existing = new Set<string>()): string {
@@ -5393,7 +5399,7 @@ function isOracleLikeDatabase(databaseType?: DatabaseType): boolean {
return databaseType === "oracle" || databaseType === "oceanbase-oracle";
}
// CQL has no table alias syntax, so Cassandra must never receive `table AS alias`
// CQL has no table alias syntax, so Cassandra must never receive `table alias`
// or standalone alias suggestions.
function supportsTableAliases(databaseType?: DatabaseType): boolean {
return databaseType !== "cassandra";