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

Commit bee9751

Browse files
committed
Checking in the testing files from #1345
1 parent c0a9bd4 commit bee9751

File tree

3 files changed

+330
-0
lines changed

3 files changed

+330
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// SequenceTest.java
6+
//
7+
// Identification: script/testing/junit/SequenceTest.java
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import java.sql.*;
14+
import org.junit.*;
15+
import org.postgresql.util.PSQLException;
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertTrue;
18+
import static junit.framework.TestCase.fail;
19+
20+
public class SequenceTest extends PLTestBase {
21+
private Connection conn1;
22+
private Connection conn2;
23+
24+
private static final String SQL_DROP_SEQ =
25+
"DROP SEQUENCE seq;";
26+
27+
private static final String SQL_CREATE_SEQ =
28+
"CREATE SEQUENCE seq;";
29+
30+
private static final String SQL_NEXTVAL =
31+
"SELECT NEXTVAL('seq')";
32+
33+
private static final String SQL_CURRVAL =
34+
"SELECT CURRVAL('seq')";
35+
36+
/**
37+
* Test sequence functions for single-statment transactions
38+
*/
39+
@Test
40+
public void test_SingleStmtTxn() throws SQLException {
41+
conn1 = makeDefaultConnection();
42+
conn1.setAutoCommit(true);
43+
Statement stmt1 = conn1.createStatement();
44+
45+
conn2 = makeDefaultConnection();
46+
conn2.setAutoCommit(true);
47+
Statement stmt2 = conn2.createStatement();
48+
49+
// Create a sequence
50+
stmt1.execute(SQL_CREATE_SEQ);
51+
52+
// Check the sequence is visible by others
53+
try {
54+
stmt2.execute(SQL_CREATE_SEQ);
55+
fail();
56+
} catch (PSQLException e) { }
57+
58+
// Check currval cannot be called before nextval
59+
try {
60+
stmt1.execute(SQL_CURRVAL);
61+
fail();
62+
} catch (PSQLException e) { }
63+
64+
// Check functionality with conn1
65+
stmt1.execute(SQL_NEXTVAL);
66+
ResultSet res1 = stmt1.executeQuery(SQL_CURRVAL);
67+
res1.next();
68+
assertEquals(1, res1.getInt(1));
69+
assertNoMoreRows(res1);
70+
71+
// Update should be visible to conn2
72+
stmt2.execute(SQL_NEXTVAL);
73+
ResultSet res2 = stmt2.executeQuery(SQL_CURRVAL);
74+
res2.next();
75+
assertEquals(2, res2.getInt(1));
76+
assertNoMoreRows(res2);
77+
78+
// Currval should be session consistent
79+
res1 = stmt1.executeQuery(SQL_CURRVAL);
80+
res1.next();
81+
assertEquals(1, res1.getInt(1));
82+
assertNoMoreRows(res1);
83+
84+
// Clean up
85+
stmt1.close();
86+
conn1.close();
87+
stmt2.close();
88+
conn2.close();
89+
}
90+
91+
/**
92+
* Test sequence functions for multi-statment transactions
93+
*/
94+
@Test
95+
public void test_MultiStmtTxn() throws SQLException {
96+
conn1 = makeDefaultConnection();
97+
conn1.setAutoCommit(false);
98+
Statement stmt1 = conn1.createStatement();
99+
100+
conn2 = makeDefaultConnection();
101+
conn2.setAutoCommit(false);
102+
Statement stmt2 = conn2.createStatement();
103+
104+
// Check functionality with conn1
105+
stmt1.execute(SQL_NEXTVAL);
106+
ResultSet res1 = stmt1.executeQuery(SQL_CURRVAL);
107+
res1.next();
108+
assertEquals(3, res1.getInt(1));
109+
assertNoMoreRows(res1);
110+
111+
// Update should be visible to conn2
112+
stmt2.execute(SQL_NEXTVAL);
113+
ResultSet res2 = stmt2.executeQuery(SQL_CURRVAL);
114+
res2.next();
115+
assertEquals(4, res2.getInt(1));
116+
assertNoMoreRows(res2);
117+
118+
// Rollback transactions
119+
conn1.rollback();
120+
conn2.rollback();
121+
122+
// Check sequence incremental will not rollback
123+
conn1.setAutoCommit(true);
124+
stmt1.execute(SQL_NEXTVAL);
125+
res1 = stmt1.executeQuery(SQL_CURRVAL);
126+
res1.next();
127+
assertEquals(5, res1.getInt(1));
128+
assertNoMoreRows(res1);
129+
130+
// Clean up
131+
stmt1.close();
132+
conn1.close();
133+
stmt2.close();
134+
conn2.close();
135+
}
136+
137+
/**
138+
* Test dropping sequence
139+
*/
140+
@Test
141+
public void test_Drop_Seq() throws SQLException {
142+
conn1 = makeDefaultConnection();
143+
conn1.setAutoCommit(true);
144+
Statement stmt1 = conn1.createStatement();
145+
146+
conn2 = makeDefaultConnection();
147+
conn2.setAutoCommit(true);
148+
Statement stmt2 = conn2.createStatement();
149+
150+
// Drop the sequence
151+
stmt1.execute(SQL_DROP_SEQ);
152+
153+
// Check the sequence is invisible to all conns
154+
try {
155+
stmt1.execute(SQL_CURRVAL);
156+
fail();
157+
} catch (PSQLException e) { }
158+
try {
159+
stmt2.execute(SQL_CURRVAL);
160+
fail();
161+
} catch (PSQLException e) { }
162+
163+
// Check the same sequence can be created w/o exception
164+
stmt2.execute(SQL_CREATE_SEQ);
165+
166+
// Clean up
167+
stmt1.close();
168+
conn1.close();
169+
stmt2.close();
170+
conn2.close();
171+
}
172+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// sequence_functions_test.cpp
6+
//
7+
// Identification: test/function/sequence_functions_test.cpp
8+
//
9+
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <set>
14+
#include <string>
15+
#include <vector>
16+
17+
#include "catalog/catalog.h"
18+
#include "catalog/sequence_catalog.h"
19+
#include "storage/abstract_table.h"
20+
#include "common/harness.h"
21+
#include "common/exception.h"
22+
#include "executor/executors.h"
23+
#include "parser/postgresparser.h"
24+
#include "planner/create_plan.h"
25+
#include "executor/executor_context.h"
26+
#include "function/sequence_functions.h"
27+
#include "concurrency/transaction_manager_factory.h"
28+
29+
using ::testing::NotNull;
30+
using ::testing::Return;
31+
32+
namespace peloton {
33+
namespace test {
34+
35+
class SequenceFunctionsTests : public PelotonTest {
36+
37+
protected:
38+
void CreateDatabaseHelper() {
39+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
40+
auto txn = txn_manager.BeginTransaction();
41+
catalog::Catalog::GetInstance()->Bootstrap();
42+
catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
43+
txn_manager.CommitTransaction(txn);
44+
}
45+
46+
void CreateSequenceHelper(std::string query,
47+
concurrency::TransactionContext *txn) {
48+
auto parser = parser::PostgresParser::GetInstance();
49+
50+
std::unique_ptr<parser::SQLStatementList> stmt_list(
51+
parser.BuildParseTree(query).release());
52+
EXPECT_TRUE(stmt_list->is_valid);
53+
EXPECT_EQ(StatementType::CREATE, stmt_list->GetStatement(0)->GetType());
54+
auto create_sequence_stmt =
55+
static_cast<parser::CreateStatement *>(stmt_list->GetStatement(0));
56+
57+
create_sequence_stmt->TryBindDatabaseName(DEFAULT_DB_NAME);
58+
// Create plans
59+
planner::CreatePlan plan(create_sequence_stmt);
60+
61+
// plan type
62+
EXPECT_EQ(CreateType::SEQUENCE, plan.GetCreateType());
63+
64+
// Execute the create sequence
65+
std::unique_ptr<executor::ExecutorContext> context(
66+
new executor::ExecutorContext(txn, {}));
67+
executor::CreateExecutor createSequenceExecutor(&plan, context.get());
68+
createSequenceExecutor.Init();
69+
createSequenceExecutor.Execute();
70+
}
71+
72+
};
73+
TEST_F(SequenceFunctionsTests, BasicTest) {
74+
CreateDatabaseHelper();
75+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
76+
auto txn = txn_manager.BeginTransaction();
77+
78+
// Create statement
79+
std::string query = "CREATE SEQUENCE seq;";
80+
81+
CreateSequenceHelper(query, txn);
82+
txn_manager.CommitTransaction(txn);
83+
}
84+
85+
TEST_F(SequenceFunctionsTests, FunctionsTest) {
86+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
87+
auto txn = txn_manager.BeginTransaction();
88+
std::unique_ptr<executor::ExecutorContext> context(
89+
new executor::ExecutorContext(txn, {}));
90+
91+
// Expect exception
92+
try {
93+
function::SequenceFunctions::Currval(*(context.get()), "seq");
94+
EXPECT_EQ(0, 1);
95+
} catch (const SequenceException &expected) {
96+
ASSERT_STREQ("currval for sequence \"seq\" is undefined for this session", expected.what());
97+
}
98+
99+
// Check nextval & currval functionality
100+
auto res = function::SequenceFunctions::Nextval(*(context.get()), "seq");
101+
EXPECT_EQ(1, res);
102+
103+
res = function::SequenceFunctions::Currval(*(context.get()), "seq");
104+
EXPECT_EQ(1, res);
105+
106+
txn_manager.CommitTransaction(txn);
107+
}
108+
} // namespace test
109+
} // namespace peloton

test/parser/postgresparser_test.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,55 @@ TEST_F(PostgresParserTests, DropTriggerTest) {
10691069
EXPECT_EQ("films", drop_trigger_stmt->GetTriggerTableName());
10701070
}
10711071

1072+
TEST_F(PostgresParserTests, CreateSequenceTest) {
1073+
auto parser = parser::PostgresParser::GetInstance();
1074+
1075+
// missing AS, CACHE and OWNED BY.
1076+
std::string query =
1077+
"CREATE SEQUENCE seq "
1078+
"INCREMENT BY 2 "
1079+
"MINVALUE 10 "
1080+
"MAXVALUE 50 "
1081+
"CYCLE "
1082+
"START 10;";
1083+
std::unique_ptr<parser::SQLStatementList> stmt_list(
1084+
parser.BuildParseTree(query).release());
1085+
EXPECT_TRUE(stmt_list->is_valid);
1086+
EXPECT_EQ(StatementType::CREATE, stmt_list->GetStatement(0)->GetType());
1087+
auto create_sequence_stmt =
1088+
static_cast<parser::CreateStatement *>(stmt_list->GetStatement(0));
1089+
1090+
// The following code checks the arguments in the create statement
1091+
// are identical to what is specified in the query.
1092+
1093+
// create type
1094+
EXPECT_EQ(parser::CreateStatement::CreateType::kSequence,
1095+
create_sequence_stmt->type);
1096+
EXPECT_EQ(10, create_sequence_stmt->seq_start);
1097+
EXPECT_EQ(2, create_sequence_stmt->seq_increment);
1098+
EXPECT_EQ(50, create_sequence_stmt->seq_max_value);
1099+
EXPECT_EQ(10, create_sequence_stmt->seq_min_value);
1100+
EXPECT_EQ(true, create_sequence_stmt->seq_cycle);
1101+
}
1102+
1103+
TEST_F(PostgresParserTests, DropSequenceTest) {
1104+
auto parser = parser::PostgresParser::GetInstance();
1105+
std::string query = "DROP SEQUENCE seq;";
1106+
std::unique_ptr<parser::SQLStatementList> stmt_list(
1107+
parser.BuildParseTree(query).release());
1108+
EXPECT_TRUE(stmt_list->is_valid);
1109+
if (!stmt_list->is_valid) {
1110+
LOG_ERROR("Message: %s, line: %d, col: %d", stmt_list->parser_msg,
1111+
stmt_list->error_line, stmt_list->error_col);
1112+
}
1113+
EXPECT_EQ(StatementType::DROP, stmt_list->GetStatement(0)->GetType());
1114+
auto drop_sequence_stmt =
1115+
static_cast<parser::DropStatement *>(stmt_list->GetStatement(0));
1116+
// drop type
1117+
EXPECT_EQ(parser::DropStatement::EntityType::kSequence,
1118+
drop_sequence_stmt->GetDropType());
1119+
}
1120+
10721121
TEST_F(PostgresParserTests, FuncCallTest) {
10731122
std::string query = "SELECT add(1,a), chr(99) FROM TEST WHERE FUN(b) > 2";
10741123

0 commit comments

Comments
 (0)