select_stmt.cpp 4.8 KB
Newer Older
W
wangyunlai.wyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2022/6/6.
//

#include "sql/stmt/select_stmt.h"
#include "sql/stmt/filter_stmt.h"
#include "common/log/log.h"
#include "common/lang/string.h"
#include "storage/common/db.h"
#include "storage/common/table.h"

SelectStmt::~SelectStmt()
{
  if (nullptr != filter_stmt_) {
    delete filter_stmt_;
    filter_stmt_ = nullptr;
  }
}

30
static void wildcard_fields(Table *table, std::vector<Field> &field_metas)
W
wangyunlai.wyl 已提交
31 32 33 34
{
  const TableMeta &table_meta = table->table_meta();
  const int field_num = table_meta.field_num();
  for (int i = table_meta.sys_field_num(); i < field_num; i++) {
35
    field_metas.push_back(Field(table, table_meta.field(i)));
W
wangyunlai.wyl 已提交
36 37 38 39 40 41 42 43 44 45
  }
}

RC SelectStmt::create(Db *db, const Selects &select_sql, Stmt *&stmt)
{
  if (nullptr == db) {
    LOG_WARN("invalid argument. db is null");
    return RC::INVALID_ARGUMENT;
  }

羽飞's avatar
羽飞 已提交
46
  // collect tables in `from` statement
W
wangyunlai.wyl 已提交
47
  std::vector<Table *> tables;
羽飞's avatar
羽飞 已提交
48
  std::unordered_map<std::string, Table *> table_map;
W
wangyunlai.wyl 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  for (int i = 0; i < select_sql.relation_num; i++) {
    const char *table_name = select_sql.relations[i];
    if (nullptr == table_name) {
      LOG_WARN("invalid argument. relation name is null. index=%d", i);
      return RC::INVALID_ARGUMENT;
    }

    Table *table = db->find_table(table_name);
    if (nullptr == table) {
      LOG_WARN("no such table. db=%s, table_name=%s", db->name(), table_name);
      return RC::SCHEMA_TABLE_NOT_EXIST;
    }

    tables.push_back(table);
    table_map.insert(std::pair<std::string, Table*>(table_name, table));
  }
  
羽飞's avatar
羽飞 已提交
66
  // collect query fields in `select` statement
67
  std::vector<Field> query_fields;
羽飞's avatar
羽飞 已提交
68
  for (int i = select_sql.attr_num - 1; i >= 0; i--) {
W
wangyunlai.wyl 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    const RelAttr &relation_attr = select_sql.attributes[i];

    if (common::is_blank(relation_attr.relation_name) && 0 == strcmp(relation_attr.attribute_name, "*")) {
      for (Table *table : tables) {
        wildcard_fields(table, query_fields);
      }

    } else if (!common::is_blank(relation_attr.relation_name)) { // TODO
      const char *table_name = relation_attr.relation_name;
      const char *field_name = relation_attr.attribute_name;

      if (0 == strcmp(table_name, "*")) {
	if (0 != strcmp(field_name, "*")) {
	  LOG_WARN("invalid field name while table is *. attr=%s", field_name);
	  return RC::SCHEMA_FIELD_MISSING;
	}
	for (Table *table : tables) {
	  wildcard_fields(table, query_fields);
	}
      } else {
	auto iter = table_map.find(table_name);
	if (iter == table_map.end()) {
	  LOG_WARN("no such table in from list: %s", table_name);
	  return RC::SCHEMA_FIELD_MISSING;
	}

	Table *table = iter->second;
羽飞's avatar
羽飞 已提交
96
	if (0 == strcmp(field_name, "*")) {
W
wangyunlai.wyl 已提交
97 98 99 100 101 102 103 104
	  wildcard_fields(table, query_fields);
	} else {
	  const FieldMeta *field_meta = table->table_meta().field(field_name);
	  if (nullptr == field_meta) {
	    LOG_WARN("no such field. field=%s.%s.%s", db->name(), table->name(), field_name);
	    return RC::SCHEMA_FIELD_MISSING;
	  }

105
	  query_fields.push_back(Field(table, field_meta));
W
wangyunlai.wyl 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	}
      }
    } else {
      if (tables.size() != 1) {
	LOG_WARN("invalid. I do not know the attr's table. attr=%s", relation_attr.attribute_name);
	return RC::SCHEMA_FIELD_MISSING;
      }

      Table *table = tables[0];
      const FieldMeta *field_meta = table->table_meta().field(relation_attr.attribute_name);
      if (nullptr == field_meta) {
	LOG_WARN("no such field. field=%s.%s.%s", db->name(), table->name(), relation_attr.attribute_name);
	return RC::SCHEMA_FIELD_MISSING;
      }

121
      query_fields.push_back(Field(table, field_meta));
W
wangyunlai.wyl 已提交
122 123 124 125 126 127 128 129 130 131
    }
  }

  LOG_INFO("got %d tables in from stmt and %d fields in query stmt", tables.size(), query_fields.size());

  Table *default_table = nullptr;
  if (tables.size() == 1) {
    default_table = tables[0];
  }

羽飞's avatar
羽飞 已提交
132
  // create filter statement in `where` statement
W
wangyunlai.wyl 已提交
133
  FilterStmt *filter_stmt = nullptr;
羽飞's avatar
羽飞 已提交
134 135
  RC rc = FilterStmt::create(db, default_table, &table_map,
			     select_sql.conditions, select_sql.condition_num, filter_stmt);
W
wangyunlai.wyl 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148
  if (rc != RC::SUCCESS) {
    LOG_WARN("cannot construct filter stmt");
    return rc;
  }

  // everything alright
  SelectStmt *select_stmt = new SelectStmt();
  select_stmt->tables_.swap(tables);
  select_stmt->query_fields_.swap(query_fields);
  select_stmt->filter_stmt_ = filter_stmt;
  stmt = select_stmt;
  return RC::SUCCESS;
}