use of com.facebook.presto.common.QualifiedObjectName in project presto by prestodb.
the class DropColumnTask method execute.
@Override
public ListenableFuture<?> execute(DropColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTable());
Optional<TableHandle> tableHandleOptional = metadata.getTableHandle(session, tableName);
if (!tableHandleOptional.isPresent()) {
if (!statement.isTableExists()) {
throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
}
return immediateFuture(null);
}
Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
if (optionalMaterializedView.isPresent()) {
if (!statement.isTableExists()) {
throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, and drop column is not supported", tableName);
}
return immediateFuture(null);
}
TableHandle tableHandle = tableHandleOptional.get();
String column = statement.getColumn().getValue().toLowerCase(ENGLISH);
accessControl.checkCanDropColumn(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName);
ColumnHandle columnHandle = metadata.getColumnHandles(session, tableHandle).get(column);
if (columnHandle == null) {
if (!statement.isColumnExists()) {
throw new SemanticException(MISSING_COLUMN, statement, "Column '%s' does not exist", column);
}
return immediateFuture(null);
}
if (metadata.getColumnMetadata(session, tableHandle, columnHandle).isHidden()) {
throw new SemanticException(NOT_SUPPORTED, statement, "Cannot drop hidden column");
}
if (metadata.getTableMetadata(session, tableHandle).getColumns().stream().filter(info -> !info.isHidden()).count() <= 1) {
throw new SemanticException(NOT_SUPPORTED, statement, "Cannot drop the only column in a table");
}
metadata.dropColumn(session, tableHandle, columnHandle);
return immediateFuture(null);
}
use of com.facebook.presto.common.QualifiedObjectName in project presto by prestodb.
the class DropViewTask method execute.
@Override
public ListenableFuture<?> execute(DropView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
Optional<ViewDefinition> view = metadata.getView(session, name);
if (!view.isPresent()) {
if (!statement.isExists()) {
throw new SemanticException(MISSING_TABLE, statement, "View '%s' does not exist", name);
}
return immediateFuture(null);
}
accessControl.checkCanDropView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), name);
metadata.dropView(session, name);
return immediateFuture(null);
}
use of com.facebook.presto.common.QualifiedObjectName in project presto by prestodb.
the class DropTableTask method execute.
@Override
public ListenableFuture<?> execute(DropTable statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTableName());
Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
if (!tableHandle.isPresent()) {
if (!statement.isExists()) {
throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
}
return immediateFuture(null);
}
Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
if (optionalMaterializedView.isPresent()) {
if (!statement.isExists()) {
throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, not a table. Use DROP MATERIALIZED VIEW to drop.", tableName);
}
return immediateFuture(null);
}
accessControl.checkCanDropTable(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName);
metadata.dropTable(session, tableHandle.get());
return immediateFuture(null);
}
use of com.facebook.presto.common.QualifiedObjectName in project presto by prestodb.
the class Analysis method buildMaterializedViewAccessControl.
/**
* For a query on materialized view, only check the actual required access controls for its base tables. For the materialized view,
* will not check access control by replacing with AllowAllAccessControl.
*/
private Map<AccessControlInfo, Map<QualifiedObjectName, Set<String>>> buildMaterializedViewAccessControl(Map<AccessControlInfo, Map<QualifiedObjectName, Set<String>>> tableColumnReferences) {
if (!(getStatement() instanceof Query) || materializedViews.isEmpty()) {
return tableColumnReferences;
}
Map<AccessControlInfo, Map<QualifiedObjectName, Set<String>>> newTableColumnReferences = new LinkedHashMap<>();
tableColumnReferences.forEach((accessControlInfo, references) -> {
AccessControlInfo allowAllAccessControlInfo = new AccessControlInfo(new AllowAllAccessControl(), accessControlInfo.getIdentity());
Map<QualifiedObjectName, Set<String>> newAllowAllReferences = newTableColumnReferences.getOrDefault(allowAllAccessControlInfo, new LinkedHashMap<>());
Map<QualifiedObjectName, Set<String>> newOtherReferences = new LinkedHashMap<>();
references.forEach((table, columns) -> {
if (materializedViews.containsKey(table)) {
newAllowAllReferences.computeIfAbsent(table, key -> new HashSet<>()).addAll(columns);
} else {
newOtherReferences.put(table, columns);
}
});
if (!newAllowAllReferences.isEmpty()) {
newTableColumnReferences.put(allowAllAccessControlInfo, newAllowAllReferences);
}
if (!newOtherReferences.isEmpty()) {
newTableColumnReferences.put(accessControlInfo, newOtherReferences);
}
});
return newTableColumnReferences;
}
use of com.facebook.presto.common.QualifiedObjectName in project presto by prestodb.
the class MaterializedViewQueryOptimizer method visitQuerySpecification.
@Override
protected Node visitQuerySpecification(QuerySpecification node, Void context) {
if (!node.getFrom().isPresent()) {
throw new IllegalStateException("Query with no From clause is not rewritable by materialized view");
}
Relation relation = node.getFrom().get();
if (relation instanceof AliasedRelation) {
removablePrefix = Optional.of(((AliasedRelation) relation).getAlias());
relation = ((AliasedRelation) relation).getRelation();
}
if (!(relation instanceof Table)) {
throw new SemanticException(NOT_SUPPORTED, node, "Relation other than Table is not supported in query optimizer");
}
Table baseTable = (Table) relation;
if (!removablePrefix.isPresent()) {
removablePrefix = Optional.of(new Identifier(baseTable.getName().toString()));
}
if (node.getGroupBy().isPresent()) {
ImmutableSet.Builder<Expression> expressionsInGroupByBuilder = ImmutableSet.builder();
for (GroupingElement element : node.getGroupBy().get().getGroupingElements()) {
element = removeGroupingElementPrefix(element, removablePrefix);
Optional<Set<Expression>> groupByOfMaterializedView = materializedViewInfo.getGroupBy();
if (groupByOfMaterializedView.isPresent()) {
for (Expression expression : element.getExpressions()) {
if (!groupByOfMaterializedView.get().contains(expression) || !materializedViewInfo.getBaseToViewColumnMap().containsKey(expression)) {
throw new IllegalStateException(format("Grouping element %s is not present in materialized view groupBy field", element));
}
}
}
expressionsInGroupByBuilder.addAll(element.getExpressions());
}
expressionsInGroupBy = Optional.of(expressionsInGroupByBuilder.build());
}
// TODO: Add HAVING validation to the validator https://github.com/prestodb/presto/issues/16406
if (node.getHaving().isPresent()) {
throw new SemanticException(NOT_SUPPORTED, node, "Having clause is not supported in query optimizer");
}
if (materializedViewInfo.getWhereClause().isPresent()) {
if (!node.getWhere().isPresent()) {
throw new IllegalStateException("Query with no where clause is not rewritable by materialized view with where clause");
}
QualifiedObjectName baseTableName = createQualifiedObjectName(session, baseTable, baseTable.getName());
Optional<TableHandle> tableHandle = metadata.getTableHandle(session, baseTableName);
if (!tableHandle.isPresent()) {
throw new SemanticException(MISSING_TABLE, node, "Table does not exist");
}
ImmutableList.Builder<Field> fields = ImmutableList.builder();
for (ColumnHandle columnHandle : metadata.getColumnHandles(session, tableHandle.get()).values()) {
ColumnMetadata columnMetadata = metadata.getColumnMetadata(session, tableHandle.get(), columnHandle);
fields.add(Field.newUnqualified(materializedViewInfo.getWhereClause().get().getLocation(), columnMetadata.getName(), columnMetadata.getType()));
}
Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields.build())).build();
// Given base query's filter condition and materialized view's filter condition, the goal is to check if MV's filters contain Base's filters (Base implies MV).
// Let base query's filter condition be A, and MV's filter condition be B.
// One way to evaluate A implies B is to evaluate logical expression A^~B and check if the output domain is none.
// If the resulting domain is none, then A^~B is false. Thus A implies B.
// For more information and examples: https://fb.quip.com/WwmxA40jLMxR
// TODO: Implement method that utilizes external SAT solver libraries. https://github.com/prestodb/presto/issues/16536
RowExpression materializedViewWhereCondition = convertToRowExpression(materializedViewInfo.getWhereClause().get(), scope);
RowExpression baseQueryWhereCondition = convertToRowExpression(node.getWhere().get(), scope);
RowExpression rewriteLogicExpression = and(baseQueryWhereCondition, call(baseQueryWhereCondition.getSourceLocation(), "not", new FunctionResolution(metadata.getFunctionAndTypeManager()).notFunction(), materializedViewWhereCondition.getType(), materializedViewWhereCondition));
RowExpression disjunctiveNormalForm = logicalRowExpressions.convertToDisjunctiveNormalForm(rewriteLogicExpression);
ExtractionResult<VariableReferenceExpression> result = domainTranslator.fromPredicate(session.toConnectorSession(), disjunctiveNormalForm, BASIC_COLUMN_EXTRACTOR);
if (!result.getTupleDomain().equals(TupleDomain.none())) {
throw new IllegalStateException("View filter condition does not contain base query's filter condition");
}
}
return new QuerySpecification((Select) process(node.getSelect(), context), node.getFrom().map(from -> (Relation) process(from, context)), node.getWhere().map(where -> (Expression) process(where, context)), node.getGroupBy().map(groupBy -> (GroupBy) process(groupBy, context)), node.getHaving().map(having -> (Expression) process(having, context)), node.getOrderBy().map(orderBy -> (OrderBy) process(orderBy, context)), node.getOffset(), node.getLimit());
}
Aggregations