Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 3f4a0ad

Browse files
committed
Move isDisjoint to its own class; move GetAffectedIndex out of .h
1 parent c5ab9c9 commit 3f4a0ad

File tree

3 files changed

+148
-99
lines changed

3 files changed

+148
-99
lines changed

src/include/planner/plan_util.h

Lines changed: 10 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@
1515
#include <set>
1616
#include <string>
1717

18-
#include "catalog/catalog_cache.h"
19-
#include "catalog/database_catalog.h"
20-
#include "catalog/table_catalog.h"
21-
#include "catalog/column_catalog.h"
22-
#include "catalog/index_catalog.h"
23-
24-
#include "parser/delete_statement.h"
25-
#include "parser/insert_statement.h"
26-
#include "parser/sql_statement.h"
27-
#include "parser/update_statement.h"
28-
2918
#include "planner/abstract_plan.h"
3019
#include "planner/abstract_scan_plan.h"
3120
#include "planner/delete_plan.h"
@@ -36,6 +25,15 @@
3625
#include "util/string_util.h"
3726

3827
namespace peloton {
28+
29+
namespace catalog {
30+
class CatalogCache;
31+
}
32+
33+
namespace parser {
34+
class SQLStatement;
35+
}
36+
3937
namespace planner {
4038

4139
class PlanUtil {
@@ -62,7 +60,7 @@ class PlanUtil {
6260
* @return set of affected index object ids
6361
*/
6462
static const std::set<oid_t> GetAffectedIndexes(
65-
catalog::CatalogCache& catalog_cache, parser::SQLStatement* sql_stmt);
63+
catalog::CatalogCache &catalog_cache, parser::SQLStatement *sql_stmt);
6664

6765
private:
6866
///
@@ -169,92 +167,5 @@ inline void PlanUtil::GetTablesReferenced(const planner::AbstractPlan *plan,
169167
}
170168
}
171169

172-
namespace {
173-
174-
template <typename T>
175-
bool isDisjoint(const std::set<T> &setA, const std::set<T> &setB) {
176-
bool disjoint = true;
177-
if (setA.empty() || setB.empty())
178-
return disjoint;
179-
180-
typename std::set<T>::const_iterator setA_it = setA.begin();
181-
typename std::set<T>::const_iterator setB_it = setB.begin();
182-
while (setA_it != setA.end() && setB_it != setB.end() && disjoint) {
183-
if (*setA_it == *setB_it)
184-
disjoint = false;
185-
else if (*setA_it < *setB_it)
186-
setA_it++;
187-
else
188-
setB_it++;
189-
}
190-
191-
return disjoint;
192-
}
193-
194-
} // namespace
195-
196-
inline const std::set<oid_t> PlanUtil::GetAffectedIndexes(
197-
catalog::CatalogCache &catalog_cache, parser::SQLStatement *sql_stmt) {
198-
std::set<oid_t> index_oids;
199-
std::string db_name, table_name;
200-
switch (sql_stmt->GetType()) {
201-
// For INSERT, DELETE, all indexes are affected
202-
case StatementType::INSERT: {
203-
auto insert_stmt = static_cast<parser::InsertStatement *>(sql_stmt);
204-
db_name = insert_stmt->GetDatabaseName();
205-
table_name = insert_stmt->GetTableName();
206-
}
207-
PELOTON_FALLTHROUGH;
208-
case StatementType::DELETE: {
209-
if (table_name.empty() || db_name.empty()) {
210-
auto delete_stmt = static_cast<parser::DeleteStatement *>(sql_stmt);
211-
db_name = delete_stmt->GetDatabaseName();
212-
table_name = delete_stmt->GetTableName();
213-
}
214-
auto indexes_map = catalog_cache.GetDatabaseObject(db_name)
215-
->GetTableObject(table_name)
216-
->GetIndexObjects();
217-
for (auto &index : indexes_map) {
218-
index_oids.insert(index.first);
219-
}
220-
}
221-
break;
222-
case StatementType::UPDATE: {
223-
auto update_stmt = static_cast<parser::UpdateStatement *>(sql_stmt);
224-
db_name = update_stmt->table->GetDatabaseName();
225-
table_name = update_stmt->table->GetTableName();
226-
auto db_object = catalog_cache.GetDatabaseObject(db_name);
227-
auto table_object = db_object->GetTableObject(table_name);
228-
229-
auto &update_clauses = update_stmt->updates;
230-
std::set<oid_t> update_oids;
231-
for (const auto &update_clause : update_clauses) {
232-
LOG_TRACE("Affected column name for table(%s) in UPDATE query: %s",
233-
table_name.c_str(), update_clause->column.c_str());
234-
auto col_object = table_object->GetColumnObject(update_clause->column);
235-
update_oids.insert(col_object->GetColumnId());
236-
}
237-
238-
auto indexes_map = table_object->GetIndexObjects();
239-
for (auto &index : indexes_map) {
240-
LOG_TRACE("Checking if UPDATE query affects index: %s",
241-
index.second->GetIndexName().c_str());
242-
const std::vector<oid_t> &key_attrs =
243-
index.second->GetKeyAttrs(); // why it's a vector, and not set?
244-
const std::set<oid_t> key_attrs_set(key_attrs.begin(), key_attrs.end());
245-
if (!isDisjoint(key_attrs_set, update_oids)) {
246-
LOG_TRACE("Index (%s) is affected",
247-
index.second->GetIndexName().c_str());
248-
index_oids.insert(index.first);
249-
}
250-
}
251-
} break;
252-
default:
253-
LOG_TRACE("Does not support finding affected indexes for query type: %d",
254-
static_cast<int>(sql_stmt->GetType()));
255-
}
256-
return (index_oids);
257-
}
258-
259170
} // namespace planner
260171
} // namespace peloton

src/include/util/set_util.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// set_util.h
6+
//
7+
// Identification: src/include/util/set_util.h
8+
//
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
15+
#include <set>
16+
17+
namespace peloton {
18+
19+
class SetUtil {
20+
public:
21+
template <typename T>
22+
static bool isDisjoint(const std::set<T> &setA, const std::set<T> &setB) {
23+
bool disjoint = true;
24+
if (setA.empty() || setB.empty()) return disjoint;
25+
26+
typename std::set<T>::const_iterator setA_it = setA.begin();
27+
typename std::set<T>::const_iterator setB_it = setB.begin();
28+
while (setA_it != setA.end() && setB_it != setB.end() && disjoint) {
29+
if (*setA_it == *setB_it) {
30+
disjoint = false;
31+
} else if (*setA_it < *setB_it) {
32+
setA_it++;
33+
} else {
34+
setB_it++;
35+
}
36+
}
37+
38+
return disjoint;
39+
}
40+
};
41+
42+
} // namespace peloton

src/planner/plan_util.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// plan_util.cpp
6+
//
7+
// Identification: src/planner/plan_util.cpp
8+
//
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <set>
14+
#include <string>
15+
16+
#include "catalog/catalog_cache.h"
17+
#include "catalog/column_catalog.h"
18+
#include "catalog/database_catalog.h"
19+
#include "catalog/index_catalog.h"
20+
#include "catalog/table_catalog.h"
21+
#include "parser/delete_statement.h"
22+
#include "parser/insert_statement.h"
23+
#include "parser/sql_statement.h"
24+
#include "parser/update_statement.h"
25+
#include "planner/plan_util.h"
26+
#include "util/set_util.h"
27+
28+
namespace peloton {
29+
namespace planner {
30+
31+
const std::set<oid_t> PlanUtil::GetAffectedIndexes(
32+
catalog::CatalogCache &catalog_cache, parser::SQLStatement *sql_stmt) {
33+
std::set<oid_t> index_oids;
34+
std::string db_name, table_name;
35+
switch (sql_stmt->GetType()) {
36+
// For INSERT, DELETE, all indexes are affected
37+
case StatementType::INSERT: {
38+
auto insert_stmt = static_cast<parser::InsertStatement *>(sql_stmt);
39+
db_name = insert_stmt->GetDatabaseName();
40+
table_name = insert_stmt->GetTableName();
41+
}
42+
PELOTON_FALLTHROUGH;
43+
case StatementType::DELETE: {
44+
if (table_name.empty() || db_name.empty()) {
45+
auto delete_stmt = static_cast<parser::DeleteStatement *>(sql_stmt);
46+
db_name = delete_stmt->GetDatabaseName();
47+
table_name = delete_stmt->GetTableName();
48+
}
49+
auto indexes_map = catalog_cache.GetDatabaseObject(db_name)
50+
->GetTableObject(table_name)
51+
->GetIndexObjects();
52+
for (auto &index : indexes_map) {
53+
index_oids.insert(index.first);
54+
}
55+
} break;
56+
case StatementType::UPDATE: {
57+
auto update_stmt = static_cast<parser::UpdateStatement *>(sql_stmt);
58+
db_name = update_stmt->table->GetDatabaseName();
59+
table_name = update_stmt->table->GetTableName();
60+
auto db_object = catalog_cache.GetDatabaseObject(db_name);
61+
auto table_object = db_object->GetTableObject(table_name);
62+
63+
auto &update_clauses = update_stmt->updates;
64+
std::set<oid_t> update_oids;
65+
for (const auto &update_clause : update_clauses) {
66+
LOG_TRACE("Affected column name for table(%s) in UPDATE query: %s",
67+
table_name.c_str(), update_clause->column.c_str());
68+
auto col_object = table_object->GetColumnObject(update_clause->column);
69+
update_oids.insert(col_object->GetColumnId());
70+
}
71+
72+
auto indexes_map = table_object->GetIndexObjects();
73+
for (auto &index : indexes_map) {
74+
LOG_TRACE("Checking if UPDATE query affects index: %s",
75+
index.second->GetIndexName().c_str());
76+
const std::vector<oid_t> &key_attrs =
77+
index.second->GetKeyAttrs(); // why it's a vector, and not set?
78+
const std::set<oid_t> key_attrs_set(key_attrs.begin(), key_attrs.end());
79+
if (!SetUtil::isDisjoint(key_attrs_set, update_oids)) {
80+
LOG_TRACE("Index (%s) is affected",
81+
index.second->GetIndexName().c_str());
82+
index_oids.insert(index.first);
83+
}
84+
}
85+
} break;
86+
case StatementType::SELECT:
87+
break;
88+
default:
89+
LOG_TRACE("Does not support finding affected indexes for query type: %d",
90+
static_cast<int>(sql_stmt->GetType()));
91+
}
92+
return (index_oids);
93+
}
94+
95+
} // namespace planner
96+
} // namespace peloton

0 commit comments

Comments
 (0)