How to add a new rewrite rule?

This is an implementation guide for adding a new rewrite rule in NebulaStream. In the following, we explain step-wise the essential development tasks of how to add your custom rewrite rule in order to manipulate the query plan in the NebulaStream optimizer component.

Tasks

  1. Create a new class that contains your rewrite rule including your specific constructors and functionality. We recommend you to take a look to already existing rules that may serve you as starting point.
  2. Add your rewrite rule to the QueryRewritePhase class. In particular, introduce a pointer to your rule in the namespace of the optimizer and add it as a private attribute of the class. Furthermore, apply your rewrite rule in the execute function of the QueryRewritePhase class to manipulate the query plan.
  3. Finally, add various test cases of your rule to verify it.
  4. Last, add all new src-files to the respective CmakeLists (in the corresponding folders, watch out for subfolders).

Location: nes-optimizer

Tasks:

  • optimizer -> QueryRewrite
  • optimizer -> Phases -> QueryRewritePhase

Tests:

  • tests -> UnitTests -> Optimizer -> QueryRewrite
  • tests -> UnitTests -> Optimizer -> Phases

The following code registers your YourRewriteRule to the QueryRewritePhase class (hpp file, Task 2).

// Task 2
namespace NES::Optimizer {
...
class AttributeSortRule;
using AttributeSortRulePtr = std::shared_ptr<AttributeSortRule>;

class YourRewriteRule;
using YourRewriteRulePtr = std::shared_ptr<YourRwriteRule>;
...
class QueryRewritePhase {
    ...
    private:
    ...
        AttributeSortRulePtr attributeSortRule;
        YourRewriteRuleRulePtr yourRewriteRule;
    ...
}
}

The following code adds your YourRewriteRule to the execute function of QueryRewritePhase class, i.e., it applies it to the received queryPlan to optimize (cpp file, Task 2).

// Task 2: 
// @brief Add your rule the the query rewrite phase
QueryPlanPtr QueryRewritePhase::execute(const QueryPlanPtr& queryPlan) {
    ... 
    // Apply rule for filter split up
    manipulatedQueryPlan = filterSplitUpRule->apply(manipulatedQueryPlan);
    // Apply your rule 
    manipulatedQueryPlan = yourRewriteRule->apply(manipulatedQueryPlan);
    ... 
    return manipulatedQueryPlan;
}