Skip to content

Escape underscore wildcard in catalog names for Thrift metadata RPCs - #1261

Merged
gopalldb merged 5 commits into
databricks:mainfrom
gopalldb:escape-catalog-wildcard
Mar 10, 2026
Merged

gopalldb merged 5 commits into
databricks:mainfrom
gopalldb:escape-catalog-wildcard

Conversation

@gopalldb

@gopalldb gopalldb commented Mar 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Thrift metadata RPCs (GetSchemas, GetTables, GetColumns, etc.) treat catalog names as patterns, so _ is interpreted as a single-character wildcard. This causes my_catalog to incorrectly match mycatalog, my1catalog, etc.
  • Adds a TreatMetadataCatalogNameAsPattern connection property (default false). When disabled, unescaped _ in catalog names are escaped with \ before passing to Thrift requests.
  • Applied to 4 metadata methods: listSchemas, listTables, listColumns, listFunctions (Thrift RPC path)

Test plan

  • Parameterized unit tests for WildcardUtil.escapeCatalogName() (null, no wildcards, single/multiple underscores, already-escaped, percent left unchanged)
  • DatabricksThriftServiceClientTest: verify listSchemas escapes catalog by default, does not escape when property is true, and listCrossReferences escapes both parent and foreign catalogs
  • Manual verification with a Databricks workspace using a catalog containing _ in its name

NO_CHANGELOG=true

🤖 Generated with Claude Code

gopalldb and others added 3 commits March 9, 2026 14:14
Thrift metadata RPCs treat catalog names as patterns, so `_` is
interpreted as a single-character wildcard. This causes incorrect
matches (e.g. `my_catalog` matching `mycatalog`). Add a
TreatMetadataCatalogNameAsPattern connection property (default false)
that escapes unescaped `_` in catalog names before passing them to
Thrift requests.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Signed-off-by: Gopal Lal <[email protected]>
…entifiers)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Signed-off-by: Gopal Lal <[email protected]>
@vikrantpuppala

Copy link
Copy Markdown
Collaborator

please remove NO_CHANGELOG=true from description

Comment thread NEXT_CHANGELOG.md Outdated
### Added
- Added connection property `OAuthWebServerTimeout` to configure the OAuth browser authentication timeout for U2M (user-to-machine) flows, and also updated hardcoded 1-hour timeout to default 120 seconds timeout.
- Added connection property `UseQueryForMetadata` to use SQL SHOW commands instead of Thrift RPCs for metadata operations (getCatalogs, getSchemas, getTables, getColumns, getFunctions). This fixes incorrect wildcard matching where `_` was treated as a single-character wildcard in Thrift metadata pattern filters.
- Added connection property `TreatMetadataCatalogNameAsPattern` to control whether catalog names are treated as patterns in Thrift metadata RPCs. When disabled (default), unescaped `_` in catalog names is escaped to prevent single-character wildcard matching.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add expectations wrt jdbc spec in this message

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @param catalogName the catalog name to escape
* @return the escaped catalog name, or {@code null} if the input is {@code null}
*/
public static String escapeCatalogName(String catalogName) {

@vikrantpuppala vikrantpuppala Mar 9, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also escape %, if not why? we should document, if we chose not to

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are escaping "_" as it is a very common character in resource names, and users don't escape this due to lack of awareness, and this affects unintended latency majority of such cases. But "%" is not a common character in resource-names.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the escaping not added to listPrimaryKeys, listImportedKeys, listExportedKeys, listCrossReferences?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In implementation for these, they are not treated as pattern. I wanted to limit the change to minimal required.

}
builder.append(ch);
}
return builder.toString();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use regex here, llm suggests:

  public static String escapeCatalogName(String catalogName) {
    if (catalogName == null) {
      return null;
    }
    // Escape unescaped underscores: match '_' not preceded by '\'
    return catalogName.replaceAll("(?<!\\\\)_", "\\\\_");
  }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loop is still better as it differentiates between _ and \_.

@gopalldb
gopalldb enabled auto-merge (squash) March 10, 2026 04:02
@gopalldb
gopalldb merged commit f369a4d into databricks:main Mar 10, 2026
13 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants