jerryshao commented on code in PR #14802:
URL: https://github.com/apache/gravitino/pull/14802#discussion_r4523322448
##########
flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/jdbc/GravitinoJdbcCatalog.java:
##########
@@ -113,6 +115,22 @@ public void open() throws CatalogException {
if (configuredDefaultDatabase == null) {
return;
}
+ // The inner Flink JDBC catalog connects to its 'default-database' and
rejects unknown options,
+ // so pass it the connection database instead when the converter tracks
one separately.
+ Map<String, String> innerCatalogOptions = new HashMap<>(mutableOptions);
+ String connectionDatabase =
+
innerCatalogOptions.remove(JdbcPropertiesConstants.GRAVITINO_CONNECTION_DATABASE);
+ if (connectionDatabase != null) {
+ innerCatalogOptions.put(
+ JdbcPropertiesConstants.FLINK_JDBC_DEFAULT_DATABASE,
connectionDatabase);
+ }
+ // Flink JDBC 4.x takes "jdbc:dialect://host:port/" as a URL that already
names a database and
+ // connects to it as is, so drop the trailing slash to have
'default-database' appended.
+ String baseUrl =
innerCatalogOptions.get(JdbcPropertiesConstants.FLINK_JDBC_URL);
+ if (baseUrl != null && baseUrl.indexOf('?') < 0) {
+ innerCatalogOptions.put(
Review Comment:
Stripping the trailing slash is applied to every dialect, but it is only
needed for Flink JDBC 4.x. Is it verified that MySQL and the older Flink
versions in the build still work with a URL without the slash?
##########
flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/jdbc/GravitinoJdbcCatalog.java:
##########
@@ -113,6 +115,22 @@ public void open() throws CatalogException {
if (configuredDefaultDatabase == null) {
return;
}
+ // The inner Flink JDBC catalog connects to its 'default-database' and
rejects unknown options,
+ // so pass it the connection database instead when the converter tracks
one separately.
+ Map<String, String> innerCatalogOptions = new HashMap<>(mutableOptions);
+ String connectionDatabase =
+
innerCatalogOptions.remove(JdbcPropertiesConstants.GRAVITINO_CONNECTION_DATABASE);
+ if (connectionDatabase != null) {
+ innerCatalogOptions.put(
+ JdbcPropertiesConstants.FLINK_JDBC_DEFAULT_DATABASE,
connectionDatabase);
+ }
+ // Flink JDBC 4.x takes "jdbc:dialect://host:port/" as a URL that already
names a database and
+ // connects to it as is, so drop the trailing slash to have
'default-database' appended.
+ String baseUrl =
innerCatalogOptions.get(JdbcPropertiesConstants.FLINK_JDBC_URL);
+ if (baseUrl != null && baseUrl.indexOf('?') < 0) {
Review Comment:
When the base-url has query parameters, the trailing slash is not stripped
and the inner Flink JDBC catalog gets a URL where it cannot correctly append
the database, while buildDatabaseUrl handles the same URL properly for table
scans. Normalize the URL the same way in both places (insert the database
before the query string) and add a test for a bypass URL with parameters.
##########
flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/jdbc/postgresql/PostgresqlPropertiesConverter.java:
##########
@@ -43,23 +49,58 @@ public String getFlinkCatalogType() {
return GravitinoJdbcCatalogFactoryOptions.POSTGRESQL_IDENTIFIER;
}
+ /**
+ * For PostgreSQL, a Flink "database" is a Gravitino schema, so
'default-database' names the
+ * schema Flink starts in, while JDBC connections always target the
catalog's PostgreSQL database.
+ * That database is resolved from jdbc-database, or from jdbc-url when
jdbc-database is not set,
+ * and also serves as 'default-database' when that is not set.
+ */
+ @Override
+ public Map<String, String> toFlinkCatalogProperties(Map<String, String>
gravitinoProperties) {
+ Map<String, String> flinkCatalogProperties =
+ super.toFlinkCatalogProperties(gravitinoProperties);
+ String connectionDatabase =
+
gravitinoProperties.get(JdbcPropertiesConstants.GRAVITINO_JDBC_DATABASE);
+ if (StringUtils.isBlank(connectionDatabase)) {
+ connectionDatabase =
+ getDatabaseFromJdbcUrl(
+
gravitinoProperties.get(JdbcPropertiesConstants.GRAVITINO_JDBC_URL));
+ }
+ if (StringUtils.isNotBlank(connectionDatabase)) {
+ flinkCatalogProperties.put(
+ JdbcPropertiesConstants.GRAVITINO_CONNECTION_DATABASE,
connectionDatabase);
+ flinkCatalogProperties.putIfAbsent(
+ JdbcPropertiesConstants.FLINK_JDBC_DEFAULT_DATABASE,
connectionDatabase);
+ }
+ return flinkCatalogProperties;
+ }
+
@Override
protected String getConnectionDatabase(
Map<String, String> flinkCatalogProperties, ObjectPath tablePath) {
- // For PostgreSQL, the Flink "database" is a Gravitino schema, not the
PostgreSQL database
- // that the JDBC connection must target. The connection must use the
catalog's configured
- // jdbc-database instead, which
JdbcPropertiesConverter#toFlinkCatalogProperties defaults into
- // the 'default-database' option.
+ // Fall back to 'default-database' when no connection database can be
resolved, which is
+ // also what the inner Flink JDBC catalog connects to in that case.
String database =
-
flinkCatalogProperties.get(JdbcPropertiesConstants.FLINK_JDBC_DEFAULT_DATABASE);
+ flinkCatalogProperties.getOrDefault(
+ JdbcPropertiesConstants.GRAVITINO_CONNECTION_DATABASE,
+
flinkCatalogProperties.get(JdbcPropertiesConstants.FLINK_JDBC_DEFAULT_DATABASE));
Preconditions.checkArgument(
!Strings.isNullOrEmpty(database),
- JdbcPropertiesConstants.FLINK_JDBC_DEFAULT_DATABASE + " should not be
null or empty.");
+ "Cannot resolve the PostgreSQL database: set '"
+ + JdbcPropertiesConstants.GRAVITINO_JDBC_DATABASE
+ + "' or include the database in '"
+ + JdbcPropertiesConstants.GRAVITINO_JDBC_URL
+ + "'.");
return database;
}
@Override
protected String getTableName(ObjectPath tablePath) {
return tablePath.getDatabaseName() + "." + tablePath.getObjectName();
}
+
+ private static String getDatabaseFromJdbcUrl(String jdbcUrl) {
Review Comment:
getDatabaseFromJdbcUrl will throw a NullPointerException if the URL is null,
and it relies on the parent method having already validated it. Add a null
check so the method is safe on its own.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]