mchades commented on code in PR #10992: URL: https://github.com/apache/gravitino/pull/10992#discussion_r3223337080
########## catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergViewCatalogOperations.java: ########## @@ -0,0 +1,402 @@ +/* + * 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.gravitino.catalog.lakehouse.iceberg; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.catalog.lakehouse.iceberg.converter.ConvertUtil; +import org.apache.gravitino.catalog.lakehouse.iceberg.ops.IcebergCatalogWrapperHelper; +import org.apache.gravitino.exceptions.NoSuchSchemaException; +import org.apache.gravitino.exceptions.NoSuchViewException; +import org.apache.gravitino.exceptions.ViewAlreadyExistsException; +import org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper; +import org.apache.gravitino.meta.AuditInfo; +import org.apache.gravitino.rel.Column; +import org.apache.gravitino.rel.Representation; +import org.apache.gravitino.rel.SQLRepresentation; +import org.apache.gravitino.rel.View; +import org.apache.gravitino.rel.ViewChange; +import org.apache.gravitino.utils.PrincipalUtils; +import org.apache.iceberg.MetadataUpdate; +import org.apache.iceberg.Schema; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.AlreadyExistsException; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.apache.iceberg.rest.requests.ImmutableCreateViewRequest; +import org.apache.iceberg.rest.requests.RenameTableRequest; +import org.apache.iceberg.rest.requests.UpdateTableRequest; +import org.apache.iceberg.rest.responses.ListTablesResponse; +import org.apache.iceberg.rest.responses.LoadViewResponse; +import org.apache.iceberg.view.ImmutableSQLViewRepresentation; +import org.apache.iceberg.view.ImmutableViewVersion; +import org.apache.iceberg.view.SQLViewRepresentation; +import org.apache.iceberg.view.ViewMetadata; +import org.apache.iceberg.view.ViewRepresentation; +import org.apache.iceberg.view.ViewVersion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** View catalog operations for Iceberg. */ +class IcebergViewCatalogOperations { + + private static final Logger LOG = LoggerFactory.getLogger(IcebergViewCatalogOperations.class); + + private final IcebergCatalogWrapper icebergCatalogWrapper; + + IcebergViewCatalogOperations(IcebergCatalogWrapper icebergCatalogWrapper) { + this.icebergCatalogWrapper = + Preconditions.checkNotNull(icebergCatalogWrapper, "icebergCatalogWrapper must not be null"); + } + + public View loadView(NameIdentifier ident) throws NoSuchViewException { + try { + LoadViewResponse response = + icebergCatalogWrapper.loadView( + IcebergCatalogWrapperHelper.buildIcebergTableIdentifier(ident)); + return IcebergView.fromLoadViewResponse(response, ident.name()); + } catch (org.apache.iceberg.exceptions.NoSuchViewException e) { + throw new NoSuchViewException(e, "Iceberg view %s does not exist", ident); + } + } + + public boolean viewExists(NameIdentifier ident) { + return icebergCatalogWrapper.viewExists( + IcebergCatalogWrapperHelper.buildIcebergTableIdentifier(ident)); + } + + public NameIdentifier[] listViews(Namespace namespace) throws NoSuchSchemaException { + org.apache.iceberg.catalog.Namespace icebergNamespace = + IcebergCatalogWrapperHelper.getIcebergNamespace(namespace); + if (!icebergCatalogWrapper.namespaceExists(icebergNamespace)) { + throw new NoSuchSchemaException("Schema %s does not exist", namespace); + } + try { + ListTablesResponse response = icebergCatalogWrapper.listView(icebergNamespace); + return response.identifiers().stream() + .map(id -> NameIdentifier.of(ArrayUtils.add(namespace.levels(), id.name()))) + .toArray(NameIdentifier[]::new); + } catch (NoSuchNamespaceException e) { + throw new NoSuchSchemaException("Schema does not exist %s in Iceberg", namespace); + } + } + + public View createView( + NameIdentifier ident, + String comment, + Column[] columns, + Representation[] representations, + String defaultCatalog, + String defaultSchema, + Map<String, String> properties) + throws NoSuchSchemaException, ViewAlreadyExistsException { + org.apache.iceberg.catalog.Namespace icebergNamespace = + IcebergCatalogWrapperHelper.getIcebergNamespace( + ident.namespace().level(ident.namespace().length() - 1)); + + Schema schema = ConvertUtil.toIcebergSchema(columns); + List<ViewRepresentation> viewRepresentations = new ArrayList<>(); + for (Representation representation : representations) { + viewRepresentations.add(toSqlViewRepresentation(representation)); + } Review Comment: Keeping current behavior intentionally. `representations` is validated upstream in `ViewCreateRequest.validate()` (non-null, non-empty, each entry validated). Adding the same check in `IcebergViewCatalogOperations` would be redundant. -- 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]
