Commit 94fa810a authored by Paul Asmuth's avatar Paul Asmuth
Browse files

simple 2d seriesadapter working

parent a2916d35
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -38,5 +38,11 @@ int Executable::getColumnIndex(const std::string& column_name) const {
  return -1;
}

void Executable::finish() {
  if (target_ != nullptr) {
    target_->finish();
  }
}

}
}
+2 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ namespace query {
class RowSink {
public:
  virtual bool nextRow(SValue* row, int row_len) = 0;
  virtual void finish() {}
};

class Executable : public RowSink {
@@ -28,12 +29,12 @@ public:
  virtual ~Executable();

  virtual void execute() = 0;

  virtual size_t getNumCols() const = 0;
  virtual const std::vector<std::string>& getColumns() const = 0;
  int getColumnIndex(const std::string& column_name) const;

  void setTarget(RowSink* target);
  void finish() override;

protected:
  bool emitRow(SValue* row, int row_len);
+3 −227
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ public:
    return true;
  }

  void finish() {
  void finish() override {
    for (const auto& series : series_list_) {
      drawable_->addSeries(series.get());
    }
@@ -115,6 +115,8 @@ public:
      stmt_->setTarget(adapter);
      adapter_.reset(adapter);
    }

    return true;
  }

protected:
@@ -140,232 +142,6 @@ protected:
    return (x->testType<TX>() && y->testType<TY>());
  }

/*
  template <typename TX, typename TY>
  void copySeries2D(
      ResultList* series,
      int name_ind,
      int x_ind,
      int y_ind) {
    std::unordered_map<std::string, Series2D<D1, D2>*> series_map;
    std::vector<Series2D<D1, D2>*> series_list;

    for (int i = 0; i < series->getNumRows(); ++i) {
      const auto& row = series->getRow(i);
      std::string name = "unnamed";

      if (name_ind >= 0) {
        name = row[name_ind].getValue<std::string>();
      }

      Series2D<D1, D2>* series = nullptr;
      const auto& series_iter = series_map.find(name);
      if (series_iter == series_map.end()) {
        series = new Series2D<D1, D2>(name);
        series_map[name] = series;
        series_list.push_back(series);
      } else {
        series = series_iter->second;
      }

      series->addDatum(
          row[x_ind].getValue<D1>(),
          row[y_ind].getValue<D2>());
    }

    for (auto series : series_list) {
      drawable->addSeries(series);
    }
  }

  template <typename TV>
  bool testType(std::string value) const {
    return true;
  }

  template <typename TV>
  TV castType(std::string value) const;
*/
/*

  void executeDrawable( {
    child_->execute();

     const auto& cols = getColumns();
    for (int i = 0; i < getNumCols(); ++i) {
      const auto& col = cols[i];

      if (col == "x" || col == "X") {
        x_ind = i;
        continue;
      }

      if (col == "y" || col == "Y") {
        y_ind = i;
        continue;
      }

      if (col == "z" || col == "Z") {
        z_ind = i;
        continue;
      }

      if (strncasecmp(col.c_str(), "name", 4) == 0) {
        name_ind = i;
      }
    }

    bool is_2d = x_ind >= 0 && y_ind >= 0;
    bool is_3d = z_ind >= 0 && is_2d;

    if (is_3d) {

    } else if (is_2d) {
      switch (rows_[0][x_ind].getType()) {
        case SValue::T_FLOAT:
        case SValue::T_INTEGER:
        case SValue::T_BOOL:
          switch (rows_[0][y_ind].getType()) {
            case SValue::T_FLOAT:
            case SValue::T_INTEGER:
            case SValue::T_BOOL:
              return executeSeries<T, double, double>(
                  drawable,
                  x_ind,
                  y_ind,
                  name_ind);

            case SValue::T_STRING:
              return executeSeries<T, double, std::string>(
                  drawable,
                  x_ind,
                  y_ind,
                  name_ind);

            case SValue::T_UNDEFINED:
              break;
          }

        case SValue::T_STRING:
          switch (rows_[0][y_ind].getType()) {
            case SValue::T_FLOAT:
            case SValue::T_INTEGER:
            case SValue::T_BOOL:
              return executeSeries<T, std::string, double>(
                  drawable,
                  x_ind,
                  y_ind,
                  name_ind);

            case SValue::T_STRING:
              return executeSeries<T, std::string, std::string>(
                  drawable,
                  x_ind,
                  y_ind,
                  name_ind);

            case SValue::T_UNDEFINED:
              break;
          }

        case SValue::T_UNDEFINED:
          break;
      }

      throw std::string("undefined SValue");
    } else {
      throw std::string("error: not enough dimensions");
    }
  }

  template <typename T, typename D1, typename D2>
  void executeSeries(T* drawable, int x_ind, int y_ind, int name_ind) {
    std::unordered_map<std::string, Series2D<D1, D2>*> series_map;
    std::vector<Series2D<D1, D2>*> series_list;

    for (const auto& row : rows_) {
      std::string name = "unnamed";

      if (name_ind >= 0) {
        name = row[name_ind].getValue<std::string>();
      }

      Series2D<D1, D2>* series = nullptr;
      const auto& series_iter = series_map.find(name);
      if (series_iter == series_map.end()) {
        series = new Series2D<D1, D2>(name);
        series_map[name] = series;
        series_list.push_back(series);
      } else {
        series = series_iter->second;
      }

      series->addDatum(
          row[x_ind].getValue<D1>(),
          row[y_ind].getValue<D2>());
    }

    for (auto series : series_list) {
      drawable->addSeries(series);
    }
  }

  template <typename T, typename D1, typename D2, typename D3>
  void executeSeries(T* drawable, int x_ind, int y_ind, int z_ind) {
    auto series = new Series3D<D1, D2, D3>();
    for (const auto& row : rows_) {
      series->addDatum(
          row[x_ind].getValue<D1>(),
          row[y_ind].getValue<D2>(),
          row[z_ind].getValue<D3>());
    }

    drawable->addSeries(series);
  }

  bool nextRow(SValue* row, int row_len) override {
    rows_.emplace_back();

    if (row_len != columns_.size()) {
      throw std::string("invalid row size");
    }

    for (int i = 0; i < row_len; ++i) {
      rows_.back().emplace_back(row[i]);
    }
    executeExpression(
        name_expr_,
        nullptr,
        row_len,
        row,
        &out_len,
        out);
    assert(out_len == 1);

    const auto& series_name = out[0].toString();
    const auto& series_iter = series_.find(series_name);
    SeriesDefinition* series;
    if (series_iter == series_.end()) {
      series = new SeriesDefinition(series_name);
      series_[series_name] = series;
    } else {
      series = series_iter->second;
    }

    // FIXPAUL execute series property expressions
    // FIXPAUL: optimization -- execute all non aggregate exprs only on last row
    // FIXPAUL: optimization -- set props only on last row

    std::vector<SValue> datum;
    assert(row_len >= columns_.size() - 1);
    for (int i = 0; i < columns_.size() - 1; ++i) {
      datum.emplace_back(row[i]);
    }
    series->addDatum(std::move(datum));
    return true;
  }

*/
protected:
  std::unique_ptr<AbstractSeriesAdapter> adapter_;
  T* drawable_;
+1 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ public:

    executeExpression(expression_, nullptr, 0, nullptr, &row_len, row);
    emitRow(row, row_len);
    finish();
  }

  size_t getNumCols() const override {
+1 −0
Original line number Diff line number Diff line
@@ -126,6 +126,7 @@ public:

  void execute() override {
    tbl_ref_->executeScan(this);
    finish();
  }

  bool nextRow(SValue* row, int row_len) override {