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 for row filtering #21913

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@
import com.facebook.presto.sql.tree.SubqueryExpression;
import com.facebook.presto.sql.tree.Table;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
Expand All @@ -70,6 +73,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -190,6 +194,9 @@ public class Analysis
// Keeps track of the subquery we are visiting, so we have access to base query information when processing materialized view status
private Optional<QuerySpecification> currentQuerySpecification = Optional.empty();

private final Multiset<RowFilterScopeEntry> rowFilterScopes = HashMultiset.create();
private final Map<NodeRef<Table>, List<Expression>> rowFilters = new LinkedHashMap<>();

public Analysis(@Nullable Statement root, Map<NodeRef<Parameter>, Expression> parameters, boolean isDescribe)
{
this.root = root;
Expand Down Expand Up @@ -994,6 +1001,32 @@ public Map<FunctionKind, Set<String>> getInvokedFunctions()
return functionMap.entrySet().stream().collect(toImmutableMap(Map.Entry::getKey, entry -> ImmutableSet.copyOf(entry.getValue())));
}

public boolean hasRowFilter(QualifiedObjectName table, String identity)
{
return rowFilterScopes.contains(new RowFilterScopeEntry(table, identity));
}

public void registerTableForRowFiltering(QualifiedObjectName table, String identity)
{
rowFilterScopes.add(new RowFilterScopeEntry(table, identity));
}

public void unregisterTableForRowFiltering(QualifiedObjectName table, String identity)
{
rowFilterScopes.remove(new RowFilterScopeEntry(table, identity));
}

public void addRowFilter(Table table, Expression filter)
{
rowFilters.computeIfAbsent(NodeRef.of(table), node -> new ArrayList<>())
.add(filter);
}

public List<Expression> getRowFilters(Table node)
{
return rowFilters.getOrDefault(NodeRef.of(node), ImmutableList.of());
}

@Immutable
public static final class Insert
{
Expand Down Expand Up @@ -1177,4 +1210,36 @@ public boolean isFromView()
return isFromView;
}
}

private static class RowFilterScopeEntry
{
private final QualifiedObjectName table;
private final String identity;

public RowFilterScopeEntry(QualifiedObjectName table, String identity)
{
this.table = requireNonNull(table, "table is null");
this.identity = requireNonNull(identity, "identity is null");
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RowFilterScopeEntry that = (RowFilterScopeEntry) o;
return table.equals(that.table) &&
identity.equals(that.identity);
}

@Override
public int hashCode()
{
return Objects.hash(table, identity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.facebook.presto.spi.security.ConnectorIdentity;
import com.facebook.presto.spi.security.PrestoPrincipal;
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.ViewExpression;

import javax.inject.Inject;

Expand Down Expand Up @@ -261,6 +262,11 @@ public void checkCanShowRoleGrants(ConnectorTransactionHandle transactionHandle,
}

@Override
public Optional<ViewExpression> getRowFilter(ConnectorTransactionHandle transactionHandle, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName)
{
return Optional.empty();
}

public void checkCanDropConstraint(ConnectorTransactionHandle transactionHandle, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName)
{
if (!allowDropConstraint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.facebook.presto.spi.security.PrestoPrincipal;
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.RoleGrant;
import com.facebook.presto.spi.security.ViewExpression;

import javax.inject.Inject;

Expand Down Expand Up @@ -411,6 +412,12 @@ public void checkCanShowRoleGrants(ConnectorTransactionHandle transactionHandle,
{
}

@Override
public Optional<ViewExpression> getRowFilter(ConnectorTransactionHandle transaction, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName)
{
return Optional.empty();
}

private boolean isAdmin(ConnectorTransactionHandle transaction, ConnectorIdentity identity, MetastoreContext metastoreContext)
{
SemiTransactionalHiveMetastore metastore = getMetastore(transaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.SystemAccessControl;
import com.facebook.presto.spi.security.SystemAccessControlFactory;
import com.facebook.presto.spi.security.ViewExpression;
import com.facebook.presto.transaction.TransactionManager;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.weakref.jmx.Managed;
Expand Down Expand Up @@ -737,6 +739,26 @@ public void checkCanShowRoleGrants(TransactionId transactionId, Identity identit
}

@Override
public List<ViewExpression> getRowFilters(TransactionId transactionId, Identity identity, AccessControlContext context, QualifiedObjectName tableName)
{
requireNonNull(transactionId, "transactionId is null");
requireNonNull(identity, "identity is null");
requireNonNull(tableName, "catalogName is null");

ImmutableList.Builder<ViewExpression> filters = ImmutableList.builder();
CatalogAccessControlEntry entry = getConnectorAccessControl(transactionId, tableName.getCatalogName());

if (entry != null) {
entry.getAccessControl().getRowFilter(entry.getTransactionHandle(transactionId), identity.toConnectorIdentity(tableName.getCatalogName()), context, toSchemaTableName(tableName))
.ifPresent(filters::add);
}

systemAccessControl.get().getRowFilter(identity, context, toCatalogSchemaTableName(tableName))
.ifPresent(filters::add);

return filters.build();
}

public void checkCanDropConstraint(TransactionId transactionId, Identity identity, AccessControlContext context, QualifiedObjectName tableName)
{
requireNonNull(identity, "identity is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.SystemAccessControl;
import com.facebook.presto.spi.security.SystemAccessControlFactory;
import com.facebook.presto.spi.security.ViewExpression;

import java.security.Principal;
import java.security.cert.X509Certificate;
Expand Down Expand Up @@ -214,6 +215,11 @@ public void checkCanRevokeTablePrivilege(Identity identity, AccessControlContext
}

@Override
public Optional<ViewExpression> getRowFilter(Identity identity, AccessControlContext context, CatalogSchemaTableName tableName)
{
return Optional.empty();
}

public void checkCanDropConstraint(Identity identity, AccessControlContext context, CatalogSchemaTableName table)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.SystemAccessControl;
import com.facebook.presto.spi.security.SystemAccessControlFactory;
import com.facebook.presto.spi.security.ViewExpression;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.airlift.units.Duration;
Expand Down Expand Up @@ -418,6 +419,12 @@ public void checkCanRevokeTablePrivilege(Identity identity, AccessControlContext
}

@Override

public Optional<ViewExpression> getRowFilter(Identity identity, AccessControlContext context, CatalogSchemaTableName tableName)
{
return Optional.empty();
}

public void checkCanDropConstraint(Identity identity, AccessControlContext context, CatalogSchemaTableName table)
{
if (!canAccessCatalog(identity, table.getCatalogName(), ALL)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.facebook.presto.spi.security.AllowAllAccessControl;
import com.facebook.presto.spi.security.Identity;
import com.facebook.presto.spi.security.ViewAccessControl;
import com.facebook.presto.spi.security.ViewExpression;
import com.facebook.presto.sql.ExpressionUtils;
import com.facebook.presto.sql.MaterializedViewUtils;
import com.facebook.presto.sql.SqlFormatterUtil;
Expand Down Expand Up @@ -201,7 +202,9 @@
import static com.facebook.presto.metadata.MetadataUtil.toSchemaTableName;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_ARGUMENTS;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_ROW_FILTER;
import static com.facebook.presto.spi.StandardErrorCode.NOT_FOUND;
import static com.facebook.presto.spi.StandardErrorCode.ROW_FILTER_TYPE_MISMATCH;
import static com.facebook.presto.spi.StandardWarningCode.PERFORMANCE_WARNING;
import static com.facebook.presto.spi.StandardWarningCode.REDUNDANT_ORDER_BY;
import static com.facebook.presto.spi.analyzer.AccessControlRole.TABLE_CREATE;
Expand Down Expand Up @@ -441,6 +444,10 @@ protected Scope visitInsert(Insert insert, Optional<Scope> scope)
tableColumnsMetadata.getTableHandle().get(),
insertColumns.stream().map(columnHandles::get).collect(toImmutableList())));

if (!accessControl.getRowFilters(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), targetTable).isEmpty()) {
throw new SemanticException(NOT_SUPPORTED, insert, "Insert into table with row filter is not supported");
}

return createAndAssignScope(insert, scope, Field.newUnqualified(insert.getLocation(), "rows", BIGINT));
}

Expand Down Expand Up @@ -591,6 +598,10 @@ protected Scope visitDelete(Delete node, Optional<Scope> scope)

analysis.addAccessControlCheckForTable(TABLE_DELETE, new AccessControlInfoForTable(accessControl, session.getIdentity(), session.getTransactionId(), session.getAccessControlContext(), tableName));

if (!accessControl.getRowFilters(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName).isEmpty()) {
throw new SemanticException(NOT_SUPPORTED, node, "Delete from table with row filter is not supported");
}

return createAndAssignScope(node, scope, Field.newUnqualified(node.getLocation(), "rows", BIGINT));
}

Expand Down Expand Up @@ -1328,6 +1339,16 @@ protected Scope visitTable(Table table, Optional<Scope> scope)
analysis.setColumn(field, columnHandle);
}

//add row filter
List<Field> outputFields = fields.build();

Scope accessControlScope = Scope.builder()
.withRelationType(RelationId.anonymous(), new RelationType(outputFields))
.build();

accessControl.getRowFilters(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), name)
.forEach(filter -> analyzeRowFilter(session.getIdentity().getUser(), table, name, accessControlScope, filter));

analysis.registerTable(table, tableHandle.get());

if (statement instanceof RefreshMaterializedView) {
Expand All @@ -1346,6 +1367,56 @@ protected Scope visitTable(Table table, Optional<Scope> scope)
return createAndAssignScope(table, scope, fields.build());
}

private void analyzeRowFilter(String currentIdentity, Table table, QualifiedObjectName name, Scope scope, ViewExpression filter)
{
if (analysis.hasRowFilter(name, currentIdentity)) {
throw new PrestoException(INVALID_ROW_FILTER, format("Row filter for '%s' is recursive", name));
}

Expression expression;
try {
expression = sqlParser.createExpression(filter.getExpression(), createParsingOptions(session));
}
catch (ParsingException e) {
throw new PrestoException(INVALID_ROW_FILTER, format("Invalid row filter for '%s': %s", name, e.getErrorMessage()), e);
}

analysis.registerTableForRowFiltering(name, currentIdentity);
ExpressionAnalysis expressionAnalysis;
try {
expressionAnalysis = ExpressionAnalyzer.analyzeExpression(
createViewSession(filter.getCatalog(), filter.getSchema(), new Identity(filter.getIdentity(), Optional.empty())), // TODO: path should be included in row filter
metadata,
accessControl,
sqlParser,
scope,
analysis,
expression,
warningCollector);
}
catch (PrestoException e) {
throw new PrestoException(e::getErrorCode, format("Invalid row filter for '%s': %s", name, e.getMessage()), e);
}
finally {
analysis.unregisterTableForRowFiltering(name, currentIdentity);
}

verifyNoAggregateWindowOrGroupingFunctions(analysis.getFunctionHandles(), functionAndTypeResolver, expression, format("Row filter for '%s'", name));

analysis.recordSubqueries(expression, expressionAnalysis);

Type actualType = expressionAnalysis.getType(expression);
if (!actualType.equals(BOOLEAN)) {
if (!metadata.getFunctionAndTypeManager().canCoerce(actualType, BOOLEAN)) {
throw new PrestoException(ROW_FILTER_TYPE_MISMATCH, format("Expected row filter for '%s' to be of type BOOLEAN, but was %s", name, actualType), null);
}

analysis.addCoercion(expression, BOOLEAN, metadata.getFunctionAndTypeManager().isTypeOnlyCoercion(actualType, BOOLEAN));
}

analysis.addRowFilter(table, expression);
}

private Optional<TableHandle> getTableHandle(TableColumnMetadata tableColumnsMetadata, Table table, QualifiedObjectName name, Optional<Scope> scope)
{
// Process table version AS OF expression
Expand Down Expand Up @@ -2826,21 +2897,7 @@ private RelationType analyzeView(Query query, QualifiedObjectName name, Optional
viewAccessControl = accessControl;
}

Session.SessionBuilder viewSessionBuilder = Session.builder(metadata.getSessionPropertyManager())
.setQueryId(session.getQueryId())
.setTransactionId(session.getTransactionId().orElse(null))
.setIdentity(identity)
.setSource(session.getSource().orElse(null))
.setCatalog(catalog.orElse(null))
.setSchema(schema.orElse(null))
.setTimeZoneKey(session.getTimeZoneKey())
.setLocale(session.getLocale())
.setRemoteUserAddress(session.getRemoteUserAddress().orElse(null))
.setUserAgent(session.getUserAgent().orElse(null))
.setClientInfo(session.getClientInfo().orElse(null))
.setStartTime(session.getStartTime());
session.getConnectorProperties().forEach((connectorId, properties) -> properties.forEach((k, v) -> viewSessionBuilder.setConnectionProperty(connectorId, k, v)));
Session viewSession = viewSessionBuilder.build();
Session viewSession = createViewSession(catalog, schema, identity);
StatementAnalyzer analyzer = new StatementAnalyzer(analysis, metadata, sqlParser, viewAccessControl, viewSession, warningCollector);
Scope queryScope = analyzer.analyze(query, Scope.create());
return queryScope.getRelationType().withAlias(name.getObjectName(), null);
Expand All @@ -2851,6 +2908,25 @@ private RelationType analyzeView(Query query, QualifiedObjectName name, Optional
}
}

private Session createViewSession(Optional<String> catalog, Optional<String> schema, Identity identity)
{
Session.SessionBuilder viewSessionBuilder = Session.builder(metadata.getSessionPropertyManager())
.setQueryId(session.getQueryId())
.setTransactionId(session.getTransactionId().orElse(null))
.setIdentity(identity)
.setSource(session.getSource().orElse(null))
.setCatalog(catalog.orElse(null))
.setSchema(schema.orElse(null))
.setTimeZoneKey(session.getTimeZoneKey())
.setLocale(session.getLocale())
.setRemoteUserAddress(session.getRemoteUserAddress().orElse(null))
.setUserAgent(session.getUserAgent().orElse(null))
.setClientInfo(session.getClientInfo().orElse(null))
.setStartTime(session.getStartTime());
session.getConnectorProperties().forEach((connectorId, properties) -> properties.forEach((k, v) -> viewSessionBuilder.setConnectionProperty(connectorId, k, v)));
return viewSessionBuilder.build();
}

private Query parseView(String view, QualifiedObjectName name, Node node)
{
try {
Expand Down
Loading
Loading