difin commented on code in PR #6449: URL: https://github.com/apache/hive/pull/6449#discussion_r3190049202
########## iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/NativeIcebergViewSupport.java: ########## @@ -0,0 +1,124 @@ +/* + * 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.hive; + +import java.io.Closeable; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.iceberg.CatalogUtil; +import org.apache.iceberg.catalog.Catalog; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.catalog.ViewCatalog; +import org.apache.iceberg.view.ViewBuilder; + +/** + * Commits a native Iceberg view through the configured default Iceberg catalog (HiveCatalog or REST + * catalog, etc.) when {@code Catalog} also implements {@link ViewCatalog}. + */ +public final class NativeIcebergViewSupport { + + /** HMS parameter aligned with Hive's {@code CreateViewDesc#ICEBERG_NATIVE_VIEW_PROPERTY}. */ + public static final String ICEBERG_NATIVE_VIEW_PROPERTY = "hive.iceberg.native.view"; + + private NativeIcebergViewSupport() { + } + + /** + * Creates or replaces a view in the Iceberg catalog. + * + * @return {@code false} if skipped because {@code ifNotExists} is true and the view already exists + */ + public static boolean createOrReplaceNativeView(Configuration conf, String databaseName, String viewName, + List<FieldSchema> fieldSchemas, String viewSql, Map<String, String> tblProperties, String comment, + boolean replace, boolean ifNotExists) throws Exception { + + TableIdentifier identifier = TableIdentifier.of(databaseName, viewName); + String catalogName = IcebergCatalogProperties.getCatalogName(conf); + Map<String, String> catalogProps = IcebergCatalogProperties.getCatalogProperties(conf, catalogName); + Catalog catalog = CatalogUtil.buildIcebergCatalog(catalogName, catalogProps, conf); + try { + ViewCatalog viewCatalog = asViewCatalog(catalog, catalogName); + if (!replace && ifNotExists && viewCatalog.viewExists(identifier)) { + return false; + } + + ViewBuilder builder = startViewBuilder(viewCatalog, identifier, fieldSchemas, viewSql); + builder = applyCommentAndTblProps(builder, tblProperties, comment); + commitView(builder, replace); + return true; + } finally { + if (catalog instanceof Closeable) { + ((Closeable) catalog).close(); + } + } + } + + private static ViewCatalog asViewCatalog(Catalog catalog, String catalogName) { + if (!(catalog instanceof ViewCatalog)) { + throw new UnsupportedOperationException( + String.format( + "Iceberg catalog '%s' does not implement ViewCatalog.", + catalogName) + + " Native views require a catalog that implements ViewCatalog (e.g. HiveCatalog or REST)."); + } + return (ViewCatalog) catalog; + } + + private static ViewBuilder startViewBuilder( Review Comment: Done - replaced the too verbose methods with inline code. -- 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]
