-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Support views in iceberg connector #2703
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bd79a54
create/get/drop views for iceberg connector
alexey-fin f2510ca
Merge branch 'master' into iceberg-support-views
alexey-fin b873beb
extract common view functions to HiveUtil
alexey-fin 112d3fd
Merge branch 'master' into iceberg-support-views
alexey-fin 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 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 |
---|---|---|
|
@@ -177,7 +177,6 @@ | |
import static io.prestosql.plugin.hive.HiveTableProperties.getOrcBloomFilterFpp; | ||
import static io.prestosql.plugin.hive.HiveTableProperties.getPartitionedBy; | ||
import static io.prestosql.plugin.hive.HiveTableProperties.getSingleCharacterProperty; | ||
import static io.prestosql.plugin.hive.HiveType.HIVE_STRING; | ||
import static io.prestosql.plugin.hive.HiveType.toHiveType; | ||
import static io.prestosql.plugin.hive.HiveWriterFactory.computeBucketedFileName; | ||
import static io.prestosql.plugin.hive.PartitionUpdate.UpdateMode.APPEND; | ||
|
@@ -188,7 +187,6 @@ | |
import static io.prestosql.plugin.hive.metastore.MetastoreUtil.getProtectMode; | ||
import static io.prestosql.plugin.hive.metastore.MetastoreUtil.verifyOnline; | ||
import static io.prestosql.plugin.hive.metastore.PrincipalPrivileges.fromHivePrivilegeInfos; | ||
import static io.prestosql.plugin.hive.metastore.StorageFormat.VIEW_STORAGE_FORMAT; | ||
import static io.prestosql.plugin.hive.metastore.StorageFormat.fromHiveStorageFormat; | ||
import static io.prestosql.plugin.hive.util.CompressionConfigUtil.configureCompression; | ||
import static io.prestosql.plugin.hive.util.ConfigurationUtils.toJobConf; | ||
|
@@ -197,8 +195,6 @@ | |
import static io.prestosql.plugin.hive.util.HiveUtil.PRESTO_VIEW_FLAG; | ||
import static io.prestosql.plugin.hive.util.HiveUtil.buildHiveViewConnectorDefinition; | ||
import static io.prestosql.plugin.hive.util.HiveUtil.columnExtraInfo; | ||
import static io.prestosql.plugin.hive.util.HiveUtil.decodeViewData; | ||
import static io.prestosql.plugin.hive.util.HiveUtil.encodeViewData; | ||
import static io.prestosql.plugin.hive.util.HiveUtil.getPartitionKeyColumnHandles; | ||
import static io.prestosql.plugin.hive.util.HiveUtil.hiveColumnHandles; | ||
import static io.prestosql.plugin.hive.util.HiveUtil.isPrestoView; | ||
|
@@ -1635,31 +1631,11 @@ private static Map<ColumnStatisticMetadata, Block> getColumnStatistics(Map<List< | |
public void createView(ConnectorSession session, SchemaTableName viewName, ConnectorViewDefinition definition, boolean replace) | ||
{ | ||
HiveIdentity identity = new HiveIdentity(session); | ||
Map<String, String> properties = ImmutableMap.<String, String>builder() | ||
.put(TABLE_COMMENT, "Presto View") | ||
.put(PRESTO_VIEW_FLAG, "true") | ||
.put(PRESTO_VERSION_NAME, prestoVersion) | ||
.put(PRESTO_QUERY_ID_NAME, session.getQueryId()) | ||
.build(); | ||
String owner = session.getUser(); | ||
|
||
Column dummyColumn = new Column("dummy", HIVE_STRING, Optional.empty()); | ||
|
||
Table.Builder tableBuilder = Table.builder() | ||
.setDatabaseName(viewName.getSchemaName()) | ||
.setTableName(viewName.getTableName()) | ||
.setOwner(session.getUser()) | ||
.setTableType(TableType.VIRTUAL_VIEW.name()) | ||
.setDataColumns(ImmutableList.of(dummyColumn)) | ||
.setPartitionColumns(ImmutableList.of()) | ||
.setParameters(properties) | ||
.setViewOriginalText(Optional.of(encodeViewData(definition))) | ||
.setViewExpandedText(Optional.of("/* Presto View */")); | ||
Table table = HiveUtil.buildViewTable(definition, viewName, owner, session.getQueryId(), prestoVersion); | ||
|
||
tableBuilder.getStorageBuilder() | ||
.setStorageFormat(VIEW_STORAGE_FORMAT) | ||
.setLocation(""); | ||
Table table = tableBuilder.build(); | ||
PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(session.getUser()); | ||
PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(owner); | ||
|
||
Optional<Table> existing = metastore.getTable(identity, viewName.getSchemaName(), viewName.getTableName()); | ||
if (existing.isPresent()) { | ||
|
@@ -1715,29 +1691,11 @@ public List<SchemaTableName> listViews(ConnectorSession session, Optional<String | |
@Override | ||
public Optional<ConnectorViewDefinition> getView(ConnectorSession session, SchemaTableName viewName) | ||
{ | ||
return metastore.getTable(new HiveIdentity(session), viewName.getSchemaName(), viewName.getTableName()) | ||
.flatMap(view -> { | ||
if (isPrestoView(view)) { | ||
ConnectorViewDefinition definition = decodeViewData(view.getViewOriginalText() | ||
.orElseThrow(() -> new PrestoException(HIVE_INVALID_METADATA, "No view original text: " + viewName))); | ||
// use owner from table metadata if it exists | ||
if (view.getOwner() != null && !definition.isRunAsInvoker()) { | ||
definition = new ConnectorViewDefinition( | ||
definition.getOriginalSql(), | ||
definition.getCatalog(), | ||
definition.getSchema(), | ||
definition.getColumns(), | ||
definition.getComment(), | ||
Optional.of(view.getOwner()), | ||
false); | ||
} | ||
return Optional.of(definition); | ||
} | ||
if (translateHiveViews && isHiveOrPrestoView(view)) { | ||
return Optional.of(buildHiveViewConnectorDefinition(catalogName, view)); | ||
} | ||
return Optional.empty(); | ||
}); | ||
HiveIdentity identity = new HiveIdentity(session); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can stay inlined in the |
||
|
||
return metastore.getTable(identity, viewName.getSchemaName(), viewName.getTableName()) | ||
.filter(HiveUtil::isPrestoView) | ||
.map(HiveUtil::buildViewDefinition); | ||
} | ||
|
||
private boolean isHiveOrPrestoView(Table table) | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -23,10 +23,13 @@ | |
import io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext; | ||
import io.prestosql.plugin.hive.HiveWrittenPartitions; | ||
import io.prestosql.plugin.hive.TableAlreadyExistsException; | ||
import io.prestosql.plugin.hive.ViewAlreadyExistsException; | ||
import io.prestosql.plugin.hive.authentication.HiveIdentity; | ||
import io.prestosql.plugin.hive.metastore.Database; | ||
import io.prestosql.plugin.hive.metastore.HiveMetastore; | ||
import io.prestosql.plugin.hive.metastore.PrincipalPrivileges; | ||
import io.prestosql.plugin.hive.metastore.Table; | ||
import io.prestosql.plugin.hive.util.HiveUtil; | ||
import io.prestosql.spi.PrestoException; | ||
import io.prestosql.spi.connector.ColumnHandle; | ||
import io.prestosql.spi.connector.ColumnMetadata; | ||
|
@@ -39,13 +42,15 @@ | |
import io.prestosql.spi.connector.ConnectorTableHandle; | ||
import io.prestosql.spi.connector.ConnectorTableMetadata; | ||
import io.prestosql.spi.connector.ConnectorTableProperties; | ||
import io.prestosql.spi.connector.ConnectorViewDefinition; | ||
import io.prestosql.spi.connector.Constraint; | ||
import io.prestosql.spi.connector.ConstraintApplicationResult; | ||
import io.prestosql.spi.connector.SchemaNotFoundException; | ||
import io.prestosql.spi.connector.SchemaTableName; | ||
import io.prestosql.spi.connector.SchemaTablePrefix; | ||
import io.prestosql.spi.connector.SystemTable; | ||
import io.prestosql.spi.connector.TableNotFoundException; | ||
import io.prestosql.spi.connector.ViewNotFoundException; | ||
import io.prestosql.spi.predicate.TupleDomain; | ||
import io.prestosql.spi.statistics.ComputedStatistics; | ||
import io.prestosql.spi.type.DecimalType; | ||
|
@@ -81,6 +86,7 @@ | |
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
import static io.prestosql.plugin.hive.HiveSchemaProperties.getLocation; | ||
import static io.prestosql.plugin.hive.metastore.MetastoreUtil.buildInitialPrivilegeSet; | ||
import static io.prestosql.plugin.hive.util.HiveWriteUtils.getTableDefaultLocation; | ||
import static io.prestosql.plugin.iceberg.DomainConverter.convertTupleDomainTypes; | ||
import static io.prestosql.plugin.iceberg.ExpressionConverter.toIcebergExpression; | ||
|
@@ -118,19 +124,22 @@ public class IcebergMetadata | |
private final HdfsEnvironment hdfsEnvironment; | ||
private final TypeManager typeManager; | ||
private final JsonCodec<CommitTaskData> commitTaskCodec; | ||
private final String prestoVersion; | ||
|
||
private Transaction transaction; | ||
|
||
public IcebergMetadata( | ||
HiveMetastore metastore, | ||
HdfsEnvironment hdfsEnvironment, | ||
TypeManager typeManager, | ||
JsonCodec<CommitTaskData> commitTaskCodec) | ||
JsonCodec<CommitTaskData> commitTaskCodec, | ||
String prestoVersion) | ||
{ | ||
this.metastore = requireNonNull(metastore, "metastore is null"); | ||
this.hdfsEnvironment = requireNonNull(hdfsEnvironment, "hdfsEnvironment is null"); | ||
this.typeManager = requireNonNull(typeManager, "typeManager is null"); | ||
this.commitTaskCodec = requireNonNull(commitTaskCodec, "commitTaskCodec is null"); | ||
this.prestoVersion = requireNonNull(prestoVersion, "prestoVersion is null"); | ||
} | ||
|
||
@Override | ||
|
@@ -564,4 +573,56 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(C | |
table.getPredicate().intersect(newDomain)), | ||
constraint.getSummary())); | ||
} | ||
|
||
@Override | ||
public void createView(ConnectorSession session, SchemaTableName viewName, ConnectorViewDefinition definition, boolean replace) | ||
{ | ||
HiveIdentity identity = new HiveIdentity(session); | ||
String owner = session.getUser(); | ||
|
||
Table table = HiveUtil.buildViewTable(definition, viewName, owner, session.getQueryId(), prestoVersion); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Static import |
||
|
||
PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(owner); | ||
|
||
Optional<Table> existing = metastore.getTable(identity, viewName.getSchemaName(), viewName.getTableName()); | ||
if (existing.isPresent()) { | ||
if (!replace || !HiveUtil.isPrestoView(existing.get())) { | ||
throw new ViewAlreadyExistsException(viewName); | ||
} | ||
|
||
metastore.replaceTable(identity, viewName.getSchemaName(), viewName.getTableName(), table, principalPrivileges); | ||
return; | ||
} | ||
|
||
try { | ||
metastore.createTable(identity, table, principalPrivileges); | ||
} | ||
catch (TableAlreadyExistsException e) { | ||
throw new ViewAlreadyExistsException(e.getTableName()); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<ConnectorViewDefinition> getView(ConnectorSession session, SchemaTableName viewName) | ||
{ | ||
return metastore.getTable(new HiveIdentity(session), viewName.getSchemaName(), viewName.getTableName()) | ||
.filter(HiveUtil::isPrestoView) | ||
.map(HiveUtil::buildViewDefinition); | ||
} | ||
|
||
@Override | ||
public void dropView(ConnectorSession session, SchemaTableName viewName) | ||
{ | ||
if (!getView(session, viewName).isPresent()) { | ||
throw new ViewNotFoundException(viewName); | ||
} | ||
|
||
try { | ||
HiveIdentity identity = new HiveIdentity(session); | ||
metastore.dropTable(identity, viewName.getSchemaName(), viewName.getTableName(), false); | ||
} | ||
catch (TableNotFoundException e) { | ||
throw new ViewNotFoundException(e.getTableName()); | ||
} | ||
} | ||
} |
This file contains 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
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.
Static import
buildViewTable