lihaosky commented on code in PR #28793:
URL: https://github.com/apache/flink/pull/28793#discussion_r4789336630


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/CatalogManager.java:
##########
@@ -2154,6 +2154,60 @@ public void alterConnection(
         }
     }
 
+    /**
+     * Rename a connection in the given fully qualified path.
+     *
+     * @param objectIdentifier The fully qualified path of the connection to 
rename.
+     * @param newConnectionName The new connection name.
+     * @param ignoreIfNotExists If false exception will be thrown if the 
connection to be renamed
+     *     does not exist.
+     */
+    public void renameConnection(
+            ObjectIdentifier objectIdentifier,
+            String newConnectionName,
+            boolean ignoreIfNotExists) {
+        checkArgument(!StringUtils.isNullOrWhitespaceOnly(newConnectionName));
+        ObjectIdentifier newIdentifier =
+                ObjectIdentifier.of(
+                        objectIdentifier.getCatalogName(),
+                        objectIdentifier.getDatabaseName(),
+                        newConnectionName);
+        CatalogConnection temporaryConnection = 
temporaryConnections.get(objectIdentifier);
+        if (temporaryConnection != null) {
+            checkConnectionNotExists(newIdentifier);
+            temporaryConnections.remove(objectIdentifier);
+            temporaryConnections.put(newIdentifier, temporaryConnection);
+            return;
+        }

Review Comment:
   [blocking] Temporary connections shouldn't be renameable here, for three 
reasons:
   - The `ALTER CONNECTION` grammar documents "Alter temporary or system 
connection is not supported" (`parserImpls.ftl:3710`).
   - ALTER TABLE/MODEL RENAME reject temporary objects in their converters.
   - This branch also skips the `CatalogModificationListener` events that 
temporary create and drop fire.
   
   Please reject temporary connections in the converter instead, for example 
with `catalogManager.getResolvedConnection(id)` plus `isTemporary()`, as 
`AbstractSqlAlterModelConverter` does. Deleting this branch alone isn't enough. 
`getConnection` returns the temporary connection first, so the permanent path 
below would then rename the permanent connection the temporary one shadows, or 
silently do nothing under `IF EXISTS`.
   



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/operations/converters/SqlAlterConnectionRenameConverter.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.planner.operations.converters;
+
+import org.apache.flink.sql.parser.ddl.connection.SqlAlterConnectionRename;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.UnresolvedIdentifier;
+import org.apache.flink.table.operations.Operation;
+import org.apache.flink.table.operations.ddl.AlterConnectionRenameOperation;
+
+/** A converter for {@link SqlAlterConnectionRename}. */
+public class SqlAlterConnectionRenameConverter
+        implements SqlNodeConverter<SqlAlterConnectionRename> {
+
+    @Override
+    public Operation convertSqlNode(
+            SqlAlterConnectionRename sqlAlterConnectionRename, ConvertContext 
context) {
+        ObjectIdentifier connectionIdentifier =
+                context.getCatalogManager()
+                        .qualifyIdentifier(
+                                
UnresolvedIdentifier.of(sqlAlterConnectionRename.getFullName()));
+
+        return new AlterConnectionRenameOperation(
+                connectionIdentifier,
+                sqlAlterConnectionRename.getNewConnectionName().getSimple(),
+                sqlAlterConnectionRename.ifConnectionExists());

Review Comment:
   [blocking] The grammar parses the `RENAME TO` target with 
`CompoundIdentifier()` (`parserImpls.ftl:3728`), and Calcite's `getSimple()` 
only guards with an `assert` before returning `names.get(0)`. With assertions 
off, as in production, `ALTER CONNECTION c1 RENAME TO db2.c2` silently renames 
`c1` to `db2`; with `-ea` it throws a bare `AssertionError`.
   
   Please reject non-simple targets, and add converter tests for `RENAME TO 
db.c2` and `RENAME TO cat.db.c2`. The alternative is to resolve a qualified 
target against the *source's* catalog and database and reject a mismatch. Note 
that `qualifyIdentifier` resolves against the session, not the source. The 
suggestion below also needs imports for `SqlIdentifier` and 
`ValidationException`:
   
   ```suggestion
           SqlIdentifier newName = 
sqlAlterConnectionRename.getNewConnectionName();
           if (!newName.isSimple()) {
               throw new ValidationException(
                       String.format(
                               "The new connection name must be a simple 
identifier, but was '%s'.",
                               newName));
           }
           return new AlterConnectionRenameOperation(
                   connectionIdentifier,
                   newName.getSimple(),
                   sqlAlterConnectionRename.ifConnectionExists());
   ```
   



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/common/sql/ConnectionITCase.java:
##########
@@ -110,6 +111,70 @@ void 
testCreatePermanentConnectionRejectedWithoutSecretStore() {
                 .hasMessageContaining("WritableSecretStore must be 
configured");
     }
 
+    @TestTemplate
+    void testAlterConnectionRenameTemporaryConnection() {

Review Comment:
   [suggestion] Once this becomes a rejection test, no test renames a 
*permanent* connection through SQL or `CatalogManager`; `CatalogTest` only 
calls `GenericInMemoryCatalog` directly. Could you add one? Create a permanent 
connection through the catalog as `testShowCreatePermanentConnection` does, run 
`ALTER CONNECTION … RENAME TO …`, and assert the old name is gone and the new 
one resolves with the same options.
   



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