2009-10-31 20:03:30 +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"
|
2009-12-09 19:45:44 +01:00
|
|
|
#include "../jrd/common.h"
|
2009-10-31 20:03:30 +01:00
|
|
|
#include "../jrd/jrd.h"
|
2010-02-13 21:29:29 +01:00
|
|
|
#include "../dsql/Nodes.h"
|
2009-12-09 19:45:44 +01:00
|
|
|
#include "../jrd/cmp_proto.h"
|
2009-10-31 20:03:30 +01:00
|
|
|
#include "../jrd/evl_proto.h"
|
2009-12-09 19:45:44 +01:00
|
|
|
#include "../jrd/exe_proto.h"
|
2009-10-31 20:03:30 +01:00
|
|
|
#include "../jrd/mov_proto.h"
|
|
|
|
#include "../jrd/vio_proto.h"
|
2009-12-25 10:55:05 +01:00
|
|
|
#include "../jrd/Attachment.h"
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
#include "RecordSource.h"
|
|
|
|
|
2009-10-31 20:03:30 +01:00
|
|
|
using namespace Firebird;
|
2009-12-09 19:45:44 +01:00
|
|
|
using namespace Jrd;
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
// ------------------------
|
|
|
|
// Data access: aggregation
|
|
|
|
// ------------------------
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 20:08:22 +01:00
|
|
|
// Note that we can have NULL order here, in case of window function with shouldCallWinPass
|
|
|
|
// returning true, with partition, and without order. Example: ROW_NUMBER() OVER (PARTITION BY N).
|
2010-01-18 22:37:47 +01:00
|
|
|
AggregatedStream::AggregatedStream(CompilerScratch* csb, UCHAR stream, jrd_nod* group,
|
2010-02-16 01:26:53 +01:00
|
|
|
jrd_nod* const map, BaseBufferedStream* next, jrd_nod* order)
|
2010-02-14 00:55:48 +01:00
|
|
|
: RecordStream(csb, stream),
|
2010-02-16 01:26:53 +01:00
|
|
|
m_bufferedStream(next),
|
2010-02-14 20:08:22 +01:00
|
|
|
m_next(m_bufferedStream),
|
2010-02-14 00:55:48 +01:00
|
|
|
m_group(group),
|
|
|
|
m_map(map),
|
2010-02-15 01:43:04 +01:00
|
|
|
m_order(order),
|
|
|
|
m_winPassMap(csb->csb_pool)
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2010-02-15 01:43:04 +01:00
|
|
|
init(csb);
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 20:08:22 +01:00
|
|
|
AggregatedStream::AggregatedStream(CompilerScratch* csb, UCHAR stream, jrd_nod* group,
|
|
|
|
jrd_nod* const map, RecordSource* next)
|
|
|
|
: RecordStream(csb, stream),
|
|
|
|
m_bufferedStream(NULL),
|
|
|
|
m_next(next),
|
|
|
|
m_group(group),
|
|
|
|
m_map(map),
|
2010-02-15 01:43:04 +01:00
|
|
|
m_order(NULL),
|
|
|
|
m_winPassMap(csb->csb_pool)
|
2010-02-14 20:08:22 +01:00
|
|
|
{
|
2010-02-15 01:43:04 +01:00
|
|
|
init(csb);
|
2010-02-14 20:08:22 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
void AggregatedStream::open(thread_db* tdbb)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
jrd_req* const request = tdbb->getRequest();
|
|
|
|
Impure* const impure = (Impure*) ((UCHAR*) request + m_impure);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
impure->irsb_flags = irsb_open;
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-10 02:32:47 +01:00
|
|
|
impure->state = STATE_GROUPING;
|
2010-02-14 00:55:48 +01:00
|
|
|
impure->pending = 0;
|
2009-12-09 19:45:44 +01:00
|
|
|
VIO_record(tdbb, &request->req_rpb[m_stream], m_format, tdbb->getDefaultPool());
|
2010-02-14 00:55:48 +01:00
|
|
|
|
|
|
|
m_next->open(tdbb);
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
void AggregatedStream::close(thread_db* tdbb)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
invalidateRecords(request);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
Impure* const impure = (Impure*) ((UCHAR*) request + m_impure);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
if (impure->irsb_flags & irsb_open)
|
|
|
|
{
|
|
|
|
impure->irsb_flags &= ~irsb_open;
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
m_next->close(tdbb);
|
|
|
|
}
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
bool AggregatedStream::getRecord(thread_db* tdbb)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
jrd_req* const request = tdbb->getRequest();
|
|
|
|
record_param* const rpb = &request->req_rpb[m_stream];
|
|
|
|
Impure* const impure = (Impure*) ((UCHAR*) request + m_impure);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
if (!(impure->irsb_flags & irsb_open))
|
|
|
|
{
|
|
|
|
rpb->rpb_number.setValid(false);
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 20:08:22 +01:00
|
|
|
if (m_bufferedStream)
|
2010-02-14 00:55:48 +01:00
|
|
|
{
|
2010-02-16 01:26:53 +01:00
|
|
|
FB_UINT64 position = m_bufferedStream->getPosition(request);
|
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
if (impure->pending == 0)
|
|
|
|
{
|
|
|
|
if (impure->state == STATE_PENDING)
|
2010-02-14 20:08:22 +01:00
|
|
|
m_bufferedStream->getRecord(tdbb);
|
2010-02-14 00:55:48 +01:00
|
|
|
|
|
|
|
impure->state = evaluateGroup(tdbb, impure->state);
|
|
|
|
|
|
|
|
if (impure->state == STATE_PROCESS_EOF)
|
|
|
|
{
|
|
|
|
rpb->rpb_number.setValid(false);
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 20:08:22 +01:00
|
|
|
impure->pending = m_bufferedStream->getPosition(request) - position -
|
2010-02-14 00:55:48 +01:00
|
|
|
(impure->state == STATE_EOF_FOUND ? 0 : 1);
|
2010-02-14 20:08:22 +01:00
|
|
|
m_bufferedStream->locate(tdbb, position);
|
2010-02-14 00:55:48 +01:00
|
|
|
}
|
|
|
|
|
2010-02-16 01:26:53 +01:00
|
|
|
if (m_winPassMap.hasData())
|
|
|
|
{
|
|
|
|
SlidingWindow window(tdbb, m_bufferedStream, m_group, request);
|
|
|
|
dsc* desc;
|
|
|
|
|
|
|
|
for (const jrd_nod* const* ptr = m_winPassMap.begin(); ptr != m_winPassMap.end(); ++ptr)
|
|
|
|
{
|
|
|
|
jrd_nod* from = (*ptr)->nod_arg[e_asgn_from];
|
|
|
|
fb_assert(from->nod_type == nod_class_exprnode_jrd);
|
|
|
|
const AggNode* aggNode = reinterpret_cast<const AggNode*>(from->nod_arg[0]);
|
|
|
|
|
|
|
|
jrd_nod* field = (*ptr)->nod_arg[e_asgn_to];
|
|
|
|
const USHORT id = (USHORT)(IPTR) field->nod_arg[e_fld_id];
|
|
|
|
Record* record = request->req_rpb[(int) (IPTR) field->nod_arg[e_fld_stream]].rpb_record;
|
|
|
|
|
|
|
|
desc = aggNode->winPass(tdbb, request, &window);
|
|
|
|
|
|
|
|
if (!desc)
|
|
|
|
SET_NULL(record, id);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MOV_move(tdbb, desc, EVL_assign_to(tdbb, field));
|
|
|
|
CLEAR_NULL(record, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
if (impure->pending > 0)
|
|
|
|
--impure->pending;
|
2010-02-14 20:08:22 +01:00
|
|
|
|
2010-02-16 01:26:53 +01:00
|
|
|
m_bufferedStream->getRecord(tdbb);
|
2010-02-14 20:08:22 +01:00
|
|
|
|
2010-02-16 01:26:53 +01:00
|
|
|
// If there is no group, we should reassign the map items.
|
|
|
|
if (!m_group)
|
2010-02-14 20:08:22 +01:00
|
|
|
{
|
2010-02-16 01:26:53 +01:00
|
|
|
for (jrd_nod** ptr = m_map->nod_arg, **end = ptr + m_map->nod_count; ptr < end; ptr++)
|
2010-02-14 20:08:22 +01:00
|
|
|
{
|
2010-02-16 01:26:53 +01:00
|
|
|
jrd_nod* from = (*ptr)->nod_arg[e_asgn_from];
|
|
|
|
const AggNode* aggNode = ExprNode::as<AggNode>(from);
|
|
|
|
|
|
|
|
if (!aggNode)
|
|
|
|
EXE_assignment(tdbb, *ptr);
|
2010-02-14 20:08:22 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-14 00:55:48 +01:00
|
|
|
}
|
|
|
|
else
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2010-02-14 00:55:48 +01:00
|
|
|
impure->state = evaluateGroup(tdbb, impure->state);
|
|
|
|
|
|
|
|
if (impure->state == STATE_PROCESS_EOF)
|
|
|
|
{
|
|
|
|
rpb->rpb_number.setValid(false);
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
rpb->rpb_number.setValid(true);
|
|
|
|
return true;
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
bool AggregatedStream::refetchRecord(thread_db* tdbb)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
return m_next->refetchRecord(tdbb);
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
bool AggregatedStream::lockRecord(thread_db* tdbb)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
status_exception::raise(Arg::Gds(isc_record_lock_not_supp));
|
|
|
|
return false; // compiler silencer
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
void AggregatedStream::dump(thread_db* tdbb, UCharBuffer& buffer)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
buffer.add(isc_info_rsb_begin);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
buffer.add(isc_info_rsb_type);
|
|
|
|
buffer.add(isc_info_rsb_aggregate);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
m_next->dump(tdbb, buffer);
|
2009-11-01 11:58:16 +01:00
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
buffer.add(isc_info_rsb_end);
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
void AggregatedStream::markRecursive()
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
m_next->markRecursive();
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
void AggregatedStream::invalidateRecords(jrd_req* request)
|
|
|
|
{
|
|
|
|
m_next->invalidateRecords(request);
|
|
|
|
}
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
void AggregatedStream::findUsedStreams(StreamsArray& streams)
|
|
|
|
{
|
|
|
|
RecordStream::findUsedStreams(streams);
|
2010-02-15 01:43:04 +01:00
|
|
|
if (m_bufferedStream)
|
|
|
|
m_bufferedStream->findUsedStreams(streams);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggregatedStream::init(CompilerScratch* csb)
|
|
|
|
{
|
|
|
|
fb_assert(m_map && m_next);
|
|
|
|
m_impure = CMP_impure(csb, sizeof(Impure));
|
|
|
|
|
|
|
|
// Separate nodes that requires the winPass call.
|
|
|
|
for (jrd_nod** ptr = m_map->nod_arg, **end = ptr + m_map->nod_count; ptr < end; ptr++)
|
|
|
|
{
|
|
|
|
jrd_nod* from = (*ptr)->nod_arg[e_asgn_from];
|
|
|
|
const AggNode* aggNode = ExprNode::as<AggNode>(from);
|
|
|
|
|
|
|
|
if (aggNode && aggNode->shouldCallWinPass())
|
|
|
|
m_winPassMap.add(*ptr);
|
|
|
|
}
|
2010-02-14 00:55:48 +01:00
|
|
|
}
|
|
|
|
|
2009-10-31 20:03:30 +01:00
|
|
|
// Compute the next aggregated record of a value group. evlGroup is driven by, and returns, a state
|
2009-12-10 02:32:47 +01:00
|
|
|
// variable.
|
|
|
|
AggregatedStream::State AggregatedStream::evaluateGroup(thread_db* tdbb, AggregatedStream::State state)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2009-10-31 20:03:30 +01:00
|
|
|
|
|
|
|
if (--tdbb->tdbb_quantum < 0)
|
|
|
|
JRD_reschedule(tdbb, 0, true);
|
|
|
|
|
|
|
|
impure_value vtemp;
|
|
|
|
vtemp.vlu_string = NULL;
|
|
|
|
|
|
|
|
jrd_nod** ptr;
|
|
|
|
const jrd_nod* const* end;
|
|
|
|
|
|
|
|
// if we found the last record last time, we're all done
|
|
|
|
|
2009-12-10 02:32:47 +01:00
|
|
|
if (state == STATE_EOF_FOUND)
|
|
|
|
return STATE_PROCESS_EOF;
|
2009-10-31 20:03:30 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2010-02-14 00:55:48 +01:00
|
|
|
// If there isn't a record pending, open the stream and get one
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
if (!m_order || state == STATE_PROCESS_EOF || state == STATE_GROUPING)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-02-14 00:55:48 +01:00
|
|
|
// Initialize the aggregate record
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
for (ptr = m_map->nod_arg, end = ptr + m_map->nod_count; ptr < end; ptr++)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-02-14 00:55:48 +01:00
|
|
|
const jrd_nod* from = (*ptr)->nod_arg[e_asgn_from];
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
switch (from->nod_type)
|
2009-12-10 02:32:47 +01:00
|
|
|
{
|
2010-02-14 00:55:48 +01:00
|
|
|
case nod_literal:
|
|
|
|
EXE_assignment(tdbb, *ptr);
|
|
|
|
break;
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
case nod_class_exprnode_jrd:
|
|
|
|
{
|
|
|
|
const AggNode* aggNode = ExprNode::as<AggNode>(from);
|
|
|
|
if (aggNode)
|
|
|
|
aggNode->aggInit(tdbb, request);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: // Shut up some compiler warnings
|
|
|
|
break;
|
|
|
|
}
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-10 02:32:47 +01:00
|
|
|
if (state == STATE_PROCESS_EOF || state == STATE_GROUPING)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
if (!m_next->getRecord(tdbb))
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-01-18 22:37:47 +01:00
|
|
|
if (m_group)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-02-13 21:29:29 +01:00
|
|
|
finiDistinct(tdbb, request);
|
2009-12-10 02:32:47 +01:00
|
|
|
return STATE_PROCESS_EOF;
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
2010-02-14 00:55:48 +01:00
|
|
|
|
2009-12-10 02:32:47 +01:00
|
|
|
state = STATE_EOF_FOUND;
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dsc* desc;
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
if (m_group)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-01-18 22:37:47 +01:00
|
|
|
for (ptr = m_group->nod_arg, end = ptr + m_group->nod_count; ptr < end; ptr++)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
|
|
|
jrd_nod* from = *ptr;
|
2010-02-16 01:26:53 +01:00
|
|
|
impure_value* impure = (impure_value*) ((SCHAR*) request + from->nod_impure);
|
2009-10-31 20:03:30 +01:00
|
|
|
desc = EVL_expr(tdbb, from);
|
|
|
|
if (request->req_flags & req_null)
|
|
|
|
impure->vlu_desc.dsc_address = NULL;
|
|
|
|
else
|
|
|
|
EVL_make_value(tdbb, desc, impure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
if (m_order)
|
|
|
|
{
|
|
|
|
for (ptr = m_order->nod_arg, end = ptr + m_order->nod_count; ptr < end; ptr++)
|
|
|
|
{
|
|
|
|
jrd_nod* from = *ptr;
|
2010-02-16 01:26:53 +01:00
|
|
|
impure_value* impure = (impure_value*) ((SCHAR*) request + from->nod_impure);
|
2010-02-14 00:55:48 +01:00
|
|
|
desc = EVL_expr(tdbb, from);
|
|
|
|
if (request->req_flags & req_null)
|
|
|
|
impure->vlu_desc.dsc_address = NULL;
|
|
|
|
else
|
|
|
|
EVL_make_value(tdbb, desc, impure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-31 20:03:30 +01:00
|
|
|
// Loop thru records until either a value change or EOF
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
2009-12-10 02:32:47 +01:00
|
|
|
while (state != STATE_EOF_FOUND)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-10 02:32:47 +01:00
|
|
|
state = STATE_PENDING;
|
2009-10-31 20:03:30 +01:00
|
|
|
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// In the case of a group by, look for a change in value of any of
|
|
|
|
// the columns; if we find one, stop aggregating and return what we have.
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
if (m_group)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-01-18 22:37:47 +01:00
|
|
|
for (ptr = m_group->nod_arg, end = ptr + m_group->nod_count; ptr < end; ptr++)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
|
|
|
jrd_nod* from = *ptr;
|
2010-02-16 01:26:53 +01:00
|
|
|
impure_value* impure = (impure_value*) ((SCHAR*) request + from->nod_impure);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
|
|
|
if (impure->vlu_desc.dsc_address)
|
|
|
|
EVL_make_value(tdbb, &impure->vlu_desc, &vtemp);
|
|
|
|
else
|
|
|
|
vtemp.vlu_desc.dsc_address = NULL;
|
|
|
|
|
|
|
|
desc = EVL_expr(tdbb, from);
|
|
|
|
|
2010-02-14 00:55:48 +01:00
|
|
|
if (request->req_flags & req_null)
|
|
|
|
{
|
|
|
|
impure->vlu_desc.dsc_address = NULL;
|
|
|
|
if (vtemp.vlu_desc.dsc_address)
|
|
|
|
{
|
|
|
|
if (m_order)
|
|
|
|
state = STATE_GROUPING;
|
|
|
|
goto break_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EVL_make_value(tdbb, desc, impure);
|
|
|
|
if (!vtemp.vlu_desc.dsc_address || MOV_compare(&vtemp.vlu_desc, desc))
|
|
|
|
{
|
|
|
|
if (m_order)
|
|
|
|
state = STATE_GROUPING;
|
|
|
|
goto break_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_order)
|
|
|
|
{
|
|
|
|
for (ptr = m_order->nod_arg, end = ptr + m_order->nod_count; ptr < end; ptr++)
|
|
|
|
{
|
|
|
|
jrd_nod* from = *ptr;
|
2010-02-16 01:26:53 +01:00
|
|
|
impure_value* impure = (impure_value*) ((SCHAR*) request + from->nod_impure);
|
2010-02-14 00:55:48 +01:00
|
|
|
|
|
|
|
if (impure->vlu_desc.dsc_address)
|
|
|
|
EVL_make_value(tdbb, &impure->vlu_desc, &vtemp);
|
|
|
|
else
|
|
|
|
vtemp.vlu_desc.dsc_address = NULL;
|
|
|
|
|
|
|
|
desc = EVL_expr(tdbb, from);
|
|
|
|
|
2009-10-31 20:03:30 +01:00
|
|
|
if (request->req_flags & req_null)
|
|
|
|
{
|
|
|
|
impure->vlu_desc.dsc_address = NULL;
|
|
|
|
if (vtemp.vlu_desc.dsc_address)
|
|
|
|
goto break_out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EVL_make_value(tdbb, desc, impure);
|
|
|
|
if (!vtemp.vlu_desc.dsc_address || MOV_compare(&vtemp.vlu_desc, desc))
|
|
|
|
goto break_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// go through and compute all the aggregates on this record
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
for (ptr = m_map->nod_arg, end = ptr + m_map->nod_count; ptr < end; ptr++)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
|
|
|
jrd_nod* from = (*ptr)->nod_arg[e_asgn_from];
|
2010-02-13 21:29:29 +01:00
|
|
|
const AggNode* aggNode = ExprNode::as<AggNode>(from);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-13 21:29:29 +01:00
|
|
|
if (aggNode)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-02-13 21:29:29 +01:00
|
|
|
aggNode->aggPass(tdbb, request);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-13 21:29:29 +01:00
|
|
|
// If a max or min has been mapped to an index, then the first record is the EOF.
|
|
|
|
if (aggNode->indexed)
|
|
|
|
state = STATE_EOF_FOUND;
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
2010-02-13 21:29:29 +01:00
|
|
|
else
|
|
|
|
EXE_assignment(tdbb, *ptr);
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-10 02:32:47 +01:00
|
|
|
if (state == STATE_EOF_FOUND)
|
2009-10-31 20:03:30 +01:00
|
|
|
break;
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
if (!m_next->getRecord(tdbb))
|
2009-12-10 02:32:47 +01:00
|
|
|
state = STATE_EOF_FOUND;
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break_out:
|
|
|
|
|
|
|
|
// Finish up any residual computations and get out
|
|
|
|
|
|
|
|
delete vtemp.vlu_string;
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
for (ptr = m_map->nod_arg, end = ptr + m_map->nod_count; ptr < end; ptr++)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
|
|
|
jrd_nod* from = (*ptr)->nod_arg[e_asgn_from];
|
2010-02-13 21:29:29 +01:00
|
|
|
const AggNode* aggNode = ExprNode::as<AggNode>(from);
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-13 21:29:29 +01:00
|
|
|
if (aggNode)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-02-13 21:29:29 +01:00
|
|
|
jrd_nod* field = (*ptr)->nod_arg[e_asgn_to];
|
|
|
|
const USHORT id = (USHORT)(IPTR) field->nod_arg[e_fld_id];
|
|
|
|
Record* record = request->req_rpb[(int) (IPTR) field->nod_arg[e_fld_stream]].rpb_record;
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-13 21:29:29 +01:00
|
|
|
desc = aggNode->execute(tdbb, request);
|
|
|
|
if (!desc || !desc->dsc_dtype)
|
2009-10-31 20:03:30 +01:00
|
|
|
SET_NULL(record, id);
|
|
|
|
else
|
|
|
|
{
|
2010-02-13 21:29:29 +01:00
|
|
|
MOV_move(tdbb, desc, EVL_assign_to(tdbb, field));
|
2009-10-31 20:03:30 +01:00
|
|
|
CLEAR_NULL(record, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-09 19:45:44 +01:00
|
|
|
catch (const Exception&)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2010-02-13 21:29:29 +01:00
|
|
|
finiDistinct(tdbb, request);
|
2009-12-09 19:45:44 +01:00
|
|
|
throw;
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
// Finalize a sort for distinct aggregate
|
2010-02-13 21:29:29 +01:00
|
|
|
void AggregatedStream::finiDistinct(thread_db* tdbb, jrd_req* request)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
|
|
|
jrd_nod** ptr;
|
|
|
|
const jrd_nod* const* end;
|
|
|
|
|
2010-01-18 22:37:47 +01:00
|
|
|
for (ptr = m_map->nod_arg, end = ptr + m_map->nod_count; ptr < end; ptr++)
|
2009-10-31 20:03:30 +01:00
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
const jrd_nod* const from = (*ptr)->nod_arg[e_asgn_from];
|
2009-10-31 20:03:30 +01:00
|
|
|
|
2010-02-13 21:29:29 +01:00
|
|
|
const AggNode* aggNode = ExprNode::as<AggNode>(from);
|
|
|
|
if (aggNode)
|
|
|
|
aggNode->aggFinish(tdbb, request);
|
2009-10-31 20:03:30 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-16 01:26:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
SlidingWindow::SlidingWindow(thread_db* aTdbb, BaseBufferedStream* aStream, jrd_nod* aGroup,
|
|
|
|
jrd_req* aRequest)
|
|
|
|
: tdbb(aTdbb), // Note: instanciate the class only as local variable
|
|
|
|
stream(aStream),
|
|
|
|
group(aGroup),
|
|
|
|
request(aRequest)
|
|
|
|
{
|
|
|
|
savedPosition = stream->getPosition(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
SlidingWindow::~SlidingWindow()
|
|
|
|
{
|
|
|
|
if (!moved)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (impure_value* impure = partitionKeys.begin(); impure != partitionKeys.end(); ++impure)
|
|
|
|
delete impure->vlu_string;
|
|
|
|
|
|
|
|
// Position the stream where we received it.
|
|
|
|
stream->locate(tdbb, savedPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move in the window without pass partition boundaries.
|
|
|
|
bool SlidingWindow::move(SINT64 delta)
|
|
|
|
{
|
2010-02-16 09:53:31 +01:00
|
|
|
const SINT64 newPosition = SINT64(savedPosition) + delta;
|
2010-02-16 01:26:53 +01:00
|
|
|
|
|
|
|
// If we try to go out of bounds, no need to check the partition.
|
|
|
|
if (newPosition < 0 || newPosition >= (SINT64) stream->getCount(request))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!group)
|
|
|
|
{
|
|
|
|
// No partition, we may go everywhere.
|
|
|
|
|
|
|
|
moved = true;
|
|
|
|
|
|
|
|
stream->locate(tdbb, newPosition);
|
|
|
|
|
|
|
|
if (!stream->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
fb_assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!moved)
|
|
|
|
{
|
|
|
|
// This is our first move. We should cache the partition values, so subsequente moves didn't
|
|
|
|
// need to evaluate them again.
|
|
|
|
|
|
|
|
if (!stream->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
fb_assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
impure_value* impure = partitionKeys.getBuffer(group->nod_count);
|
|
|
|
memset(impure, 0, sizeof(impure_value) * group->nod_count);
|
|
|
|
|
|
|
|
dsc* desc;
|
|
|
|
|
|
|
|
for (jrd_nod** ptr = group->nod_arg, **end = ptr + group->nod_count; ptr < end;
|
|
|
|
++ptr, ++impure)
|
|
|
|
{
|
|
|
|
jrd_nod* from = *ptr;
|
|
|
|
desc = EVL_expr(tdbb, from);
|
|
|
|
if (request->req_flags & req_null)
|
|
|
|
impure->vlu_desc.dsc_address = NULL;
|
|
|
|
else
|
|
|
|
EVL_make_value(tdbb, desc, impure);
|
|
|
|
}
|
|
|
|
|
|
|
|
moved = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->locate(tdbb, newPosition);
|
|
|
|
|
|
|
|
if (!stream->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
fb_assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify if we're still inside the same partition.
|
|
|
|
|
|
|
|
impure_value* impure = partitionKeys.begin();
|
|
|
|
dsc* desc;
|
|
|
|
|
|
|
|
for (jrd_nod** ptr = group->nod_arg, **end = ptr + group->nod_count; ptr < end; ++ptr, ++impure)
|
|
|
|
{
|
|
|
|
jrd_nod* from = *ptr;
|
|
|
|
desc = EVL_expr(tdbb, from);
|
|
|
|
|
|
|
|
if (request->req_flags & req_null)
|
|
|
|
{
|
|
|
|
if (impure->vlu_desc.dsc_address)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!impure->vlu_desc.dsc_address || MOV_compare(&impure->vlu_desc, desc) != 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|