2009-12-09 19:45:44 +01:00
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Interbase Public
|
|
|
|
* License Version 1.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at http://www.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Inprise Corporation
|
|
|
|
* and its predecessors. Portions created by Inprise Corporation are
|
|
|
|
* Copyright (C) Inprise Corporation.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "firebird.h"
|
|
|
|
#include "../jrd/jrd.h"
|
|
|
|
#include "../jrd/req.h"
|
|
|
|
#include "../jrd/cmp_proto.h"
|
|
|
|
#include "../jrd/evl_proto.h"
|
|
|
|
#include "../jrd/met_proto.h"
|
|
|
|
#include "../jrd/vio_proto.h"
|
|
|
|
|
|
|
|
#include "RecordSource.h"
|
|
|
|
|
|
|
|
using namespace Firebird;
|
|
|
|
using namespace Jrd;
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// Data access: nested loops join
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
NestedLoopJoin::NestedLoopJoin(CompilerScratch* csb, size_t count, RecordSource* const* args)
|
2009-12-10 13:17:00 +01:00
|
|
|
: m_outerJoin(false), m_semiJoin(false), m_antiJoin(false), m_args(csb->csb_pool),
|
|
|
|
m_boolean(NULL)
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
m_impure = CMP_impure(csb, sizeof(Impure));
|
|
|
|
|
|
|
|
m_args.resize(count);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
m_args[i] = args[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NestedLoopJoin::NestedLoopJoin(CompilerScratch* csb, RecordSource* outer, RecordSource* inner,
|
2010-10-12 13:36:51 +02:00
|
|
|
BoolExprNode* boolean, bool semiJoin, bool antiJoin)
|
2009-12-10 02:32:47 +01:00
|
|
|
: m_outerJoin(true), m_semiJoin(semiJoin), m_antiJoin(antiJoin), m_args(csb->csb_pool),
|
|
|
|
m_boolean(boolean)
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
fb_assert(outer && inner);
|
|
|
|
|
|
|
|
m_impure = CMP_impure(csb, sizeof(Impure));
|
|
|
|
|
|
|
|
m_args.add(outer);
|
|
|
|
m_args.add(inner);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void NestedLoopJoin::open(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
impure->irsb_flags = irsb_open | irsb_first | irsb_mustread;
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void NestedLoopJoin::close(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
|
|
|
|
|
|
|
invalidateRecords(request);
|
|
|
|
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
if (impure->irsb_flags & irsb_open)
|
|
|
|
{
|
|
|
|
impure->irsb_flags &= ~irsb_open;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_args.getCount(); i++)
|
|
|
|
{
|
|
|
|
m_args[i]->close(tdbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool NestedLoopJoin::getRecord(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2012-12-17 18:33:45 +01:00
|
|
|
if (--tdbb->tdbb_quantum < 0)
|
|
|
|
JRD_reschedule(tdbb, 0, true);
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
if (!(impure->irsb_flags & irsb_open))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_outerJoin)
|
|
|
|
{
|
|
|
|
fb_assert(m_args.getCount() == 2);
|
|
|
|
|
2010-07-06 13:09:32 +02:00
|
|
|
const RecordSource* const outer = m_args[0];
|
|
|
|
const RecordSource* const inner = m_args[1];
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
if (impure->irsb_flags & irsb_first)
|
|
|
|
{
|
|
|
|
outer->open(tdbb);
|
|
|
|
impure->irsb_flags &= ~irsb_first;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (impure->irsb_flags & irsb_mustread)
|
|
|
|
{
|
|
|
|
if (!outer->getRecord(tdbb))
|
|
|
|
return false;
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
if (m_boolean && !m_boolean->execute(tdbb, request))
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
// The boolean pertaining to the left sub-stream is false
|
|
|
|
// so just join sub-stream to a null valued right sub-stream
|
|
|
|
inner->nullRecords(tdbb);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
impure->irsb_flags &= ~(irsb_mustread | irsb_joined);
|
|
|
|
inner->open(tdbb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_semiJoin)
|
|
|
|
{
|
|
|
|
if (inner->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
impure->irsb_flags &= ~irsb_joined;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
impure->irsb_flags |= irsb_joined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_antiJoin)
|
|
|
|
{
|
|
|
|
if (inner->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
impure->irsb_flags |= irsb_joined;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
impure->irsb_flags &= ~irsb_joined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-12 15:54:33 +01:00
|
|
|
if (inner->getRecord(tdbb))
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
impure->irsb_flags |= irsb_joined;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner->close(tdbb);
|
|
|
|
impure->irsb_flags |= irsb_mustread;
|
|
|
|
|
|
|
|
if (!(impure->irsb_flags & irsb_joined))
|
|
|
|
{
|
|
|
|
// The current left sub-stream record has not been joined to anything.
|
|
|
|
// Join it to a null valued right sub-stream.
|
|
|
|
inner->nullRecords(tdbb);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (impure->irsb_flags & irsb_first)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_args.getCount(); i++)
|
|
|
|
{
|
|
|
|
m_args[i]->open(tdbb);
|
|
|
|
|
|
|
|
if (!fetchRecord(tdbb, i))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impure->irsb_flags &= ~irsb_first;
|
|
|
|
}
|
|
|
|
// hvlad: self referenced members are removed from recursive SELECT's
|
|
|
|
// in recursive CTE (it is done in dsql\pass1.cpp). If there are no other
|
|
|
|
// members in such SELECT then rsb_count will be zero. Handle it.
|
|
|
|
else if (m_args.isEmpty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (!fetchRecord(tdbb, m_args.getCount() - 1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
bool NestedLoopJoin::refetchRecord(thread_db* /*tdbb*/) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
bool NestedLoopJoin::lockRecord(thread_db* /*tdbb*/) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
status_exception::raise(Arg::Gds(isc_record_lock_not_supp));
|
|
|
|
return false; // compiler silencer
|
|
|
|
}
|
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
void NestedLoopJoin::print(thread_db* tdbb, string& plan, bool detailed, unsigned level) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2011-02-02 12:31:04 +01:00
|
|
|
if (detailed)
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2011-02-02 12:31:04 +01:00
|
|
|
plan += printIndent(++level) + " Nested Loop Join ";
|
|
|
|
plan += m_semiJoin ? "(semi)" : m_antiJoin ? "(anti)" : m_outerJoin ? "(outer)" : "(inner)";
|
2011-04-03 05:03:23 +02:00
|
|
|
for (size_t i = 0; i < m_args.getCount(); i++)
|
2011-02-02 12:31:04 +01:00
|
|
|
{
|
|
|
|
m_args[i]->print(tdbb, plan, true, level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level++;
|
|
|
|
plan += "JOIN (";
|
2011-04-03 05:03:23 +02:00
|
|
|
for (size_t i = 0; i < m_args.getCount(); i++)
|
2011-02-02 12:31:04 +01:00
|
|
|
{
|
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
plan += ", ";
|
|
|
|
}
|
|
|
|
m_args[i]->print(tdbb, plan, false, level);
|
|
|
|
}
|
|
|
|
plan += ")";
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NestedLoopJoin::markRecursive()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_args.getCount(); i++)
|
|
|
|
{
|
|
|
|
m_args[i]->markRecursive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 07:45:24 +01:00
|
|
|
void NestedLoopJoin::findUsedStreams(StreamList& streams, bool expandAll) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_args.getCount(); i++)
|
|
|
|
{
|
2011-12-14 07:45:24 +01:00
|
|
|
m_args[i]->findUsedStreams(streams, expandAll);
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void NestedLoopJoin::invalidateRecords(jrd_req* request) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_args.getCount(); i++)
|
|
|
|
{
|
|
|
|
m_args[i]->invalidateRecords(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void NestedLoopJoin::nullRecords(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_args.getCount(); i++)
|
|
|
|
{
|
|
|
|
m_args[i]->nullRecords(tdbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool NestedLoopJoin::fetchRecord(thread_db* tdbb, size_t n) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2010-07-06 13:09:32 +02:00
|
|
|
const RecordSource* const arg = m_args[n];
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
if (arg->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have exhausted this stream, so close it; if there is
|
|
|
|
// another candidate record from the n-1 streams to the left,
|
|
|
|
// then reopen the stream and start again from the beginning.
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
arg->close(tdbb);
|
|
|
|
|
|
|
|
if (n == 0 || !fetchRecord(tdbb, n - 1))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg->open(tdbb);
|
|
|
|
|
|
|
|
if (arg->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|