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

Add property schema to CTElement types #920

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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 @@ -30,7 +30,7 @@ import org.opencypher.okapi.api.schema.PropertyGraphSchema
import org.opencypher.okapi.api.table.CypherRecords
import org.opencypher.okapi.api.types.{CTNode, CTRelationship}
import org.opencypher.okapi.api.value.CypherValue.CypherMap

import org.opencypher.okapi.api.schema.LabelPropertyMap._
/**
* A Property Graph as defined by the openCypher Property Graph Model.
*
Expand Down Expand Up @@ -62,20 +62,20 @@ trait PropertyGraph {
* Returns all nodes in this graph with the given [[org.opencypher.okapi.api.types.CTNode]] type.
*
* @param name field name for the returned nodes
* @param nodeCypherType node type used for selection
* @param knownLabels TODO: node type used for selection
* @param exactLabelMatch return only nodes that have exactly the given labels
* @return table of nodes of the specified type
*/
def nodes(name: String, nodeCypherType: CTNode = CTNode, exactLabelMatch: Boolean = false): CypherRecords
def nodes(name: String, knownLabels: Set[String] = Set.empty, exactLabelMatch: Boolean = false): CypherRecords

/**
* Returns all relationships in this graph with the given [[org.opencypher.okapi.api.types.CTRelationship]] type.
*
* @param name field name for the returned relationships
* @param relCypherType relationship type used for selection
* @param knownType TODO: relationship type used for selection
* @return table of relationships of the specified type
*/
def relationships(name: String, relCypherType: CTRelationship = CTRelationship): CypherRecords
def relationships(name: String, knownType: Option[String] = None): CypherRecords

/**
* Constructs the union of this graph and the argument graphs. Note that the argument graphs have to
Expand Down Expand Up @@ -108,7 +108,10 @@ trait PropertyGraph {
*
* @return patterns that the graph can provide
*/
def patterns: Set[Pattern] =
schema.labelCombinations.combos.map(c => NodePattern(CTNode(c))) ++
schema.relationshipTypes.map(r => RelationshipPattern(CTRelationship(r)))
def patterns: Set[Pattern] = {
val nodePatterns = schema.labelCombinations.combos.flatMap { c => schema.nodeType(c).map(NodePattern) }
val relPatterns = schema.relationshipTypes.flatMap { c => schema.relationshipType(c).map(RelationshipPattern) }

nodePatterns ++ relPatterns
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ case class ElementMapping(
}

pattern.elements.foreach {
case e@PatternElement(_, CTRelationship(types, _)) if types.size != 1 =>
case e@PatternElement(_, CTRelationship(types, _, _)) if types.size != 1 || types.alternatives.head.size != 1 =>
throw IllegalArgumentException(
s"A single implied type for element $e",
types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
package org.opencypher.okapi.api.io.conversion

import org.opencypher.okapi.api.graph._
import org.opencypher.okapi.api.types.CTNode
import org.opencypher.okapi.api.types.{CTNode, CypherType}

object NodeMappingBuilder {
/**
Expand Down Expand Up @@ -113,7 +113,8 @@ final case class NodeMappingBuilder(
copy(propertyMapping = updatedPropertyMapping)

override def build: ElementMapping = {
val pattern: NodePattern = NodePattern(CTNode(impliedNodeLabels))
// TODO: fill the node with property information
val pattern: NodePattern = NodePattern(CTNode.fromCombo(impliedNodeLabels, Map.empty[String, CypherType]))
val properties: Map[PatternElement, Map[String, String]] = Map(pattern.nodeElement -> propertyMapping)
val idKeys: Map[PatternElement, Map[IdKey, String]] = Map(pattern.nodeElement -> Map(SourceIdKey -> nodeIdKey))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
package org.opencypher.okapi.api.io.conversion

import org.opencypher.okapi.api.graph._
import org.opencypher.okapi.api.types.CTRelationship
import org.opencypher.okapi.api.types.{CTRelationship, CypherType}
import org.opencypher.okapi.impl.exception.IllegalArgumentException

object RelationshipMappingBuilder {
Expand Down Expand Up @@ -171,7 +171,8 @@ final case class RelationshipMappingBuilder(
override def build: ElementMapping = {
validate()

val pattern: RelationshipPattern = RelationshipPattern(CTRelationship(relType))
//TODO: review if we should fill with property information
val pattern: RelationshipPattern = RelationshipPattern(CTRelationship(relType, Map.empty[String, CypherType]))

val properties: Map[PatternElement, Map[String, String]] = Map(pattern.relElement -> propertyMapping)
val idKeys: Map[PatternElement, Map[IdKey, String]] = Map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ package object types {

val CTBoolean: CTUnion = CTUnion(Set[CypherType](CTTrue, CTFalse))

val CTElement: CTUnion = CTUnion(Set[CypherType](CTNode, CTRelationship))

val CTAny: CTUnion = CTUnion(Set[CypherType](CTAnyMaterial, CTNull))

val CTTemporalInstant: CTUnion = CTUnion(Set[CypherType](CTLocalDateTime, CTDate))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package org.opencypher.okapi.api.schema
import cats.instances.all._
import cats.syntax.semigroup._
import org.opencypher.okapi.api.schema.PropertyKeys.PropertyKeys
import org.opencypher.okapi.api.types.CypherType
import org.opencypher.okapi.api.types.{CTNode, CTVoid, CypherType}
import org.opencypher.okapi.api.types.CypherType.joinMonoid

object PropertyKeys {
Expand Down Expand Up @@ -71,6 +71,11 @@ object LabelPropertyMap {
*/
def properties(labels: Set[String]): PropertyKeys = map.getOrElse(labels, PropertyKeys.empty)


def cypherType(labels: Set[String]): Option[CTNode] = {
map.get(labels).map(CTNode.fromCombo(labels, _))
}

/**
* Merges this LabelPropertyMap with the given map. Property keys for label sets that exist in both maps are being
* merged, diverging types are being joined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package org.opencypher.okapi.api.schema
import org.opencypher.okapi.api.schema.LabelPropertyMap._
import org.opencypher.okapi.api.schema.PropertyKeys.PropertyKeys
import org.opencypher.okapi.api.schema.RelTypePropertyMap._
import org.opencypher.okapi.api.types.{CTRelationship, CypherType}
import org.opencypher.okapi.api.types.{CTNode, CTRelationship, CypherType}
import org.opencypher.okapi.impl.annotations.experimental
import org.opencypher.okapi.impl.schema.PropertyGraphSchemaImpl._
import org.opencypher.okapi.impl.schema.{ImpliedLabels, LabelCombinations, PropertyGraphSchemaImpl}
Expand Down Expand Up @@ -143,6 +143,8 @@ trait PropertyGraphSchema {
*/
def nodePropertyKeys(labelCombination: Set[String]): PropertyKeys

def nodeType(labelCombination: Set[String]): Option[CTNode] = labelPropertyMap.cypherType(labelCombination)

/**
* Returns some property type for a property given the known labels of a node.
* Returns none if this property does not appear on nodes with the given label combination.
Expand All @@ -152,7 +154,7 @@ trait PropertyGraphSchema {
* @param key property key
* @return Cypher type of the property on nodes with the given label combination
*/
def nodePropertyKeyType(knownLabels: Set[String], key: String): Option[CypherType]
def nodePropertyKeyType(knownLabels: Set[Set[String]], key: String): Option[CypherType]

/**
* Returns all combinations of labels that exist on a node in the graph.
Expand All @@ -162,7 +164,7 @@ trait PropertyGraphSchema {
/**
* Given a set of labels that a node definitely has, returns all combinations of labels that the node could possibly have.
*/
def combinationsFor(knownLabels: Set[String]): Set[Set[String]]
def combinationsFor(knownLabels: Set[Set[String]]): Set[Set[String]]

/**
* Returns property keys for the set of label combinations.
Expand All @@ -179,6 +181,8 @@ trait PropertyGraphSchema {
*/
def relationshipPropertyKeys(relType: String): PropertyKeys

def relationshipType(relType: String): Option[CTRelationship] = relTypePropertyMap.cypherType(relType)

/**
* Returns some property type for a property given the possible types of a relationship.
* Returns none if this property does not appear on relationships with one of the given types.
Expand Down Expand Up @@ -334,7 +338,7 @@ trait PropertyGraphSchema {
* @param knownLabels Specifies the labels that the node is guaranteed to have
* @return sub-schema for `knownLabels`
*/
private[opencypher] def forNode(knownLabels: Set[String]): PropertyGraphSchema
private[opencypher] def forNode(knownLabels: Set[Set[String]]): PropertyGraphSchema

/**
* Returns the sub-schema for `relType`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package org.opencypher.okapi.api.schema
import cats.instances.all._
import cats.syntax.semigroup._
import org.opencypher.okapi.api.schema.PropertyKeys.PropertyKeys
import org.opencypher.okapi.api.types.CypherType
import org.opencypher.okapi.api.types.{CTNode, CTRelationship, CypherType}
import org.opencypher.okapi.api.types.CypherType.joinMonoid

object RelTypePropertyMap {
Expand All @@ -47,6 +47,9 @@ object RelTypePropertyMap {

def properties(relKey: String): PropertyKeys = map.getOrElse(relKey, Map.empty)

def cypherType(relKey: String): Option[CTRelationship] =
map.get(relKey).map(CTRelationship(relKey, _))

def filterForRelTypes(relType: Set[String]): RelTypePropertyMap = map.filterKeys(relType.contains)

def ++(other: RelTypePropertyMap): RelTypePropertyMap = map |+| other
Expand Down
Loading