nssalian commented on a change in pull request #3275:
URL: https://github.com/apache/iceberg/pull/3275#discussion_r752695272
##########
File path: core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java
##########
@@ -340,14 +438,120 @@ public boolean dropNamespace(Namespace namespace) throws
NamespaceNotEmptyExcept
@Override
public boolean setProperties(Namespace namespace, Map<String, String>
properties) throws
NoSuchNamespaceException {
- throw new UnsupportedOperationException(
- "Cannot set properties " + namespace + " : setProperties is not
supported");
+ if (!namespaceExists(namespace)) {
+ throw new NoSuchNamespaceException("Namespace does not exist: %s",
namespace);
+ }
+
+ if (properties == null || properties.isEmpty()) {
+ throw new IllegalArgumentException("Cannot setProperties to a namespace
with null or empty properties");
+ }
+
+ Map<String, String> namespaceProperties = getProperties(namespace);
+ Map<String, String> propertiesToInsert = Maps.newHashMap();
+ Map<String, String> propertiesToUpdate = Maps.newHashMap();
+
+ for (String key : properties.keySet()) {
+ String value = properties.get(key);
+ if (namespaceProperties.containsKey(key)) {
+ if (!namespaceProperties.get(key).equals(value)) {
+ propertiesToUpdate.put(key, value);
+ }
+ } else {
+ propertiesToInsert.put(key, value);
+ }
+ }
+
+ boolean insertedProperties = true;
+ if (!propertiesToInsert.isEmpty()) {
+ insertedProperties = insertProperties(namespace, propertiesToInsert);
+ }
+
+ boolean updatedProperties = true;
+ if (!propertiesToUpdate.isEmpty()) {
+ updatedProperties = updateProperties(namespace, propertiesToUpdate);
+ }
+
+ return (insertedProperties && updatedProperties);
}
@Override
public boolean removeProperties(Namespace namespace, Set<String> properties)
throws NoSuchNamespaceException {
- throw new UnsupportedOperationException(
- "Cannot remove properties " + namespace + " : removeProperties is not
supported");
+ if (!namespaceExists(namespace)) {
+ throw new NoSuchNamespaceException("Namespace does not exist: %s",
namespace);
+ }
+
+ if (properties == null || properties.isEmpty()) {
+ throw new IllegalArgumentException("Cannot removeProperties with null or
empty properties");
+ }
+
+ String namespaceName = JdbcUtil.namespaceToString(namespace);
+
+ try {
+ int deletedRecords = connections.run(conn -> {
+ String sqlStatement = JdbcUtil.deletePropertiesStatement(properties);
+
+ try (PreparedStatement sql = conn.prepareStatement(sqlStatement)) {
+ sql.setString(1, catalogName);
+ sql.setString(2, namespaceName);
+ int valueIndex = 2;
+ for (String property : properties) {
+ sql.setString(++valueIndex, property);
+ }
+ return sql.executeUpdate();
+ }
+ });
+
+ if (deletedRecords == properties.size()) {
+ LOG.info("Successfully committed to namespace: {}", namespaceName);
+ return true;
+ } else {
+ throw new CommitFailedException("Failed to remove properties for
namespace %s in catalog %s", namespaceName,
+ catalogName);
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new UncheckedInterruptedException(e,
+ "Interrupted in call to removeProperties(namespace, properties)
Namespace: %s", namespace);
+ } catch (SQLException e) {
+ throw new UncheckedSQLException(e, "Failed to removeProperties for
namespace: %s in catalog: %s", namespace,
+ catalogName);
+ }
+ }
+
+ public Map<String, String> getProperties(Namespace namespace) {
Review comment:
Actually this helps us extract the properties of the table directly and
works in setProperties.
I can call it fetchProperties.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]