Skip to content

Instantly share code, notes, and snippets.

@tobias-johansson
Last active November 11, 2022 16:56
Show Gist options
  • Save tobias-johansson/130d680677d3e44eb5652286851c1c5b to your computer and use it in GitHub Desktop.
Save tobias-johansson/130d680677d3e44eb5652286851c1c5b to your computer and use it in GitHub Desktop.
LDBC SNB Fabric
CALL {
USE fabric.persons
MATCH (person:Person {id: $Person})-[:KNOWS]-(friend)
RETURN collect(friend.id) AS personIds
}
WITH *, [g in fabric.graphIds() WHERE g <> 0] AS gids
UNWIND gids AS gid
CALL {
USE fabric.graph(gid)
WITH personIds
UNWIND personIds AS personId
MATCH (:Person {id:personId})<-[:POST_HAS_CREATOR]-(post)-[:POST_HAS_TAG]->(tag)
WHERE $Date0 > post.creationDate
RETURN DISTINCT tag.id AS invalidTagId
}
WITH gids, personIds, collect(DISTINCT invalidTagId) AS invalidTagIds
UNWIND gids AS gid
CALL {
USE fabric.graph(gid)
WITH personIds, invalidTagIds
UNWIND personIds AS personId
MATCH (:Person {id:personId})<-[:POST_HAS_CREATOR]-(post)-[:POST_HAS_TAG]->(tag)
WHERE NOT tag.id IN invalidTagIds AND $Date0+($Duration*24*60*60*1000) > post.creationDate >= $Date0
RETURN tag.name AS tagName, count(post) AS shardPostCount
}
RETURN tagName, sum(shardPostCount) AS postCount
ORDER BY postCount DESC, tagName ASC
LIMIT 20
CALL {
USE fabric.persons
MATCH (person:Person {id:$Person})-[:KNOWS*1..2]-(friend)
WHERE NOT person=friend
WITH DISTINCT friend.id AS fid
RETURN collect(fid) AS fids
}
UNWIND [x IN fabric.graphIds() WHERE x <> 0 | x] AS gid
CALL {
USE fabric.graph(gid)
WITH fids
MATCH (friend:Person) WHERE friend.id IN fids
MATCH (knownTag:Tag {name:$Tag})
MATCH (friend)<-[:POST_HAS_CREATOR]-(post)-[:POST_HAS_TAG]->(knownTag)
WITH post, knownTag
MATCH (post)-[:POST_HAS_TAG]->(tag)
WHERE NOT tag=knownTag
WITH tag, count(post) AS postCount
RETURN tag.name AS tagName, postCount
ORDER BY postCount DESC, tagName ASC
}
RETURN tagName, sum(postCount) AS postCount
ORDER BY postCount DESC, tagName ASC
LIMIT 20
WITH *, [g in fabric.graphIds() WHERE g <> 0] AS gids
UNWIND gids AS gid
CALL {
USE fabric.graph(gid)
MATCH (:Person {id:$Person})<-[:POST_HAS_CREATOR|COMMENT_HAS_CREATOR]-(message),
(message)<-[like:LIKES_POST|LIKES_COMMENT]-(liker)
RETURN liker.id AS likerId, message, like.creationDate AS likeTime
}
WITH *
ORDER BY likeTime DESC, message.id ASC
WITH likerId,
head(collect(message)) AS globalTopMessage,
head(collect(likeTime)) AS globalTopLikeTime
CALL {
USE fabric.persons
WITH likerId
MATCH (liker:Person {id:likerId})
RETURN liker, not((liker)-[:KNOWS]-({id:$Person})) AS isNew
}
RETURN liker.id AS personId,
liker.firstName AS personFirstName,
liker.lastName AS personLastName,
globalTopLikeTime,
isNew,
globalTopMessage.id AS messageId,
coalesce(globalTopMessage.content,globalTopMessage.imageFile) AS messageContent,
globalTopMessage.creationDate AS messageCreationDate
ORDER BY globalTopLikeTime DESC, personId ASC
LIMIT 20
CALL {
USE fabric.persons
MATCH (person:Person {id: $Person})-[:KNOWS*1..2]-(friend)
WHERE NOT person = friend
WITH DISTINCT friend
WITH {id: friend.id, firstName: friend.firstName, lastName: friend.lastName} AS person
RETURN collect(person) AS persons
}
UNWIND [x IN fabric.graphIds() WHERE x <> 0 | x] AS gid
CALL {
USE fabric.graph(gid)
WITH persons
UNWIND persons AS person
MATCH (friend:Person {id: person.id})<-[:POST_HAS_CREATOR|COMMENT_HAS_CREATOR]-(message)
WHERE message.creationDate < $Date0
RETURN person, message
ORDER BY message.creationDate DESC, message.id ASC
LIMIT 20
}
RETURN message.id AS messageId,
coalesce(message.content,message.imageFile) AS messageContent,
message.creationDate AS messageCreationDate,
person.id AS personId,
person.firstName AS personFirstName,
person.lastName AS personLastName
ORDER BY messageCreationDate DESC, messageId ASC
LIMIT 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment