Skip to content

Commit

Permalink
These should be the last files that we need from from cmu-db#1345
Browse files Browse the repository at this point in the history
  • Loading branch information
apavlo committed Jun 11, 2018
1 parent bee9751 commit 1e21a52
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/codegen/proxy/sequence_functions_proxy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// sequence_functions_proxy.cpp
//
// Identification: src/codegen/proxy/sequence_functions_proxy.cpp
//
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "codegen/proxy/sequence_functions_proxy.h"

#include "codegen/proxy/executor_context_proxy.h"

namespace peloton {
namespace codegen {

DEFINE_METHOD(peloton::function, SequenceFunctions, Nextval);
DEFINE_METHOD(peloton::function, SequenceFunctions, Currval);

} // namespace codegen
} // namespace peloton
2 changes: 1 addition & 1 deletion src/codegen/type/bigint_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "codegen/type/boolean_type.h"
#include "codegen/type/decimal_type.h"
#include "codegen/type/integer_type.h"
#include "codegen/type/varchar_type.h"
#include "common/exception.h"
#include "type/limits.h"
#include "util/string_util.h"
Expand Down Expand Up @@ -513,7 +514,6 @@ struct Nextval : public TypeSystem::UnaryOperatorHandleNull {
Type ResultType(UNUSED_ATTRIBUTE const Type &val_type) const override {
return BigInt::Instance();
}

Value Impl(CodeGen &codegen, const Value &val,
const TypeSystem::InvocationContext &ctx) const override {
llvm::Value *executor_ctx = ctx.executor_context;
Expand Down
145 changes: 145 additions & 0 deletions src/function/sequence_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// sequence_functions.cpp
//
// Identification: src/function/sequence_functions.cpp
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "function/sequence_functions.h"

#include "common/macros.h"
#include "executor/executor_context.h"
#include "catalog/catalog.h"
#include "catalog/database_catalog.h"
#include "catalog/sequence_catalog.h"
#include "concurrency/transaction_context.h"
#include "concurrency/transaction_manager_factory.h"
#include "type/value_factory.h"

namespace peloton {
namespace function {

/*
* @brief The actual implementation to get the incremented value for the specified sequence
* @param sequence name
* @param executor context
* @return the next value for the sequence
* @exception the sequence does not exist
*/
uint32_t SequenceFunctions::Nextval(executor::ExecutorContext &ctx,
const char *sequence_name) {
PELOTON_ASSERT(sequence_name != nullptr);
concurrency::TransactionContext* txn = ctx.GetTransaction();
// get the database oid for this transaction
oid_t database_oid = catalog::Catalog::GetInstance()
->GetDatabaseObject(ctx.GetDatabaseName(), txn)->GetDatabaseOid();
LOG_DEBUG("Get database oid: %u", database_oid);

// initialize a new transaction for incrementing sequence value
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto mini_txn = txn_manager.BeginTransaction();

// evict the old cached copy of sequence
txn->catalog_cache.EvictSequenceObject(sequence_name,database_oid);
auto sequence_object =
catalog::Catalog::GetInstance()
->GetSystemCatalogs(database_oid)
->GetSequenceCatalog()
->GetSequence(database_oid, sequence_name, mini_txn);

if (sequence_object != nullptr) {
uint32_t val = sequence_object->GetNextVal();
int64_t curr_val = sequence_object->GetCurrVal();
// insert the new copy of sequence into cache for future currval
txn->catalog_cache.InsertSequenceObject(sequence_object);

auto ret = txn_manager.CommitTransaction(mini_txn);
if (ret != ResultType::SUCCESS) {
txn_manager.AbortTransaction(mini_txn);
return Nextval(ctx, sequence_name);
}

catalog::Catalog::GetInstance()
->GetSystemCatalogs(database_oid)
->GetSequenceCatalog()
->InsertCurrValCache(txn->GetTemporarySchemaName(),
sequence_name, curr_val);
return val;
} else {
throw SequenceException(
StringUtil::Format("Sequence \"%s\" does not exist", sequence_name));
}
}

/*
* @brief The actual implementation to get the current value for the specified sequence
* @param sequence name
* @param executor context
* @return the current value of a sequence
* @exception either the sequence does not exist, or 'call nextval before currval'
*/
uint32_t SequenceFunctions::Currval(executor::ExecutorContext &ctx,
const char *sequence_name) {
PELOTON_ASSERT(sequence_name != nullptr);
concurrency::TransactionContext* txn = ctx.GetTransaction();
// get the database oid for this transaction
oid_t database_oid = catalog::Catalog::GetInstance()
->GetDatabaseObject(ctx.GetDatabaseName(), txn)->GetDatabaseOid();
LOG_DEBUG("Get database oid: %u", database_oid);

// get the sequence copy from cache
auto sequence_catalog = catalog::Catalog::GetInstance()
->GetSystemCatalogs(database_oid)
->GetSequenceCatalog();

if(sequence_catalog->CheckCachedCurrValExistence(
txn->GetTemporarySchemaName(), std::string(sequence_name))) {
return sequence_catalog->GetCachedCurrVal(
txn->GetTemporarySchemaName(), std::string(sequence_name));
} else {
// get sequence from catalog
auto sequence_object = sequence_catalog
->GetSequence(database_oid, sequence_name, txn);
if (sequence_object != nullptr) {
throw SequenceException(
StringUtil::Format("currval for sequence \"%s\" is undefined for this session",
sequence_name));
} else {
// sequence does not exist
throw SequenceException(
StringUtil::Format("Sequence \"%s\" does not exist", sequence_name));
}
}
}

/*
* @brief The wrapper function to get the incremented value for the specified sequence
* @param sequence name
* @param executor context
* @return the result of executing NextVal
*/
type::Value SequenceFunctions::_Nextval(const std::vector<type::Value> &args) {
executor::ExecutorContext* ctx = (executor::ExecutorContext*)args[1].GetAs<uint64_t>();
uint32_t ret = SequenceFunctions::Nextval(*ctx, args[0].GetAs<const char *>());
return type::ValueFactory::GetIntegerValue(ret);
}

/*
* @brief The wrapper function to get the current value for the specified sequence
* @param sequence name
* @param executor context
* @return the result of executing CurrVal
*/
type::Value SequenceFunctions::_Currval(const std::vector<type::Value> &args) {
executor::ExecutorContext* ctx = (executor::ExecutorContext*)args[1].GetAs<uint64_t>();
uint32_t ret = SequenceFunctions::Currval(*ctx, args[0].GetAs<const char *>());
return type::ValueFactory::GetIntegerValue(ret);
}

} // namespace function
} // namespace peloton
28 changes: 28 additions & 0 deletions src/include/codegen/proxy/sequence_functions_proxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// string_functions_proxy.h
//
// Identification: src/include/codegen/proxy/string_functions_proxy.h
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once

#include "codegen/proxy/proxy.h"
#include "codegen/proxy/type_builder.h"
#include "function/sequence_functions.h"

namespace peloton {
namespace codegen {

PROXY(SequenceFunctions) {
DECLARE_METHOD(Nextval);
DECLARE_METHOD(Currval);
};

} // namespace codegen
} // namespace peloton
41 changes: 41 additions & 0 deletions src/include/function/sequence_functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// sequence_functions.h
//
// Identification: src/include/function/sequence_functions.h
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#pragma once

#include <cstdint>
#include "type/value.h"

namespace peloton {

namespace executor {
class ExecutorContext;
} // namespace executor

namespace function {

class SequenceFunctions {
public:

// Nextval will return the next value of the given sequence
static uint32_t Nextval(executor::ExecutorContext &ctx, const char *sequence_name);

// Currval will return the current value of the given sequence
static uint32_t Currval(executor::ExecutorContext &ctx, const char *sequence_name);

// Wrapper function used for AddBuiltin Functions
static type::Value _Nextval(const std::vector<type::Value> &args);
static type::Value _Currval(const std::vector<type::Value> &args);
};

} // namespace function
} // namespace peloton
6 changes: 2 additions & 4 deletions src/include/planner/create_plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class CreatePlan : public AbstractPlan {
public:
CreatePlan() = delete;

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

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

OnCommitAction GetCommitOption() const { return commit_option; }

protected:
// This is a helper method for extracting foreign key information
// and storing it in an internal struct.
Expand Down
8 changes: 8 additions & 0 deletions src/parser/drop_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ const std::string DropStatement::GetInfo(int num_indent) const {
<< "Trigger name: " << trigger_name_;
break;
}
case kSequence: {
os << "DropType: Sequence\n";
os << StringUtil::Indent(num_indent + 1)
<< "Sequence database name: " << GetDatabaseName() << std::endl;
os << StringUtil::Indent(num_indent + 1)
<< "Sequence name: " << sequence_name_;
break;
}
}
os << std::endl;
os << StringUtil::Indent(num_indent + 1)
Expand Down

0 comments on commit 1e21a52

Please sign in to comment.