Skip to content

Commit

Permalink
predicate problems
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-maynard committed Sep 10, 2024
1 parent 3fc5939 commit 850baf4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.xml.parsers.DocumentBuilder;
Expand All @@ -47,6 +46,8 @@
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import jakarta.persistence.criteria.Predicate;
import org.apache.polaris.core.PolarisCallContext;
import org.apache.polaris.core.catalog.pagination.EntityIdPageToken;
import org.apache.polaris.core.catalog.pagination.PageToken;
Expand Down Expand Up @@ -521,15 +522,15 @@ public List<PolarisEntityActiveRecord> lookupEntityActiveBatch(
long parentId,
@NotNull PolarisEntityType entityType,
@NotNull PageToken pageToken,
@NotNull Predicate<PolarisBaseEntity> entityFilter) {
@NotNull List<Predicate> predicates) {
// full range scan under the parent for that type
return listActiveEntities(
callCtx,
catalogId,
parentId,
entityType,
pageToken,
entityFilter,
predicates,
entity ->
new PolarisEntityActiveRecord(
entity.getCatalogId(),
Expand All @@ -547,15 +548,14 @@ public List<PolarisEntityActiveRecord> lookupEntityActiveBatch(
long parentId,
@NotNull PolarisEntityType entityType,
@NotNull PageToken pageToken,
@NotNull Predicate<PolarisBaseEntity> entityFilter,
@NotNull List<Predicate> predicates,
@NotNull Function<PolarisBaseEntity, T> transformer) {
List<T> data =
this.store
.lookupFullEntitiesActive(
localSession.get(), catalogId, parentId, entityType, pageToken)
localSession.get(), catalogId, parentId, entityType, pageToken, predicates)
.stream()
.map(ModelEntity::toEntity)
.filter(entityFilter)
.map(transformer)
.collect(Collectors.toList());
return pageToken.buildNextPage(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.apache.polaris.core.PolarisDiagnostics;
import org.apache.polaris.core.catalog.pagination.EntityIdPageToken;
import org.apache.polaris.core.catalog.pagination.PageToken;
Expand Down Expand Up @@ -281,34 +286,37 @@ List<ModelEntity> lookupFullEntitiesActive(
long catalogId,
long parentId,
@NotNull PolarisEntityType entityType,
@NotNull PageToken pageToken) {
@NotNull PageToken pageToken,
@NotNull List<Predicate> additionalPredicates) {

diagnosticServices.check(session != null, "session_is_null");
diagnosticServices.check(
(pageToken instanceof EntityIdPageToken || pageToken instanceof ReadEverythingPageToken),
"unexpected_page_token");

// Currently check against ENTITIES not joining with ENTITIES_ACTIVE
String hql =
"SELECT m from ModelEntity m "
+ " where m.catalogId=:catalogId and m.parentId=:parentId and m.typeCode=:typeCode and m.id > :tokenId";
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<ModelEntity> cq = cb.createQuery(ModelEntity.class);
Root<ModelEntity> root = cq.from(ModelEntity.class);

// Base predicates
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(root.get("catalogId"), catalogId));
predicates.add(cb.equal(root.get("parentId"), parentId));
predicates.add(cb.equal(root.get("typeCode"), entityType.getCode()));
predicates.addAll(additionalPredicates);

if (pageToken instanceof EntityIdPageToken) {
hql += " ORDER BY m.id ASC";
long tokenId = ((EntityIdPageToken) pageToken).id;
predicates.add(cb.greaterThan(root.get("id"), tokenId));
cq.orderBy(cb.asc(root.get("id")));
}

TypedQuery<ModelEntity> query =
session
.createQuery(hql, ModelEntity.class)
.setParameter("catalogId", catalogId)
.setParameter("parentId", parentId)
.setParameter("typeCode", entityType.getCode())
.setParameter("tokenId", -1L);
cq.select(root).where(cb.and(predicates.toArray(new Predicate[0])));

TypedQuery<ModelEntity> query = session.createQuery(cq);

if (pageToken instanceof EntityIdPageToken) {
query =
query
.setParameter("tokenId", ((EntityIdPageToken) pageToken).id)
.setMaxResults(pageToken.pageSize);
query.setMaxResults(pageToken.pageSize);
}

return query.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import jakarta.persistence.criteria.Predicate;
import org.apache.polaris.core.PolarisCallContext;
import org.apache.polaris.core.catalog.pagination.PageToken;
import org.apache.polaris.core.catalog.pagination.PolarisPage;
Expand Down Expand Up @@ -322,8 +323,8 @@ PolarisPage<PolarisEntityActiveRecord> listActiveEntities(
* @param catalogId catalog id for that entity, NULL_ID if the entity is top-level
* @param parentId id of the parent, can be the special 0 value representing the root entity
* @param entityType type of entities to list
* @param entityFilter the filter to be applied to each entity. Only entities where the predicate
* returns true are returned in the list
* @param predicates predicates to be applied to each entity. Only entities where the predicates
* all return true are returned in the list
* @return the list of entities for which the predicate returns true
*/
@NotNull
Expand All @@ -333,7 +334,7 @@ PolarisPage<PolarisEntityActiveRecord> listActiveEntities(
long parentId,
@NotNull PolarisEntityType entityType,
@NotNull PageToken pageToken,
@NotNull Predicate<PolarisBaseEntity> entityFilter);
@NotNull List<Predicate> predicates);

/**
* List active entities where some predicate returns true and transform the entities with a
Expand All @@ -344,8 +345,8 @@ PolarisPage<PolarisEntityActiveRecord> listActiveEntities(
* @param parentId id of the parent, can be the special 0 value representing the root entity
* @param entityType type of entities to list
* @param pageToken the pagination token to use
* @param entityFilter the filter to be applied to each entity. Only entities where the predicate
* returns true are returned in the list
* @param predicates predicates to be applied to each entity. Only entities where the predicates
* all return true are returned in the list
* @param transformer the transformation function applied to the {@link PolarisBaseEntity} before
* returning
* @return the list of entities for which the predicate returns true
Expand All @@ -357,7 +358,7 @@ <T> PolarisPage<T> listActiveEntities(
long parentId,
@NotNull PolarisEntityType entityType,
@NotNull PageToken pageToken,
@NotNull Predicate<PolarisBaseEntity> entityFilter,
@NotNull List<Predicate> predicates,
@NotNull Function<PolarisBaseEntity, T> transformer);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public List<PolarisEntityActiveRecord> lookupEntityActiveBatch(
long parentId,
@NotNull PolarisEntityType entityType,
@NotNull PageToken pageToken,
@NotNull Predicate<PolarisBaseEntity> entityFilter) {
@NotNull List<Predicate> predicates) {
// full range scan under the parent for that type
return listActiveEntities(
callCtx,
Expand Down

0 comments on commit 850baf4

Please sign in to comment.