Skip to content
51 changes: 44 additions & 7 deletions database/src/main/java/io/hstore/db/query/AccessPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import io.hstore.db.schema.TypeDef;
import io.hstore.db.semantic.SemanticPlane;
import io.hstore.db.value.Value;
import io.hstore.engine.tree.Tree;
import io.hstore.engine.tree.TreeAlgebra;
import io.hstore.engine.Dictionary;
import io.hstore.engine.index.IndexValues;
import io.hstore.engine.index.PostingCursor;

import java.util.Arrays;
import java.util.BitSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.LongStream;

public sealed interface AccessPath {
Expand Down Expand Up @@ -52,21 +59,51 @@ public LongStream scan(Reader reader) {
}
}

record Incident(List<Long> atoms) implements AccessPath {
record Incident(List<Leg> legs) implements AccessPath {
public record Leg(long atom, List<String> roles) {
public Leg {
roles = List.copyOf(roles);
}

String describe() {
return "@" + atom + (roles.isEmpty() ? "" : " AS " + String.join("&", roles));
}
}

public Incident {
legs = List.copyOf(legs);
}

public String kind() {
return "incident";
}

public String describe() {
return "IncidentIntersection" + atoms.stream().map(atom -> "@" + atom).toList();
return "IncidentIntersection" + legs.stream().map(Leg::describe).toList();
}

public LongStream scan(Reader reader) {
if (!atoms.stream().allMatch(reader::visible)) {
if (legs.isEmpty() || !legs.stream().allMatch(leg -> reader.visible(leg.atom()))) {
return LongStream.empty();
}
List<Tree<?>> postings = atoms.stream().<Tree<?>>map(atom -> reader.view().incidentTree(atom)).toList();
return TreeAlgebra.intersectKeys(postings);
Dictionary dictionary = reader.database().engine().dictionary();
List<PostingCursor<IndexValues.Incident>> cursors = legs.stream()
.map(leg -> leg.roles().isEmpty()
? reader.view().incidentCursor(leg.atom())
: reader.view().incidentCursor(leg.atom(), accepting(dictionary, leg.roles())))
.toList();
return PostingCursor.intersect(cursors);
}

private static Predicate<IndexValues.Incident> accepting(Dictionary dictionary, List<String> roles) {
BitSet accepted = dictionary.roleSetsWithAll(roles);
return incident -> accepted.get(incident.roleSet());
}

static Incident merge(List<Leg> legs) {
Map<Long, Set<String>> roles = new LinkedHashMap<>();
legs.forEach(leg -> roles.computeIfAbsent(leg.atom(), _ -> new LinkedHashSet<>()).addAll(leg.roles()));
return new Incident(roles.entrySet().stream().map(entry -> new Leg(entry.getKey(), List.copyOf(entry.getValue()))).toList());
}
}

Expand Down
32 changes: 23 additions & 9 deletions database/src/main/java/io/hstore/db/query/Planner.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ Plan plan(Ast.Match match) {
candidates.add(new Candidate(catalogScan.path(), List.of(), catalogScan.rows(), catalogScan.cost() + VERIFY_WEIGHT * catalogScan.rows()));
type.ifPresent(found -> candidates.add(candidate(new AccessPath.TypeScan(found), List.of(), population, conjuncts)));
List<Candidate> exactParts = new ArrayList<>();
List<AccessPath.Incident.Leg> legs = new ArrayList<>();
List<Expr> incidentSources = new ArrayList<>();
for (Expr conjunct : conjuncts) {
accessPath(match, type, conjunct).or(() -> union(match, type, conjunct)).ifPresent(path -> {
Optional<AccessPath> direct = accessPath(match, type, conjunct);
if (direct.isPresent() && direct.get() instanceof AccessPath.Incident(List<AccessPath.Incident.Leg> found)) {
legs.addAll(found);
incidentSources.add(conjunct);
continue;
}
direct.or(() -> union(match, type, conjunct)).ifPresent(path -> {
List<Expr> consumed = exact(path, conjunct) ? List.of(conjunct) : List.of();
Candidate single = candidate(path, consumed, estimate(path, catalogSize), conjuncts);
candidates.add(single);
Expand All @@ -96,6 +104,12 @@ Plan plan(Ast.Match match) {
}
});
}
if (!legs.isEmpty()) {
AccessPath merged = AccessPath.Incident.merge(legs);
Candidate incident = candidate(merged, List.copyOf(incidentSources), estimate(merged, catalogSize), conjuncts);
candidates.add(incident);
exactParts.add(incident);
}
exactParts.sort(Comparator.comparingDouble(Candidate::rows));
for (int size = 2; size <= Math.min(MAX_INTERSECTED, exactParts.size()); size++) {
candidates.add(intersection(exactParts.subList(0, size), population, conjuncts));
Expand Down Expand Up @@ -168,8 +182,8 @@ private double estimate(AccessPath path, double population) {

private double unrecordedEstimate(AccessPath path, double population) {
return switch (path) {
case AccessPath.Incident(List<Long> atoms) -> {
double[] degrees = atoms.stream().mapToDouble(reader::degree).sorted().toArray();
case AccessPath.Incident(List<AccessPath.Incident.Leg> legs) -> {
double[] degrees = legs.stream().mapToDouble(leg -> reader.degree(leg.atom())).sorted().toArray();
double rows = degrees.length == 0 ? 0 : degrees[0];
for (int i = 1; i < degrees.length; i++) {
rows *= Math.min(1.0, degrees[i] / Math.max(1.0, population));
Expand Down Expand Up @@ -201,10 +215,11 @@ private double indexEstimate(AccessPath.IndexRange range) {
private Optional<AccessPath> accessPath(Ast.Match match, Optional<TypeDef> type, Expr conjunct) {
String variable = match.variable();
return switch (conjunct) {
case Expr.Contains(String target, List<Ast.Ref> members) when target.equals(variable) && match.edges() ->
Optional.of(new AccessPath.Incident(members.stream().map(resolver::applyAsLong).toList()));
case Expr.Has(String target, Ast.Ref member, Optional<String> _) when target.equals(variable) && match.edges() ->
Optional.of(new AccessPath.Incident(List.of(resolver.applyAsLong(member))));
case Expr.Contains(String target, List<Ast.Ref> members) when target.equals(variable) && match.edges() && !members.isEmpty() ->
Optional.of(AccessPath.Incident.merge(members.stream()
.map(member -> new AccessPath.Incident.Leg(resolver.applyAsLong(member), List.of())).toList()));
case Expr.Has(String target, Ast.Ref member, Optional<String> role) when target.equals(variable) && match.edges() ->
Optional.of(new AccessPath.Incident(List.of(new AccessPath.Incident.Leg(resolver.applyAsLong(member), role.stream().toList()))));
case Expr.In(String target, Ast.Ref edge) when target.equals(variable) ->
Optional.of(new AccessPath.Members(resolver.applyAsLong(edge)));
case Expr.Similar(String target, String text, int k, var consistency) when target.equals(variable) ->
Expand Down Expand Up @@ -259,8 +274,7 @@ private static Comparison flip(Comparison op) {
private static boolean exact(AccessPath path, Expr conjunct) {
return switch (path) {
case AccessPath.Semantic _, AccessPath.TypeScan _, AccessPath.AllAtoms _ -> false;
case AccessPath.Incident _ -> !(conjunct instanceof Expr.Has(String _, Ast.Ref _, Optional<String> role) && role.isPresent());
case AccessPath.Members _, AccessPath.IndexRange _, AccessPath.Fixed _ -> true;
case AccessPath.Incident _, AccessPath.Members _, AccessPath.IndexRange _, AccessPath.Fixed _ -> true;
case AccessPath.Union(List<AccessPath> parts) -> conjunct instanceof Expr.Or(List<Expr> terms) && terms.size() == parts.size()
&& IntStream.range(0, parts.size()).allMatch(i -> exact(parts.get(i), terms.get(i)));
case AccessPath.Intersection _ -> false;
Expand Down
25 changes: 25 additions & 0 deletions database/src/test/java/io/hstore/db/SecurityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,31 @@ void membershipsAcrossTenantsAreNotFollowed() {
}
}

@Test
void roleFilteredMatchesStayInsideTheTenant() {
try (Session alice = login("alice", "s3cret-pass"); Session bob = login("bob", "hunter2-pass")) {
alice.executeAll("INSERT NODE Patient 'p1' {mrn: 'A-1'} AS $p; INSERT EDGE Encounter MEMBERS ($p AS patient) AS $e;");
bob.executeAll("INSERT NODE Patient 'b1' {mrn: 'B-1'} AS $p; INSERT EDGE Encounter MEMBERS ($p AS patient) AS $e;");
long acmePatient = alice.variables().get("p");
long acmeEdge = alice.variables().get("e");
long globexPatient = bob.variables().get("p");
long globexEdge = bob.variables().get("e");
int patient = database.engine().dictionary().roleSet(List.of("patient"));
database.write(writer -> {
writer.transaction().upsert(globexEdge, acmePatient, _ -> new Incidence(acmePatient, patient, Weight.ONE,
Long.MIN_VALUE, Long.MAX_VALUE, 0, 0));
return null;
});
String query = "MATCH EDGE e:Encounter WHERE e HAS @" + acmePatient + " AS patient RETURN e";
assertEquals(List.of("@" + acmeEdge), alice.execute(query).rows().stream()
.map(row -> row.getFirst().render().split(" ")[0]).toList());
assertEquals("1", cell(alice.execute("MATCH EDGE e WHERE e HAS @" + acmePatient + " AS patient RETURN count(*)")));
assertEquals("0", cell(bob.execute("MATCH EDGE e:Encounter WHERE e HAS @" + acmePatient + " AS patient RETURN count(*)")));
assertEquals("0", cell(bob.execute("MATCH EDGE e:Encounter WHERE e HAS @" + acmePatient + " AS patient AND e HAS @"
+ globexPatient + " AS patient RETURN count(*)")));
}
}

@Test
void qualifyOnlyAcceptsVisibleMembers() {
try (Session alice = login("alice", "s3cret-pass"); Session bob = login("bob", "hunter2-pass")) {
Expand Down
189 changes: 189 additions & 0 deletions database/src/test/java/io/hstore/db/query/RoleRetrievalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// SPDX-FileCopyrightText: MetaCognition India Pvt Limited
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

package io.hstore.db.query;

import io.hstore.db.DatabaseOptions;
import io.hstore.db.HypergraphDatabase;
import io.hstore.db.Member;
import io.hstore.engine.EngineOptions;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class RoleRetrievalTest {

private static final List<String> ROLES = List.of("actor", "reviewer", "target");

@TempDir
Path directory;

HypergraphDatabase database;
Session session;

@BeforeEach
void open() {
database = HypergraphDatabase.open(directory, DatabaseOptions.defaults()
.withEngine(EngineOptions.defaults().withPageSize(4096)));
session = new Session(database);
session.executeAll("""
CREATE NODE TYPE Person;
CREATE SET EDGE TYPE Review ROLES (actor, reviewer, target);
""");
}

@AfterEach
void close() {
session.close();
database.close();
}

private record Condition(String text, Predicate<Map<Long, List<String>>> holds) {
}

private static List<Long> ids(QueryResult result) {
return result.rows().stream().map(row -> Long.parseLong(row.getFirst().render().substring(1).split(" ")[0])).sorted().toList();
}

private Condition has(long atom, String role) {
return role == null
? new Condition("e HAS @" + atom, members -> members.containsKey(atom))
: new Condition("e HAS @" + atom + " AS " + role,
members -> members.containsKey(atom) && members.get(atom).contains(role));
}

private Condition random(RandomGenerator random, List<Long> people) {
long a = people.get(random.nextInt(people.size()));
long b = people.get(random.nextInt(people.size()));
String role = ROLES.get(random.nextInt(ROLES.size()));
String other = ROLES.get(random.nextInt(ROLES.size()));
return switch (random.nextInt(6)) {
case 0 -> new Condition("e CONTAINS (@" + a + ", @" + b + ")", members -> members.containsKey(a) && members.containsKey(b));
case 1 -> has(a, null);
case 2, 3 -> has(a, role);
case 4 -> {
Condition left = has(a, role);
Condition right = has(b, other);
yield new Condition("(" + left.text() + " OR " + right.text() + ")", members -> left.holds().test(members) || right.holds().test(members));
}
default -> {
Condition inner = has(a, role);
yield new Condition("NOT " + inner.text(), members -> !inner.holds().test(members));
}
};
}

@Test
void roleFilteredMatchesAgreeWithMembers() {
RandomGenerator random = RandomGeneratorFactory.of("L64X128MixRandom").create(11);
List<Long> people = new ArrayList<>();
database.write(writer -> {
for (int i = 0; i < 30; i++) {
people.add(writer.node("Person", "p" + i));
}
for (int e = 0; e < 600; e++) {
long edge = writer.edge("Review");
int size = 1 + random.nextInt(4);
for (int k = 0; k < size; k++) {
long person = k == 0 && random.nextInt(3) > 0 ? people.get(random.nextInt(3)) : people.get(random.nextInt(people.size()));
List<String> roles = ROLES.stream().filter(_ -> random.nextInt(3) == 0).toList();
writer.add(edge, person, roles.toArray(String[]::new));
}
}
return null;
});
Map<Long, Map<Long, List<String>>> edges = database.read(reader -> reader.atoms("Review").boxed()
.collect(Collectors.toMap(edge -> edge, edge -> reader.members(edge)
.collect(Collectors.toMap(Member::atom, Member::roles)))));
for (int round = 0; round < 250; round++) {
List<Condition> conditions = new ArrayList<>();
for (int i = 0, n = 1 + random.nextInt(3); i < n; i++) {
conditions.add(random(random, people));
}
String where = conditions.stream().map(Condition::text).collect(Collectors.joining(" AND "));
List<Long> expected = edges.entrySet().stream()
.filter(entry -> conditions.stream().allMatch(condition -> condition.holds().test(entry.getValue())))
.map(Map.Entry::getKey).sorted().toList();
assertEquals(expected, ids(session.execute("MATCH EDGE e:Review WHERE " + where + " RETURN e")), where);
assertEquals(String.valueOf(expected.size()),
session.execute("MATCH EDGE e:Review WHERE " + where + " RETURN count(*)").rows().getFirst().getFirst().render(), where);
}
}

@Test
void roleRequirementsOnTheSameAnchorShareOneExactLeg() {
session.executeAll("""
INSERT NODE Person 'alice' AS $alice;
INSERT NODE Person 'bob' AS $bob;
INSERT NODE Person 'doc' AS $doc;
INSERT EDGE Review MEMBERS ($alice AS actor, $bob AS reviewer, $doc AS target) AS $e10;
INSERT EDGE Review MEMBERS ($alice AS actor|reviewer, $bob AS actor, $doc AS target) AS $e11;
INSERT EDGE Review MEMBERS ($alice AS reviewer, $bob AS reviewer) AS $e13;
""");
long alice = session.variables().get("alice");
long bob = session.variables().get("bob");
String query = "MATCH EDGE e:Review WHERE e HAS $alice AS actor AND e HAS $bob AS reviewer";
String plan = session.execute("EXPLAIN " + query).message();
assertTrue(plan.startsWith("IncidentIntersection[@" + alice + " AS actor, @" + bob + " AS reviewer]"), plan);
assertFalse(plan.contains("Verify"), plan);
assertEquals(List.of(session.variables().get("e10")), ids(session.execute(query + " RETURN e")));
String both = "MATCH EDGE e:Review WHERE e HAS $alice AS actor AND e HAS $alice AS reviewer AND e CONTAINS ($alice)";
String merged = session.execute("EXPLAIN " + both).message();
assertTrue(merged.startsWith("IncidentIntersection[@" + alice + " AS actor&reviewer]"), merged);
assertEquals(List.of(session.variables().get("e11")), ids(session.execute(both + " RETURN e")));
assertEquals(List.of(), ids(session.execute("MATCH EDGE e:Review WHERE e HAS $alice AS approver RETURN e")));
}

@Test
void transactionsMatchTheirOwnRoles() {
session.executeAll("""
INSERT NODE Person 'alice' AS $alice;
INSERT NODE Person 'bob' AS $bob;
INSERT EDGE Review MEMBERS ($alice AS actor) AS $committed;
BEGIN;
INSERT EDGE Review MEMBERS ($alice AS actor, $bob AS target) AS $pending;
""");
List<Long> inside = ids(session.execute("MATCH EDGE e:Review WHERE e HAS $alice AS actor RETURN e"));
assertEquals(List.of(session.variables().get("committed"), session.variables().get("pending")), inside);
session.execute("ROLLBACK");
assertEquals(List.of(session.variables().get("committed")),
ids(session.execute("MATCH EDGE e:Review WHERE e HAS $alice AS actor RETURN e")));
}

@Test
void olderGenerationsAndRestartsKeepRoleMatches() {
session.executeAll("""
INSERT NODE Person 'alice' AS $alice;
INSERT EDGE Review MEMBERS ($alice AS actor) AS $first;
""");
long alice = session.variables().get("alice");
long first = session.variables().get("first");
long generation = database.read(reader -> reader.generation());
session.execute("INSERT EDGE Review MEMBERS ($alice AS actor|target) AS $second");
long second = session.variables().get("second");
assertEquals(List.of(first), ids(session.execute("AT GENERATION " + generation + " MATCH EDGE e:Review WHERE e HAS $alice AS actor RETURN e")));
assertEquals(List.of(), ids(session.execute("AT GENERATION " + generation + " MATCH EDGE e:Review WHERE e HAS $alice AS target RETURN e")));
assertEquals(List.of(first, second), ids(session.execute("MATCH EDGE e:Review WHERE e HAS $alice AS actor RETURN e")));
session.close();
database.close();
database = HypergraphDatabase.open(directory, DatabaseOptions.defaults().withEngine(EngineOptions.defaults().withPageSize(4096)));
session = new Session(database);
assertEquals(List.of(second), ids(session.execute("MATCH EDGE e:Review WHERE e HAS @" + alice + " AS target RETURN e")));
assertEquals(List.of(second), ids(session.execute("MATCH EDGE e:Review WHERE e HAS @" + alice + " AS actor AND e HAS @" + alice + " AS target RETURN e")));
}
}
Loading
Loading