Commit c081d63c authored by Paul Asmuth's avatar Paul Asmuth
Browse files

ASTUtil::columnNamesFromSelectList

parent f9a68a57
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ set(FNORDMETRIC_SOURCES
    stage/src/fnordmetric/sql/expressions/datetime.cc
    stage/src/fnordmetric/sql/expressions/math.cc
    stage/src/fnordmetric/sql/parser/astnode.cc
    stage/src/fnordmetric/sql/parser/astutil.cc
    stage/src/fnordmetric/sql/parser/parser.cc
    stage/src/fnordmetric/sql/parser/token.cc
    stage/src/fnordmetric/sql/parser/tokenize.cc
+66 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <fnordmetric/sql/parser/astnode.h>
#include <fnordmetric/sql/parser/astutil.h>
#include <fnordmetric/sql/parser/token.h>
#include <fnordmetric/sql/runtime/tablerepository.h>
#include <fnordmetric/util/runtimeexception.h>

namespace fnordmetric {
namespace query {

std::vector<std::string> ASTUtil::columnNamesFromSelectList(
    ASTNode* select_list,
    TableRef* tbl_ref /* = nullptr */) {
  std::vector<std::string> column_names;
  for (auto col : select_list->getChildren()) {
    if (col->getType() != ASTNode::T_DERIVED_COLUMN) {
      RAISE(kRuntimeError, "corrupt AST");
    }

    auto derived = col->getChildren();

    // column with AS clause
    if (derived.size() == 2) {
      if (derived[1]->getType() != ASTNode::T_COLUMN_ALIAS) {
        RAISE(kRuntimeError, "corrupt AST");
      }

      auto colname_token = derived[1]->getToken();

      if (!(colname_token && *colname_token == Token::T_IDENTIFIER)) {
        RAISE(kRuntimeError, "corrupt AST");
      }

      column_names.emplace_back(colname_token->getString());
      continue;
    }

    // resolved column name
    if (derived.size() == 1 && *derived[0] == ASTNode::T_RESOLVED_COLUMN) {
      if (tbl_ref == nullptr) {
        column_names.emplace_back("col" + std::to_string(derived[0]->getID()));
      } else {
        auto col_name = tbl_ref->getColumnName(derived[0]->getID());
        column_names.emplace_back(col_name);
      }

      continue;
    }

    // expression
    column_names.emplace_back("<expr>"); // FIXPAUL!!
  }

  return column_names;
}

}
}
+31 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#ifndef _FNORDMETRIC_SQL_ASTUTIL_H
#define _FNORDMETRIC_SQL_ASTUTIL_H
#include <string>
#include <vector>

namespace fnordmetric {
namespace query {
class ASTNode;
class TableRef;

class ASTUtil {
public:

  static std::vector<std::string> columnNamesFromSelectList(
      ASTNode* select_list,
      TableRef* tbl_ref = nullptr);

};

}
}
#endif
+3 −9
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
 */
#include <stdlib.h>
#include <fnordmetric/sql/parser/astnode.h>
#include <fnordmetric/sql/parser/astutil.h>
#include <fnordmetric/sql/runtime/queryplanbuilder.h>
#include <fnordmetric/sql/runtime/queryplannode.h>
#include <fnordmetric/sql/runtime/tablelessselect.h>
@@ -363,10 +364,7 @@ QueryPlanNode* QueryPlanBuilder::buildGroupBy(
  }

  /* resolve output column names */
  std::vector<std::string> column_names;
  for (const auto& col : select_list->getChildren()) {
    column_names.push_back("unnamed"); // FIXPAUL
  }
  auto column_names = ASTUtil::columnNamesFromSelectList(select_list);

  return new GroupBy(
      std::move(column_names),
@@ -483,10 +481,7 @@ QueryPlanNode* QueryPlanBuilder::buildGroupOverTimewindow(
  }

  /* resolve output column names */
  std::vector<std::string> column_names;
  for (const auto& col : select_list->getChildren()) {
    column_names.push_back("unnamed"); // FIXPAUL
  }
  auto column_names = ASTUtil::columnNamesFromSelectList(select_list);

  return new GroupOverTimewindow(
      std::move(column_names),
@@ -511,7 +506,6 @@ bool QueryPlanBuilder::buildInternalSelectList(
    /* check if this column already exists in the select list */
    const auto& candidates = target_select_list->getChildren();
    for (int i = 0; i < candidates.size(); ++i) {
      candidates[i]->debugPrint(4);
      if (candidates[i]->getType() == ASTNode::T_DERIVED_COLUMN) {
        if (candidates[i]->getChildren().size() == 1) {
          auto colname = candidates[i]->getChildren()[0];
+3 −25
Original line number Diff line number Diff line
@@ -12,8 +12,9 @@
#include <stdlib.h>
#include <string>
#include <vector>
#include <fnordmetric/sql/parser/token.h>
#include <fnordmetric/sql/parser/astutil.h>
#include <fnordmetric/sql/parser/astnode.h>
#include <fnordmetric/sql/parser/token.h>
#include <fnordmetric/sql/runtime/queryplannode.h>
#include <fnordmetric/sql/runtime/compile.h>
#include <fnordmetric/sql/runtime/execute.h>
@@ -42,30 +43,7 @@ public:
    }

    /* column names */
    std::vector<std::string> column_names;
    for (auto col : select_list->getChildren()) {
      if (!(*col == ASTNode::T_DERIVED_COLUMN)) {
        RAISE(kRuntimeError, "corrupt AST");
      }

      auto derived = col->getChildren();

      if (derived.size() == 2) {
        if (!(*derived[1] == ASTNode::T_COLUMN_ALIAS)) {
          RAISE(kRuntimeError, "corrupt AST");
        }

        auto colname_token = derived[1]->getToken();
        if (!(colname_token && *colname_token == Token::T_IDENTIFIER)) {
          RAISE(kRuntimeError, "corrupt AST");
        }

        column_names.emplace_back(colname_token->getString());
      } else {
        column_names.emplace_back("unnamed");
      }
    }

    auto column_names = ASTUtil::columnNamesFromSelectList(select_list);
    return new TablelessSelect(std::move(column_names), expr);
  }

Loading