diqiu50 commented on code in PR #14802:
URL: https://github.com/apache/gravitino/pull/14802#discussion_r4535662988
##########
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:
Yes, intended. For PostgreSQL `default-database` is the schema, and the
connection always uses `jdbc-database`. Added a note to the docs.
##########
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:
With parameters, the inner catalog leaves the base-url to Flink. On JDBC 4.x
only `host/db?params` works, and Flink checks `db` against the default
database, so it matches the table URL. Other forms are rejected by Flink. JDBC
3.x does not support parameters in the base-url.
##########
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:
Yes. `testNativeCatalogConnectsToConnectionDatabase` covers PostgreSQL and
MySQL on Flink 1.19, 1.20, 2.1 and 2.2 with the real `JdbcCatalogFactory`.
--
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]