ajantha-bhat commented on code in PR #9487: URL: https://github.com/apache/iceberg/pull/9487#discussion_r1458173630
########## core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java: ########## @@ -303,6 +287,325 @@ public static Properties filterAndRemovePrefix(Map<String, String> properties, S return result; } + static String getTableOrViewSql(boolean table) { + String tableOrViewTableName = table ? CATALOG_TABLE_NAME : CATALOG_VIEW_NAME; + String tableOrViewNamespace = table ? TABLE_NAMESPACE : VIEW_NAMESPACE; + String tableOrViewName = table ? TABLE_NAME : VIEW_NAME; + return String.format( + GET_TABLE_OR_VIEW_SQL, tableOrViewTableName, tableOrViewNamespace, tableOrViewName); + } + + private static String getTableOrViewNamespaceSql(boolean table) { + String tableOrViewTableName = table ? CATALOG_TABLE_NAME : CATALOG_VIEW_NAME; + String tableOrViewNamespace = table ? TABLE_NAMESPACE : VIEW_NAMESPACE; + return String.format( + GET_TABLE_OR_VIEW_NAMESPACE_SQL, + tableOrViewNamespace, + tableOrViewTableName, + tableOrViewNamespace, + tableOrViewNamespace); + } + + public static String getTableNamespaceSql() { Review Comment: can we double check and update all the new `public` methods? I think these can be package private considering that previous declarations of SQL was package private. ########## core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java: ########## @@ -303,6 +287,325 @@ public static Properties filterAndRemovePrefix(Map<String, String> properties, S return result; } + static String getTableOrViewSql(boolean table) { Review Comment: I think we can split this one into `getTableSql()` and `getViewSql()` like other package private methods we have now. ########## core/src/main/java/org/apache/iceberg/jdbc/JdbcViewOperations.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.sql.SQLException; +import java.sql.SQLIntegrityConstraintViolationException; +import java.util.Map; +import java.util.Objects; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.AlreadyExistsException; +import org.apache.iceberg.exceptions.CommitFailedException; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.apache.iceberg.exceptions.NoSuchViewException; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.view.BaseViewOperations; +import org.apache.iceberg.view.ViewMetadata; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** JDBC implementation of Iceberg ViewOperations. */ +public class JdbcViewOperations extends BaseViewOperations { + + private static final Logger LOG = LoggerFactory.getLogger(JdbcViewOperations.class); + private final String catalogName; + private final TableIdentifier viewIdentifier; + private final FileIO fileIO; + private final JdbcClientPool connections; + private final Map<String, String> catalogProperties; + + protected JdbcViewOperations( + JdbcClientPool dbConnPool, + FileIO fileIO, + String catalogName, + TableIdentifier viewIdentifier, + Map<String, String> catalogProperties) { + this.catalogName = catalogName; + this.viewIdentifier = viewIdentifier; + this.fileIO = fileIO; + this.connections = dbConnPool; + this.catalogProperties = catalogProperties; + } + + @Override + protected void doRefresh() { + Map<String, String> view; + + try { + view = JdbcUtil.getView(connections, catalogName, viewIdentifier); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new UncheckedInterruptedException(e, "Interrupted during refresh"); + } catch (SQLException e) { + // SQL exception happened when getting view from catalog + throw new UncheckedSQLException( + e, "Failed to get view %s from catalog %s", viewIdentifier, catalogName); + } + + if (view.isEmpty()) { + if (currentMetadataLocation() != null) { + throw new NoSuchViewException("View does not exist: %s", viewIdentifier); + } else { + this.disableRefresh(); + return; + } + } + + String newMetadataLocation = view.get(JdbcTableOperations.METADATA_LOCATION_PROP); + Preconditions.checkState( + newMetadataLocation != null, "Invalid view %s: metadata location is null", viewIdentifier); + refreshFromMetadataLocation(newMetadataLocation); + } + + @Override + protected void doCommit(ViewMetadata base, ViewMetadata metadata) { + boolean newView = base == null; + String newMetadataLocation = writeNewMetadataIfRequired(metadata); + try { + Map<String, String> view = JdbcUtil.getView(connections, catalogName, viewIdentifier); + + if (base != null) { + validateMetadataLocation(view, base); + String oldMetadataLocation = base.metadataFileLocation(); + // Start atomic update + LOG.debug("Committing existing view: {}", viewName()); + updateView(newMetadataLocation, oldMetadataLocation); + } else { + // view not exists create it + LOG.debug("Committing new view: {}", viewName()); + createView(newMetadataLocation); + } + Review Comment: nit: redundant new line. (please check all the places) Thumb rule is that after every block (if, try, switch, for), we should have new line if it has some more statements. If no more statement, new line is not needed. Spotless don't have intelligence to fix this newline added as of now. Sorry for formatting nits. I hate these comments too :) But good to keep the code in the same style as other files. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org