Multiple Subqueries

You can include more than one subquery in a QGraph query. Because subqueries are always annotated, you must ensure that queries containing subqueries obey QGraph’s annotation rules. For example, because QGraph prohibits edges connecting two annotated elements, you cannot connect two subqueries by a single edge.

In the query shown in Figure 6.20, two subqueries are connected by the Z edge. Because the Z edge connects two annotated elements, this query is illegal.

Illegal query

Figure 6.20. Illegal query


It may be easier to see whether your query obeys all annotation rules by visualizing any subqueries as vertices. Figure 6.21 shows such a view of the above query:

Conceptual structure of illegal query

Figure 6.21. Conceptual structure of illegal query


Visually collapsing the subqueries into single vertices helps to illustrate how the Z edge connects two annotated query elements.

Compare the query in Figure 6.20 to the one shown below.

Legal query

Figure 6.22. Legal query


This query adds another vertex (vertex E) between the two subqueries. Visualizing the subqueries as vertices yields the structure shown in Figure 6.23.

Conceptual structure of legal query with two subqueries

Figure 6.23. Conceptual structure of legal query with two subqueries


This view helps us see that this structure obeys QGraph’s requirements for annotations, and is thus a legal query.

Nested subqueries

Although not yet supported by Proximity, QGraph also lets you nest subqueries inside other subqueries. This further increases QGraph’s expressive power and lets you match more complex structures than would be possible with a single level of subquery. The query shown in Figure 6.24 shows a query that includes a nested subquery.

Nested subqueries

Figure 6.24. Nested subqueries


Nested subqueries must obey the same annotation requirements that apply to any query element. In the above query, both the top-level subquery and nested subquery are annotated, as are the X and Y incident edges.

This query extends the pattern we saw in the query shown in Figure 6.1, which returned 2d-star subgraphs—clusters of linked objects two hops away from a central object. In this case, using nested subqueries lets us find and return 3d-star subgraphs. Deeper nesting offers the opportunity to match even more complex structures.

We can continue our technique of visualizing subqueries as vertices to help us understand more complex queries. Visualizing the nested subquery in Figure 6.24 as a single vertex gives us the structure shown below.

Conceptual structure of nested subqueries

Figure 6.25. Conceptual structure of nested subqueries


By treating a complex structure (the nested subquery) as a vertex inside the top-level subquery, we can more clearly see how the structure of this query resembles the 2d-star query we saw in Figure 6.1.