You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by bl...@apache.org on 2021/09/26 22:55:50 UTC

[iceberg] branch master updated: Core: Fix JDBC properties, only keep keys with jdbc. prefix (#3078)

This is an automated email from the ASF dual-hosted git repository.

blue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/master by this push:
     new 539a3d0  Core: Fix JDBC properties, only keep keys with jdbc. prefix (#3078)
539a3d0 is described below

commit 539a3d0f11c177f9b769fde9fa25e890fbea5e2c
Author: Đặng Minh Dũng <du...@live.com>
AuthorDate: Mon Sep 27 05:55:37 2021 +0700

    Core: Fix JDBC properties, only keep keys with jdbc. prefix (#3078)
---
 .../org/apache/iceberg/jdbc/JdbcClientPool.java    |  3 +-
 .../java/org/apache/iceberg/jdbc/JdbcUtil.java     | 13 ++++++
 .../java/org/apache/iceberg/jdbc/TestJdbcUtil.java | 48 ++++++++++++++++++++++
 3 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/iceberg/jdbc/JdbcClientPool.java b/core/src/main/java/org/apache/iceberg/jdbc/JdbcClientPool.java
index 5b73fe8..8219585 100644
--- a/core/src/main/java/org/apache/iceberg/jdbc/JdbcClientPool.java
+++ b/core/src/main/java/org/apache/iceberg/jdbc/JdbcClientPool.java
@@ -47,8 +47,7 @@ class JdbcClientPool extends ClientPoolImpl<Connection, SQLException> {
   @Override
   protected Connection newClient() {
     try {
-      Properties dbProps = new Properties();
-      properties.forEach((key, value) -> dbProps.put(key.replace(JdbcCatalog.PROPERTY_PREFIX, ""), value));
+      Properties dbProps = JdbcUtil.filterAndRemovePrefix(properties, JdbcCatalog.PROPERTY_PREFIX);
       return DriverManager.getConnection(dbUrl, dbProps);
     } catch (SQLException e) {
       throw new UncheckedSQLException(e, "Failed to connect: %s", dbUrl);
diff --git a/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java b/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
index b1eacbf..0b55839 100644
--- a/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
+++ b/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
@@ -19,6 +19,8 @@
 
 package org.apache.iceberg.jdbc;
 
+import java.util.Map;
+import java.util.Properties;
 import org.apache.iceberg.catalog.Namespace;
 import org.apache.iceberg.catalog.TableIdentifier;
 import org.apache.iceberg.relocated.com.google.common.base.Joiner;
@@ -86,4 +88,15 @@ final class JdbcUtil {
     return TableIdentifier.of(JdbcUtil.stringToNamespace(tableNamespace), tableName);
   }
 
+  public static Properties filterAndRemovePrefix(Map<String, String> properties,
+                                                 String prefix) {
+    Properties result = new Properties();
+    properties.forEach((key, value) -> {
+      if (key.startsWith(prefix)) {
+        result.put(key.substring(prefix.length()), value);
+      }
+    });
+
+    return result;
+  }
 }
diff --git a/core/src/test/java/org/apache/iceberg/jdbc/TestJdbcUtil.java b/core/src/test/java/org/apache/iceberg/jdbc/TestJdbcUtil.java
new file mode 100644
index 0000000..92fa90e
--- /dev/null
+++ b/core/src/test/java/org/apache/iceberg/jdbc/TestJdbcUtil.java
@@ -0,0 +1,48 @@
+/*
+ * 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.iceberg.jdbc;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import org.assertj.core.api.Assertions;
+import org.junit.Test;
+
+public class TestJdbcUtil {
+
+  @Test
+  public void testFilterAndRemovePrefix() {
+    Map<String, String> input = new HashMap<>();
+    input.put("warehouse", "/tmp/warehouse");
+    input.put("user", "foo");
+    input.put("jdbc.user", "bar");
+    input.put("jdbc.pass", "secret");
+    input.put("jdbc.jdbc.abcxyz", "abcxyz");
+
+    Properties expected = new Properties();
+    expected.put("user", "bar");
+    expected.put("pass", "secret");
+    expected.put("jdbc.abcxyz", "abcxyz");
+
+    Properties actual = JdbcUtil.filterAndRemovePrefix(input, "jdbc.");
+
+    Assertions.assertThat(expected).isEqualTo(actual);
+  }
+}