diqiu50 commented on code in PR #14802:
URL: https://github.com/apache/gravitino/pull/14802#discussion_r4536649611


##########
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(
+          JdbcPropertiesConstants.FLINK_JDBC_URL, 
StringUtils.stripEnd(baseUrl.trim(), "/"));

Review Comment:
   Yes, verified. JDBC 3.x appends the slash itself when the base-url has none, 
so its default URL is the same either way. 
`testNativeCatalogConnectsToConnectionDatabase` checks the URL the real catalog 
connects to for PostgreSQL and MySQL on all four Flink versions, so no version 
gate is needed.



##########
flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/jdbc/JdbcPropertiesConverter.java:
##########
@@ -66,16 +70,16 @@ public Map<String, String> 
toFlinkCatalogProperties(Map<String, String> gravitin
             + JdbcPropertiesConstants.GRAVITINO_JDBC_URL
             + "'.");
 
-    // The URL in FlinkJdbcCatalog does not support database and other 
parameters.
-    flinkCatalogProperties.put(
-        JdbcPropertiesConstants.FLINK_JDBC_URL, 
getBaseUrlFromJdbcUrl(gravitinoJdbcUrl));
+    // The URL in FlinkJdbcCatalog does not support database and other 
parameters. An explicit
+    // flink.bypass.base-url takes precedence, so Flink clients can reach the 
database through a
+    // different address than the Gravitino server.
+    if 
(!flinkCatalogProperties.containsKey(JdbcPropertiesConstants.FLINK_JDBC_URL)) {

Review Comment:
   It is validated by Flink, the same as a `base-url` set on a Flink JDBC 
catalog. Documented the expected format.



##########
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.
+   */
+  @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);
+    }
+    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(

Review Comment:
   Agreed, removed the fallback. It now fails fast when neither `jdbc-database` 
nor `jdbc-url` gives a database, which the server already requires for 
PostgreSQL catalogs.



-- 
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]

Reply via email to