Skip to content

Commit

Permalink
fix: remapper error when Project has no first column or column order …
Browse files Browse the repository at this point in the history
…is reverse order (#398)
  • Loading branch information
KKould authored Dec 29, 2023
1 parent ada746c commit 39f8b17
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/planner/bound/base_table_ref.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public:
replace_field<SharedPtr<DataType>>(*column_types_, indices);
};

void RetainColumnByIds(Vector<SizeT> &&ids) {
if (ids.empty()) {
return;
}
Vector<SizeT> indices;
indices.reserve(ids.size());

std::sort(ids.begin(), ids.end());
for (SizeT i = 0, ids_i = 0; i < column_ids_.size() && ids_i < ids.size(); ++i) {
if (column_ids_[i] == ids[ids_i]) {
indices.push_back(i);
ids_i ++;
}
}
RetainColumnByIndices(Move(indices));
}

SharedPtr<String> schema_name() const { return table_entry_ptr_->table_collection_meta_->db_entry_->db_name_; }

SharedPtr<String> table_name() const { return table_entry_ptr_->table_collection_name_; }
Expand Down
12 changes: 6 additions & 6 deletions src/planner/optimizer/lazy_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,27 @@ void CleanScan::VisitNode(LogicalNode &op) {
switch (op.operator_type()) {
case LogicalNodeType::kTableScan: {
auto table_scan = dynamic_cast<LogicalTableScan &>(op);
Vector<SizeT> project_indices = LoadedColumn(last_op_load_metas_.get(), table_scan.base_table_ref_.get());
Vector<SizeT> project_ids = LoadedColumn(last_op_load_metas_.get(), table_scan.base_table_ref_.get());

scan_table_indexes_.push_back(table_scan.base_table_ref_->table_index_);
table_scan.base_table_ref_->RetainColumnByIndices(Move(project_indices));
table_scan.base_table_ref_->RetainColumnByIds(Move(project_ids));
table_scan.add_row_id_ = true;
break;
}
case LogicalNodeType::kKnnScan: {
auto knn_scan = dynamic_cast<LogicalKnnScan &>(op);
Vector<SizeT> project_indices = LoadedColumn(last_op_load_metas_.get(), knn_scan.base_table_ref_.get());
Vector<SizeT> project_ids = LoadedColumn(last_op_load_metas_.get(), knn_scan.base_table_ref_.get());

scan_table_indexes_.push_back(knn_scan.base_table_ref_->table_index_);
knn_scan.base_table_ref_->RetainColumnByIndices(Move(project_indices));
knn_scan.base_table_ref_->RetainColumnByIds(Move(project_ids));
break;
}
case LogicalNodeType::kMatch: {
auto match = dynamic_cast<LogicalMatch &>(op);
Vector<SizeT> project_indices = LoadedColumn(last_op_load_metas_.get(), match.base_table_ref_.get());
Vector<SizeT> project_ids = LoadedColumn(last_op_load_metas_.get(), match.base_table_ref_.get());

scan_table_indexes_.push_back(match.base_table_ref_->table_index_);
match.base_table_ref_->RetainColumnByIndices(Move(project_indices));
match.base_table_ref_->RetainColumnByIds(Move(project_ids));
break;
}
case LogicalNodeType::kLimit:
Expand Down
48 changes: 48 additions & 0 deletions test/sql/dql/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,65 @@ CREATE TABLE select1 (id INTEGER PRIMARY KEY, name VARCHAR, age INTEGER);
statement ok
CREATE TABLE select2 (id INTEGER , age INTEGER);

statement ok
CREATE TABLE select3 (c1 INTEGER, c2 INTEGER, c3 INTEGER);

# copy data from csv file
query I
COPY select2 FROM '/tmp/infinity/test_data/nation.csv' WITH ( DELIMITER ',' );
----

statement ok
INSERT INTO select3 VALUES(0,1,2),(3,4,5),(6,7,8);

#query ITI
#SELECT * FROM select1 ORDER by age ASC;
#----
#2 Jane 25
#1 John 30

query III
SELECT * from select3;
----
0 1 2
3 4 5
6 7 8

query III
SELECT c1, c2, c3 from select3;
----
0 1 2
3 4 5
6 7 8

query III
SELECT c2, c3 from select3;
----
1 2
4 5
7 8

query III
SELECT c2 from select3;
----
1
4
7

query III
SELECT c3, c2, c1 from select3;
----
2 1 0
5 4 3
8 7 6

query III
SELECT c2, c1 from select3;
----
1 0
4 3
7 6

statement ok
DROP TABLE select1;

Expand Down

0 comments on commit 39f8b17

Please sign in to comment.