Skip to content

Commit

Permalink
refactor: upgrade multi-key sort (#77)
Browse files Browse the repository at this point in the history
As suggested by @bkietz this PR upgrades the sort routine to utilize
tuple's built-in compare behavior. This reduces the complexity of
evaluating each set of four possibilities for each key manually.

---------

Co-authored-by: Benjamin Kietzman <bengilgit@gmail.com>
  • Loading branch information
EpsilonPrime and bkietz committed Jun 27, 2023
1 parent 4601d5c commit 4c57470
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/substrait/textplan/converter/ReferenceNormalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,19 @@ namespace {
bool compareExtensionFunctions(
const ::substrait::proto::extensions::SimpleExtensionDeclaration& a,
const ::substrait::proto::extensions::SimpleExtensionDeclaration& b) {
// First sort so that extension functions proceed any other kind of extension.
if (a.has_extension_function() && !b.has_extension_function()) {
return true;
} else if (!a.has_extension_function() && b.has_extension_function()) {
// Extension functions always come first.
return false;
} else if (!a.has_extension_function() && !b.has_extension_function()) {
// Both are not extension functions, no difference in ordering.
return false;
}
// Now sort by space.
if (a.extension_function().extension_uri_reference() <
b.extension_function().extension_uri_reference()) {
return true;
} else if (
a.extension_function().extension_uri_reference() >
b.extension_function().extension_uri_reference()) {
return false;
}
// Finally sort by name within a space.
return a.extension_function().name() < b.extension_function().name();
auto ord = [](const auto& decl) {
return make_tuple(
// First sort so that extension functions precede any other kind of
// extension.
not decl.has_extension_function(),
// Next sort by space.
decl.extension_function().extension_uri_reference(),
// Finally sort by name within a space.
std::string_view{decl.extension_function().name()});
};

// Now let the default tuple compare do the rest of the work.
return ord(a) > ord(b);
}

void normalizeFunctionsForExpression(
Expand Down

0 comments on commit 4c57470

Please sign in to comment.