2009-12-09 19:45:44 +01:00
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's 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.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* 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 Adriano dos Santos Fernandes
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
2010-01-18 22:37:47 +01:00
|
|
|
* Copyright (c) 2009 Adriano dos Santos Fernandes <adrianosf@gmail.com>
|
2009-12-09 19:45:44 +01:00
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "firebird.h"
|
2010-02-13 21:29:29 +01:00
|
|
|
#include "../dsql/Nodes.h"
|
2010-01-20 01:02:58 +01:00
|
|
|
#include "../jrd/mov_proto.h"
|
2010-01-18 22:37:47 +01:00
|
|
|
#include "../jrd/opt_proto.h"
|
2010-01-20 01:02:58 +01:00
|
|
|
#include "../jrd/evl_proto.h"
|
2009-12-09 19:45:44 +01:00
|
|
|
#include "../jrd/exe_proto.h"
|
2010-01-23 04:02:53 +01:00
|
|
|
#include "../jrd/par_proto.h"
|
2009-12-09 19:45:44 +01:00
|
|
|
#include "RecordSource.h"
|
|
|
|
|
|
|
|
using namespace Firebird;
|
|
|
|
using namespace Jrd;
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// Data access: window expression
|
|
|
|
// ------------------------------
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// This stream makes possible to reuse a BufferedStream, so each usage maintains a different
|
|
|
|
// cursor position.
|
2010-02-16 01:26:53 +01:00
|
|
|
class BufferedStreamWindow : public BaseBufferedStream
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
|
|
|
struct Impure : public RecordSource::Impure
|
|
|
|
{
|
|
|
|
FB_UINT64 irsb_position;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
BufferedStreamWindow(CompilerScratch* csb, BufferedStream* next);
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void open(thread_db* tdbb) const;
|
|
|
|
void close(thread_db* tdbb) const;
|
2010-01-18 22:37:47 +01:00
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool getRecord(thread_db* tdbb) const;
|
|
|
|
bool refetchRecord(thread_db* tdbb) const;
|
|
|
|
bool lockRecord(thread_db* tdbb) const;
|
2010-01-18 22:37:47 +01:00
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
void print(thread_db* tdbb, Firebird::string& plan, bool detailed, unsigned level) const;
|
2010-01-18 22:37:47 +01:00
|
|
|
|
|
|
|
void markRecursive();
|
2010-07-05 20:37:35 +02:00
|
|
|
void invalidateRecords(jrd_req* request) const;
|
2010-01-18 22:37:47 +01:00
|
|
|
|
2011-12-14 07:45:24 +01:00
|
|
|
void findUsedStreams(StreamList& streams, bool expandAll) const;
|
2010-07-05 20:37:35 +02:00
|
|
|
void nullRecords(thread_db* tdbb) const;
|
2010-01-18 22:37:47 +01:00
|
|
|
|
2010-07-06 13:09:32 +02:00
|
|
|
void locate(thread_db* tdbb, FB_UINT64 position) const
|
2010-02-16 01:26:53 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2010-02-16 01:26:53 +01:00
|
|
|
impure->irsb_position = position;
|
|
|
|
}
|
|
|
|
|
2013-09-20 09:06:08 +02:00
|
|
|
FB_UINT64 getCount(thread_db* tdbb) const
|
2010-02-16 01:26:53 +01:00
|
|
|
{
|
2013-09-20 09:06:08 +02:00
|
|
|
return m_next->getCount(tdbb);
|
2010-02-16 01:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FB_UINT64 getPosition(jrd_req* request) const
|
|
|
|
{
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2010-02-16 01:26:53 +01:00
|
|
|
return impure->irsb_position;
|
|
|
|
}
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
public:
|
2010-08-09 17:48:51 +02:00
|
|
|
NestConst<BufferedStream> m_next;
|
2010-01-18 22:37:47 +01:00
|
|
|
};
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
#ifdef NOT_USED_OR_REPLACED
|
2010-01-20 01:02:58 +01:00
|
|
|
// Make join between outer stream and already sorted (aggregated) partition.
|
|
|
|
class WindowJoin : public RecordSource
|
|
|
|
{
|
|
|
|
struct DscNull
|
|
|
|
{
|
|
|
|
dsc* desc;
|
|
|
|
bool null;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Impure : public RecordSource::Impure
|
|
|
|
{
|
|
|
|
FB_UINT64 innerRecordCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
WindowJoin(CompilerScratch* csb, RecordSource* outer, RecordSource* inner,
|
2010-10-12 13:36:51 +02:00
|
|
|
const SortNode* outerKeys, const SortNode* innerKeys);
|
2010-01-20 01:02:58 +01:00
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void open(thread_db* tdbb) const;
|
|
|
|
void close(thread_db* tdbb) const;
|
2010-01-20 01:02:58 +01:00
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool getRecord(thread_db* tdbb) const;
|
|
|
|
bool refetchRecord(thread_db* tdbb) const;
|
|
|
|
bool lockRecord(thread_db* tdbb) const;
|
2010-01-20 01:02:58 +01:00
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
void print(thread_db* tdbb, Firebird::string& plan, bool detailed, unsigned level) const;
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
void markRecursive();
|
2010-07-05 20:37:35 +02:00
|
|
|
void invalidateRecords(jrd_req* request) const;
|
2010-01-20 01:02:58 +01:00
|
|
|
|
2011-12-14 07:45:24 +01:00
|
|
|
void findUsedStreams(StreamList& streams, bool expandAll) const;
|
2010-07-05 20:37:35 +02:00
|
|
|
void nullRecords(thread_db* tdbb) const;
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
private:
|
2010-07-05 20:37:35 +02:00
|
|
|
int compareKeys(thread_db* tdbb, jrd_req* request, DscNull* outerValues) const;
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
RecordSource* const m_outer;
|
|
|
|
BufferedStream* const m_inner;
|
2010-10-12 13:36:51 +02:00
|
|
|
const SortNode* const m_outerKeys;
|
|
|
|
const SortNode* const m_innerKeys;
|
2010-01-20 01:02:58 +01:00
|
|
|
};
|
2010-10-12 13:36:51 +02:00
|
|
|
#endif // NOT_USED_OR_REPLACED
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
// BufferedStreamWindow implementation
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
BufferedStreamWindow::BufferedStreamWindow(CompilerScratch* csb, BufferedStream* next)
|
|
|
|
: m_next(next)
|
|
|
|
{
|
|
|
|
m_impure = CMP_impure(csb, sizeof(Impure));
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void BufferedStreamWindow::open(thread_db* tdbb) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2010-01-18 22:37:47 +01:00
|
|
|
|
|
|
|
impure->irsb_flags = irsb_open;
|
|
|
|
impure->irsb_position = 0;
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void BufferedStreamWindow::close(thread_db* tdbb) const
|
2010-01-18 22:37:47 +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);
|
2010-01-18 22:37:47 +01:00
|
|
|
|
|
|
|
if (impure->irsb_flags & irsb_open)
|
|
|
|
impure->irsb_flags &= ~irsb_open;
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool BufferedStreamWindow::getRecord(thread_db* tdbb) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2010-01-18 22:37:47 +01:00
|
|
|
|
|
|
|
if (!(impure->irsb_flags & irsb_open))
|
|
|
|
return false;
|
|
|
|
|
2010-02-16 01:26:53 +01:00
|
|
|
m_next->locate(tdbb, impure->irsb_position);
|
|
|
|
if (!m_next->getRecord(tdbb))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++impure->irsb_position;
|
|
|
|
return true;
|
2010-01-18 22:37:47 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool BufferedStreamWindow::refetchRecord(thread_db* tdbb) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
|
|
|
return m_next->refetchRecord(tdbb);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool BufferedStreamWindow::lockRecord(thread_db* tdbb) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
|
|
|
return m_next->lockRecord(tdbb);
|
|
|
|
}
|
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
void BufferedStreamWindow::print(thread_db* tdbb, string& plan, bool detailed, unsigned level) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
2011-02-02 12:31:04 +01:00
|
|
|
m_next->print(tdbb, plan, detailed, level);
|
2010-01-18 22:37:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BufferedStreamWindow::markRecursive()
|
|
|
|
{
|
|
|
|
m_next->markRecursive();
|
|
|
|
}
|
|
|
|
|
2011-12-14 07:45:24 +01:00
|
|
|
void BufferedStreamWindow::findUsedStreams(StreamList& streams, bool expandAll) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
2011-12-14 07:45:24 +01:00
|
|
|
m_next->findUsedStreams(streams, expandAll);
|
2010-01-18 22:37:47 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void BufferedStreamWindow::invalidateRecords(jrd_req* request) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
|
|
|
m_next->invalidateRecords(request);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void BufferedStreamWindow::nullRecords(thread_db* tdbb) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
|
|
|
m_next->nullRecords(tdbb);
|
|
|
|
}
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
#ifdef NOT_USED_OR_REPLACED
|
2010-01-20 01:02:58 +01:00
|
|
|
// WindowJoin implementation
|
|
|
|
|
|
|
|
WindowJoin::WindowJoin(CompilerScratch* csb, RecordSource* outer, RecordSource* inner,
|
2010-10-12 13:36:51 +02:00
|
|
|
const SortNode* outerKeys,
|
|
|
|
const SortNode* innerKeys)
|
2015-10-12 16:26:00 +02:00
|
|
|
: m_outer(outer), m_inner(FB_NEW_POOL(csb->csb_pool) BufferedStream(csb, inner)),
|
2010-01-20 01:02:58 +01:00
|
|
|
m_outerKeys(outerKeys), m_innerKeys(innerKeys)
|
|
|
|
{
|
2010-10-12 13:36:51 +02:00
|
|
|
fb_assert(m_outerKeys && m_innerKeys);
|
|
|
|
fb_assert(m_outer && m_inner &&
|
|
|
|
m_innerKeys->expressions.getCount() == m_outerKeys->expressions.getCount());
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
m_impure = CMP_impure(csb, sizeof(Impure));
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void WindowJoin::open(thread_db* tdbb) const
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
impure->irsb_flags = irsb_open;
|
|
|
|
|
|
|
|
// Read and cache the inner stream. Also gets its total number of records.
|
|
|
|
|
|
|
|
m_inner->open(tdbb);
|
|
|
|
FB_UINT64 position = 0;
|
|
|
|
|
|
|
|
while (m_inner->getRecord(tdbb))
|
|
|
|
++position;
|
|
|
|
|
|
|
|
impure->innerRecordCount = position;
|
|
|
|
|
|
|
|
m_outer->open(tdbb);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void WindowJoin::close(thread_db* tdbb) const
|
2010-01-20 01:02:58 +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);
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
if (impure->irsb_flags & irsb_open)
|
|
|
|
{
|
|
|
|
impure->irsb_flags &= ~irsb_open;
|
|
|
|
|
|
|
|
m_outer->close(tdbb);
|
|
|
|
m_inner->close(tdbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool WindowJoin::getRecord(thread_db* tdbb) const
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
if (!(impure->irsb_flags & irsb_open))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!m_outer->getRecord(tdbb))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Evaluate the outer stream keys.
|
|
|
|
|
|
|
|
HalfStaticArray<DscNull, 8> outerValues;
|
2010-10-12 13:36:51 +02:00
|
|
|
DscNull* outerValue = outerValues.getBuffer(m_outerKeys->expressions.getCount());
|
2010-01-20 01:02:58 +01:00
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
for (unsigned i = 0; i < m_outerKeys->expressions.getCount(); ++i)
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
2010-10-12 13:36:51 +02:00
|
|
|
outerValue->desc = EVL_expr(tdbb, m_outerKeys->expressions[i]);
|
2010-01-20 01:02:58 +01:00
|
|
|
outerValue->null = (request->req_flags & req_null);
|
|
|
|
++outerValue;
|
|
|
|
}
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
outerValue -= m_outerKeys->expressions.getCount(); // go back to begin
|
2010-01-20 01:02:58 +01:00
|
|
|
|
|
|
|
// Join the streams. That should be a 1-to-1 join.
|
|
|
|
|
|
|
|
SINT64 start = 0;
|
|
|
|
SINT64 finish = impure->innerRecordCount;
|
|
|
|
SINT64 pos = finish / 2;
|
|
|
|
|
|
|
|
while (pos >= start && pos < finish)
|
|
|
|
{
|
|
|
|
m_inner->locate(tdbb, pos);
|
|
|
|
if (!m_inner->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
fb_assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmp = compareKeys(tdbb, request, outerValue);
|
|
|
|
|
|
|
|
if (cmp == 0)
|
|
|
|
return true;
|
2010-01-26 09:20:27 +01:00
|
|
|
|
|
|
|
if (cmp < 0)
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
finish = pos;
|
|
|
|
pos -= MAX(1, (finish - start) / 2);
|
|
|
|
}
|
|
|
|
else //if (cmp > 0)
|
|
|
|
{
|
|
|
|
start = pos;
|
|
|
|
pos += MAX(1, (finish - start) / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fb_assert(false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool WindowJoin::refetchRecord(thread_db* tdbb) const
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool WindowJoin::lockRecord(thread_db* tdbb) const
|
2010-01-20 01:02:58 +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 WindowJoin::print(thread_db* tdbb, string& plan, bool detailed, unsigned level) const
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
2011-02-02 12:31:04 +01:00
|
|
|
if (detailed)
|
|
|
|
{
|
|
|
|
plan += printIndent(level) + "Window Join";
|
|
|
|
m_outer->print(tdbb, plan, true, level + 1);
|
|
|
|
m_inner->print(tdbb, plan, true, level + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-02-04 13:13:46 +01:00
|
|
|
if (!level)
|
|
|
|
{
|
|
|
|
plan += "(";
|
|
|
|
}
|
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
m_outer->print(tdbb, plan, false, level + 1);
|
2011-02-04 13:13:46 +01:00
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
plan += ", ";
|
2011-02-04 13:13:46 +01:00
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
m_inner->print(tdbb, plan, false, level + 1);
|
2011-02-04 13:13:46 +01:00
|
|
|
|
|
|
|
if (!level)
|
|
|
|
{
|
|
|
|
plan += ")";
|
|
|
|
}
|
2011-02-02 12:31:04 +01:00
|
|
|
}
|
2010-01-20 01:02:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowJoin::markRecursive()
|
|
|
|
{
|
|
|
|
m_outer->markRecursive();
|
|
|
|
m_inner->markRecursive();
|
|
|
|
}
|
|
|
|
|
2011-02-20 16:34:08 +01:00
|
|
|
void WindowJoin::findUsedStreams(StreamList& streams) const
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
m_outer->findUsedStreams(streams);
|
|
|
|
m_inner->findUsedStreams(streams);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void WindowJoin::invalidateRecords(jrd_req* request) const
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
m_outer->invalidateRecords(request);
|
|
|
|
m_inner->invalidateRecords(request);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void WindowJoin::nullRecords(thread_db* tdbb) const
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
m_outer->nullRecords(tdbb);
|
|
|
|
m_inner->nullRecords(tdbb);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
int WindowJoin::compareKeys(thread_db* tdbb, jrd_req* request, DscNull* outerValues) const
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
int cmp;
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
for (size_t i = 0; i < m_innerKeys->expressions.getCount(); i++)
|
2010-01-20 01:02:58 +01:00
|
|
|
{
|
|
|
|
const DscNull& outerValue = outerValues[i];
|
2010-10-12 13:36:51 +02:00
|
|
|
const dsc* const innerDesc = EVL_expr(tdbb, m_innerKeys->expressions[i]);
|
2010-01-20 01:02:58 +01:00
|
|
|
const bool innerNull = (request->req_flags & req_null);
|
|
|
|
|
|
|
|
if (outerValue.null && !innerNull)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!outerValue.null && innerNull)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!outerValue.null && !innerNull && (cmp = MOV_compare(outerValue.desc, innerDesc)) != 0)
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-12 13:36:51 +02:00
|
|
|
#endif // NOT_USED_OR_REPLACED
|
2010-01-18 22:37:47 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
|
2011-12-04 19:36:26 +01:00
|
|
|
WindowedStream::WindowedStream(thread_db* tdbb, CompilerScratch* csb,
|
2010-10-12 13:36:51 +02:00
|
|
|
ObjectsArray<WindowSourceNode::Partition>& partitions, RecordSource* next)
|
2015-10-12 16:26:00 +02:00
|
|
|
: m_next(FB_NEW_POOL(csb->csb_pool) BufferedStream(csb, next)),
|
2010-07-06 13:09:32 +02:00
|
|
|
m_joinedStream(NULL)
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2010-04-09 17:35:52 +02:00
|
|
|
m_impure = CMP_impure(csb, sizeof(Impure));
|
2010-01-18 22:37:47 +01:00
|
|
|
|
2010-02-16 01:26:53 +01:00
|
|
|
// Process the unpartioned and unordered map, if existent.
|
2010-01-18 22:37:47 +01:00
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
for (ObjectsArray<WindowSourceNode::Partition>::iterator partition = partitions.begin();
|
|
|
|
partition != partitions.end();
|
|
|
|
++partition)
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
2010-10-12 10:02:57 +02:00
|
|
|
// While here, verify not supported functions/clauses.
|
2010-08-29 20:20:44 +02:00
|
|
|
|
2010-11-21 04:47:29 +01:00
|
|
|
const NestConst<ValueExprNode>* source = partition->map->sourceList.begin();
|
2010-10-12 13:36:51 +02:00
|
|
|
|
2010-11-21 04:47:29 +01:00
|
|
|
for (const NestConst<ValueExprNode>* const end = partition->map->sourceList.end();
|
|
|
|
source != end;
|
|
|
|
++source)
|
2010-02-14 20:08:22 +01:00
|
|
|
{
|
2010-11-21 04:47:29 +01:00
|
|
|
const AggNode* aggNode = (*source)->as<AggNode>();
|
2010-02-14 20:08:22 +01:00
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
if (partition->order && aggNode)
|
2010-02-16 01:26:53 +01:00
|
|
|
aggNode->checkOrderedWindowCapable();
|
2010-02-14 20:08:22 +01:00
|
|
|
}
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
if (!partition->group && !partition->order)
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
2010-02-16 01:26:53 +01:00
|
|
|
fb_assert(!m_joinedStream);
|
2010-01-18 22:37:47 +01:00
|
|
|
|
2015-10-12 16:26:00 +02:00
|
|
|
m_joinedStream = FB_NEW_POOL(csb->csb_pool) AggregatedStream(tdbb, csb, partition->stream,
|
|
|
|
NULL, partition->map, FB_NEW_POOL(csb->csb_pool) BufferedStreamWindow(csb, m_next),
|
2011-12-04 19:36:26 +01:00
|
|
|
NULL);
|
2010-01-18 22:37:47 +01:00
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
OPT_gen_aggregate_distincts(tdbb, csb, partition->map);
|
2010-01-18 22:37:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 01:26:53 +01:00
|
|
|
if (!m_joinedStream)
|
2015-10-12 16:26:00 +02:00
|
|
|
m_joinedStream = FB_NEW_POOL(csb->csb_pool) BufferedStreamWindow(csb, m_next);
|
2010-02-16 01:26:53 +01:00
|
|
|
|
2010-01-23 04:02:53 +01:00
|
|
|
// Process ordered partitions.
|
|
|
|
|
2011-02-20 16:34:08 +01:00
|
|
|
StreamList streams;
|
2010-02-16 01:26:53 +01:00
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
for (ObjectsArray<WindowSourceNode::Partition>::iterator partition = partitions.begin();
|
|
|
|
partition != partitions.end();
|
|
|
|
++partition)
|
2010-01-23 04:02:53 +01:00
|
|
|
{
|
|
|
|
// Refresh the stream list based on the last m_joinedStream.
|
|
|
|
streams.clear();
|
|
|
|
m_joinedStream->findUsedStreams(streams);
|
|
|
|
|
|
|
|
// Build the sort key. It's the order items following the partition items.
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
SortNode* partitionOrder;
|
2010-01-23 04:02:53 +01:00
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
if (partition->group)
|
2010-01-23 04:02:53 +01:00
|
|
|
{
|
2015-10-12 16:26:00 +02:00
|
|
|
partitionOrder = FB_NEW_POOL(csb->csb_pool) SortNode(csb->csb_pool);
|
2010-10-12 13:36:51 +02:00
|
|
|
partitionOrder->expressions.join(partition->group->expressions);
|
|
|
|
partitionOrder->descending.join(partition->group->descending);
|
|
|
|
partitionOrder->nullOrder.join(partition->group->nullOrder);
|
2010-01-23 04:02:53 +01:00
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
if (partition->order)
|
2010-01-23 04:02:53 +01:00
|
|
|
{
|
2010-10-12 13:36:51 +02:00
|
|
|
partitionOrder->expressions.join(partition->order->expressions);
|
|
|
|
partitionOrder->descending.join(partition->order->descending);
|
|
|
|
partitionOrder->nullOrder.join(partition->order->nullOrder);
|
2010-01-23 04:02:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2010-10-12 13:36:51 +02:00
|
|
|
partitionOrder = partition->order;
|
2010-01-23 04:02:53 +01:00
|
|
|
|
2010-02-16 01:26:53 +01:00
|
|
|
if (partitionOrder)
|
2010-02-15 01:43:04 +01:00
|
|
|
{
|
2012-04-12 11:02:13 +02:00
|
|
|
SortedStream* sortedStream = OPT_gen_sort(tdbb, csb, streams, NULL,
|
2010-02-16 01:26:53 +01:00
|
|
|
m_joinedStream, partitionOrder, false);
|
2010-02-15 01:43:04 +01:00
|
|
|
|
2015-10-12 16:26:00 +02:00
|
|
|
m_joinedStream = FB_NEW_POOL(csb->csb_pool) AggregatedStream(tdbb, csb, partition->stream,
|
2010-10-12 13:36:51 +02:00
|
|
|
(partition->group ? &partition->group->expressions : NULL),
|
|
|
|
partition->map,
|
2015-10-12 16:26:00 +02:00
|
|
|
FB_NEW_POOL(csb->csb_pool) BufferedStream(csb, sortedStream),
|
2010-10-12 13:36:51 +02:00
|
|
|
(partition->order ? &partition->order->expressions : NULL));
|
2010-02-16 01:26:53 +01:00
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
OPT_gen_aggregate_distincts(tdbb, csb, partition->map);
|
2010-02-15 01:43:04 +01:00
|
|
|
}
|
2010-01-18 22:37:47 +01:00
|
|
|
}
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void WindowedStream::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;
|
|
|
|
|
|
|
|
m_next->open(tdbb);
|
2010-01-18 22:37:47 +01:00
|
|
|
m_joinedStream->open(tdbb);
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void WindowedStream::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;
|
2010-01-18 22:37:47 +01:00
|
|
|
m_joinedStream->close(tdbb);
|
2009-12-09 19:45:44 +01:00
|
|
|
m_next->close(tdbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool WindowedStream::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;
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
if (!m_joinedStream->getRecord(tdbb))
|
|
|
|
return false;
|
2009-12-09 19:45:44 +01:00
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
return true;
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool WindowedStream::refetchRecord(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2010-01-18 22:37:47 +01:00
|
|
|
return m_joinedStream->refetchRecord(tdbb);
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2010-10-12 13:36:51 +02:00
|
|
|
bool WindowedStream::lockRecord(thread_db* /*tdbb*/) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2010-01-23 04:02:53 +01:00
|
|
|
status_exception::raise(Arg::Gds(isc_record_lock_not_supp));
|
|
|
|
return false; // compiler silencer
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
void WindowedStream::print(thread_db* tdbb, string& plan, bool detailed, unsigned level) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2013-12-05 17:37:25 +01:00
|
|
|
m_joinedStream->print(tdbb, plan, detailed, level);
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowedStream::markRecursive()
|
|
|
|
{
|
2010-01-18 22:37:47 +01:00
|
|
|
m_joinedStream->markRecursive();
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void WindowedStream::invalidateRecords(jrd_req* request) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2010-01-18 22:37:47 +01:00
|
|
|
m_joinedStream->invalidateRecords(request);
|
|
|
|
}
|
|
|
|
|
2011-12-14 07:45:24 +01:00
|
|
|
void WindowedStream::findUsedStreams(StreamList& streams, bool expandAll) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
2011-12-14 07:45:24 +01:00
|
|
|
m_joinedStream->findUsedStreams(streams, expandAll);
|
2010-01-18 22:37:47 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void WindowedStream::nullRecords(thread_db* tdbb) const
|
2010-01-18 22:37:47 +01:00
|
|
|
{
|
|
|
|
m_joinedStream->nullRecords(tdbb);
|
|
|
|
}
|