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

Enforce globally unique table locations #67

Closed
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 @@ -461,7 +461,8 @@ public List<PolarisEntityActiveRecord> lookupEntityActiveBatch(
entity.getParentId(),
entity.getName(),
entity.getTypeCode(),
entity.getSubTypeCode()));
entity.getSubTypeCode(),
entity.getLocation()));
}

@Override
Expand Down Expand Up @@ -690,4 +691,9 @@ public void rollback() {
session.getTransaction().rollback();
}
}

@Override
public boolean locationOverlapsWithExistingTableLike(@NotNull PolarisCallContext callContext, String location) {
return this.store.locationOverlapsWithExistingTableLike(localSession.get(), location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.polaris.extension.persistence.impl.eclipselink;

import io.polaris.core.PolarisDiagnostics;
import io.polaris.core.PolarisUtils;
import io.polaris.core.entity.PolarisBaseEntity;
import io.polaris.core.entity.PolarisEntitiesActiveKey;
import io.polaris.core.entity.PolarisEntityActiveRecord;
Expand All @@ -34,7 +35,10 @@
import jakarta.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -409,4 +413,58 @@ void deletePrincipalSecrets(EntityManager session, String clientId) {

session.remove(modelPrincipalSecrets);
}

boolean locationOverlapsWithExistingTableLike(EntityManager session, String location) {
if (location == null) {
return false;
}
diagnosticServices.check(session != null, "session_is_null");

List<String> directoryList = new ArrayList<>();
String queryString = """
SELECT
location
FROM
ModelEntityActive
WHERE
location IS NOT NULL
AND typeCode = :table_code
AND (
location LIKE CONCAT(:location, '%')
OR :location LIKE CONCAT(location, '%')
)
""";

int MAX_DIRECTORIES_IN_QUERY = 32;
Optional<List<String>> directories = PolarisUtils.pathToDirectories(location, MAX_DIRECTORIES_IN_QUERY);
if (directories.isPresent()) {
directoryList = directories.get();
queryString = IntStream.range(0, directoryList.size())
.mapToObj(i ->
"SELECT location " +
"FROM ModelEntityActive " +
"WHERE location IS NOT NULL " +
"AND typeCode = :table_code " +
"AND location = :directory_" + i
)
.collect(Collectors.joining(" UNION ALL "));

queryString += " UNION ALL " +
"SELECT location " +
"FROM ModelEntityActive " +
"WHERE location IS NOT NULL " +
"AND typeCode = :table_code " +
"AND location LIKE :locationPrefix";
}
Comment on lines +442 to +458
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not do a location IN (:directory_list) query? I think EclipseLink must support lists as parameters.


TypedQuery<String> query = session.createQuery(queryString, String.class)
.setParameter("table_code", PolarisEntityType.TABLE_LIKE.getCode())
.setParameter("locationPrefix", location + "%");

for (int i = 0; i < directoryList.size(); i++) {
query.setParameter("directory_" + i, directoryList.get(i));
}

return query.getResultStream().findFirst().isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.polaris.extension.persistence.impl.eclipselink.PolarisEclipseLinkMetaStoreSessionImpl;
import io.polaris.extension.persistence.impl.eclipselink.PolarisEclipseLinkStore;
import java.time.ZoneId;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class PolarisConfiguration {
public static final String ALLOW_NAMESPACE_LOCATION_OVERLAP = "ALLOW_NAMESPACE_LOCATION_OVERLAP";
public static final String ALLOW_EXTERNAL_METADATA_FILE_LOCATION =
"ALLOW_EXTERNAL_METADATA_FILE_LOCATION";
public static final String ENFORCE_GLOBALLY_UNIQUE_TABLE_LOCATIONS =
"ENFORCE_GLOBALLY_UNIQUE_TABLE_LOCATIONS";
public static final String ENFORCE_TABLE_LOCATIONS_INSIDE_NAMESPACE_LOCATIONS =
"ENFORCE_TABLE_LOCATIONS_INSIDE_NAMESPACE_LOCATIONS";

public static final String ALLOW_OVERLAPPING_CATALOG_URLS = "ALLOW_OVERLAPPING_CATALOG_URLS";

Expand All @@ -39,6 +43,8 @@ public class PolarisConfiguration {
public static final boolean DEFAULT_ALLOW_TABLE_LOCATION_OVERLAP = false;
public static final boolean DEFAULT_ALLOW_EXTERNAL_METADATA_FILE_LOCATION = false;
public static final boolean DEFAULT_ALLOW_NAMESPACE_LOCATION_OVERLAP = false;
public static final boolean DEFAULT_ENFORCE_GLOBALLY_UNIQUE_TABLE_LOCATIONS = false;
public static final boolean DEFAULT_ENFORCE_TABLE_LOCATIONS_INSIDE_NAMESPACE_LOCATIONS = true;

private PolarisConfiguration() {}
}
56 changes: 56 additions & 0 deletions polaris-core/src/main/java/io/polaris/core/PolarisUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Snowflake Computing Inc.
*
* 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.polaris.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/** A collection of utilities for polaris-core */
public class PolarisUtils {

/** Given some path, append a `/` if it doesn't already end with one */
public static String terminateWithSlash(String path) {
if (path == null) {
return null;
} else if (!path.endsWith("/")) {
return path + "/";
} else {
return path;
}
}

/**
* Given a path like `/a/b/c`, find all directories in the path such as [`/`, `/a/`, `/a/b/`,
* `/a/b/c`]
*/
public static Optional<List<String>> pathToDirectories(String path, int maxSegments) {
List<String> directories = new ArrayList<>();
String[] splitPath = path.split("/", -1);

if (splitPath.length - 2 > maxSegments) {
return Optional.empty();
} else {
StringBuilder currentPath = new StringBuilder();
for (int i = 0; i < splitPath.length - 1; i++) {
currentPath.append(splitPath[i]).append("/");
directories.add(currentPath.toString());
}
// Chop off the protocol:
return Optional.of(directories.subList(1, directories.size()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.polaris.core.PolarisUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -113,6 +114,15 @@ public String getProperties() {
return properties != null ? properties : EMPTY_MAP_STRING;
}

public String getLocation() {
if (getType() == PolarisEntityType.TABLE_LIKE) {
return PolarisUtils.terminateWithSlash(
getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION));
} else {
return null;
}
}

@JsonIgnore
public Map<String, String> getPropertiesAsMap() {
if (properties == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class PolarisEntityActiveRecord {
// code representing the subtype of that entity
private final int subTypeCode;

// location of the entity where applicable
private final String location;

public long getCatalogId() {
return catalogId;
}
Expand Down Expand Up @@ -66,6 +69,10 @@ public int getSubTypeCode() {
return subTypeCode;
}

public String getLocation() {
return location;
}

public PolarisEntitySubType getSubType() {
return PolarisEntitySubType.fromCode(this.subTypeCode);
}
Expand All @@ -77,13 +84,15 @@ public PolarisEntityActiveRecord(
@JsonProperty("parentId") long parentId,
@JsonProperty("name") String name,
@JsonProperty("typeCode") int typeCode,
@JsonProperty("subTypeCode") int subTypeCode) {
@JsonProperty("subTypeCode") int subTypeCode,
@JsonProperty("location") String location) {
this.catalogId = catalogId;
this.id = id;
this.parentId = parentId;
this.name = name;
this.typeCode = typeCode;
this.subTypeCode = subTypeCode;
this.location = location;
}

/** Constructor to create the object with provided entity */
Expand All @@ -94,6 +103,7 @@ public PolarisEntityActiveRecord(PolarisBaseEntity entity) {
this.typeCode = entity.getTypeCode();
this.name = entity.getName();
this.subTypeCode = entity.getSubTypeCode();
this.location = entity.getLocation();
}

@Override
Expand All @@ -106,12 +116,13 @@ public boolean equals(Object o) {
&& parentId == that.parentId
&& typeCode == that.typeCode
&& subTypeCode == that.subTypeCode
&& Objects.equals(name, that.name);
&& Objects.equals(name, that.name)
&& Objects.equals(location, that.location);
}

@Override
public int hashCode() {
return Objects.hash(catalogId, id, parentId, name, typeCode, subTypeCode);
return Objects.hash(catalogId, id, parentId, name, typeCode, subTypeCode, location);
}

@Override
Expand All @@ -130,6 +141,9 @@ public String toString() {
+ typeCode
+ ", subTypeCode="
+ subTypeCode
+ ", location='"
+ location
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1479,4 +1479,7 @@ PolarisMetaStoreManager.CachedEntryResult refreshCachedEntity(
@NotNull PolarisEntityType entityType,
long entityCatalogId,
long entityId);

boolean locationOverlapsWithExistingTableLike(
@NotNull PolarisCallContext callCtx, @NotNull String location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ private void persistNewEntity(
@NotNull PolarisMetaStoreSession ms,
@NotNull PolarisBaseEntity entity) {

if (entity.getType().getCode() == PolarisEntityType.TABLE_LIKE.getCode()) { // TODO remove
System.out.println("DEBUG");
}

// validate the entity type and subtype
callCtx.getDiagServices().checkNotNull(entity, "unexpected_null_entity");
callCtx
Expand Down Expand Up @@ -1163,6 +1167,8 @@ public Map<String, String> deserializeProperties(PolarisCallContext callCtx, Str
// get metastore we should be using
PolarisMetaStoreSession ms = callCtx.getMetaStore();

// TODO remove this comment

// need to run inside a read/write transaction
return ms.runInTransaction(
callCtx,
Expand Down Expand Up @@ -2410,4 +2416,12 @@ public Map<String, String> getInternalPropertyMap(
entityCatalogId,
entityId));
}

public boolean locationOverlapsWithExistingTableLike(
@NotNull PolarisCallContext callCtx, @NotNull String location) {
// get metastore we should be using
PolarisMetaStoreSession ms = callCtx.getMetaStore();

return ms.locationOverlapsWithExistingTableLike(callCtx, location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,14 @@ boolean hasChildren(

/** Rollback the current transaction */
void rollback();

/**
* Check if the given location overlaps with any existing entity
*
* @param callContext the polaris call context
* @param location the location to check existing entities against
* @return true if any entity's location overlaps with the specified location
*/
boolean locationOverlapsWithExistingTableLike(
@NotNull PolarisCallContext callContext, String location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ public PolarisEntityActiveRecord lookupEntityActive(
entity.getParentId(),
entity.getName(),
entity.getTypeCode(),
entity.getSubTypeCode());
entity.getSubTypeCode(),
entity.getLocation());
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -345,7 +346,8 @@ public List<PolarisEntityActiveRecord> lookupEntityActiveBatch(
entity.getParentId(),
entity.getName(),
entity.getTypeCode(),
entity.getSubTypeCode()));
entity.getSubTypeCode(),
entity.getLocation()));
}

@Override
Expand Down Expand Up @@ -565,4 +567,19 @@ PolarisStorageIntegration<T> loadPolarisStorageIntegration(
public void rollback() {
this.store.rollback();
}

@Override
public boolean locationOverlapsWithExistingTableLike(
@NotNull PolarisCallContext callContext, String location) {
return this.store
.getSliceEntitiesActive()
.valueExists(
polarisBaseEntity -> {
return location != null
&& polarisBaseEntity.getLocation() != null
&& polarisBaseEntity.getType() == PolarisEntityType.TABLE_LIKE
&& (polarisBaseEntity.getLocation().startsWith(location)
|| location.startsWith(polarisBaseEntity.getLocation()));
});
}
}
Loading
Loading