-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add support for View Refresh operation #25906
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* 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.execution; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.inject.Inject; | ||
import io.trino.Session; | ||
import io.trino.execution.warnings.WarningCollector; | ||
import io.trino.metadata.Metadata; | ||
import io.trino.metadata.QualifiedObjectName; | ||
import io.trino.metadata.ViewColumn; | ||
import io.trino.metadata.ViewDefinition; | ||
import io.trino.security.AccessControl; | ||
import io.trino.security.ViewAccessControl; | ||
import io.trino.spi.security.GroupProvider; | ||
import io.trino.spi.security.Identity; | ||
import io.trino.sql.PlannerContext; | ||
import io.trino.sql.analyzer.Analysis; | ||
import io.trino.sql.analyzer.AnalyzerFactory; | ||
import io.trino.sql.parser.SqlParser; | ||
import io.trino.sql.tree.Expression; | ||
import io.trino.sql.tree.RefreshView; | ||
import io.trino.sql.tree.Statement; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
import static com.google.common.util.concurrent.Futures.immediateVoidFuture; | ||
import static io.trino.metadata.MetadataUtil.createQualifiedObjectName; | ||
import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; | ||
import static io.trino.sql.analyzer.SemanticExceptions.semanticException; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class RefreshViewTask | ||
implements DataDefinitionTask<RefreshView> | ||
{ | ||
private final PlannerContext plannerContext; | ||
private final AccessControl accessControl; | ||
private final GroupProvider groupProvider; | ||
private final SqlParser sqlParser; | ||
private final AnalyzerFactory analyzerFactory; | ||
|
||
@Inject | ||
public RefreshViewTask( | ||
PlannerContext plannerContext, | ||
AccessControl accessControl, | ||
GroupProvider groupProvider, | ||
SqlParser sqlParser, | ||
AnalyzerFactory analyzerFactory) | ||
{ | ||
this.plannerContext = requireNonNull(plannerContext, "plannerContext is null"); | ||
this.accessControl = requireNonNull(accessControl, "accessControl is null"); | ||
this.groupProvider = requireNonNull(groupProvider, "groupProvider is null"); | ||
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null"); | ||
this.analyzerFactory = requireNonNull(analyzerFactory, "analyzerFactory is null"); | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return "REFRESH VIEW"; | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Void> execute( | ||
RefreshView refreshView, | ||
QueryStateMachine stateMachine, | ||
List<Expression> parameters, | ||
WarningCollector warningCollector) | ||
{ | ||
Metadata metadata = plannerContext.getMetadata(); | ||
Session session = stateMachine.getSession(); | ||
QualifiedObjectName viewName = createQualifiedObjectName(session, refreshView, refreshView.getName()); | ||
|
||
ViewDefinition viewDefinition = metadata.getView(session, viewName) | ||
.orElseThrow(() -> semanticException(TABLE_NOT_FOUND, refreshView, "View '%s' not found", viewName)); | ||
|
||
accessControl.checkCanRefreshView(session.toSecurityContext(), viewName); | ||
|
||
Identity identity = session.getIdentity(); | ||
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. I think that ideally the view definition should always be refreshed by the same identity that initially created the view. 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. I would like to retain the secuirty mode for refresh like we do for execution - Ideally the same could be enforced via access control mechansim as well.
Can you explain it a bit. 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.
It is not obvious to me that we should refresh in the same mode as we execute the view. 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.
I agree with this - we should redfine it via the owner identity.
But it wouldn;t work for the definer views right ? Let’s say if we have an access to the table only for the owner and accessing/refreshing via 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. Agreed. The refresh can fail when run by a non-owner. I was thinking of a hypothetical situation when the view semantics actually differs for the owner and the executing identity -- for example if there was a mechanism to hide some columns from some users. If a random user refreshed the view, everyone would get their subset of columns. This is why I said we should always refresh as the owner. 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.
But the same would happen for executing the views as well right ? Today a user could create a view 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. Agreed, I think we're getting to the same point. I the owner refreshes the view, we get the original intended semantics. If another user refreshes the view, they will impose their semantics not only for themselves but also for everyone who will execute that view later. Regarding the current implementation: the view is refreshed
We want it to be always the owner. But the issue is that for "invoker" views we don't know who the owner is. 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.
Yes - either we need to disable refreshing those views and maybe we could |
||
AccessControl viewAccessControl = accessControl; | ||
|
||
if (!viewDefinition.isRunAsInvoker()) { | ||
checkArgument(viewDefinition.getRunAsIdentity().isPresent(), "View owner detail is missing"); | ||
Identity owner = viewDefinition.getRunAsIdentity().get(); | ||
identity = Identity.from(owner) | ||
.withGroups(groupProvider.getGroups(owner.getUser())) | ||
.build(); | ||
// View owner does not need GRANT OPTION to grant access themselves | ||
if (!owner.getUser().equals(session.getIdentity().getUser())) { | ||
viewAccessControl = new ViewAccessControl(accessControl); | ||
} | ||
} | ||
|
||
Session viewSession = session.createViewSession(viewDefinition.getCatalog(), viewDefinition.getSchema(), identity, viewDefinition.getPath()); | ||
|
||
Statement viewDefinitionSql = sqlParser.createStatement(viewDefinition.getOriginalSql()); | ||
|
||
Analysis analysis = analyzerFactory.createAnalyzer( | ||
viewSession, | ||
parameters, | ||
viewAccessControl, | ||
ImmutableMap.of(), | ||
stateMachine.getWarningCollector(), | ||
stateMachine.getPlanOptimizersStatsCollector()) | ||
.analyze(viewDefinitionSql); | ||
|
||
Map<String, String> columnComments = | ||
viewDefinition.getColumns() | ||
.stream() | ||
.filter(viewColumn -> viewColumn.comment().isPresent()) | ||
.collect(toImmutableMap(ViewColumn::name, viewColumn -> viewColumn.comment().get())); | ||
kasiafi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
List<ViewColumn> columns = analysis.getOutputDescriptor(viewDefinitionSql) | ||
.getVisibleFields().stream() | ||
.map(field -> new ViewColumn(field.getName().get(), field.getType().getTypeId(), Optional.ofNullable(columnComments.get(field.getName().get())))) | ||
.collect(toImmutableList()); | ||
|
||
ViewDefinition viewDefinitionWithNewColumns = new ViewDefinition( | ||
viewDefinition.getOriginalSql(), | ||
viewDefinition.getCatalog(), | ||
viewDefinition.getSchema(), | ||
columns, | ||
viewDefinition.getComment(), | ||
viewDefinition.getRunAsIdentity(), | ||
viewDefinition.getPath()); | ||
|
||
metadata.refreshView(session, viewName, viewDefinitionWithNewColumns); | ||
|
||
return immediateVoidFuture(); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.