mchades commented on code in PR #10998: URL: https://github.com/apache/gravitino/pull/10998#discussion_r3252570149
########## catalogs/catalog-hive/src/main/java/org/apache/gravitino/catalog/hive/HiveViewCatalogOperations.java: ########## @@ -0,0 +1,471 @@ +/* + * 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.hive; + +import static org.apache.gravitino.catalog.hive.HiveConstants.COMMENT; +import static org.apache.gravitino.catalog.hive.HiveConstants.HIVE_FILTER_FIELD_PARAMS; +import static org.apache.gravitino.catalog.hive.HiveConstants.TABLE_TYPE; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; +import java.util.function.Supplier; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.exceptions.NoSuchSchemaException; +import org.apache.gravitino.exceptions.NoSuchViewException; +import org.apache.gravitino.exceptions.ViewAlreadyExistsException; +import org.apache.gravitino.hive.CachedClientPool; +import org.apache.gravitino.hive.HiveTable; +import org.apache.gravitino.meta.AuditInfo; +import org.apache.gravitino.rel.Column; +import org.apache.gravitino.rel.Dialects; +import org.apache.gravitino.rel.Representation; +import org.apache.gravitino.rel.SQLRepresentation; +import org.apache.gravitino.rel.View; +import org.apache.gravitino.rel.ViewCatalog; +import org.apache.gravitino.rel.ViewChange; +import org.apache.gravitino.utils.PrincipalUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class HiveViewCatalogOperations implements ViewCatalog { + private static final Logger LOG = LoggerFactory.getLogger(HiveViewCatalogOperations.class); + private static final short MAX_TABLES = -1; + + private final Supplier<CachedClientPool> clientPoolSupplier; + private final Supplier<String> catalogNameSupplier; + private final Predicate<NameIdentifier> schemaExistsChecker; + + HiveViewCatalogOperations( + Supplier<CachedClientPool> clientPoolSupplier, + Supplier<String> catalogNameSupplier, + Predicate<NameIdentifier> schemaExistsChecker) { + this.clientPoolSupplier = clientPoolSupplier; + this.catalogNameSupplier = catalogNameSupplier; + this.schemaExistsChecker = schemaExistsChecker; + } + + @Override + public NameIdentifier[] listViews(Namespace namespace) throws NoSuchSchemaException { + NameIdentifier schemaIdent = NameIdentifier.of(namespace.levels()); + if (!schemaExistsChecker.test(schemaIdent)) { + throw new NoSuchSchemaException("Schema %s does not exist", namespace); + } + try { + String viewFilter = + String.format("%stableType like \"VIRTUAL_VIEW\"", HIVE_FILTER_FIELD_PARAMS); + List<String> views = + clientPool() + .run( + c -> + c.listTableNamesByFilter( + catalogName(), schemaIdent.name(), viewFilter, MAX_TABLES)); + return views.stream() + .map(name -> NameIdentifier.of(namespace, name)) + .toArray(NameIdentifier[]::new); + } catch (InterruptedException e) { + throw new RuntimeException("Failed to list Hive views in " + namespace, e); + } + } + + @Override + public View loadView(NameIdentifier ident) throws NoSuchViewException { + return loadHiveView(ident); + } + + @Override + public View createView( + NameIdentifier ident, + String comment, + Column[] columns, + Representation[] representations, + String defaultCatalog, + String defaultSchema, + Map<String, String> properties) + throws NoSuchSchemaException, ViewAlreadyExistsException { + NameIdentifier schemaIdent = NameIdentifier.of(ident.namespace().levels()); + if (!schemaExistsChecker.test(schemaIdent)) { + throw new NoSuchSchemaException("Schema %s does not exist", schemaIdent); + } + SQLRepresentation sqlRepresentation = + validateSQLRepresentation(representations, defaultCatalog, defaultSchema, ident); + + try { + Map<String, String> params = + Maps.newHashMap(properties == null ? ImmutableMap.of() : properties); + params.put(TABLE_TYPE, TableType.VIRTUAL_VIEW.name()); + String viewOriginalText = toHmsViewOriginalText(sqlRepresentation, ident); + + HiveTable hiveTable = + HiveTable.builder() + .withName(ident.name()) + .withComment(comment) + .withColumns(copyColumns(columns)) + .withProperties(params) + .withAuditInfo( + AuditInfo.builder() + .withCreator(PrincipalUtils.getCurrentUserName()) + .withCreateTime(Instant.now()) + .build()) + .withCatalogName(catalogName()) + .withDatabaseName(schemaIdent.name()) + .withViewOriginalText(viewOriginalText) + .build(); + + clientPool() + .run( + c -> { + c.createTable(hiveTable); Review Comment: yes, HMS treats view as a specific table type. -- 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]
