Skip to content

Commit

Permalink
Fix nested order by and limit
Browse files Browse the repository at this point in the history
Currently if there's an ORDER BY and a LIMIT in a nested query, then
we get a segfault. The issue is that ORDER BY is converted into a
PropertySort, and properties are not pushed down into nested queries.
Additionally LIMIT nodes look at the sort expressions and order that
is stored in the node itself instead of looking at the required
properties during ChildPropertyDeriver::Visit. This creates a
disconnect between what the LIMIT nodes in nested queries expects
the required properties to be and what they actually are. Relying on
the actual properties instead of what's stored in the LIMIT node
ensures that the output properties and required properties stay in
synce.

The result of this change is that nested queries will ignore ORDER
BYs unless it is accompanied by a LIMIT. This is due to the fact that
the PropertySort will not be pushed down into nested queries, but
LIMITs store their own sort information and generate an OrderBy node
in the PlanGenerator.

Fixes cmu-db#1423
  • Loading branch information
jkosh44 committed Jan 3, 2021
1 parent 8a6f19a commit fefcc60
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
24 changes: 24 additions & 0 deletions script/testing/junit/traces/nested-query.test
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,30 @@ SELECT sno FROM supplier WHERE sno >= (SELECT MIN(shipment.sno) FROM shipment IN
----
1 values hashing to 1dcca23355272056f04fe8bf20edfce0

query IIIT nosort
SELECT * FROM shipment WHERE pno IN (SELECT pno FROM part ORDER BY pno LIMIT 2);
----
1
1
1
item_11
2
1
1
item_11
3
1
1
item_11
2
1
2
item_12
3
1
3
item_13

statement ok


Expand Down
12 changes: 9 additions & 3 deletions src/optimizer/child_property_deriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ void ChildPropertyDeriver::Visit(const Limit *op) {
// Limit fulfill the internal sort property
std::vector<PropertySet *> child_input_properties{new PropertySet()};
auto provided_prop = new PropertySet();
if (!op->GetSortExpressions().empty()) {
const std::vector<common::ManagedPointer<parser::AbstractExpression>> &exprs = op->GetSortExpressions();
const std::vector<OrderByOrderingType> &sorts{op->GetSortAscending()};
auto sort_prop = requirements_->GetPropertyOfTypeAs<PropertySort>(PropertyType::SORT);
if (sort_prop != nullptr) {
std::vector<common::ManagedPointer<parser::AbstractExpression>> exprs;
std::vector<OrderByOrderingType> sorts;
auto sort_col_size = sort_prop->GetSortColumnSize();
for (size_t idx = 0; idx < sort_col_size; idx++) {
exprs.push_back(sort_prop->GetSortColumn(idx));
sorts.push_back(sort_prop->GetSortAscending(idx));
}
provided_prop->AddProperty(new PropertySort(exprs, sorts));
}

Expand Down

0 comments on commit fefcc60

Please sign in to comment.