Chapter 3. Conditions

Table of Contents

Attribute Value Conditions
Existence Conditions
Evaluating Conditions
Evaluating attribute value conditions
Evaluating existence conditions
Complex Conditions
Implementation in Proximity
Case sensitivity
Attribute values in Proximity
Comparison operators
Complex conditions in Proximity
Efficiency considerations
Summary

Being able to only match structure is of limited usefulness in querying the database. We must also be able to consider the semantic information contained in object and link attributes when defining and matching queries. Conditions allow queries to move beyond purely structural matches to consider database content. Specifically, conditions serve to restrict the number of query matches by placing requirements on the attributes of an object or link in the database.

Conditions are commonly, but certainly not exclusively, used to enforce type restrictions in query matches. However, QGraph only tests and compares attribute values; it makes no assumptions about how type information is stored. It’s up to you to specify the appropriate attribute to examine.

QGraph provides two types of conditions:

Attribute Value Conditions

To see attribute value conditions in action, let’s revisit the database originally shown in Figure 2.2 and repeated below. This database contains information on authors, books, movies, and the relationships among these entities.

Database fragment [Basics_DB01.xml]

Figure 3.1. Database fragment [Basics_DB01.xml]


In this database, object type information is represented as the value of the ObjType attribute and link type information is represented as the value of the LinkType attribute. The query in Figure 3.2 identifies all author-book pairs in the data by matching database structures that connect person objects to book objects via author-of links.

Using conditions for type matching [Basics_DB01_Q02.qg2.xml]

Figure 3.2. Using conditions for type matching [Basics_DB01_Q02.qg2.xml]


The text under each query element specifies a condition on that element. The author vertex has the condition ObjType = person. Only objects having the value person for the ObjType attribute will match this vertex. Similarly, the query will only match links whose LinkType attribute have the value author-of.

Once we add conditions, it’s common to use semantically meaningful names for the vertices and edges in a query. Such names usually reflect the restrictions imposed by the conditions, such as we see above. Although these names may duplicate required attribute values, it is the condition, not the label, that determines query behavior. Executing this query on the database fragment above yields the results shown in Figure 3.3.

Results of using conditions for type matching

Figure 3.3. Results of using conditions for type matching


Compare these results to the purely structural matches shown in Figure 2.4. Adding conditions to the query limits the results to only those subgraphs that have the required attribute values. Specifically, the new query requires that the author vertex match only person objects, the book vertex matches only book objects, and the author-of edge matches only author-of links. The new query yields only two matches versus the four subgraphs found by the original query.

As we noted earlier, unlike the SELECT statement in SQL, a QGraph query does not specify which attributes of matching objects or edges are included in the query results (subgraphs). This continues to hold even when queries include conditions that mention only some of these attributes. All the object and link attributes, not just those mentioned in the query, are available for inspection in the query results.

In addition to testing for equality, conditions can compare attribute values to the specified values using the comparison operators <>, <, <=, >, and >=. For example, to find authors who have published before 1990, we create the following query:

Query [Basics_DB01_Q03.qg2.xml]

Figure 3.4. Query [Basics_DB01_Q03.qg2.xml]


Because the book The Talisman is the only object with a PubDate before 1990, this query returns the same results as the earlier example.