SPOQL Every query in SPOQL is written as a SELECT statement. Every query in SPOQL (spoqlquery) returns SPOs. Syntax: := SELECT FROM [WHERE CONDITIONAL ] [] brackets are used to denote optional elements. <> brackets represent variables. All keywords are capitalized. cnd - convention used to represent conditional, cnt - convention used to represent context, var - convention used to represent variable, tbl - convention used to represent table, - name of random variable - name of context variable 1. := * | * is used when there is no projection operation to be carried out. is represented as a list of atomic projection conditions. (separated by commas) := , , ... := cnd. | cnt. | var. Examples: Select cnd.CS505, var.CS605, cnt.time from advising The order for the is from left to right. Projection is commutative. In the above query, projection on conditional (CS505) is performed first. The resultant sp-relation is used in the projection on variable (CS605) operation and finally the resultant sp-relation is projected on the context variable (time). Note: cnd, cnt and var can be addressed by sp-relation names as well. If we want to address conditional variable CS505 of sp-relation advising, it is represented as advising.cnd.505 Similarly one can address context (cnt) and random (var) variables. This helps when there are many sp-relations in . 2. represents the combination of sp-relations which are addressed in the , and . Between each pair of sp-relations there should be a operator. := | := | ()[] | ()[] := JOIN | TIMES | , JOIN represents the join operation in sp-algebra TIMES represents the Cartesian product in sp-algebra , represents the Mix operation in sp-algebra := single sp-relation | ()[Name] | ()[Name] represents a single sp-relation, nested spoql query, or an explicit join or Cartesian product of two sp-relations. The nested spoql query and explicit join or Cartesian sp-relation pairs can be given an optional 'Name'. Examples: Select * from testing JOIN (testing1 TIMES testing2) In this query, an explicit Cartesian product of sp-relation testing1 and testing2 is performed, The resultant sp-relation is left-joined with sp-relation testing. If the brackets were absent then the query operations would have resulted in JOIN operation between testing and testing1. Their resultant would be used while performing the Cartesian product with sp-relation testing2. Select * from testing JOIN (Select cnt.year from testing1) In this query we have a nested SPOQL query. Thus this query is a two-level query. The sp-relation testing1 undergoes projection operation and is then left-joined with sp-relation testing. Note on validation of SPOQL query: A SPOQL query is validated based on scope of operation. The scope is determined by the level of the SPOQL query. Let us look at an example given below to understand the validation rule. Select cnt.time from testing JOIN (Select cnt.year from testing1) Here cnt.time refers to the projection on context of the resultant sp-relation of the outermost SPOQL query. If we want to project cnt.time for sp-relation testing we have to re-write he query as follows: Select testing.cnt.time from testing JOIN (Select cnt.year from testing) Now what is the validation? At any level of a SPOQL query, the elements in the , , and will address the sp-relations in the defined at the same level. If the elements do not address any sp-relation then by default, the elements address the sp-relation which is the resultant at that level. We do not permit SPOQL query of the type: Select testing.cnd.year from testing1 JOIN (Select * from testing where cnt.year = "1999") Here the in the outer SPOQL query is trying to address an sp-relation in the inner SPOQL query. This query can be re-written as: Select * from testing1 JOIN (Select testing.cnd.year from testing where cnt.year = "1999") OR Select * from testing1 JOIN (Select cnd.year from testing where cnt.year = "1999") 3. represents all the atomic selection conditions (selection on context, variable, conditional, probability and table) or logical combination of selection conditions. : = | [(] AND [)] | [(] OR [)] | NOT := cnt. IN T | cnt. = "value" | cnd. IN C | cnd. = "value" | var. IN V | tbl. = "value" | tbl.prob Op Rvalue | cnt. = cnt. Rvalue lies in [0,1] Op: = | < | <= | >= | != Note: cnd, cnt and var can be addressed by sp-relation names as well. Examples: Selection on context: Select * from advising where cnt.major IN T This query returns spos from sp-relation advising that have major as context variable Select * from advising where cnt.networkname = "situation 63" This query returns spos from sp-relation advising that have networkname as context variable and its value is situation63 Similarly we can write SPOQL queries for selection on conditional and selection and variables. Select * from advising where cnd.505 IN C Select * from advising where cnd.505 = "A" Select * from advising where var.CS505 IN V Selection on table is written as follows: Select * from advising where tbl.CS505 = "B" Selection on probability is written as follows: Select * from advising where tbl.prob > 0.4 We can also write logical combination of different selection conditions. Selection queries are commutative, thus the order does not matter. Select * from advising where var.CS505 IN V AND cnt.networkname IN T Select * from advising where tbl.prob > 0.4 AND var.CS505 IN V AND cnt.networkname = "demo network" 4. represents an atomic conditionalization expression or combination (AND) of atomic conditionalization expressions. := | AND := var. = "value" Note: var can be addressed by sp-relation names as well. Examples: Select * from testing conditional var.CS505 = "A" The sp-relation testing undergoes conditionalization with random variable CS505 having value A. Note, that the participating spos in relation testing should have atleast two participating random variables. (one of them should be of course CS505) Select * from testing JOIN testing1 conditional var.CS505 = "A" and var.CS605 = "A" The sp-relation testing undergoes conditionalization with random variable CS505 having value A. The resultant sp-relation undergoes conditionalization with random variable CS605 having value A. If all the above operations are present for a given sp-relation in a SPOQL query, the following precedence order is followed for all the operations presen at the same level: 1. conditonalization 2. selection 3. projection 4. cartesian product/join If a user wants to over-ride the precedence then the user can do it by writing nested queries. In case of cartesian product or join operation, higher priority is given to explicit conditions (using brackets). Examples: Select testing1.cnt.year from testing JOIN testing1 where testing1.tbl.prob = 0.1 conditional testing.var.SE = "A" and var.LY = "A" There are two sp-relations (testing and testing1) involved in this SPOQL query. The query is a single level query (since no nested Select statement). The sp-relation testing1 undergoes a selection based on probability. The resultant sp-relation is projected with context variable 'year'. Now, the relation testing1 can be joined with sp-relation testing. But before that, sp-relation testing undergoes condiitonalization (var.SE = "A"). After these relations are joined, their resultant is conditionalized (var.LY = "A"). Note: The nesting of Select statement is limited to the i.e. the FROM clause of a SPOQL query. Author: Kevin Mathias Date: 05-25-2006