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

Commit 1e21a52

Browse files
committed
These should be the last files that we need from from #1345
1 parent bee9751 commit 1e21a52

File tree

7 files changed

+249
-5
lines changed

7 files changed

+249
-5
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// sequence_functions_proxy.cpp
6+
//
7+
// Identification: src/codegen/proxy/sequence_functions_proxy.cpp
8+
//
9+
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "codegen/proxy/sequence_functions_proxy.h"
14+
15+
#include "codegen/proxy/executor_context_proxy.h"
16+
17+
namespace peloton {
18+
namespace codegen {
19+
20+
DEFINE_METHOD(peloton::function, SequenceFunctions, Nextval);
21+
DEFINE_METHOD(peloton::function, SequenceFunctions, Currval);
22+
23+
} // namespace codegen
24+
} // namespace peloton

src/codegen/type/bigint_type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "codegen/type/boolean_type.h"
2121
#include "codegen/type/decimal_type.h"
2222
#include "codegen/type/integer_type.h"
23+
#include "codegen/type/varchar_type.h"
2324
#include "common/exception.h"
2425
#include "type/limits.h"
2526
#include "util/string_util.h"
@@ -513,7 +514,6 @@ struct Nextval : public TypeSystem::UnaryOperatorHandleNull {
513514
Type ResultType(UNUSED_ATTRIBUTE const Type &val_type) const override {
514515
return BigInt::Instance();
515516
}
516-
517517
Value Impl(CodeGen &codegen, const Value &val,
518518
const TypeSystem::InvocationContext &ctx) const override {
519519
llvm::Value *executor_ctx = ctx.executor_context;
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// sequence_functions.cpp
6+
//
7+
// Identification: src/function/sequence_functions.cpp
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "function/sequence_functions.h"
14+
15+
#include "common/macros.h"
16+
#include "executor/executor_context.h"
17+
#include "catalog/catalog.h"
18+
#include "catalog/database_catalog.h"
19+
#include "catalog/sequence_catalog.h"
20+
#include "concurrency/transaction_context.h"
21+
#include "concurrency/transaction_manager_factory.h"
22+
#include "type/value_factory.h"
23+
24+
namespace peloton {
25+
namespace function {
26+
27+
/*
28+
* @brief The actual implementation to get the incremented value for the specified sequence
29+
* @param sequence name
30+
* @param executor context
31+
* @return the next value for the sequence
32+
* @exception the sequence does not exist
33+
*/
34+
uint32_t SequenceFunctions::Nextval(executor::ExecutorContext &ctx,
35+
const char *sequence_name) {
36+
PELOTON_ASSERT(sequence_name != nullptr);
37+
concurrency::TransactionContext* txn = ctx.GetTransaction();
38+
// get the database oid for this transaction
39+
oid_t database_oid = catalog::Catalog::GetInstance()
40+
->GetDatabaseObject(ctx.GetDatabaseName(), txn)->GetDatabaseOid();
41+
LOG_DEBUG("Get database oid: %u", database_oid);
42+
43+
// initialize a new transaction for incrementing sequence value
44+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
45+
auto mini_txn = txn_manager.BeginTransaction();
46+
47+
// evict the old cached copy of sequence
48+
txn->catalog_cache.EvictSequenceObject(sequence_name,database_oid);
49+
auto sequence_object =
50+
catalog::Catalog::GetInstance()
51+
->GetSystemCatalogs(database_oid)
52+
->GetSequenceCatalog()
53+
->GetSequence(database_oid, sequence_name, mini_txn);
54+
55+
if (sequence_object != nullptr) {
56+
uint32_t val = sequence_object->GetNextVal();
57+
int64_t curr_val = sequence_object->GetCurrVal();
58+
// insert the new copy of sequence into cache for future currval
59+
txn->catalog_cache.InsertSequenceObject(sequence_object);
60+
61+
auto ret = txn_manager.CommitTransaction(mini_txn);
62+
if (ret != ResultType::SUCCESS) {
63+
txn_manager.AbortTransaction(mini_txn);
64+
return Nextval(ctx, sequence_name);
65+
}
66+
67+
catalog::Catalog::GetInstance()
68+
->GetSystemCatalogs(database_oid)
69+
->GetSequenceCatalog()
70+
->InsertCurrValCache(txn->GetTemporarySchemaName(),
71+
sequence_name, curr_val);
72+
return val;
73+
} else {
74+
throw SequenceException(
75+
StringUtil::Format("Sequence \"%s\" does not exist", sequence_name));
76+
}
77+
}
78+
79+
/*
80+
* @brief The actual implementation to get the current value for the specified sequence
81+
* @param sequence name
82+
* @param executor context
83+
* @return the current value of a sequence
84+
* @exception either the sequence does not exist, or 'call nextval before currval'
85+
*/
86+
uint32_t SequenceFunctions::Currval(executor::ExecutorContext &ctx,
87+
const char *sequence_name) {
88+
PELOTON_ASSERT(sequence_name != nullptr);
89+
concurrency::TransactionContext* txn = ctx.GetTransaction();
90+
// get the database oid for this transaction
91+
oid_t database_oid = catalog::Catalog::GetInstance()
92+
->GetDatabaseObject(ctx.GetDatabaseName(), txn)->GetDatabaseOid();
93+
LOG_DEBUG("Get database oid: %u", database_oid);
94+
95+
// get the sequence copy from cache
96+
auto sequence_catalog = catalog::Catalog::GetInstance()
97+
->GetSystemCatalogs(database_oid)
98+
->GetSequenceCatalog();
99+
100+
if(sequence_catalog->CheckCachedCurrValExistence(
101+
txn->GetTemporarySchemaName(), std::string(sequence_name))) {
102+
return sequence_catalog->GetCachedCurrVal(
103+
txn->GetTemporarySchemaName(), std::string(sequence_name));
104+
} else {
105+
// get sequence from catalog
106+
auto sequence_object = sequence_catalog
107+
->GetSequence(database_oid, sequence_name, txn);
108+
if (sequence_object != nullptr) {
109+
throw SequenceException(
110+
StringUtil::Format("currval for sequence \"%s\" is undefined for this session",
111+
sequence_name));
112+
} else {
113+
// sequence does not exist
114+
throw SequenceException(
115+
StringUtil::Format("Sequence \"%s\" does not exist", sequence_name));
116+
}
117+
}
118+
}
119+
120+
/*
121+
* @brief The wrapper function to get the incremented value for the specified sequence
122+
* @param sequence name
123+
* @param executor context
124+
* @return the result of executing NextVal
125+
*/
126+
type::Value SequenceFunctions::_Nextval(const std::vector<type::Value> &args) {
127+
executor::ExecutorContext* ctx = (executor::ExecutorContext*)args[1].GetAs<uint64_t>();
128+
uint32_t ret = SequenceFunctions::Nextval(*ctx, args[0].GetAs<const char *>());
129+
return type::ValueFactory::GetIntegerValue(ret);
130+
}
131+
132+
/*
133+
* @brief The wrapper function to get the current value for the specified sequence
134+
* @param sequence name
135+
* @param executor context
136+
* @return the result of executing CurrVal
137+
*/
138+
type::Value SequenceFunctions::_Currval(const std::vector<type::Value> &args) {
139+
executor::ExecutorContext* ctx = (executor::ExecutorContext*)args[1].GetAs<uint64_t>();
140+
uint32_t ret = SequenceFunctions::Currval(*ctx, args[0].GetAs<const char *>());
141+
return type::ValueFactory::GetIntegerValue(ret);
142+
}
143+
144+
} // namespace function
145+
} // namespace peloton
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// string_functions_proxy.h
6+
//
7+
// Identification: src/include/codegen/proxy/string_functions_proxy.h
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
15+
#include "codegen/proxy/proxy.h"
16+
#include "codegen/proxy/type_builder.h"
17+
#include "function/sequence_functions.h"
18+
19+
namespace peloton {
20+
namespace codegen {
21+
22+
PROXY(SequenceFunctions) {
23+
DECLARE_METHOD(Nextval);
24+
DECLARE_METHOD(Currval);
25+
};
26+
27+
} // namespace codegen
28+
} // namespace peloton
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// sequence_functions.h
6+
//
7+
// Identification: src/include/function/sequence_functions.h
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
15+
#include <cstdint>
16+
#include "type/value.h"
17+
18+
namespace peloton {
19+
20+
namespace executor {
21+
class ExecutorContext;
22+
} // namespace executor
23+
24+
namespace function {
25+
26+
class SequenceFunctions {
27+
public:
28+
29+
// Nextval will return the next value of the given sequence
30+
static uint32_t Nextval(executor::ExecutorContext &ctx, const char *sequence_name);
31+
32+
// Currval will return the current value of the given sequence
33+
static uint32_t Currval(executor::ExecutorContext &ctx, const char *sequence_name);
34+
35+
// Wrapper function used for AddBuiltin Functions
36+
static type::Value _Nextval(const std::vector<type::Value> &args);
37+
static type::Value _Currval(const std::vector<type::Value> &args);
38+
};
39+
40+
} // namespace function
41+
} // namespace peloton

src/include/planner/create_plan.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ class CreatePlan : public AbstractPlan {
5151
public:
5252
CreatePlan() = delete;
5353

54-
// This construnctor is for Create Database Test used only
54+
// This constructor is for Create Database Test used only
5555
explicit CreatePlan(std::string database_name, CreateType c_type);
5656

57-
// This construnctor is for copy() used only
57+
// This constructor is for copy() used only
5858
explicit CreatePlan(std::string table_name, std::string schema_name,
5959
std::string database_name,
6060
std::unique_ptr<catalog::Schema> schema,
@@ -122,8 +122,6 @@ class CreatePlan : public AbstractPlan {
122122
int64_t GetSequenceCacheSize() const { return seq_cache; }
123123
bool GetSequenceCycle() const { return seq_cycle; }
124124

125-
OnCommitAction GetCommitOption() const { return commit_option; }
126-
127125
protected:
128126
// This is a helper method for extracting foreign key information
129127
// and storing it in an internal struct.

src/parser/drop_statement.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ const std::string DropStatement::GetInfo(int num_indent) const {
6666
<< "Trigger name: " << trigger_name_;
6767
break;
6868
}
69+
case kSequence: {
70+
os << "DropType: Sequence\n";
71+
os << StringUtil::Indent(num_indent + 1)
72+
<< "Sequence database name: " << GetDatabaseName() << std::endl;
73+
os << StringUtil::Indent(num_indent + 1)
74+
<< "Sequence name: " << sequence_name_;
75+
break;
76+
}
6977
}
7078
os << std::endl;
7179
os << StringUtil::Indent(num_indent + 1)

0 commit comments

Comments
 (0)