Skip to content

Commit

Permalink
SNOW-1636286: Use specific class for context aware metadata search
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dprzybysz committed Jan 14, 2025
1 parent b4ced28 commit 95c06ca
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 111 deletions.
123 changes: 70 additions & 53 deletions src/main/java/net/snowflake/client/jdbc/SnowflakeDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import net.snowflake.client.log.ArgSupplier;
import net.snowflake.client.log.SFLogger;
import net.snowflake.client.log.SFLoggerFactory;
import net.snowflake.client.util.SFTriple;
import net.snowflake.common.core.SqlState;
import net.snowflake.common.util.Wildcard;

Expand Down Expand Up @@ -1137,11 +1136,10 @@ public ResultSet getProcedures(
return SnowflakeDatabaseMetaDataResultSet.getEmptyResultSet(GET_PROCEDURES, statement);
}

SFTriple<String, String, Boolean> result =
applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.first();
String schemaPattern = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.database();
String schemaPattern = result.schema();
boolean isExactSchema = result.isExactSchema();

final Pattern compiledSchemaPattern = Wildcard.toRegexPattern(schemaPattern, true);
final Pattern compiledProcedurePattern = Wildcard.toRegexPattern(procedureNamePattern, true);
Expand Down Expand Up @@ -1169,7 +1167,7 @@ public boolean next() throws SQLException {
|| compiledProcedurePattern.matcher(procedureName).matches())
&& (compiledSchemaPattern == null
|| compiledSchemaPattern.matcher(schemaName).matches()
|| isExactSchema)) {
|| isExactSchema && schemaPattern.equals(schemaPattern))) {
logger.trace("Found a matched function:" + schemaName + "." + procedureName);

nextRow[0] = catalogName;
Expand Down Expand Up @@ -1402,8 +1400,7 @@ else if (i == 0) {
}

// apply session context when catalog is unspecified
private SFTriple<String, String, Boolean> applySessionContext(
String catalog, String schemaPattern) {
private ContextAwareMetadataSearch applySessionContext(String catalog, String schemaPattern) {
if (metadataRequestUseConnectionCtx) {
// CLIENT_METADATA_USE_SESSION_DATABASE = TRUE
if (catalog == null) {
Expand All @@ -1420,18 +1417,19 @@ private SFTriple<String, String, Boolean> applySessionContext(
}
}
}
return new SFTriple<>(catalog, schemaPattern, exactSchemaSearchEnabled && useSessionSchema);
return new ContextAwareMetadataSearch(
catalog, schemaPattern, exactSchemaSearchEnabled && useSessionSchema);
}

/* helper function for getProcedures, getFunctionColumns, etc. Returns sql command to show some type of result such
as procedures or udfs */
private String getFirstResultSetCommand(
String catalog, String schemaPattern, String name, String type) {
// apply session context when catalog is unspecified
SFTriple<String, String, Boolean> result = applySessionContext(catalog, schemaPattern);
catalog = result.first();
schemaPattern = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(catalog, schemaPattern);
catalog = result.database();
schemaPattern = result.schema();
boolean isExactSchema = result.isExactSchema();

String showProcedureCommand = "show /* JDBC:DatabaseMetaData.getProcedures() */ " + type;

Expand Down Expand Up @@ -1524,11 +1522,10 @@ public ResultSet getTables(
return SnowflakeDatabaseMetaDataResultSet.getEmptyResultSet(GET_TABLES, statement);
}

SFTriple<String, String, Boolean> result =
applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.first();
String schemaPattern = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.database();
String schemaPattern = result.schema();
boolean isExactSchema = result.isExactSchema();

final Pattern compiledSchemaPattern = Wildcard.toRegexPattern(schemaPattern, true);
final Pattern compiledTablePattern = Wildcard.toRegexPattern(tableNamePattern, true);
Expand Down Expand Up @@ -1715,11 +1712,10 @@ public ResultSet getColumns(
Statement statement = connection.createStatement();

// apply session context when catalog is unspecified
SFTriple<String, String, Boolean> result =
applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.first();
String schemaPattern = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.database();
String schemaPattern = result.schema();
boolean isExactSchema = result.isExactSchema();

final Pattern compiledSchemaPattern = Wildcard.toRegexPattern(schemaPattern, true);
final Pattern compiledTablePattern = Wildcard.toRegexPattern(tableNamePattern, true);
Expand Down Expand Up @@ -1986,11 +1982,10 @@ public ResultSet getTablePrivileges(
return SnowflakeDatabaseMetaDataResultSet.getEmptyResultSet(GET_TABLE_PRIVILEGES, statement);
}
// apply session context when catalog is unspecified
SFTriple<String, String, Boolean> result =
applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.first();
String schemaPattern = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.database();
String schemaPattern = result.schema();
boolean isExactSchema = result.isExactSchema();

String showView = "select * from ";

Expand Down Expand Up @@ -2108,10 +2103,10 @@ public ResultSet getPrimaryKeys(String originalCatalog, String originalSchema, f
String showPKCommand = "show /* JDBC:DatabaseMetaData.getPrimaryKeys() */ primary keys in ";

// apply session context when catalog is unspecified
SFTriple<String, String, Boolean> result = applySessionContext(originalCatalog, originalSchema);
String catalog = result.first();
String schema = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(originalCatalog, originalSchema);
String catalog = result.database();
String schema = result.schema();
boolean isExactSchema = result.isExactSchema();

// These Patterns will only be used if the connection property enablePatternSearch=true
final Pattern compiledSchemaPattern = Wildcard.toRegexPattern(schema, true);
Expand Down Expand Up @@ -2272,11 +2267,11 @@ private ResultSet getForeignKeys(
StringBuilder commandBuilder = new StringBuilder();

// apply session context when catalog is unspecified
SFTriple<String, String, Boolean> result =
ContextAwareMetadataSearch result =
applySessionContext(originalParentCatalog, originalParentSchema);
String parentCatalog = result.first();
String parentSchema = result.second();
boolean isExactSchema = result.third();
String parentCatalog = result.database();
String parentSchema = result.schema();
boolean isExactSchema = result.isExactSchema();

// These Patterns will only be used to filter results if the connection property
// enablePatternSearch=true
Expand Down Expand Up @@ -2885,11 +2880,10 @@ public ResultSet getStreams(
Statement statement = connection.createStatement();

// apply session context when catalog is unspecified
SFTriple<String, String, Boolean> result =
applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.first();
String schemaPattern = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.database();
String schemaPattern = result.schema();
boolean isExactSchema = result.isExactSchema();

final Pattern compiledSchemaPattern = Wildcard.toRegexPattern(schemaPattern, true);
final Pattern compiledStreamNamePattern = Wildcard.toRegexPattern(streamName, true);
Expand Down Expand Up @@ -3290,10 +3284,10 @@ public ResultSet getSchemas(String originalCatalog, String originalSchema) throw
raiseSQLExceptionIfConnectionIsClosed();

// apply session context when catalog is unspecified
SFTriple<String, String, Boolean> result = applySessionContext(originalCatalog, originalSchema);
final String catalog = result.first();
final String schemaPattern = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(originalCatalog, originalSchema);
final String catalog = result.database();
final String schemaPattern = result.schema();
boolean isExactSchema = result.isExactSchema();

final Pattern compiledSchemaPattern = Wildcard.toRegexPattern(schemaPattern, true);

Expand Down Expand Up @@ -3343,7 +3337,7 @@ public boolean next() throws SQLException {

if (compiledSchemaPattern == null
|| compiledSchemaPattern.matcher(schemaName).matches()
|| isExactSchema) {
|| isExactSchema && schemaPattern.equals(schemaPattern)) {
nextRow[0] = schemaName;
nextRow[1] = dbName;
return true;
Expand Down Expand Up @@ -3397,11 +3391,10 @@ public ResultSet getFunctions(
if (showFunctionCommand.isEmpty()) {
return SnowflakeDatabaseMetaDataResultSet.getEmptyResultSet(GET_FUNCTIONS, statement);
}
SFTriple<String, String, Boolean> result =
applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.first();
String schemaPattern = result.second();
boolean isExactSchema = result.third();
ContextAwareMetadataSearch result = applySessionContext(originalCatalog, originalSchemaPattern);
String catalog = result.database();
String schemaPattern = result.schema();
boolean isExactSchema = result.isExactSchema();

final Pattern compiledSchemaPattern = Wildcard.toRegexPattern(schemaPattern, true);
final Pattern compiledFunctionPattern = Wildcard.toRegexPattern(functionNamePattern, true);
Expand Down Expand Up @@ -3432,7 +3425,7 @@ public boolean next() throws SQLException {
|| compiledFunctionPattern.matcher(functionName).matches())
&& (compiledSchemaPattern == null
|| compiledSchemaPattern.matcher(schemaName).matches()
|| isExactSchema)) {
|| isExactSchema && schemaPattern.equals(schemaPattern))) {
logger.debug("Found a matched function:" + schemaName + "." + functionName);

nextRow[0] = catalogName;
Expand Down Expand Up @@ -3714,4 +3707,28 @@ else if (sql.contains("desc function")
}
return resultSet;
}

private static class ContextAwareMetadataSearch {
private final String database;
private final String schema;
private final boolean isExactSchema;

public ContextAwareMetadataSearch(String database, String schema, boolean isExactSchema) {
this.database = database;
this.schema = schema;
this.isExactSchema = isExactSchema;
}

public String database() {
return database;
}

public String schema() {
return schema;
}

public boolean isExactSchema() {
return isExactSchema;
}
}
}
53 changes: 0 additions & 53 deletions src/main/java/net/snowflake/client/util/SFTriple.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2490,12 +2490,12 @@ public void testExactSchemaSearching() throws SQLException {
assertFalse(tables.next());
}

try (ResultSet tablePriviliges =
try (ResultSet tablePrivileges =
connectionWithContext.getMetaData().getTablePrivileges(null, null, "mytable1")) {
assertTrue(tablePriviliges.next());
assertEquals("mytable1", tablePriviliges.getString("TABLE_NAME"));
assertEquals(schemaName, tablePriviliges.getString(schemaNameColumnInMetadata));
assertFalse(tablePriviliges.next());
assertTrue(tablePrivileges.next());
assertEquals("mytable1", tablePrivileges.getString("TABLE_NAME"));
assertEquals(schemaName, tablePrivileges.getString(schemaNameColumnInMetadata));
assertFalse(tablePrivileges.next());
}

try (ResultSet columns =
Expand Down

0 comments on commit 95c06ca

Please sign in to comment.