-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add Synonyms to SqlServerConnector #25440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add Synonyms to SqlServerConnector #25440
Conversation
Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to [email protected]. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla |
Adds a configuration option to allow users to opt-in to having Trino be able to interact with SQL Server Synonym table types. This closes trinodb#24206
2f0410b
to
8514b4e
Compare
Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to [email protected]. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla |
|
||
public TestSynonym(SqlExecutor sqlExecutor, String namePrefix, String definition) | ||
{ | ||
this.sqlExecutor = sqlExecutor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add requireNonNull
:
requireNonNull(sqlExecutor, "sqlExecutor is null");
requireNonNull(namePrefix, "namePrefix is null");
requireNonNull(definition, "definition is null");
sqlExecutor.execute(format("CREATE SYNONYM %s %s", name, definition)); | ||
} | ||
|
||
public String getName() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -120,6 +120,14 @@ public void testReadFromView() | |||
} | |||
} | |||
|
|||
@Test | |||
public void testSynonyms() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a new test class enabling sqlserver.synonyms.enabled
config property and verify that the connector can access synonyms.
@kirkhansen Did you submit CLA? |
@ebyhr I did submit that yesterday. Also, upon further investigation, it looks to me (a very untrained eye in both Java and JDBC) that this may be a much bigger lift than anticipated. I tried testing this by building this jar and shoving it into a docker container with trino as the tests copied from oracle didn't seem to test the positive case of someone wanting to include synonyms. I also stood up an mssql docker container, created a synonym, then pointed trino via a catalog. Sadly, the synonym didn't show up. It appears that the metadata object may not contain SYNONYMS. I ran something like this inside of an overridden getTables within the sql connector. DatabaseMetaData metadata = connection.getMetaData();
ResultSet rs = metadata.getTableTypes();
log.debug("Available Table Types:");
while (rs.next()) {
log.debug(rs.getString("TABLE_TYPE"));
}
I believe this means the JDBC driver in use doesn't see SYNONYMS from MSSQL via common convention at all. Since this Any thoughts here on how this could get implemented would be great. I could see a combination of a SQL statement hitting I naively thought something like: @Override
public ResultSet getTables(Connection connection, Optional<String> remoteSchemaName, Optional<String> remoteTableName)
throws SQLException
{
// Unfortunately, the connection.getMetaData only returns table types of `SYSTEM TABLE`, `TABLE`, `VIEW`.
// If synonyms are passed, we'll need to handle that logic ourselves here.
DatabaseMetaData metadata = connection.getMetaData();
// Fetch normal tables/views using JDBC metadata
ResultSet standardTables = metadata.getTables(
connection.getCatalog(),
escapeObjectNameForMetadataQuery(remoteSchemaName, metadata.getSearchStringEscape()).orElse(null),
escapeObjectNameForMetadataQuery(remoteTableName, metadata.getSearchStringEscape()).orElse(null),
getTableTypes().map(types -> types.toArray(String[]::new)).orElse(null));
// If synonyms are enabled, manually fetch from sys.synonyms
if (synonymsEnabled) {
String sql = "SELECT name AS TABLE_NAME, schema_name(schema_id) AS TABLE_SCHEM, 'SYNONYM' AS TABLE_TYPE FROM sys.synonyms";
Statement stmt = connection.createStatement();
ResultSet synonyms = stmt.executeQuery(sql);
log.debug("Fetched synonyms from sys.synonyms");
// Combine standard tables and synonyms into a single ResultSet
return new CombinedResultSet(standardTables, synonyms);
}
return standardTables;
} but quickly realized we'd need to implement the full ResultSet interface in order to operate on two result sets which seems really silly. |
I see it's a known issue of SQL Server's JDBC driver microsoft/mssql-jdbc#1814. |
Is there an appetite on your end for seeing an implementation that works by running system table queries manually here, or do you have other ideas you'd like to see tried? |
Perhaps the right way forward here is an attempt at a PR to the MSSQL driver to support this. I imagine the code would look really similar between what this would have to do and what the driver would be doing. If the MSSQL folks don't accept, it could get ported over here, if the Trino team would accept that kind of change here.
|
This pull request has gone a while without any activity. Ask for help on #core-dev on Trino slack. |
Description
Adds a configuration option to allow users to opt-in to having Trino be able to interact with SQL Server Synonym table types.
Fixes #24206
A lot of the implementation here was shared from the OracleConnector's version of handling synonyms.
Additional context and related issues
Release notes
( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
(x ) Release notes are required, with the following suggested text: