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

Commit df1d952

Browse files
authored
Merge pull request #1266 from poojanilangekar/attribute-order-fix
Fixes the attribute reordering in update statements
2 parents adb9f6f + f9f088d commit df1d952

File tree

2 files changed

+116
-9
lines changed

2 files changed

+116
-9
lines changed

src/codegen/operator/update_translator.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ UpdateTranslator::UpdateTranslator(const planner::UpdatePlan &update_plan,
4141
UpdaterProxy::GetType(GetCodeGen()));
4242
}
4343

44-
bool IsTarget(const TargetList &target_list, uint32_t index) {
45-
for (const auto &target : target_list) {
46-
if (target.first == index) {
47-
return true;
44+
oid_t GetTargetIndex(const TargetList &target_list, uint32_t index) {
45+
oid_t target_size = target_list.size();
46+
for (oid_t i = 0; i < target_size; i++) {
47+
if (target_list[i].first == index) {
48+
return i;
4849
}
4950
}
50-
return false;
51+
return INVALID_OID;
5152
}
5253

5354
void UpdateTranslator::InitializeState() {
@@ -93,13 +94,13 @@ void UpdateTranslator::Consume(ConsumerContext &, RowBatch::Row &row) const {
9394

9495
// Collect all the column values
9596
std::vector<codegen::Value> values;
96-
for (uint32_t i = 0, target_id = 0; i < column_num; i++) {
97+
for (uint32_t i = 0; i < column_num; i++) {
9798
codegen::Value val;
98-
if (IsTarget(target_list, i)) {
99+
uint32_t target_index = GetTargetIndex(target_list, i);
100+
if (target_index != INVALID_OID) {
99101
// Set the value for the update
100-
const auto &derived_attribute = target_list[target_id].second;
102+
const auto &derived_attribute = target_list[target_index].second;
101103
val = row.DeriveValue(codegen, *derived_attribute.expr);
102-
target_id++;
103104
} else {
104105
val = row.DeriveValue(codegen, ais[i]);
105106
}

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)