From fefcc60c4646a66019e31df16974b46405faa024 Mon Sep 17 00:00:00 2001 From: Joseph Koshakow Date: Sun, 3 Jan 2021 10:09:16 -0500 Subject: [PATCH] Fix nested order by and limit 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 #1423 --- script/testing/junit/traces/nested-query.test | 24 +++++++++++++++++++ src/optimizer/child_property_deriver.cpp | 12 +++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/script/testing/junit/traces/nested-query.test b/script/testing/junit/traces/nested-query.test index e69dfdcec3..08e80f2807 100644 --- a/script/testing/junit/traces/nested-query.test +++ b/script/testing/junit/traces/nested-query.test @@ -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 diff --git a/src/optimizer/child_property_deriver.cpp b/src/optimizer/child_property_deriver.cpp index f36f3fa61c..51cf29e61e 100644 --- a/src/optimizer/child_property_deriver.cpp +++ b/src/optimizer/child_property_deriver.cpp @@ -93,9 +93,15 @@ void ChildPropertyDeriver::Visit(const Limit *op) { // Limit fulfill the internal sort property std::vector child_input_properties{new PropertySet()}; auto provided_prop = new PropertySet(); - if (!op->GetSortExpressions().empty()) { - const std::vector> &exprs = op->GetSortExpressions(); - const std::vector &sorts{op->GetSortAscending()}; + auto sort_prop = requirements_->GetPropertyOfTypeAs(PropertyType::SORT); + if (sort_prop != nullptr) { + std::vector> exprs; + std::vector 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)); }