message: reordering expressions
diff: @@ -22,10 +22,40 @@ void ExpressionHeuristics::VisitOperator(LogicalOperator &op) {
  }
  void ExpressionHeuristics::ReorderExpressions(vector<unique_ptr<Expression>> &expressions) {
  - if (expressions.size() != 1) {
  +
  + struct expr {
  + index_t idx;
  + index_t cost;
  +
  + bool operator==(const expr &p) const {
  + return idx == p.idx && cost == p.cost;
  + }
  + bool operator<(const expr &p) const {
  + return cost < p.cost || (cost == p.cost && idx < p.idx);
  + }
  + };
  +
  + vector<expr> exprCosts;
  +
  + if (expressions.size() > 1) {
  //iterate expressions, get cost for each one and order by that cost
  for (index_t i = 0; i < expressions.size(); i++) {
  - index_t cost = Cost(*expressions[i]); //TODO: reorder by cost
  + exprCosts.push_back({i, Cost(*expressions[i])});
  + }
  +
  + sort(exprCosts.begin(), exprCosts.end());
  +
  + assert(exprCosts.size() == expressions.size());
  +
  + //for all elements to put in place
  + for(index_t i = 0; i < expressions.size() - 1; ++i) {
  + //while the element i is not yet in place
  + while(i != exprCosts[i].idx) {
  + //swap it with the element at its final place
  + index_t alt = exprCosts[i].idx;
  + swap(expressions[i], expressions[alt]);
  + swap(exprCosts[i], exprCosts[alt]);
  + }
  }
  }
  }
  