# Query Rewriting for Incremental Continuous Query Evaluation in HIFUN
## 4 Methods
### System Implementation
#### 4.2.2 Continuous HIFUN Queries Using MapReduce
Common Grouping Rewriting RuleThe evaluation of this rewriting rule is slightly different to the evaluation of the _Common Grouping and Measuring Rewriting Rule_. In this case \(n\) different measuring attributes are reduced to the common grouping attribute, applying \(n\) possible different operations. In the _Query Input Preparation_ step, the information is extracted from the initial unstructured dataset using the common grouping and the \(n\) different measuring attributes which appear in the query set \(Q\). A mapping function is used to apply the given common grouping attribute and the \(n\) measuring attributes to each record of the initial DStream. It then returns a new DStream, containing the values of the common grouping and measuring attributes for each record, useful for the next evaluation steps. As a next evaluation step, a map function constructs the grouping partition as follows: The mapper receives the tuples created from the previous step and emits the key-value pairs \(<K,V>\), where \(K\) is the value of the common grouping attribute for each tuple and the value \(V\) is synthetic, and carries a list of \(n\) measuring attribute values. The length \(n\) of the list depends on the number of the measuring attributes which appear in the query set \(Q\). As the final evaluation step, the _Grouping Partition Reduction_ is performed as follows: The reduce function applies for each key \(K\), the \(n\) operations on the list of \(n\) values and produces the query answer in the form of key-value pairs \(<K_{q},V_{k}>\), where \(K_{q}\) is the query key and \(V_{k}\) its synthetic value containing the reduced value for each operation applicable to the measuring attributes. The answer of the _Common Grouping Rewriting Rule_ is completed when a set of \(<K_{q},V_{k}>\) is created. Common Measuring and Operation Rewriting RuleIn this rule, the same measuring attribute and the same operation are associated with the \(n\) grouping attributes. The evaluation of the base query is first required as described in the generic evaluation schema, and produces an intermediate result required to the evaluation of the projection queries. The intermediate result is produced in the form of key-value pairs \(<K_{b_{q}},V_{b_{q}k}>\), where \(K_{b_{q}}\) is the key of the base query and \(V_{b_{q}k}\) its reduced value. For each projection query, a map function receives the pair tuples of the intermediate result and a MapReduce job constructs the answer as follows: In the _Grouping Partition Construction_ step a mapper emits key-value pairs \(<K^{\prime},V^{\prime}>\), where the key \(K^{\prime}\) is emitted as the value of the subset of the key \(K_{b_{q}}\), which is specified by the projection operation, and the value \(V_{b_{q}k}\) is emitted as a new \(V^{\prime}\) value assigned to \(K^{\prime}\). Finally, to perform the _Grouping Partition Reduction_ step, the reduce function is applied and produces the answer for each projection query in the form of key-value pairs \(<K_{q},V_{k}>\), where \(K_{q}\) is the query key and \(V_{k}\) its value. The final answer of this rule is completed when \(n\) sets of \(<K_{q},V_{k}>\) are created. Basic Rewriting RuleThe evaluation of the base query is required first as described in the generic evaluation schema, and produces an intermediate result of the \"inner\" query required for the evaluation of the \"outer\" query. The intermediate result is produced in the form of key-value pairs \(<K_{b_{q}},V_{b_{q}k}>\), where \(K_{b_{q}}\) is the key of the base query and \(V_{h_{q}k}\) its value. A set of key-value pairs is available for the evaluation of the \"outer\" query as follows. A map function receives the key-value pair of the intermediate result and constructs a new set of the key-value pairs \(<K^{\prime},V^{\prime}>\) as follows. For each key \(K_{b_{q}}\) a new key \(K^{\prime}\) is emitted, specified by the association between \(K_{b_{q}}\) and \(K^{\prime}\) as defined in the context.