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


##########
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:
   A user-supplied flink.bypass.base-url is now used as is, without checking 
that it is a valid jdbc URL. A malformed value only fails later at connection 
time, so consider validating it or documenting 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:
   If jdbc-database is blank and the URL has no database, the code silently 
falls back to default-database, which is the schema name for PostgreSQL. That 
can connect to the wrong database, so consider failing fast instead of falling 
back.



##########
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:
   This strips the trailing slash from the base URL for every dialect and every 
supported Flink JDBC connector version, not only 4.x. Have you verified that 
Flink JDBC 3.x (used by the older Flink modules) and MySQL still build the 
correct default URL, or should this be gated on the connector version?



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