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

View redirection support #23485

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions core/trino-main/src/main/java/io/trino/execution/DropViewTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.RedirectionAwareViewHandle;
import io.trino.metadata.ViewDefinition;
import io.trino.security.AccessControl;
import io.trino.sql.tree.DropView;
import io.trino.sql.tree.Expression;

import java.util.List;
import java.util.Optional;

import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static io.trino.metadata.MetadataUtil.createQualifiedObjectName;
Expand Down Expand Up @@ -60,6 +63,15 @@ public ListenableFuture<Void> execute(
Session session = stateMachine.getSession();
QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());

RedirectionAwareViewHandle redirectionView = metadata.getRedirectionAwareViewHandle(session, name);
Optional<QualifiedObjectName> targetViewName = redirectionView.redirectedViewName();
if (targetViewName.isPresent()) {
Optional<ViewDefinition> targetView = metadata.getView(session, targetViewName.get());
if (targetView.isPresent()) {
name = targetViewName.get();
}
}

if (metadata.isMaterializedView(session, name)) {
throw semanticException(
GENERIC_USER_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.RedirectionAwareViewHandle;
import io.trino.metadata.ViewDefinition;
import io.trino.security.AccessControl;
import io.trino.spi.security.TrinoPrincipal;
import io.trino.sql.tree.Expression;
Expand Down Expand Up @@ -64,6 +66,16 @@ public ListenableFuture<Void> execute(
{
Session session = stateMachine.getSession();
QualifiedObjectName viewName = createQualifiedObjectName(session, statement, statement.getSource());

RedirectionAwareViewHandle redirectionView = metadata.getRedirectionAwareViewHandle(session, viewName);
Optional<QualifiedObjectName> targetViewName = redirectionView.redirectedViewName();
if (targetViewName.isPresent()) {
Optional<ViewDefinition> targetView = metadata.getView(session, targetViewName.get());
if (targetView.isPresent()) {
viewName = targetViewName.get();
}
}

getRequiredCatalogHandle(metadata, session, statement, viewName.catalogName());
if (!metadata.isView(session, viewName)) {
throw semanticException(TABLE_NOT_FOUND, statement, "View '%s' does not exist", viewName);
Expand Down
10 changes: 10 additions & 0 deletions core/trino-main/src/main/java/io/trino/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,16 @@ default boolean isMaterializedView(Session session, QualifiedObjectName viewName
*/
RedirectionAwareTableHandle getRedirectionAwareTableHandle(Session session, QualifiedObjectName tableName, Optional<TableVersion> startVersion, Optional<TableVersion> endVersion);

/**
* Get the target view handle after performing redirection.
*/
RedirectionAwareViewHandle getRedirectionAwareViewHandle(Session session, QualifiedObjectName viewName);

/**
* Get the target view handle after performing redirection.
*/
RedirectionAwareViewHandle getRedirectionAwareViewHandle(Session session, QualifiedObjectName viewName, Optional<TableVersion> startVersion, Optional<TableVersion> endVersion);

/**
* Returns a table handle for the specified table name with a specified version
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND;
import static io.trino.spi.StandardErrorCode.TABLE_REDIRECTION_ERROR;
import static io.trino.spi.StandardErrorCode.UNSUPPORTED_TABLE_TYPE;
import static io.trino.spi.StandardErrorCode.VIEW_REDIRECTION_ERROR;
import static io.trino.spi.connector.MaterializedViewFreshness.Freshness.STALE;
import static io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager;
import static io.trino.type.InternalTypeManager.TESTING_TYPE_MANAGER;
Expand All @@ -189,6 +190,7 @@ public final class MetadataManager

@VisibleForTesting
public static final int MAX_TABLE_REDIRECTIONS = 10;
public static final int MAX_VIEW_REDIRECTIONS = 5;

private final AccessControl accessControl;
private final GlobalFunctionCatalog functions;
Expand Down Expand Up @@ -1933,6 +1935,51 @@ private QualifiedObjectName getRedirectedTableName(Session session, QualifiedObj
throw new TrinoException(TABLE_REDIRECTION_ERROR, format("Table redirected too many times (%d): %s", MAX_TABLE_REDIRECTIONS, visitedTableNames));
}

private QualifiedObjectName getRedirectedViewName(Session session, QualifiedObjectName originalViewName, Optional<TableVersion> startVersion, Optional<TableVersion> endVersion)
{
requireNonNull(session, "session is null");
requireNonNull(originalViewName, "originalViewName is null");
if (cannotExist(originalViewName)) {
return originalViewName;
}

QualifiedObjectName viewName = originalViewName;
Set<QualifiedObjectName> visitedTableNames = new LinkedHashSet<>();
visitedTableNames.add(viewName);

for (int count = 0; count < MAX_VIEW_REDIRECTIONS; count++) {
Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, viewName.catalogName());

if (catalog.isEmpty()) {
// Stop redirection
return viewName;
}

CatalogMetadata catalogMetadata = catalog.get();
CatalogHandle catalogHandle = catalogMetadata.getCatalogHandle(session, viewName, toConnectorVersion(startVersion), toConnectorVersion(endVersion));
ConnectorMetadata metadata = catalogMetadata.getMetadataFor(session, catalogHandle);

Optional<QualifiedObjectName> redirectedViewName = metadata.redirectView(session.toConnectorSession(catalogHandle), viewName.asSchemaTableName())
.map(name -> convertFromSchemaTableName(String.valueOf(name.getCatalogName())).apply(originalViewName.asSchemaTableName()));

if (redirectedViewName.isEmpty()) {
return viewName;
}

viewName = redirectedViewName.get();

// Check for loop in redirection
if (!visitedTableNames.add(viewName)) {
throw new TrinoException(VIEW_REDIRECTION_ERROR,
format("View redirections form a loop: %s",
Streams.concat(visitedTableNames.stream(), Stream.of(viewName))
.map(QualifiedObjectName::toString)
.collect(Collectors.joining(" -> "))));
}
}
throw new TrinoException(VIEW_REDIRECTION_ERROR, format("View redirected too many times (%d): %s", MAX_VIEW_REDIRECTIONS, visitedTableNames));
}

@Override
public RedirectionAwareTableHandle getRedirectionAwareTableHandle(Session session, QualifiedObjectName tableName)
{
Expand Down Expand Up @@ -1962,6 +2009,22 @@ public RedirectionAwareTableHandle getRedirectionAwareTableHandle(Session sessio
throw new TrinoException(TABLE_REDIRECTION_ERROR, format("Table '%s' redirected to '%s', but the target table '%s' does not exist", tableName, targetTableName, targetTableName));
}

@Override
public RedirectionAwareViewHandle getRedirectionAwareViewHandle(Session session, QualifiedObjectName viewName)
{
return getRedirectionAwareViewHandle(session, viewName, Optional.empty(), Optional.empty());
}

@Override
public RedirectionAwareViewHandle getRedirectionAwareViewHandle(Session session, QualifiedObjectName viewName, Optional<TableVersion> startVersion, Optional<TableVersion> endVersion)
{
QualifiedObjectName targetViewName = getRedirectedViewName(session, viewName, startVersion, endVersion);
if (targetViewName.equals(viewName)) {
return RedirectionAwareViewHandle.noRedirection();
}
return RedirectionAwareViewHandle.withRedirectionTo(targetViewName);
}

@Override
public Optional<ResolvedIndex> resolveIndex(Session session, TableHandle tableHandle, Set<ColumnHandle> indexableColumns, Set<ColumnHandle> outputColumns, TupleDomain<ColumnHandle> tupleDomain)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed 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 io.trino.metadata;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

public record RedirectionAwareViewHandle(
// the target view name after redirection. Optional.empty() if the view is not redirected.
Optional<QualifiedObjectName> redirectedViewName)
{
public static RedirectionAwareViewHandle withRedirectionTo(QualifiedObjectName redirectedViewName)
{
return new RedirectionAwareViewHandle(Optional.of(redirectedViewName));
}

public static RedirectionAwareViewHandle noRedirection()
{
return new RedirectionAwareViewHandle(Optional.empty());
}

public RedirectionAwareViewHandle
{
requireNonNull(redirectedViewName, "redirectedViewName is null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.trino.metadata.OperatorNotFoundException;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.RedirectionAwareTableHandle;
import io.trino.metadata.RedirectionAwareViewHandle;
import io.trino.metadata.ResolvedFunction;
import io.trino.metadata.TableExecuteHandle;
import io.trino.metadata.TableFunctionMetadata;
Expand Down Expand Up @@ -2280,6 +2281,16 @@ protected Scope visitTable(Table table, Optional<Scope> scope)
return createScopeForMaterializedView(table, name, scope, materializedViewDefinition, Optional.empty());
}

RedirectionAwareViewHandle redirectionView = metadata.getRedirectionAwareViewHandle(session, name);
Optional<QualifiedObjectName> targetViewName = redirectionView.redirectedViewName();

if (targetViewName.isPresent()) {
Optional<ViewDefinition> targetView = metadata.getView(session, targetViewName.get());
if (targetView.isPresent()) {
return createScopeForView(table, name, scope, targetView.get());
}
}

// This could be a reference to a logical view or a table
Optional<ViewDefinition> optionalView = metadata.getView(session, name);
if (optionalView.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.trino.metadata.MetadataUtil;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.RedirectionAwareTableHandle;
import io.trino.metadata.RedirectionAwareViewHandle;
import io.trino.metadata.SchemaPropertyManager;
import io.trino.metadata.SessionPropertyManager;
import io.trino.metadata.SessionPropertyManager.SessionPropertyValue;
Expand Down Expand Up @@ -571,9 +572,12 @@ private Query showCreateView(ShowCreate node)
if (metadata.isMaterializedView(session, objectName)) {
throw semanticException(NOT_SUPPORTED, node, "Relation '%s' is a materialized view, not a view", objectName);
}

RedirectionAwareViewHandle redirectionView = metadata.getRedirectionAwareViewHandle(session, objectName);
Optional<QualifiedObjectName> targetViewName = redirectionView.redirectedViewName();
if (targetViewName.isPresent()) {
objectName = targetViewName.get();
}
Optional<ViewDefinition> viewDefinition = metadata.getView(session, objectName);

if (viewDefinition.isEmpty()) {
if (metadata.getTableHandle(session, objectName).isPresent()) {
throw semanticException(NOT_SUPPORTED, node, "Relation '%s' is a table, not a view", objectName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,15 @@ public Optional<CatalogSchemaTableName> redirectTable(ConnectorSession session,
}
}

@Override
public Optional<CatalogSchemaTableName> redirectView(ConnectorSession session, SchemaTableName viewName)
{
Span span = startSpan("redirectView", viewName);
try (var ignored = scopedSpan(span)) {
return delegate.redirectView(session, viewName);
}
}

@Override
public OptionalInt getMaxWriterTasks(ConnectorSession session)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.QualifiedTablePrefix;
import io.trino.metadata.RedirectionAwareTableHandle;
import io.trino.metadata.RedirectionAwareViewHandle;
import io.trino.metadata.ResolvedFunction;
import io.trino.metadata.ResolvedIndex;
import io.trino.metadata.TableExecuteHandle;
Expand Down Expand Up @@ -1504,6 +1505,24 @@ public RedirectionAwareTableHandle getRedirectionAwareTableHandle(Session sessio
}
}

@Override
public RedirectionAwareViewHandle getRedirectionAwareViewHandle(Session session, QualifiedObjectName viewName)
{
Span span = startSpan("getRedirectionAwareViewHandle", viewName);
try (var ignored = scopedSpan(span)) {
return delegate.getRedirectionAwareViewHandle(session, viewName);
}
}

@Override
public RedirectionAwareViewHandle getRedirectionAwareViewHandle(Session session, QualifiedObjectName viewName, Optional<TableVersion> startVersion, Optional<TableVersion> endVersion)
{
Span span = startSpan("getRedirectionAwareViewHandle", viewName);
try (var ignored = scopedSpan(span)) {
return delegate.getRedirectionAwareViewHandle(session, viewName, startVersion, endVersion);
}
}

@Override
public Optional<TableHandle> getTableHandle(Session session, QualifiedObjectName tableName, Optional<TableVersion> startVersion, Optional<TableVersion> endVersion)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import static io.trino.metadata.GlobalFunctionCatalog.BUILTIN_SCHEMA;
import static io.trino.metadata.GlobalFunctionCatalog.builtinFunctionName;
import static io.trino.metadata.RedirectionAwareTableHandle.noRedirection;
import static io.trino.metadata.RedirectionAwareViewHandle.noRedirection;
import static io.trino.spi.StandardErrorCode.FUNCTION_NOT_FOUND;
import static io.trino.spi.function.FunctionDependencyDeclaration.NO_DEPENDENCIES;
import static io.trino.spi.function.FunctionId.toFunctionId;
Expand Down Expand Up @@ -1007,6 +1008,18 @@ public RedirectionAwareTableHandle getRedirectionAwareTableHandle(Session sessio
throw new UnsupportedOperationException();
}

@Override
public RedirectionAwareViewHandle getRedirectionAwareViewHandle(Session session, QualifiedObjectName tableName)
{
return RedirectionAwareViewHandle.noRedirection();
}

@Override
public RedirectionAwareViewHandle getRedirectionAwareViewHandle(Session session, QualifiedObjectName tableName, Optional<TableVersion> startVersion, Optional<TableVersion> endVersion)
{
throw new UnsupportedOperationException();
}

@Override
public Optional<TableHandle> getTableHandle(Session session, QualifiedObjectName table, Optional<TableVersion> startVersion, Optional<TableVersion> endVersion)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public enum StandardErrorCode
INVALID_VIEW_PROPERTY(130, USER_ERROR),
INVALID_ENTITY_KIND(131, USER_ERROR),
QUERY_EXCEEDED_COMPILER_LIMIT(132, USER_ERROR),
VIEW_REDIRECTION_ERROR(133, USER_ERROR),

GENERIC_INTERNAL_ERROR(65536, INTERNAL_ERROR),
TOO_MANY_REQUESTS_FAILED(65537, INTERNAL_ERROR),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,14 @@ default Optional<CatalogSchemaTableName> redirectTable(ConnectorSession session,
return Optional.empty();
}

/**
* Redirects view to other view which may or may not be in the same catalog.
*/
default Optional<CatalogSchemaTableName> redirectView(ConnectorSession session, SchemaTableName tableName)
{
return Optional.empty();
}

default OptionalInt getMaxWriterTasks(ConnectorSession session)
{
return OptionalInt.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,14 @@ public Optional<CatalogSchemaTableName> redirectTable(ConnectorSession session,
}
}

@Override
public Optional<CatalogSchemaTableName> redirectView(ConnectorSession session, SchemaTableName viewName)
{
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(classLoader)) {
return delegate.redirectView(session, viewName);
}
}

@Override
public ConnectorTableHandle getTableHandle(ConnectorSession session, SchemaTableName tableName, Optional<ConnectorTableVersion> startVersion, Optional<ConnectorTableVersion> endVersion)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3511,6 +3511,16 @@ public Optional<CatalogSchemaTableName> redirectTable(ConnectorSession session,
return catalog.redirectTable(session, tableName, targetCatalogName.get());
}

@Override
public Optional<CatalogSchemaTableName> redirectView(ConnectorSession session, SchemaTableName viewName)
{
Optional<String> targetCatalogName = getHiveCatalogName(session);
if (targetCatalogName.isEmpty()) {
return Optional.empty();
}
return catalog.redirectView(session, viewName, targetCatalogName.get());
}

@Override
public boolean allowSplittingReadIntoMultipleSubQueries(ConnectorSession session, ConnectorTableHandle connectorTableHandle)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,9 @@ void createMaterializedView(
void updateColumnComment(ConnectorSession session, SchemaTableName schemaTableName, ColumnIdentity columnIdentity, Optional<String> comment);

Optional<CatalogSchemaTableName> redirectTable(ConnectorSession session, SchemaTableName tableName, String hiveCatalogName);

default Optional<CatalogSchemaTableName> redirectView(ConnectorSession session, SchemaTableName viewName, String hiveCatalogName)
{
return Optional.empty();
}
}
Loading
Loading