jerryshao commented on code in PR #10998: URL: https://github.com/apache/gravitino/pull/10998#discussion_r3256271083
########## catalogs/catalog-hive/src/main/java/org/apache/gravitino/catalog/hive/HiveView.java: ########## @@ -0,0 +1,158 @@ +/* + * 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 com.google.common.base.Preconditions; +import java.util.HashMap; +import java.util.Map; +import javax.annotation.Nullable; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.apache.gravitino.annotation.Unstable; +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; + +/** + * Represents a view stored in Hive Metastore (VIRTUAL_VIEW table type). The SQL dialect is detected + * from table properties: Trino views start with "/* Presto View:", Spark views carry {@code + * spark.sql.create.version} in their parameters, and all other views are treated as native Hive SQL + * views. + */ +@Unstable +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@Builder(setterPrefix = "with", builderClassName = "Builder") +@EqualsAndHashCode +@ToString +public class HiveView implements View { + + private static final String SPARK_VERSION_KEY = "spark.sql.create.version"; + private static final String TRINO_VIEW_MARKER_KEY = "presto_view"; + private static final String TRINO_VIEW_PREFIX = "/* Presto View:"; + + private String name; + private String comment; + private Column[] columns; + private String defaultCatalog; + private String defaultSchema; + private Map<String, String> properties; + private AuditInfo auditInfo; + private SQLRepresentation[] representations; + + @Override + public String name() { + return name; + } + + @Override + public String comment() { + return comment; + } + + @Override + public Column[] columns() { + return columns == null ? new Column[0] : columns; + } + + @Override + @Nullable + public String defaultCatalog() { + return defaultCatalog; + } + + @Override + @Nullable + public String defaultSchema() { + return defaultSchema; + } + + @Override + public Representation[] representations() { + return representations == null ? new SQLRepresentation[0] : representations; + } + + @Override + public Map<String, String> properties() { + return properties; + } + + @Override + public AuditInfo auditInfo() { + return auditInfo; + } + + /** + * Detects the SQL dialect from HMS table properties and view text. + * + * @param viewOriginalText The original view text from HMS. + * @param parameters The HMS table parameters map. + * @return The detected dialect string: "trino", "spark", or "hive". + */ + static String detectDialect(String viewOriginalText, Map<String, String> parameters) { + if (parameters != null && "true".equalsIgnoreCase(parameters.get(TRINO_VIEW_MARKER_KEY))) { + return Dialects.TRINO; + } + if (viewOriginalText != null && viewOriginalText.startsWith(TRINO_VIEW_PREFIX)) { + return Dialects.TRINO; + } + if (parameters != null && parameters.containsKey(SPARK_VERSION_KEY)) { + return Dialects.SPARK; + } + return Dialects.HIVE; + } + + /** Builder for {@link HiveView}. */ + public static class Builder { + /** + * Builds the {@link HiveView} instance. + * + * @return The constructed view. + */ + public HiveView build() { + Preconditions.checkArgument(name != null && !name.isEmpty(), "View name is required"); Review Comment: Can you use `StringUtils` here? -- 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]
