# Towards Interactive Analytics over RDF Graphs
## 6 Translation of HIFUN Queries to SPARQL
### Complex Grouping Queries
#### 6.4.2 Pairing  
Suppose that we would like to find the total quantities delivered by branch and product. This query would be expressed in HIFUN as ((_takesPlaceAt \(\otimes\) delivers_), inQuantity, SUIM_) and in SPARQL as:  
SELECT?x2?x4 SUIM(?x3) WHERE { ?x1 ex:takesPlaceAt?x2. ?x1 ex:inQuantity?x3. ?x1 ex:delivers?x4.  } GROUP BY x2?x4
```

Therefore, a HIFUN query (\(f_{k}\otimes...,\otimes f_{2}\otimes f_{1}\), \(m\), \(op\)) would be translated as follows: we would add (i) the triple patterns?x1 \(f_{1}\)_right_(\(f_{1}\)),?x1 \(f_{2}\)_right_(\(f_{2}\)),...,?x1 \(f_{k}\)_right_(\(f_{k}\)) to the WHERE clause and (iii) the variables right(\(f_{1}\)), right(\(f_{2}\)),..., right(\(f_{k}\)) to the SELECT and to the GROUP BY clauses. In other words, we would join the pairing functions i.e., \(f_{1}\), \(f_{2}\),..., \(f_{k}\) on their shared variable i.e.,?x1. ### The Full Algorithm for Translating a HIFUN Query to a SPARQL Query

Let us now describe the full translation algorithm. Let \(q=(gE/rg,mE/rm,opE/ro)\) be a HIFUN query where

* \(gE\) is the grouping expression,
* \(mE\) is the measuring expression,
* \(opE\) is the operation expression, and
* \(rg\) is a restriction on the grouping expression,
* \(rm\) is a restriction on he measuring expression, and
* \(ro\) is a restriction on the operation expression. The final query is constructed by concatenating strings as shown below: \(Q=\) \"SELECT\" +retVars(\(gE\)) + \" \"+opE(\(mE\)) +\"\(\backslash n\)\" + \"WHERE [\" +\" \(\backslash n\)\" + triplePatterns(\(gE\)) +\" \(\backslash n\)\" + triplePatterns(\(mE\)) +\" \(\backslash n\)\" + \")\" + \"\(\backslash n\)\" + \"GROUP BY \" +retVars(\(gE\)) +\" \(\backslash n\)\" +\" \(\backslash n\)\" + \" HAVING \" +retVars(\(Q_{ans}\)) The way the variables \(retVars(gE)\), \(opE(mE)\), \(triplePatterns(gE)\), \(triplePatterns(mE)\), \(retVars(gE)\), and \(restr(Q_{ans})\) are constructed, is described next

1. We start the translation with the grouping expression \(gE\) by creating the string format of the triple patterns in which the terms \(g_{i}\) of \(gE\) participates, \(triplePatterns(gE)\) += \(\cap x_{i}\)\(g_{i}\)\(right(g_{i})\), as described in Sections 6.1 and 6.4. If \(gE\) contains any restriction \(rg\) we supplementarily create the string format of the triple pattern expressing that constraint: 1. if \(rg\) refers to a URI, then \(triplePatterns(gE)\) += \(\cap x_{i}\)\(g_{i}\)\(rg\), 1. if \(rg\) is represented with a LITERAL, then \(triplePatterns(gE)\) += \(FILTER(right(g_{i})\)\(rg)\), as described in Section 6.2. 2. We proceed with the translation of the measuring expression \(mE\) by creating the string format of the triple patterns in which the terms \(m_{i}\) of \(mE\) participates, \(triplePatterns(mE)\) += \(x_{i}\)\(m_{i}\)\(right(m_{i})\). Since this expression can also be complex, the translation is made as described in Sections 6.1 and 6.4. If \(mE\) contains any restriction \(rm\) we supplementarily create the string format of the triple pattern expressing that constraint: 2. if \(rm\) refers to a URI, then \(triplePatterns(mE)\) += \(\cap x_{i}\)\(m_{i}\)\(rm\)\(rm\)\(rm\)\(rm\)\(rm\)\(rm\)\(rm\)\(rm\)\(rm\)\(rm\)\(rm\)\(\rm\)\



) GROUP BY?x2?x5 HAVING (SUM(?x3) > 1000) ```
The pseudocode of the algorithm that defines the variables \(retVars(gE)\), \(opE(mE)\), \(triplePatterns(gE)\), \(triplePatterns(mE)\), \(retVars(gE)\), and \(restr(Q_{ans})\), for the simple case, where the HIFUN query _does not contain compositions and pairings_, is given in Algorithm 1.