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


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/ddl/CreateConnectionOperation.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.operations.ddl;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.internal.TableResultImpl;
+import org.apache.flink.table.api.internal.TableResultInternal;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.SensitiveConnection;
+import org.apache.flink.table.operations.Operation;
+import org.apache.flink.table.operations.OperationUtils;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/** Operation to describe a CREATE CONNECTION statement. */
+@Internal
+public class CreateConnectionOperation implements CreateOperation {
+
+    private static final String MASKED_VALUE = "****";
+
+    private final ObjectIdentifier connectionIdentifier;
+    private final SensitiveConnection sensitiveConnection;
+    private final boolean ignoreIfExists;
+    private final boolean isTemporary;
+
+    public CreateConnectionOperation(
+            ObjectIdentifier connectionIdentifier,
+            SensitiveConnection sensitiveConnection,
+            boolean ignoreIfExists,
+            boolean isTemporary) {
+        this.connectionIdentifier = connectionIdentifier;
+        this.sensitiveConnection = sensitiveConnection;
+        this.ignoreIfExists = ignoreIfExists;
+        this.isTemporary = isTemporary;
+    }
+
+    public ObjectIdentifier getConnectionIdentifier() {
+        return connectionIdentifier;
+    }
+
+    public SensitiveConnection getSensitiveConnection() {
+        return sensitiveConnection;
+    }
+
+    public boolean isIgnoreIfExists() {
+        return ignoreIfExists;
+    }
+
+    public boolean isTemporary() {
+        return isTemporary;
+    }
+
+    @Override
+    public String asSummaryString() {
+        Map<String, String> maskedOptions =
+                sensitiveConnection.getOptions().entrySet().stream()
+                        .collect(
+                                Collectors.toMap(
+                                        Map.Entry::getKey,
+                                        e -> MASKED_VALUE,
+                                        (a, b) -> a,
+                                        LinkedHashMap::new));
+        Map<String, Object> params = new LinkedHashMap<>();
+        params.put("connectionOptions", maskedOptions);
+        params.put("identifier", connectionIdentifier);
+        params.put("ignoreIfExists", ignoreIfExists);
+        params.put("isTemporary", isTemporary);
+
+        return OperationUtils.formatWithChildren(
+                "CREATE CONNECTION", params, Collections.emptyList(), 
Operation::asSummaryString);
+    }
+
+    @Override
+    public TableResultInternal execute(Context ctx) {
+        if (isTemporary) {

Review Comment:
   We also need to handle temporary system connection. Do you plan to handle it 
later?



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/operations/SqlConnectionOperationConverterTest.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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;
+
+import org.apache.flink.sql.parser.error.SqlValidateException;
+import org.apache.flink.table.api.TableConfig;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.catalog.CatalogManager;
+import org.apache.flink.table.catalog.FunctionCatalog;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.SensitiveConnection;
+import org.apache.flink.table.module.ModuleManager;
+import org.apache.flink.table.operations.ExecutableOperation;
+import org.apache.flink.table.operations.Operation;
+import org.apache.flink.table.operations.ddl.CreateConnectionOperation;
+import org.apache.flink.table.resource.ResourceManager;
+
+import org.apache.calcite.sql.parser.SqlParseException;
+import org.junit.jupiter.api.Test;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Test for converting connection statements to operations. */
+class SqlConnectionOperationConverterTest extends 
SqlNodeToOperationConversionTestBase {
+
+    @Test
+    void testCreateConnection() {
+        Operation operation = parse("CREATE CONNECTION my_conn WITH ('k' = 
'v')");
+        assertThat(operation).isInstanceOf(CreateConnectionOperation.class);
+        CreateConnectionOperation op = (CreateConnectionOperation) operation;
+
+        assertThat(op.getConnectionIdentifier())
+                .isEqualTo(ObjectIdentifier.of("builtin", "default", 
"my_conn"));
+        
assertThat(op.getSensitiveConnection().getOptions()).isEqualTo(Map.of("k", 
"v"));
+        assertThat(op.getSensitiveConnection().getComment()).isNull();
+        assertThat(op.isIgnoreIfExists()).isFalse();
+        assertThat(op.isTemporary()).isFalse();
+    }
+
+    @Test
+    void testCreateConnectionIfNotExists() {
+        Operation operation = parse("CREATE CONNECTION IF NOT EXISTS my_conn 
WITH ('k' = 'v')");
+        CreateConnectionOperation op = (CreateConnectionOperation) operation;
+        assertThat(op.isIgnoreIfExists()).isTrue();
+        assertThat(op.isTemporary()).isFalse();
+    }
+
+    @Test
+    void testCreateTemporaryConnection() {
+        Operation operation = parse("CREATE TEMPORARY CONNECTION my_conn WITH 
('k' = 'v')");
+        CreateConnectionOperation op = (CreateConnectionOperation) operation;
+        assertThat(op.isTemporary()).isTrue();
+        assertThat(op.isIgnoreIfExists()).isFalse();
+    }
+
+    @Test
+    void testCreateTemporarySystemConnection() {
+        Operation operation = parse("CREATE TEMPORARY SYSTEM CONNECTION 
my_conn WITH ('k' = 'v')");
+        CreateConnectionOperation op = (CreateConnectionOperation) operation;
+        assertThat(op.isTemporary()).isTrue();
+    }
+
+    @Test
+    void testCreateConnectionWithComment() {
+        Operation operation =
+                parse("CREATE CONNECTION my_conn COMMENT 'hi there' WITH ('k' 
= 'v')");
+        CreateConnectionOperation op = (CreateConnectionOperation) operation;
+        SensitiveConnection conn = op.getSensitiveConnection();
+        assertThat(conn.getComment()).isEqualTo("hi there");
+    }
+
+    @Test
+    void testCreateConnectionWithFullyQualifiedName() {
+        Operation operation = parse("CREATE CONNECTION cat1.db1.my_conn WITH 
('k' = 'v')");
+        CreateConnectionOperation op = (CreateConnectionOperation) operation;
+        assertThat(op.getConnectionIdentifier())
+                .isEqualTo(ObjectIdentifier.of("cat1", "db1", "my_conn"));
+    }
+
+    @Test
+    void testCreateConnectionOptions() {
+        Operation operation =
+                parse("CREATE CONNECTION my_conn WITH ('k1' = 'v1', 'k2' = 
'v2', 'k3' = 'v3')");
+        CreateConnectionOperation op = (CreateConnectionOperation) operation;
+        Map<String, String> expected = new LinkedHashMap<>();
+        expected.put("k1", "v1");
+        expected.put("k2", "v2");
+        expected.put("k3", "v3");
+        
assertThat(op.getSensitiveConnection().getOptions()).containsAllEntriesOf(expected);
+    }
+
+    @Test
+    void testAsSummaryStringMasksOptionValues() {
+        Operation operation =
+                parse(
+                        "CREATE CONNECTION my_conn WITH ('user' = 'alice', 
'password' = 'super-secret')");
+        String summary = operation.asSummaryString();
+        
assertThat(summary).contains("user").contains("password").contains("****");
+        
assertThat(summary).doesNotContain("alice").doesNotContain("super-secret");
+    }
+
+    @Test
+    void testCreateSystemConnectionWithoutTemporaryRejected() {

Review Comment:
   This test failed in CI



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