-
Notifications
You must be signed in to change notification settings - Fork 4.8k
HIVE-29578: Iceberg: add support for logical 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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
db655f2
HIVE-29578: Iceberg: support for Iceberg logical views.
f3ddf97
code review comments May 27
1484278
Disabling setting partition keys in REST Catalog client with a TODO c…
b90b769
code review comments May 29.
3b8d48b
code review comments June 02.
136c3d9
code review comments June 03.
926e3c7
code review comments June 08.
17bb8a0
code review comments June 09.
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
143 changes: 143 additions & 0 deletions
143
iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/IcebergViewSupport.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,143 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| 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.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 IcebergViewSupport { | ||
|
|
||
| private IcebergViewSupport() { | ||
| } | ||
|
|
||
| /** | ||
| * Loads the native Iceberg logical view definition and applies SQL, schema, and Iceberg params to {@code hmsTable} | ||
| */ | ||
| public static void enrichHmsTableFromIcebergView( | ||
| org.apache.hadoop.hive.metastore.api.Table hmsTable, Configuration conf) { | ||
| TableIdentifier identifier = TableIdentifier.of(hmsTable.getDbName(), hmsTable.getTableName()); | ||
| String catalogName = IcebergCatalogProperties.getCatalogName(conf); | ||
| Map<String, String> catalogProps = IcebergCatalogProperties.getCatalogProperties(conf, catalogName); | ||
| Catalog catalog = CatalogUtil.buildIcebergCatalog(catalogName, catalogProps, conf); | ||
| runWithCatalog(catalog, () -> loadAndApplyView(hmsTable, conf, catalog, catalogName, identifier)); | ||
| } | ||
|
|
||
| private static void loadAndApplyView( | ||
| org.apache.hadoop.hive.metastore.api.Table hmsTable, | ||
| Configuration conf, | ||
| Catalog catalog, | ||
| String catalogName, | ||
| TableIdentifier identifier) { | ||
| ViewCatalog viewCatalog = asViewCatalog(catalog, catalogName); | ||
| MetastoreUtil.applyIcebergViewToHmsTable(hmsTable, viewCatalog.loadView(identifier), conf); | ||
| } | ||
|
|
||
| public static void createOrReplaceView( | ||
| Configuration conf, | ||
| String databaseName, | ||
| String viewName, | ||
| List<FieldSchema> fieldSchemas, | ||
| String viewSql, | ||
| Map<String, String> tblProperties, | ||
| String comment) { | ||
|
|
||
| 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); | ||
| runWithCatalog( | ||
| catalog, | ||
| () -> commitView(catalog, catalogName, identifier, fieldSchemas, viewSql, tblProperties, comment)); | ||
| } | ||
|
|
||
| /** | ||
| * Runs {@code action} with {@code catalog}, then closes it when the catalog implements | ||
| * {@link Closeable} (e.g. REST catalog clients). | ||
| */ | ||
| private static void runWithCatalog(Catalog catalog, Runnable action) { | ||
| if (catalog instanceof Closeable closeable) { | ||
| try (Closeable ignored = closeable) { | ||
| action.run(); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to close Iceberg catalog", e); | ||
| } | ||
| } else { | ||
| action.run(); | ||
| } | ||
| } | ||
|
|
||
| private static void commitView( | ||
| Catalog catalog, | ||
| String catalogName, | ||
| TableIdentifier identifier, | ||
| List<FieldSchema> fieldSchemas, | ||
| String viewSql, | ||
| Map<String, String> tblProperties, | ||
| String comment) { | ||
| ViewCatalog viewCatalog = asViewCatalog(catalog, catalogName); | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| Map<String, String> tblProps = | ||
| tblProperties == null ? Maps.newHashMap() : Maps.newHashMap(tblProperties); | ||
|
|
||
| builder.withProperties(tblProps); | ||
|
|
||
| builder.createOrReplace(); | ||
| } | ||
|
|
||
| private static ViewCatalog asViewCatalog(Catalog catalog, String catalogName) { | ||
| if (catalog instanceof ViewCatalog viewCatalog) { | ||
| return viewCatalog; | ||
| } | ||
| throw new UnsupportedOperationException( | ||
| String.format( | ||
| "Iceberg catalog '%s' does not implement ViewCatalog.", | ||
| catalogName) + | ||
| " Iceberg views require a catalog that implements ViewCatalog (e.g. HiveCatalog or REST)."); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.