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

Commit 943964c

Browse files
Added a test case
1 parent 1bfd3eb commit 943964c

File tree

2 files changed

+112
-6
lines changed

2 files changed

+112
-6
lines changed

src/codegen/operator/update_translator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include <include/codegen/type/varchar_type.h>
1413
#include "codegen/lang/if.h"
1514
#include "codegen/proxy/storage_manager_proxy.h"
1615
#include "codegen/proxy/target_proxy.h"
@@ -43,11 +42,11 @@ UpdateTranslator::UpdateTranslator(const planner::UpdatePlan &update_plan,
4342
}
4443

4544
oid_t GetTargetIndex(const TargetList &target_list, uint32_t index) {
46-
oid_t target_size = target_list.size();
45+
oid_t target_size = target_list.size();
4746
for (oid_t i = 0; i < target_size; i++) {
48-
if (target_list[i].first == index) {
49-
return i;
50-
}
47+
if (target_list[i].first == index) {
48+
return i;
49+
}
5150
}
5251
return INVALID_OID;
5352
}
@@ -97,8 +96,9 @@ void UpdateTranslator::Consume(ConsumerContext &, RowBatch::Row &row) const {
9796
std::vector<codegen::Value> values;
9897
for (uint32_t i = 0; i < column_num; i++) {
9998
codegen::Value val;
100-
uint32_t target_index = GetTargetIndex(target_list,i);
99+
uint32_t target_index = GetTargetIndex(target_list, i);
101100
if (target_index != INVALID_OID) {
101+
// Set the value for the update
102102
const auto &derived_attribute = target_list[target_index].second;
103103
val = row.DeriveValue(codegen, *derived_attribute.expr);
104104
} else {

test/sql/update_sql_test.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,5 +527,111 @@ TEST_F(UpdateSQLTests, MultiTileGroupUpdateSQLTest) {
527527
txn_manager.CommitTransaction(txn);
528528
}
529529

530+
TEST_F(UpdateSQLTests, AttributeOrderUpdateSQLTest) {
531+
// This test updates the attributes of the table in different orders.
532+
// It ensure that the updates in the data_table are in the correct
533+
// order irrespective of the order of attributes in the update statement.
534+
535+
LOG_DEBUG("Bootstrapping...");
536+
537+
auto catalog = catalog::Catalog::GetInstance();
538+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
539+
auto txn = txn_manager.BeginTransaction();
540+
catalog->CreateDatabase(DEFAULT_DB_NAME, txn);
541+
txn_manager.CommitTransaction(txn);
542+
543+
LOG_DEBUG("Bootstrapping completed!");
544+
545+
txn = txn_manager.BeginTransaction();
546+
// Create a table first
547+
LOG_DEBUG("Creating a table...");
548+
LOG_DEBUG("Query: CREATE TABLE test(a INT, b INT)");
549+
550+
TestingSQLUtil::ExecuteSQLQuery("CREATE TABLE test(a INT, b INT);");
551+
txn_manager.CommitTransaction(txn);
552+
553+
LOG_DEBUG("Table created!");
554+
555+
txn = txn_manager.BeginTransaction();
556+
// Insert a tuple into table
557+
LOG_DEBUG("Inserting a tuple...");
558+
559+
LOG_DEBUG("Query: INSERT INTO test VALUES (1, 100)");
560+
TestingSQLUtil::ExecuteSQLQuery("INSERT INTO test VALUES (1, 100);");
561+
562+
txn_manager.CommitTransaction(txn);
563+
LOG_DEBUG("Tuple inserted!");
564+
565+
txn = txn_manager.BeginTransaction();
566+
// Update a tuple in the table in the order (b, a)
567+
LOG_DEBUG("Updating a tuple...");
568+
LOG_DEBUG("Query: UPDATE test SET b = b * 2, a = a * 2");
569+
570+
std::vector<ResultValue> result;
571+
std::vector<FieldInfo> tuple_descriptor;
572+
std::string error_message;
573+
int rows_affected;
574+
575+
TestingSQLUtil::ExecuteSQLQuery("UPDATE test SET b = b * 2, a = a * 2;",
576+
result, tuple_descriptor, rows_affected,
577+
error_message);
578+
579+
txn_manager.CommitTransaction(txn);
580+
// Check the return value
581+
EXPECT_EQ(1, rows_affected);
582+
LOG_DEBUG("Tuple Update successful!");
583+
584+
// Check the updated values.
585+
txn = txn_manager.BeginTransaction();
586+
// Update a tuple in the table in the order (b, a)
587+
LOG_DEBUG("Selecting the updated tuple...");
588+
LOG_DEBUG("Query: SELECT a, b FROM test;");
589+
590+
TestingSQLUtil::ExecuteSQLQuery("SELECT a, b FROM test;", result,
591+
tuple_descriptor, rows_affected,
592+
error_message);
593+
594+
txn_manager.CommitTransaction(txn);
595+
// Check the return value
596+
EXPECT_EQ(TestingSQLUtil::GetResultValueAsString(result, 0), "2");
597+
EXPECT_EQ(TestingSQLUtil::GetResultValueAsString(result, 1), "200");
598+
LOG_DEBUG("Attributes updated in the correct order!");
599+
600+
txn = txn_manager.BeginTransaction();
601+
// Update a tuple in the table in the order (a, b)
602+
LOG_DEBUG("Updating a tuple again...");
603+
LOG_DEBUG("Query: UPDATE test SET a = a * 2, b = b * 2");
604+
605+
TestingSQLUtil::ExecuteSQLQuery("UPDATE test SET a = a * 2, b = b * 2;",
606+
result, tuple_descriptor, rows_affected,
607+
error_message);
608+
609+
txn_manager.CommitTransaction(txn);
610+
// Check the return value
611+
EXPECT_EQ(1, rows_affected);
612+
LOG_DEBUG("Tuple Update successful, agin!");
613+
614+
// Check the updated values.
615+
txn = txn_manager.BeginTransaction();
616+
// Update a tuple in the table in the order (b, a)
617+
LOG_DEBUG("Selecting the updated tuple...");
618+
LOG_DEBUG("Query: SELECT a, b FROM test;");
619+
620+
TestingSQLUtil::ExecuteSQLQuery("SELECT a, b FROM test;", result,
621+
tuple_descriptor, rows_affected,
622+
error_message);
623+
624+
txn_manager.CommitTransaction(txn);
625+
// Check the return value
626+
EXPECT_EQ(TestingSQLUtil::GetResultValueAsString(result, 0), "4");
627+
EXPECT_EQ(TestingSQLUtil::GetResultValueAsString(result, 1), "400");
628+
LOG_DEBUG("Attributes updated in the correct order, again!");
629+
630+
// free the database just created
631+
txn = txn_manager.BeginTransaction();
632+
catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
633+
txn_manager.CommitTransaction(txn);
634+
}
635+
530636
} // namespace test
531637
} // namespace peloton

0 commit comments

Comments
 (0)