# Query Rewriting for Incremental Continuous Query Evaluation in HIFUN
## 4 Methods
### System Implementation
#### 4.2.3 Translating Continuous HIFUN Queries to SQL
Consider again the context in Figure 5 and assume we need to evaluate the following query \(Q=((s\circ p)\times(c\circ p),q,sum)\) asking for the totals by supplier and category. In this query, the grouping attributes supplier and category  
Figure 5: A context and its underlying data stored in the form of a relation schema. appear different in the table from the measuring attribute quantity. We map Q to the following SQL query over a star schema:  
```
SelectSupplier,Category,sum(Quantity) As\(ans_{Q}(Supplier,Category)\) Fromjoin(FT,\(DT_{Product}\)) GroupbySupplier,Category
```  
In the _Input Preparation_ step, the grouping attributes are selected, which are the grouping attributes supplier and category and the measuring attribute quantity. The attributes supplier and category appear in the dimensional table \(DT_{Product}\) so the fact table \(FT\) and the dimensional table \(DT_{Product}\) are joined accordingly. The _Grouping Partition Construction_ is performed by the \"group by\" clause to group rows that have the same attributes supplier and category. In _Grouping Partition Reduction_, the query operation sum is applied to the measuring attribute quantity. The \('ans_{Q}(Supplier,Category)'\) is a user defined attribute and the query returns the answer of \(Q\) in the form of a table with two attributes, \(Supplier\times Category\) and \(ans_{Q}(Supplier,Category)\). Rewritings to SQL  
In this section, we present a detailed description of how the evaluation mechanism leverages these rules and an HIFUN rewritten set Q mapped to a physical-level mechanism of Spark Structured Streaming, and the semantics of SQL when the evolving datasets are stored in an unbounded append-only relation table. Common Grouping and Measuring Rewriting RuleIn this rewriting rule, the SQL query is created by customizing the reduction step of SQL group-by query by adding the aggregate functions related to the \(n\) operations on the common measuring attribute, which appears in the rewritten HIFUN \(Q\) set. Common Grouping Rewriting RuleIn this rewriting, the SQL query is created similarly to the previous rewriting rule. In the reduction step of the SQL group-by query, the aggregate functions are added to the \(n\) operations for the \(n\) corresponding measuring attributes--which appear in the rewritten HIFUN \(Q\) set. Common Measuring and Operation Rewriting Ruleand Basic Rewriting RuleThese rewriting rules are not supported when Spark Structured Streaming is used as the physical-level evaluation engine. Firstly, the evaluation of the the base query produces the base table used to evaluate the next queries. Each one of the outer queries uses the base table to produce the final answer. The above computations are achievable under the SQL semantics by mapping the base HIFUN query to a SQL group-by query, and each one of the outer queries also to SQL group-by queries. In the current version of Spark Structured Streaming, multiple streaming aggregations (i.e., chain of aggregation on a streaming DataFrame) are not supported yet.