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

Commit 724d3ce

Browse files
authored
Merge pull request #1105 from pranavk/queryplan_gh
Information about indexes affected by a given query
2 parents 8c656b7 + aeb7040 commit 724d3ce

File tree

9 files changed

+391
-5
lines changed

9 files changed

+391
-5
lines changed

src/include/catalog/catalog_cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
#include "common/internal_types.h"
1919

2020
namespace peloton {
21+
22+
namespace planner {
23+
class PlanUtil;
24+
} // namespace planner
25+
2126
namespace catalog {
2227

2328
class DatabaseCatalogObject;
@@ -32,6 +37,7 @@ class CatalogCache {
3237
friend class DatabaseCatalogObject;
3338
friend class TableCatalogObject;
3439
friend class IndexCatalogObject;
40+
friend class planner::PlanUtil;
3541

3642
public:
3743
CatalogCache() {}

src/include/parser/insert_statement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace parser {
2424
* @brief Represents "INSERT INTO students VALUES ('Max', 1112233,
2525
* 'Musterhausen', 2.3)"
2626
*/
27-
class InsertStatement : SQLStatement {
27+
class InsertStatement : public SQLStatement {
2828
public:
2929
InsertStatement(InsertType type)
3030
: SQLStatement(StatementType::INSERT), type(type), select(nullptr) {}

src/include/parser/sql_statement.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
#include <vector>
2121

22+
#include "common/internal_types.h"
2223
#include "common/macros.h"
2324
#include "common/printable.h"
2425
#include "common/sql_node_visitor.h"
25-
#include "common/internal_types.h"
2626

2727
namespace peloton {
2828

@@ -43,7 +43,7 @@ class SQLStatement : public Printable {
4343

4444
virtual ~SQLStatement() {}
4545

46-
virtual StatementType GetType() { return stmt_type; }
46+
virtual StatementType GetType() const { return stmt_type; }
4747

4848
// Get a string representation for debugging
4949
virtual const std::string GetInfo(int num_indent) const;

src/include/parser/update_statement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
#include <cstring>
1616

17-
#include "expression/abstract_expression.h"
1817
#include "common/sql_node_visitor.h"
18+
#include "expression/abstract_expression.h"
1919
#include "parser/sql_statement.h"
2020
#include "parser/table_ref.h"
2121

src/include/planner/plan_util.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@
1919
#include "planner/abstract_scan_plan.h"
2020
#include "planner/delete_plan.h"
2121
#include "planner/insert_plan.h"
22-
#include "planner/update_plan.h"
2322
#include "planner/populate_index_plan.h"
23+
#include "planner/update_plan.h"
2424
#include "storage/data_table.h"
2525
#include "util/string_util.h"
2626

2727
namespace peloton {
28+
29+
namespace catalog {
30+
class CatalogCache;
31+
} // namespace catalog
32+
33+
namespace parser {
34+
class SQLStatement;
35+
} // namespace parser
36+
2837
namespace planner {
2938

3039
class PlanUtil {
@@ -44,6 +53,16 @@ class PlanUtil {
4453
static const std::set<oid_t> GetTablesReferenced(
4554
const planner::AbstractPlan *plan);
4655

56+
/**
57+
* @brief Get the indexes affected by a given query
58+
* @param CatalogCache
59+
* @param SQLStatement
60+
* @return set of affected index object ids
61+
*/
62+
static const std::set<oid_t> GetAffectedIndexes(
63+
catalog::CatalogCache &catalog_cache,
64+
const parser::SQLStatement &sql_stmt);
65+
4766
private:
4867
///
4968
/// Helpers for GetInfo() and GetTablesReferenced()

src/include/util/set_util.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// set_util.h
6+
//
7+
// Identification: src/include/util/set_util.h
8+
//
9+
// Copyright (c) 2015-2018, 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+
};
24+
25+
template <typename T>
26+
inline bool SetUtil::IsDisjoint(const std::set<T> &setA,
27+
const std::set<T> &setB) {
28+
bool disjoint = true;
29+
if (setA.empty() || setB.empty()) return disjoint;
30+
31+
auto setA_it = setA.begin();
32+
auto setB_it = setB.begin();
33+
while (setA_it != setA.end() && setB_it != setB.end() && disjoint) {
34+
if (*setA_it == *setB_it) {
35+
disjoint = false;
36+
} else if (*setA_it < *setB_it) {
37+
setA_it++;
38+
} else {
39+
setB_it++;
40+
}
41+
}
42+
43+
return disjoint;
44+
}
45+
46+
} // namespace peloton

src/planner/plan_util.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// plan_util.cpp
6+
//
7+
// Identification: src/planner/plan_util.cpp
8+
//
9+
// Copyright (c) 2015-2018, 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,
33+
const parser::SQLStatement &sql_stmt) {
34+
std::set<oid_t> index_oids;
35+
std::string db_name, table_name;
36+
switch (sql_stmt.GetType()) {
37+
// For INSERT, DELETE, all indexes are affected
38+
case StatementType::INSERT: {
39+
auto &insert_stmt =
40+
static_cast<const parser::InsertStatement &>(sql_stmt);
41+
db_name = insert_stmt.GetDatabaseName();
42+
table_name = insert_stmt.GetTableName();
43+
}
44+
PELOTON_FALLTHROUGH;
45+
case StatementType::DELETE: {
46+
if (table_name.empty() || db_name.empty()) {
47+
auto &delete_stmt =
48+
static_cast<const parser::DeleteStatement &>(sql_stmt);
49+
db_name = delete_stmt.GetDatabaseName();
50+
table_name = delete_stmt.GetTableName();
51+
}
52+
auto indexes_map = catalog_cache.GetDatabaseObject(db_name)
53+
->GetTableObject(table_name)
54+
->GetIndexObjects();
55+
for (auto &index : indexes_map) {
56+
index_oids.insert(index.first);
57+
}
58+
} break;
59+
case StatementType::UPDATE: {
60+
auto &update_stmt =
61+
static_cast<const parser::UpdateStatement &>(sql_stmt);
62+
db_name = update_stmt.table->GetDatabaseName();
63+
table_name = update_stmt.table->GetTableName();
64+
auto db_object = catalog_cache.GetDatabaseObject(db_name);
65+
auto table_object = db_object->GetTableObject(table_name);
66+
67+
auto &update_clauses = update_stmt.updates;
68+
std::set<oid_t> update_oids;
69+
for (const auto &update_clause : update_clauses) {
70+
LOG_TRACE("Affected column name for table(%s) in UPDATE query: %s",
71+
table_name.c_str(), update_clause->column.c_str());
72+
auto col_object = table_object->GetColumnObject(update_clause->column);
73+
update_oids.insert(col_object->GetColumnId());
74+
}
75+
76+
auto indexes_map = table_object->GetIndexObjects();
77+
for (auto &index : indexes_map) {
78+
LOG_TRACE("Checking if UPDATE query affects index: %s",
79+
index.second->GetIndexName().c_str());
80+
const std::vector<oid_t> &key_attrs =
81+
index.second->GetKeyAttrs(); // why it's a vector, and not set?
82+
const std::set<oid_t> key_attrs_set(key_attrs.begin(), key_attrs.end());
83+
if (!SetUtil::IsDisjoint(key_attrs_set, update_oids)) {
84+
LOG_TRACE("Index (%s) is affected",
85+
index.second->GetIndexName().c_str());
86+
index_oids.insert(index.first);
87+
}
88+
}
89+
} break;
90+
case StatementType::SELECT:
91+
break;
92+
default:
93+
LOG_TRACE("Does not support finding affected indexes for query type: %d",
94+
static_cast<int>(sql_stmt.GetType()));
95+
}
96+
return (index_oids);
97+
}
98+
99+
} // namespace planner
100+
} // namespace peloton

0 commit comments

Comments
 (0)