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


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

Review Comment:
   Users who today set flink.bypass.default-database to a real PostgreSQL 
database name will now silently connect to jdbc-database instead. Is this 
intended, and should it be called out as a behavior change in the docs or 
release notes?



##########
flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/jdbc/JdbcPropertiesConverter.java:
##########
@@ -66,16 +66,14 @@ 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(
+    // 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.
+    flinkCatalogProperties.putIfAbsent(

Review Comment:
   getBaseUrlFromJdbcUrl is still evaluated eagerly even when a bypass base-url 
is supplied, so a jdbc-url it cannot parse still fails. Consider computing it 
only when the bypass value is absent.



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