Skip to content
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

Add support to redirect table reads from Hive to Iceberg #8340

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ public class HiveConfig

private boolean legacyHiveViewTranslation;

private Optional<String> icebergCatalogName = Optional.empty();

public int getMaxInitialSplits()
{
return maxInitialSplits;
Expand Down Expand Up @@ -1039,4 +1041,17 @@ public boolean isLegacyHiveViewTranslation()
{
return this.legacyHiveViewTranslation;
}

public Optional<String> getIcebergCatalogName()
{
return icebergCatalogName;
}

@Config("hive.iceberg-catalog-name")
@ConfigDescription("The catalog to redirect iceberg tables to")
public HiveConfig setIcebergCatalogName(String icebergCatalogName)
{
this.icebergCatalogName = Optional.ofNullable(icebergCatalogName);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.trino.spi.block.Block;
import io.trino.spi.connector.Assignment;
import io.trino.spi.connector.CatalogSchemaName;
import io.trino.spi.connector.CatalogSchemaTableName;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ColumnMetadata;
import io.trino.spi.connector.ConnectorInsertTableHandle;
Expand All @@ -80,6 +81,7 @@
import io.trino.spi.connector.SchemaTablePrefix;
import io.trino.spi.connector.SortingProperty;
import io.trino.spi.connector.SystemTable;
import io.trino.spi.connector.TableColumnsMetadata;
import io.trino.spi.connector.TableNotFoundException;
import io.trino.spi.connector.TableScanRedirectApplicationResult;
import io.trino.spi.connector.ViewNotFoundException;
Expand Down Expand Up @@ -123,6 +125,7 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
Expand Down Expand Up @@ -315,6 +318,7 @@ public class HiveMetadata
private final boolean writesToNonManagedTablesEnabled;
private final boolean createsOfNonManagedTablesEnabled;
private final boolean translateHiveViews;
private final Optional<String> icebergCatalogName;
private final boolean hideDeltaLakeTables;
private final String prestoVersion;
private final HiveStatisticsProvider hiveStatisticsProvider;
Expand All @@ -331,6 +335,7 @@ public HiveMetadata(
boolean writesToNonManagedTablesEnabled,
boolean createsOfNonManagedTablesEnabled,
boolean translateHiveViews,
Optional<String> icebergCatalogName,
boolean hideDeltaLakeTables,
TypeManager typeManager,
LocationService locationService,
Expand All @@ -352,6 +357,7 @@ public HiveMetadata(
this.writesToNonManagedTablesEnabled = writesToNonManagedTablesEnabled;
this.createsOfNonManagedTablesEnabled = createsOfNonManagedTablesEnabled;
this.translateHiveViews = translateHiveViews;
this.icebergCatalogName = requireNonNull(icebergCatalogName, "icebergCatalogName is null");
this.hideDeltaLakeTables = hideDeltaLakeTables;
this.prestoVersion = requireNonNull(trinoVersion, "trinoVersion is null");
this.hiveStatisticsProvider = requireNonNull(hiveStatisticsProvider, "hiveStatisticsProvider is null");
Expand Down Expand Up @@ -379,6 +385,8 @@ public List<String> listSchemaNames(ConnectorSession session)
public HiveTableHandle getTableHandle(ConnectorSession session, SchemaTableName tableName)
{
requireNonNull(tableName, "tableName is null");
checkTableNotRedirected(session, tableName);

if (isHiveSystemSchema(tableName.getSchemaName())) {
return null;
}
Expand Down Expand Up @@ -463,10 +471,20 @@ public Optional<SystemTable> getSystemTable(ConnectorSession session, SchemaTabl
return systemTable;
}
}

return Optional.empty();
}

void checkSourceTableNotRedirected(ConnectorSession session, SchemaTableName systemTableName, SchemaTableName sourceTableName)
{
Optional<CatalogSchemaTableName> redirected = redirectTable(session, sourceTableName);
if (redirected.isPresent()) {
throw new TrinoException(NOT_SUPPORTED,
format("Querying system table '%s' is not supported, because the source table is redirected to '%s'",
new CatalogSchemaTableName(catalogName.toString(), systemTableName),
redirected.get()));
}
}

@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle tableHandle)
{
Expand Down Expand Up @@ -664,28 +682,32 @@ public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, Conn

@SuppressWarnings("TryWithIdenticalCatches")
@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix)
public Stream<TableColumnsMetadata> streamTableColumns(ConnectorSession session, SchemaTablePrefix prefix)
{
requireNonNull(prefix, "prefix is null");
ImmutableMap.Builder<SchemaTableName, List<ColumnMetadata>> columns = ImmutableMap.builder();
for (SchemaTableName tableName : listTables(session, prefix)) {
try {
columns.put(tableName, getTableMetadata(session, tableName).getColumns());
}
catch (HiveViewNotSupportedException e) {
// view is not supported
}
catch (TableNotFoundException e) {
// table disappeared during listing operation
}
catch (TrinoException e) {
// Skip this table if there's a failure due to Hive, a bad Serde, or bad metadata
if (!e.getErrorCode().getType().equals(ErrorType.EXTERNAL)) {
throw e;
}
}
}
return columns.build();
return listTables(session, prefix).stream()
.map(tableName -> {
try {
return redirectTable(session, tableName)
.map(CatalogSchemaTableName::getSchemaTableName)
.map(TableColumnsMetadata::forRedirectedTable)
.orElse(TableColumnsMetadata.forTable(tableName, getTableMetadata(session, tableName).getColumns()));
}
catch (HiveViewNotSupportedException e) {
// view is not supported
}
catch (TableNotFoundException e) {
// table disappeared during listing operation
}
catch (TrinoException e) {
// Skip this table if there's a failure due to Hive, a bad Serde, or bad metadata
if (!e.getErrorCode().getType().equals(ErrorType.EXTERNAL)) {
throw e;
}
}
return null;
})
.filter(Objects::nonNull);
}

@Override
Expand Down Expand Up @@ -1123,6 +1145,7 @@ public void dropColumn(ConnectorSession session, ConnectorTableHandle tableHandl
@Override
public void setTableAuthorization(ConnectorSession session, SchemaTableName table, TrinoPrincipal principal)
{
checkTableNotRedirected(session, table);
metastore.setTableOwner(new HiveIdentity(session), table.getSchemaName(), table.getTableName(), HivePrincipal.from(principal));
}

Expand Down Expand Up @@ -2705,12 +2728,14 @@ public Set<String> listEnabledRoles(ConnectorSession session)
@Override
public void grantTablePrivileges(ConnectorSession session, SchemaTableName schemaTableName, Set<Privilege> privileges, TrinoPrincipal grantee, boolean grantOption)
{
checkTableNotRedirected(session, schemaTableName);
accessControlMetadata.grantTablePrivileges(session, schemaTableName, privileges, HivePrincipal.from(grantee), grantOption);
}

@Override
public void revokeTablePrivileges(ConnectorSession session, SchemaTableName schemaTableName, Set<Privilege> privileges, TrinoPrincipal grantee, boolean grantOption)
{
checkTableNotRedirected(session, schemaTableName);
accessControlMetadata.revokeTablePrivileges(session, schemaTableName, privileges, HivePrincipal.from(grantee), grantOption);
}

Expand Down Expand Up @@ -2996,6 +3021,37 @@ public CompletableFuture<?> refreshMaterializedView(ConnectorSession session, Sc
return hiveMaterializedViewMetadata.refreshMaterializedView(session, name);
}

@Override
public Optional<CatalogSchemaTableName> redirectTable(ConnectorSession session, SchemaTableName tableName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update this to #9870?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update this to #9870?

I've taken liberty to update the code myself.
I also copied & adapted an awesome test template done by @ssheikin too.
posted #10173 with the changes

Copy link
Contributor

@ssheikin ssheikin Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These credits belong to @MiguelWeezardo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These credits belong to @MiguelWeezardo

thanks @ssheikin for the comment. added @MiguelWeezardo as co-author in the other PR

{
if (icebergCatalogName.isPresent()
&& !isHiveSystemSchema(tableName.getSchemaName())
&& isExistingIcebergTable(session, tableName)) {
return Optional.of(new CatalogSchemaTableName(icebergCatalogName.get(), tableName));
}

return Optional.empty();
}

private boolean isExistingIcebergTable(ConnectorSession session, SchemaTableName schemaTableName)
{
return metastore.getTable(new HiveIdentity(session), schemaTableName.getSchemaName(), schemaTableName.getTableName())
.map(HiveUtil::isIcebergTable)
.orElse(false);
}

private void checkTableNotRedirected(ConnectorSession session, SchemaTableName tableName)
{
redirectTable(session, tableName).ifPresent(targetTable -> {
throw new TrinoException(NOT_SUPPORTED,
format("This operation is not supported on '%s', because it is redirected to '%s'."
+ " Modification operations (insert, alter, drop, delete, update, comment, grant, revoke)"
+ " are not supported when redirection is enabled.",
tableName,
targetTable));
});
}

public static Optional<SchemaTableName> getSourceTableNameFromSystemTable(SchemaTableName tableName)
{
return Stream.of(SystemTableHandler.values())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class HiveMetadataFactory
private final boolean writesToNonManagedTablesEnabled;
private final boolean createsOfNonManagedTablesEnabled;
private final boolean translateHiveViews;
private final Optional<String> icebergCatalogName;
private final boolean hideDeltaLakeTables;
private final long perTransactionCacheMaximumSize;
private final HiveMetastore metastore;
Expand Down Expand Up @@ -100,6 +101,7 @@ public HiveMetadataFactory(
hiveConfig.isTranslateHiveViews(),
hiveConfig.getPerTransactionMetastoreCacheMaximumSize(),
hiveConfig.getHiveTransactionHeartbeatInterval(),
hiveConfig.getIcebergCatalogName(),
metastoreConfig.isHideDeltaLakeTables(),
typeManager,
locationService,
Expand Down Expand Up @@ -128,6 +130,7 @@ public HiveMetadataFactory(
boolean translateHiveViews,
long perTransactionCacheMaximumSize,
Optional<Duration> hiveTransactionHeartbeatInterval,
Optional<String> icebergCatalogName,
boolean hideDeltaLakeTables,
TypeManager typeManager,
LocationService locationService,
Expand All @@ -146,6 +149,7 @@ public HiveMetadataFactory(
this.writesToNonManagedTablesEnabled = writesToNonManagedTablesEnabled;
this.createsOfNonManagedTablesEnabled = createsOfNonManagedTablesEnabled;
this.translateHiveViews = translateHiveViews;
this.icebergCatalogName = requireNonNull(icebergCatalogName, "icebergCatalogName is null");
this.hideDeltaLakeTables = hideDeltaLakeTables;
this.perTransactionCacheMaximumSize = perTransactionCacheMaximumSize;

Expand Down Expand Up @@ -198,6 +202,7 @@ public TransactionalMetadata create()
writesToNonManagedTablesEnabled,
createsOfNonManagedTablesEnabled,
translateHiveViews,
icebergCatalogName,
hideDeltaLakeTables,
typeManager,
locationService,
Expand All @@ -218,6 +223,7 @@ protected TransactionalMetadata create(
boolean writesToNonManagedTablesEnabled,
boolean createsOfNonManagedTablesEnabled,
boolean translateHiveViews,
Optional<String> icebergCatalogName,
boolean hideDeltaLakeTables,
TypeManager typeManager,
LocationService locationService,
Expand All @@ -237,6 +243,7 @@ protected TransactionalMetadata create(
writesToNonManagedTablesEnabled,
createsOfNonManagedTablesEnabled,
translateHiveViews,
icebergCatalogName,
hideDeltaLakeTables,
typeManager,
locationService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public Optional<SystemTable> getSystemTable(HiveMetadata metadata, ConnectorSess
}

SchemaTableName sourceTableName = PARTITIONS.getSourceTableName(tableName);
metadata.checkSourceTableNotRedirected(session, tableName, sourceTableName);
HiveTableHandle sourceTableHandle = metadata.getTableHandle(session, sourceTableName);

if (sourceTableHandle == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public Optional<SystemTable> getSystemTable(HiveMetadata metadata, ConnectorSess
}

SchemaTableName sourceTableName = PROPERTIES.getSourceTableName(tableName);
metadata.checkSourceTableNotRedirected(session, tableName, sourceTableName);

Optional<Table> table = metadata.getMetastore().getTable(new HiveIdentity(session), sourceTableName.getSchemaName(), sourceTableName.getTableName());
if (table.isEmpty() || table.get().getTableType().equals(TableType.VIRTUAL_VIEW.name())) {
throw new TableNotFoundException(tableName);
Expand Down
Loading