Skip to content

Commit

Permalink
Consider predicate columns for encryption
Browse files Browse the repository at this point in the history
In encryption we pass the list of columns that are being read to help with
getting the credentials only for those columns and allow for more granular
access control.

Uptil now, only the columns being requested in the select statement were
the only ones being considered. This commit adds support for columns in
the predicate as well. We now consider the predicate columns as well.

A special case to callout is `ROW`. Presto currently supports encryption
granularity of a subfield, however, it is non-trivial to get the exact
subfields being accessed for a `ROW`. This PR puts the whole `ROW` column
if any subfield is being used in the predicate. This is a limitation for
now.
  • Loading branch information
mayankgarg1990 committed Sep 18, 2020
1 parent 06d5f49 commit 4f86b23
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import com.facebook.presto.spi.WarningCollector;
import com.facebook.presto.spi.connector.ConnectorSplitManager;
import com.facebook.presto.spi.connector.ConnectorTransactionHandle;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
Expand All @@ -54,8 +56,10 @@
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.stream.Stream;

import static com.facebook.presto.hive.BackgroundHiveSplitLoader.BucketSplitInfo.createBucketSplitInfo;
import static com.facebook.presto.hive.HiveColumnHandle.ColumnType.REGULAR;
import static com.facebook.presto.hive.HiveColumnHandle.isPathColumnHandle;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_INVALID_METADATA;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_PARTITION_DROPPED_DURING_QUERY;
Expand All @@ -74,12 +78,15 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.Iterables.transform;
import static java.lang.Math.min;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.reducing;

public class HiveSplitManager
implements ConnectorSplitManager
Expand Down Expand Up @@ -235,7 +242,8 @@ public ConnectorSplitSource getSplits(
bucketHandle,
session,
splitSchedulingContext.getWarningCollector(),
layout.getRequestedColumns());
layout.getRequestedColumns(),
ImmutableSet.copyOf(layout.getPredicateColumns().values()));

HiveSplitLoader hiveSplitLoader = new BackgroundHiveSplitLoader(
table,
Expand Down Expand Up @@ -326,20 +334,23 @@ private Iterable<HivePartitionMetadata> getPartitionMetadata(
Optional<HiveBucketHandle> hiveBucketHandle,
ConnectorSession session,
WarningCollector warningCollector,
Optional<Set<HiveColumnHandle>> requestedColumns)
Optional<Set<HiveColumnHandle>> requestedColumns,
Set<HiveColumnHandle> predicateColumns)
{
if (hivePartitions.isEmpty()) {
return ImmutableList.of();
}

Optional<Set<HiveColumnHandle>> allRequestedColumns = mergeRequestedAndPredicateColumns(requestedColumns, predicateColumns);

if (hivePartitions.size() == 1) {
HivePartition firstPartition = getOnlyElement(hivePartitions);
if (firstPartition.getPartitionId().equals(UNPARTITIONED_ID)) {
return ImmutableList.of(new HivePartitionMetadata(
firstPartition,
Optional.empty(),
ImmutableMap.of(),
encryptionInformationProvider.getReadEncryptionInformation(session, table, requestedColumns)));
encryptionInformationProvider.getReadEncryptionInformation(session, table, allRequestedColumns)));
}
}

Expand All @@ -364,7 +375,7 @@ private Iterable<HivePartitionMetadata> getPartitionMetadata(
Optional<Map<String, EncryptionInformation>> encryptionInformationForPartitions = encryptionInformationProvider.getReadEncryptionInformation(
session,
table,
requestedColumns,
allRequestedColumns,
partitions);

ImmutableList.Builder<HivePartitionMetadata> results = ImmutableList.builder();
Expand Down Expand Up @@ -470,6 +481,37 @@ private Iterable<HivePartitionMetadata> getPartitionMetadata(
return concat(partitionBatches);
}

@VisibleForTesting
static Optional<Set<HiveColumnHandle>> mergeRequestedAndPredicateColumns(Optional<Set<HiveColumnHandle>> requestedColumns, Set<HiveColumnHandle> predicateColumns)
{
if (!requestedColumns.isPresent() || predicateColumns.isEmpty()) {
return requestedColumns;
}

return Optional.of(
Stream.concat(requestedColumns.get().stream(), predicateColumns.stream())
.filter(column -> column.getColumnType() == REGULAR)
.collect(groupingBy(
HiveColumnHandle::getName,
reducing((handle1, handle2) -> {
if (handle1.getRequiredSubfields().isEmpty()) {
return handle1;
}

if (handle2.getRequiredSubfields().isEmpty()) {
return handle2;
}

return (HiveColumnHandle) handle1.withRequiredSubfields(ImmutableList.copyOf(ImmutableSet.copyOf(
ImmutableList.<Subfield>builder().addAll(handle1.getRequiredSubfields()).addAll(handle2.getRequiredSubfields()).build())));
})))
.values()
.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toImmutableSet()));
}

static boolean isBucketCountCompatible(int tableBucketCount, int partitionBucketCount)
{
checkArgument(tableBucketCount > 0 && partitionBucketCount > 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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 com.facebook.presto.hive;

import com.facebook.presto.common.Subfield;
import com.facebook.presto.common.type.TypeSignature;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.Test;

import java.util.Optional;
import java.util.Set;

import static com.facebook.presto.common.type.StandardTypes.BIGINT;
import static com.facebook.presto.common.type.StandardTypes.VARCHAR;
import static com.facebook.presto.hive.HiveColumnHandle.ColumnType.REGULAR;
import static com.facebook.presto.hive.HiveSplitManager.mergeRequestedAndPredicateColumns;
import static com.facebook.presto.hive.HiveType.HIVE_LONG;
import static com.facebook.presto.hive.HiveType.HIVE_STRING;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class TestMergeRequestedAndPredicateColumns
{
private static final HiveType STRUCT_TYPE = HiveType.valueOf("struct<a:string,b:string>");
private static final HiveColumnHandle VARCHAR_COL = new HiveColumnHandle(
"varchar_col",
HIVE_STRING,
TypeSignature.parseTypeSignature(VARCHAR),
0,
REGULAR,
Optional.empty(),
Optional.empty());
private static final HiveColumnHandle BIGINT_COL = new HiveColumnHandle(
"bigint_col",
HIVE_LONG,
TypeSignature.parseTypeSignature(BIGINT),
0,
REGULAR,
Optional.empty(),
Optional.empty());
private static final HiveColumnHandle STRUCT_WITHOUT_SUBFIELD = new HiveColumnHandle(
"struct_col",
STRUCT_TYPE,
STRUCT_TYPE.getTypeSignature(),
0,
REGULAR,
Optional.empty(),
Optional.empty());
private static final HiveColumnHandle STRUCT_WITH_SUBFIELD_A = new HiveColumnHandle(
"struct_col",
STRUCT_TYPE,
STRUCT_TYPE.getTypeSignature(),
0,
REGULAR,
Optional.empty(),
ImmutableList.of(new Subfield("struct_col.a")),
Optional.empty());
private static final HiveColumnHandle STRUCT_WITH_SUBFIELD_B = new HiveColumnHandle(
"struct_col",
STRUCT_TYPE,
STRUCT_TYPE.getTypeSignature(),
0,
REGULAR,
Optional.empty(),
ImmutableList.of(new Subfield("struct_col.b")),
Optional.empty());
private static final HiveColumnHandle STRUCT_WITH_SUBFIELD_AB = new HiveColumnHandle(
"struct_col",
STRUCT_TYPE,
STRUCT_TYPE.getTypeSignature(),
0,
REGULAR,
Optional.empty(),
ImmutableList.of(new Subfield("struct_col.a"), new Subfield("struct_col.b")),
Optional.empty());

@Test
public void testAbsentRequestedCols()
{
Optional<Set<HiveColumnHandle>> result = mergeRequestedAndPredicateColumns(Optional.empty(), ImmutableSet.of(STRUCT_WITH_SUBFIELD_A));
assertFalse(result.isPresent());
}

@Test
public void testEmptyRequestedCols()
{
Optional<Set<HiveColumnHandle>> result = mergeRequestedAndPredicateColumns(Optional.of(ImmutableSet.of()), ImmutableSet.of(STRUCT_WITH_SUBFIELD_A));
assertTrue(result.isPresent());
assertEquals(result.get().size(), 1);
assertEquals(ImmutableList.copyOf(result.get()).get(0), STRUCT_WITH_SUBFIELD_A);
}

@Test
public void testEmptyPredicateCols()
{
Optional<Set<HiveColumnHandle>> result = mergeRequestedAndPredicateColumns(Optional.of(ImmutableSet.of(VARCHAR_COL, BIGINT_COL, STRUCT_WITHOUT_SUBFIELD)), ImmutableSet.of());
assertTrue(result.isPresent());
assertEquals(result.get().size(), 3);
assertEquals(result.get(), ImmutableSet.of(VARCHAR_COL, BIGINT_COL, STRUCT_WITHOUT_SUBFIELD));
}

@Test
public void testBothPresent()
{
Optional<Set<HiveColumnHandle>> result = mergeRequestedAndPredicateColumns(
Optional.of(ImmutableSet.of(VARCHAR_COL, BIGINT_COL)),
ImmutableSet.of(BIGINT_COL, STRUCT_WITHOUT_SUBFIELD));
assertTrue(result.isPresent());
assertEquals(result.get().size(), 3);
assertEquals(result.get(), ImmutableSet.of(VARCHAR_COL, BIGINT_COL, STRUCT_WITHOUT_SUBFIELD));
}

@Test
public void testStructs()
{
Optional<Set<HiveColumnHandle>> result = mergeRequestedAndPredicateColumns(
Optional.of(ImmutableSet.of(STRUCT_WITHOUT_SUBFIELD)),
ImmutableSet.of(STRUCT_WITH_SUBFIELD_A));
assertTrue(result.isPresent());
assertEquals(result.get().size(), 1);
assertEquals(result.get(), ImmutableSet.of(STRUCT_WITHOUT_SUBFIELD));

result = mergeRequestedAndPredicateColumns(
Optional.of(ImmutableSet.of(STRUCT_WITH_SUBFIELD_A)),
ImmutableSet.of(STRUCT_WITH_SUBFIELD_B));
assertTrue(result.isPresent());
assertEquals(result.get().size(), 1);
assertEquals(result.get(), ImmutableSet.of(STRUCT_WITH_SUBFIELD_AB));

result = mergeRequestedAndPredicateColumns(
Optional.of(ImmutableSet.of(STRUCT_WITH_SUBFIELD_A)),
ImmutableSet.of(STRUCT_WITH_SUBFIELD_AB));
assertTrue(result.isPresent());
assertEquals(result.get().size(), 1);
assertEquals(result.get(), ImmutableSet.of(STRUCT_WITH_SUBFIELD_AB));
}
}

0 comments on commit 4f86b23

Please sign in to comment.