Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
package com.facebook.presto.sql.analyzer;

import com.facebook.airlift.log.Logger;
import com.facebook.presto.common.NotSupportedException;
import com.facebook.presto.common.QualifiedObjectName;
import com.facebook.presto.common.type.MapType;
import com.facebook.presto.spi.PrestoWarning;
import com.facebook.presto.spi.WarningCollector;
import com.facebook.presto.spi.analyzer.AccessControlInfo;
import com.facebook.presto.sql.tree.AliasedRelation;
import com.facebook.presto.sql.tree.Analyze;
import com.facebook.presto.sql.tree.Cube;
import com.facebook.presto.sql.tree.DefaultTraversalVisitor;
import com.facebook.presto.sql.tree.Delete;
import com.facebook.presto.sql.tree.DereferenceExpression;
import com.facebook.presto.sql.tree.Except;
import com.facebook.presto.sql.tree.ExistsPredicate;
Expand Down Expand Up @@ -51,6 +54,7 @@
import com.facebook.presto.sql.tree.TableSubquery;
import com.facebook.presto.sql.tree.Union;
import com.facebook.presto.sql.tree.Unnest;
import com.facebook.presto.sql.tree.Update;
import com.facebook.presto.sql.tree.Values;
import com.facebook.presto.sql.tree.With;
import com.facebook.presto.sql.tree.WithQuery;
Expand Down Expand Up @@ -477,6 +481,30 @@ protected Void visitFieldReference(FieldReference fieldReference, Context contex
return null;
}

//TODO : Add actual fixes for these nodes https://github.com/prestodb/presto/issues/26466
@Override
protected Void visitUpdate(Update node, Context context)
{
return notSupportedNode(node);
}

@Override
protected Void visitDelete(Delete node, Context context)
{
return notSupportedNode(node);
}

@Override
protected Void visitAnalyze(Analyze node, Context context)
{
return notSupportedNode(node);
}

public Void notSupportedNode(Node node)
{
throw new NotSupportedException(String.format("Utilized Column check not implemented for : %s", node));
}

private void handleRelation(Relation relation, Context context, Relation... children)
{
for (FieldId fieldId : context.getFieldIdsToExploreInRelation(relation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.intellij.lang.annotations.Language;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -644,6 +645,31 @@ public void testDefinerView()
QualifiedObjectName.valueOf("tpch.s1.t13"), ImmutableSet.of("y"))));
}

public void testFallsBackToAllColumnsForUnsupportedNodes()
{
// Only WHERE clause columns are captured for UPDATE
assertUtilizedTableColumns("UPDATE t1 set a=1 where b=2 and c=3",
ImmutableMap.of("AllowAllAccessControl:user", ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of("b", "c"))));

assertUtilizedTableColumns("UPDATE t1 set a=1 where b IN (select a from (select * from t6))",
ImmutableMap.of("AllowAllAccessControl:user",
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of("b"),
// All t6 columns selected for access control since we fall back
QualifiedObjectName.valueOf("tpch.s1.t6"), ImmutableSet.of("a", "b", "c", "d"))));

assertUtilizedTableColumns("DELETE FROM t1 where b=2 and c=3",
ImmutableMap.of("AllowAllAccessControl:user", ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of("b", "c"))));

assertUtilizedTableColumns("DELETE FROM t1 where b IN (select a from (select * from t6))",
ImmutableMap.of("AllowAllAccessControl:user",
ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of("b"),
// All t6 columns selected for access control since we fall back
QualifiedObjectName.valueOf("tpch.s1.t6"), ImmutableSet.of("a", "b", "c", "d"))));

assertUtilizedTableColumns("ANALYZE t1",
ImmutableMap.of("AllowAllAccessControl:user", ImmutableMap.of(QualifiedObjectName.valueOf("tpch.s1.t1"), ImmutableSet.of("a", "b", "c", "d"))));
}

private String extractAccessControlInfo(AccessControlInfo accessControlInfo)
{
return accessControlInfo.getAccessControl().getClass().getSimpleName() + ":" + accessControlInfo.getIdentity().getUser();
Expand All @@ -659,7 +685,32 @@ private void assertUtilizedTableColumns(@Language("SQL") String query, Map<Strin
Analyzer analyzer = createAnalyzer(session, metadata, WarningCollector.NOOP, query);
Statement statement = SQL_PARSER.createStatement(query);
Analysis analysis = analyzer.analyze(statement);
assertEquals(analysis.getUtilizedTableColumnReferences().entrySet().stream().collect(Collectors.toMap(entry -> extractAccessControlInfo(entry.getKey()), Map.Entry::getValue)), expected);

Map<String, Map<QualifiedObjectName, Set<String>>> mergedMap = analysis.getUtilizedTableColumnReferences()
.entrySet()
.stream()
.collect(Collectors.toMap(
entry -> extractAccessControlInfo(entry.getKey()),
entry -> entry.getValue().entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
innerEntry -> ImmutableSet.copyOf(innerEntry.getValue()),
(existing, replacement) -> ImmutableSet.<String>builder()
.addAll(existing)
.addAll(replacement)
.build())),
(existing, replacement) -> {
Map<QualifiedObjectName, Set<String>> merged = new HashMap<>(existing);
replacement.forEach((key, value) ->
merged.merge(key, value, (existingSet, newSet) ->
ImmutableSet.<String>builder()
.addAll(existingSet)
.addAll(newSet)
.build()));
return ImmutableMap.copyOf(merged);
}));

assertEquals(mergedMap, expected);
});
}
}
Loading