Skip to content

Commit

Permalink
Restored (in the modified form) the code wrongly removed three years …
Browse files Browse the repository at this point in the history
…ago. This acts as a complimentary fix for CORE-4142.
  • Loading branch information
dyemanov committed Jul 9, 2013
1 parent e5901e9 commit 837e115
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/jrd/opt.cpp
Expand Up @@ -661,21 +661,34 @@ RecordSource* OPT_compile(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
for (StreamList::iterator i = opt->subStreams.begin(); i != opt->subStreams.end(); ++i)
csb->csb_rpt[*i].activate();

bool sortCanBeUsed = true;
SortNode* const orgSortNode = sort;

// When DISTINCT and ORDER BY are done on different fields,
// and ORDER BY can be mapped to an index, then the records
// are returned in the wrong order because DISTINCT sort is
// performed after the navigational walk of the index.
// For that reason, we need to de-optimize this case so that
// ORDER BY does not use an index.
if (sort && project)
{
sort = NULL;
sortCanBeUsed = false;
}

// outer joins require some extra processing
if (rse->rse_jointype != blr_inner)
rsb = gen_outer(tdbb, opt, rse, rivers, &sort);
else
{
bool sort_can_be_used = true;
SortNode* const saved_sort_node = sort;

// AB: If previous rsb's are already on the stack we can't use
// a navigational-retrieval for an ORDER BY because the next
// streams are JOINed to the previous ones
if (rivers.hasData())
{
sort = NULL;
sort_can_be_used = false;
sortCanBeUsed = false;

// AB: We could already have multiple rivers at this
// point so try to do some hashing or sort/merging now.
while (gen_equi_join(tdbb, opt, rivers))
Expand Down Expand Up @@ -706,7 +719,7 @@ RecordSource* OPT_compile(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,
if (dependent_streams.getCount() && free_streams.getCount())
{
sort = NULL;
sort_can_be_used = false;
sortCanBeUsed = false;
}

if (dependent_streams.getCount())
Expand Down Expand Up @@ -751,16 +764,14 @@ RecordSource* OPT_compile(thread_db* tdbb, CompilerScratch* csb, RseNode* rse,

rsb = CrossJoin(csb, rivers).getRecordSource();

// Assign the sort node back if it wasn't used by the index navigation
if (saved_sort_node && !sort_can_be_used)
{
sort = saved_sort_node;
}

// Pick up any residual boolean that may have fallen thru the cracks
rsb = gen_residual_boolean(tdbb, opt, rsb);
}

// Assign the sort node back if it wasn't used by the index navigation
if (orgSortNode && !sortCanBeUsed)
sort = orgSortNode;

// if the aggregate was not optimized via an index, get rid of the
// sort and flag the fact to the calling routine
if (aggregate && sort)
Expand Down

0 comments on commit 837e115

Please sign in to comment.