fix(snowflake): resolve database and schema completion namespaces
Closes #10953
This commit is contained in:
1 parent
6d1c3c8373
commit
59b6de2cfb
12 files changed
+378
-23
No files matched your search
@@ -64,6 +64,15 @@ export const SQL_SEMANTIC_DIALECTS: Record<string, SqlSemanticDialectAdapter> =
|
||||
quoteIdentifier: (identifier) => quoteWith(identifier, '"'),
|
||||
qualifierRole: roleForGenericQualifier,
|
||||
},
|
||||
snowflake: {
|
||||
id: "snowflake",
|
||||
identifierQuotes: [{ open: '"', close: '"' }],
|
||||
supportsAsForTableAlias: true,
|
||||
projectionAliasVisibility: defaultProjectionAliasVisibility,
|
||||
normalizeIdentifier: upperUnquoted,
|
||||
quoteIdentifier: (identifier) => quoteWith(identifier, '"'),
|
||||
qualifierRole: roleForGenericQualifier,
|
||||
},
|
||||
postgres: {
|
||||
id: "postgres",
|
||||
identifierQuotes: [{ open: '"', close: '"' }],
|
||||
@@ -190,6 +199,7 @@ export function sqlReferenceAnalysisDialectFor(options: { databaseType?: Databas
|
||||
}
|
||||
|
||||
export function sqlSemanticDialectFor(options: { databaseType?: DatabaseType; dialect?: "mysql" | "postgres" | "sqlserver" | "clickhouse" | "doris" | "soql" }): SqlSemanticDialectAdapter {
|
||||
if (options.databaseType === "snowflake") return SQL_SEMANTIC_DIALECTS.snowflake;
|
||||
if (options.databaseType === "salesforce") return SQL_SEMANTIC_DIALECTS.soql;
|
||||
if (options.databaseType === "clickhouse") return SQL_SEMANTIC_DIALECTS.clickhouse;
|
||||
// Doris/StarRocks connections ride the editor's MySQL fallback dialect (codeMirrorSqlDialect maps
|
||||
|
||||
@@ -1471,7 +1471,7 @@ type SqlCompletionApplyDialect = "mysql" | "postgres" | "sqlserver" | "oracle" |
|
||||
const MYSQL_LIKE_IDENTIFIER_DATABASES = new Set<DatabaseType>(["mysql", "clickhouse", "hive", "argo", "transwarp", "kyuubi", "impala", "spark", "databend", "tdengine", "access", "doris", "starrocks"]);
|
||||
const POSTGRES_LIKE_IDENTIFIER_DATABASES = new Set<DatabaseType>(["postgres", "redshift", "gaussdb", "kingbase", "highgo", "uxdb", "vastbase", "kwdb", "opengauss"]);
|
||||
export const ORACLE_COMPAT_IDENTIFIER_DATABASES = new Set<DatabaseType>(["oracle", "oceanbase-oracle", "yashandb", "oscar", "xugu"]);
|
||||
const UPPER_FOLDING_IDENTIFIER_DATABASES = new Set<DatabaseType>(["dameng", "db2"]);
|
||||
const UPPER_FOLDING_IDENTIFIER_DATABASES = new Set<DatabaseType>(["dameng", "db2", "snowflake"]);
|
||||
|
||||
function sqlCompletionApplyDialect(databaseType: DatabaseType | undefined, fallback: "mysql" | "postgres" | "sqlserver" | undefined): SqlCompletionApplyDialect | undefined {
|
||||
if (!databaseType) return fallback;
|
||||
@@ -2850,7 +2850,7 @@ function parseTrailingIdentifierContext(input: string, databaseType?: DatabaseTy
|
||||
index -= 1;
|
||||
continue;
|
||||
}
|
||||
parts.unshift(unquoteIdentifier(parsed.raw));
|
||||
parts.unshift(parsed.raw);
|
||||
index = parsed.start;
|
||||
if (index <= 0 || tail[index - 1] !== ".") break;
|
||||
index -= 1;
|
||||
@@ -2860,8 +2860,8 @@ function parseTrailingIdentifierContext(input: string, databaseType?: DatabaseTy
|
||||
const start = index;
|
||||
|
||||
if (parts.length >= 2 || endsWithDot) {
|
||||
const qualifierParts = endsWithDot ? parts : parts.slice(0, -1);
|
||||
const prefixPart = endsWithDot ? "" : (parts[parts.length - 1] ?? "");
|
||||
const qualifierParts = (endsWithDot ? parts : parts.slice(0, -1)).map((part) => completionLookupIdentifier(part, databaseType));
|
||||
const prefixPart = endsWithDot ? "" : unquoteIdentifier(parts[parts.length - 1] ?? "");
|
||||
const qualifierValue = qualifierParts.join(".");
|
||||
return {
|
||||
start,
|
||||
@@ -2873,7 +2873,7 @@ function parseTrailingIdentifierContext(input: string, databaseType?: DatabaseTy
|
||||
|
||||
return {
|
||||
start,
|
||||
prefix: parts[0] ?? "",
|
||||
prefix: unquoteIdentifier(parts[0] ?? ""),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3625,8 +3625,8 @@ function extractReferencedTables(sql: string, databaseType?: DatabaseType): SqlC
|
||||
}
|
||||
const rawParts = splitQualifiedNameRawParts(rawName);
|
||||
const omittedSqlServerSchema = databaseType === "sqlserver" && rawParts.length >= 3 && rawParts[rawParts.length - 2] === "";
|
||||
const unquotedRawParts = rawParts.map((part) => unquoteIdentifier(part));
|
||||
const parts = rawParts.map((part) => unquoteIdentifier(part)).filter(Boolean);
|
||||
const unquotedRawParts = rawParts.map((part) => completionLookupIdentifier(part, databaseType));
|
||||
const parts = unquotedRawParts.filter(Boolean);
|
||||
const name = unquotedRawParts[unquotedRawParts.length - 1];
|
||||
if (!name) continue;
|
||||
const table: SqlCompletionReferencedTable = {
|
||||
@@ -3994,6 +3994,12 @@ function unquoteIdentifier(value: string): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
function completionLookupIdentifier(value: string, databaseType?: DatabaseType): string {
|
||||
const name = unquoteIdentifier(value);
|
||||
if (databaseType !== "snowflake") return name;
|
||||
return value.startsWith('"') ? name.replaceAll('""', '"') : name.toUpperCase();
|
||||
}
|
||||
|
||||
export function quoteSqlIdentifier(identifier: string, dialect?: SqlCompletionApplyDialect): string {
|
||||
if (dialect === "oracle") {
|
||||
// Oracle folds bare identifiers to uppercase, so only all-uppercase names
|
||||
|
||||
@@ -274,6 +274,7 @@ export function resolveSqlCompletionSchemaLookupDatabase(options: {
|
||||
}
|
||||
|
||||
export function resolveSqlCompletionTableLookupTarget(options: {
|
||||
databaseType?: DatabaseType;
|
||||
currentDatabase: string;
|
||||
currentSchema?: string;
|
||||
supportsDatabaseQualifier: boolean;
|
||||
@@ -287,7 +288,7 @@ export function resolveSqlCompletionTableLookupTarget(options: {
|
||||
if (options.supportsDatabaseSchemaQualifier && completionContext.suggestTables && !completionContext.insertTable && qualifierParts.length >= 2) {
|
||||
const databaseQualifier = qualifierParts[qualifierParts.length - 2]!;
|
||||
const schema = qualifierParts[qualifierParts.length - 1]!;
|
||||
const database = findExactName(options.knownDatabases, databaseQualifier) ?? databaseQualifier;
|
||||
const database = (options.databaseType === "snowflake" ? findCaseSensitiveName(options.knownDatabases, databaseQualifier) : findExactName(options.knownDatabases, databaseQualifier)) ?? databaseQualifier;
|
||||
return {
|
||||
database,
|
||||
schema,
|
||||
|
||||
Reference in new issue
Block a user