-
Notifications
You must be signed in to change notification settings - Fork 4.8k
HIVE-29578: Iceberg: add support for native views #6449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
difin
wants to merge
2
commits into
apache:master
Choose a base branch
from
difin:iceberg_native_views
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/NativeIcebergViewSupport.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * 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.commons.lang3.StringUtils; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.hive.conf.Constants; | ||
| 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.relocated.com.google.common.collect.Maps; | ||
| 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 { | ||
|
|
||
| /** Value stored with {@link Constants#NATIVE_VIEW_STORAGE_HANDLER_CLASS_PARAM} for Iceberg native views. */ | ||
| public static final String NATIVE_ICEBERG_VIEW_HANDLER_FQCN = "org.apache.iceberg.mr.hive.HiveIcebergStorageHandler"; | ||
|
|
||
| /** | ||
| * HMS / Iceberg view marker entries for a native Iceberg catalog view (same map as | ||
| * {@code HiveIcebergStorageHandler#getNativeViewHmsTableProperties()}). | ||
| */ | ||
| public static Map<String, String> defaultNativeViewMarkerTableProperties() { | ||
| return Map.of(Constants.NATIVE_VIEW_STORAGE_HANDLER_CLASS_PARAM, NATIVE_ICEBERG_VIEW_HANDLER_FQCN); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| Map<String, String> mergedProps = mergeDefaultNativeViewTableProperties(tblProperties); | ||
| ViewBuilder builder = | ||
| viewCatalog | ||
| .buildView(identifier) | ||
| .withSchema(HiveSchemaUtil.convert(fieldSchemas, Collections.emptyMap(), true)) | ||
| .withDefaultNamespace(Namespace.of(identifier.namespace().level(0))) | ||
| .withQuery("hive", viewSql); | ||
| if (StringUtils.isNotBlank(comment)) { | ||
| builder = builder.withProperty("comment", comment); | ||
| } | ||
| for (Map.Entry<String, String> e : mergedProps.entrySet()) { | ||
| if (e.getKey() != null && e.getValue() != null) { | ||
| builder = builder.withProperty(e.getKey(), e.getValue()); | ||
| } | ||
| } | ||
| if (replace) { | ||
| builder.createOrReplace(); | ||
| } else { | ||
| builder.create(); | ||
| } | ||
| 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; | ||
| } | ||
|
|
||
| /** | ||
| * Fills Iceberg native-view HMS / view marker properties when absent (e.g. direct catalog callers). | ||
| * Handlers that delegate here after {@code HiveStorageHandler#getNativeViewHmsTableProperties()} already | ||
| * supplied markers get the same result. | ||
| */ | ||
| public static Map<String, String> mergeDefaultNativeViewTableProperties(Map<String, String> tblProperties) { | ||
| Map<String, String> merged = Maps.newHashMap(); | ||
| if (tblProperties != null) { | ||
| merged.putAll(tblProperties); | ||
| } | ||
| for (Map.Entry<String, String> e : defaultNativeViewMarkerTableProperties().entrySet()) { | ||
| merged.putIfAbsent(e.getKey(), e.getValue()); | ||
| } | ||
| return merged; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is
hiveEngineEnabledand why is itfalse?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hiveEngineEnabledswitches howHiveOperationsBase.storageDescriptorfills the Storage Desacriptor: withHiveIcebergInputFormat / HiveIcebergOutputFormat / HiveIcebergSerDewhen true, or the usual placeholderFileInputFormat / FileOutputFormat / LazySimpleSerDewhen false.Why it’s false in
toHiveView:This path materializes an HMS
VIRTUAL_VIEWfor REST catalog that expose Iceberg view metadata through the HMS API. That row isn’t meant to drive a Hive table scan the way a real Iceberg table commit does; execution still comes from the view definition / catalog, not from wiring Iceberg MR formats on the stub.HiveViewOperationsdoes the same thing (hiveEngineEnabled = false).So we keep a minimal SD consistent with normal virtual views and avoid implying this HMS object is an Iceberg-backed table for the Hive engine. For tables,
HiveTableOperationsstill turns engine integration on/off via metadata +ConfigProperties.ENGINE_HIVE_ENABLEDwhere that actually matters.