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

Commit

Permalink
merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allisonwang committed Nov 17, 2017
1 parent 6470a0f commit d429ac6
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 247 deletions.
6 changes: 1 addition & 5 deletions src/codegen/values_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ void ValuesRuntime::OutputDecimal(char *values, uint32_t idx, double val) {
void ValuesRuntime::OutputVarchar(char *values, uint32_t idx, char *str,
UNUSED_ATTRIBUTE uint32_t len) {
type::Value *vals = reinterpret_cast<type::Value *>(values);
// There are two types of VARCHAR: one is from storage and the other is from
// the query, e.g. CASE. In the latter case, the variable 'len' does not
// include a null terminator at the end of the array, while it does in the
// former. This function is used for both of them, so computes based on 'str'.
vals[idx] = type::ValueFactory::GetVarcharValue(str, false);
vals[idx] = type::ValueFactory::GetVarcharValue(str, len, false);
}

void ValuesRuntime::OutputVarbinary(char *values, uint32_t idx, char *ptr,
Expand Down
189 changes: 189 additions & 0 deletions test/sql/type_sql_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// type_sql_test.cpp
//
// Identification: test/sql/type_sql_test.cpp
//
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include <memory>

#include "catalog/catalog.h"
#include "common/harness.h"
#include "concurrency/transaction_manager_factory.h"
#include "sql/testing_sql_util.h"

namespace peloton {
namespace test {

class TypeSQLTests : public PelotonTest {
protected:
virtual void SetUp() override {
PelotonTest::SetUp();

auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto txn = txn_manager.BeginTransaction();
catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
catalog::Catalog::GetInstance()->Bootstrap();
txn_manager.CommitTransaction(txn);
}

virtual void TearDown() override {
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto txn = txn_manager.BeginTransaction();
catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
txn_manager.CommitTransaction(txn);

PelotonTest::TearDown();
}
};

/**
* Check whether we can INSERT values that we have reserved for our NULL
* indicators The DBMS should throw an error to prevent you from doing that
*/
TEST_F(TypeSQLTests, TypeLimitSQLTest) {
const std::vector<type::TypeId> typeLimitSQLTestTypes = {
type::TypeId::BOOLEAN, type::TypeId::TINYINT, type::TypeId::SMALLINT,
type::TypeId::INTEGER, type::TypeId::TIMESTAMP,
// FIXME type::TypeId::BIGINT,
// FIXME type::TypeId::DECIMAL,
// FIXME type::TypeId::DATE
};

for (auto col_type : typeLimitSQLTestTypes) {
// CREATE TABLE that contains a column for each type
std::string table_name = "tbl" + TypeIdToString(col_type);
std::string sql = StringUtil::Format(
"CREATE TABLE %s(id INT PRIMARY KEY, b %s);", table_name.c_str(),
TypeIdToString(col_type).c_str());
LOG_TRACE("SQL: %s", sql.c_str());
TestingSQLUtil::ExecuteSQLQuery(sql);

// Then try to insert the min value
std::ostringstream os;
os << "INSERT INTO " << table_name << " VALUES (1, ";
switch (col_type) {
case type::TypeId::BOOLEAN:
os << (int32_t)type::PELOTON_BOOLEAN_NULL;
break;
case type::TypeId::TINYINT:
os << (int32_t)type::PELOTON_INT8_NULL;
break;
case type::TypeId::SMALLINT:
os << type::PELOTON_INT16_NULL;
break;
case type::TypeId::INTEGER:
os << type::PELOTON_INT32_NULL;
break;
case type::TypeId::BIGINT:
os << type::PELOTON_INT64_NULL;
break;
case type::TypeId::DECIMAL:
os << type::PELOTON_DECIMAL_NULL;
break;
case type::TypeId::TIMESTAMP:
os << type::PELOTON_TIMESTAMP_NULL;
break;
case type::TypeId::DATE:
os << type::PELOTON_DATE_NULL;
break;
default: {
// Nothing to do!
}
} // SWITCH
os << ");";
// This should throw an error because the query
// is trying to insert a value that is not in a valid range for the type
auto result = TestingSQLUtil::ExecuteSQLQuery(os.str());
LOG_TRACE("%s => %s", TypeIdToString(col_type).c_str(), os.str().c_str());
EXPECT_EQ(ResultType::FAILURE, result);
}
}

void CheckQueryResult(std::vector<StatementResult> result,
std::vector<std::string> expected,
size_t tuple_descriptor_size) {
EXPECT_EQ(result.size(), expected.size());
for (size_t i = 0; i < result.size(); i++) {
for (size_t j = 0; j < tuple_descriptor_size; j++) {
int idx = i * tuple_descriptor_size + j;
std::string s =
std::string(result[idx].second.begin(), result[idx].second.end());
EXPECT_EQ(s, expected[i]);
}
}
}

TEST_F(TypeSQLTests, VarcharTest) {
TestingSQLUtil::ExecuteSQLQuery("CREATE TABLE foo(name varchar(250));");
std::vector<std::string> names{"Alice", "Peter", "Cathy",
"Bob", "Alicia", "David"};
for (size_t i = 0; i < names.size(); i++) {
std::string sql = "INSERT INTO foo VALUES ('" + names[i] + "');";
TestingSQLUtil::ExecuteSQLQuery(sql);
}

std::vector<StatementResult> result;
std::vector<FieldInfo> tuple_descriptor;
std::string error_message;
int rows_changed;

std::string query;
std::vector<std::string> expected{};

query = "SELECT * FROM foo WHERE name = 'Alice';";
expected = {"Alice"};
TestingSQLUtil::ExecuteSQLQuery(query.c_str(), result, tuple_descriptor,
rows_changed, error_message);
CheckQueryResult(result, expected, tuple_descriptor.size());

query = "SELECT * FROM foo WHERE name = 'david';";
expected = {};
TestingSQLUtil::ExecuteSQLQuery(query.c_str(), result, tuple_descriptor,
rows_changed, error_message);
CheckQueryResult(result, expected, tuple_descriptor.size());

query = "SELECT * FROM foo WHERE name = 'Ann';";
expected = {};
TestingSQLUtil::ExecuteSQLQuery(query.c_str(), result, tuple_descriptor,
rows_changed, error_message);
CheckQueryResult(result, expected, tuple_descriptor.size());

query = "SELECT * FROM foo WHERE name = 'Alice' OR name = 'Alicia';";
expected = {"Alice", "Alicia"};
TestingSQLUtil::ExecuteSQLQuery(query.c_str(), result, tuple_descriptor,
rows_changed, error_message);
CheckQueryResult(result, expected, tuple_descriptor.size());

query = "SELECT * FROM foo WHERE name != 'Bob' AND name != 'David';";
expected = {"Alice", "Peter", "Cathy", "Alicia"};
TestingSQLUtil::ExecuteSQLQuery(query.c_str(), result, tuple_descriptor,
rows_changed, error_message);
CheckQueryResult(result, expected, tuple_descriptor.size());

query = "SELECT * FROM foo WHERE name >= 'A' AND name <= 'D';";
expected = {"Alice", "Cathy", "Bob", "Alicia"};
TestingSQLUtil::ExecuteSQLQuery(query.c_str(), result, tuple_descriptor,
rows_changed, error_message);
CheckQueryResult(result, expected, tuple_descriptor.size());

query = "SELECT * FROM foo WHERE name > 'David';";
expected = {"Peter"};
TestingSQLUtil::ExecuteSQLQuery(query.c_str(), result, tuple_descriptor,
rows_changed, error_message);
CheckQueryResult(result, expected, tuple_descriptor.size());

query = "SELECT * FROM foo WHERE name <= 'Alicia';";
expected = {"Alice", "Alicia"};
TestingSQLUtil::ExecuteSQLQuery(query.c_str(), result, tuple_descriptor,
rows_changed, error_message);
CheckQueryResult(result, expected, tuple_descriptor.size());
}

} // namespace test
} // namespace peloton
104 changes: 0 additions & 104 deletions test/sql/typelimit_sql_test.cpp

This file was deleted.

0 comments on commit d429ac6

Please sign in to comment.